7f3422fb8b2890d649e934d4829e4e55e14f0666
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.v3po.util;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkState;
21
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.openvpp.jvpp.future.FutureJVpp;
32
33 /**
34  * Validation WriteCustomizers for Interface subnodes.
35  * Validates the type of interface.
36  *
37  * TODO this should be validated on model/DataTree level. However DataTree does not enforce When conditions
38  * Delete this class when DataTree handles when constraints properly
39  */
40 public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject>
41     extends FutureJVppCustomizer implements WriterCustomizer<D> {
42
43     protected AbstractInterfaceTypeCustomizer(final FutureJVpp futureJvpp) {
44         super(futureJvpp);
45     }
46
47     private void checkProperInterfaceType(@Nonnull final WriteContext writeContext,
48                                           @Nonnull final InstanceIdentifier<D> id) {
49         final InstanceIdentifier<Interface> ifcTypeFromIid = id.firstIdentifierOf(Interface.class);
50         checkArgument(ifcTypeFromIid != null, "Instance identifier does not contain {} type", Interface.class);
51         checkArgument(id.firstKeyOf(Interface.class) != null, "Instance identifier does not contain keyed {} type",
52             Interface.class);
53         final Optional<Interface> interfaceConfig = writeContext.readAfter(ifcTypeFromIid);
54         checkState(interfaceConfig.isPresent(),
55             "Unable to get Interface configuration for an interface: %s currently being updated", ifcTypeFromIid);
56
57         IllegalInterfaceTypeException
58             .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType());
59     }
60
61     protected abstract Class<? extends InterfaceType> getExpectedInterfaceType();
62
63     /**
64      * Validate expected interface type
65      */
66     @Override
67     public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter,
68                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
69         checkProperInterfaceType(writeContext, id);
70         writeInterface(id, dataAfter, writeContext);
71     }
72
73     protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter,
74                                            final WriteContext writeContext)
75         throws WriteFailedException;
76
77     // Validation for update and delete is not necessary
78
79     /**
80      * Indicates unexpected interface type
81      */
82     protected static final class IllegalInterfaceTypeException extends IllegalArgumentException {
83
84         private IllegalInterfaceTypeException(final String msg) {
85             super(msg);
86         }
87
88         /**
89          * Check the type of interface equals expected type
90          *
91          * @throws IllegalInterfaceTypeException if type of interface is null or not expected
92          */
93         static void checkInterfaceType(@Nonnull final Interface ifc,
94                                        @Nonnull final Class<? extends InterfaceType> expectedType) {
95             if (ifc.getType() == null || !expectedType.equals(ifc.getType())) {
96                 throw new IllegalInterfaceTypeException(String.format(
97                     "Unexpected interface type: %s for interface: %s. Expected interface is: %s", ifc.getType(),
98                     ifc.getName(), expectedType));
99             }
100         }
101
102     }
103 }