HONEYCOMB-58 - Routing Api
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / vpp / util / AbstractInterfaceTypeCustomizer.java
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.vpp.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 io.fd.vpp.jvpp.core.future.FutureJVppCore;
32
33 /**
34  * Validation WriteCustomizers for Interface subnodes.
35  * Validates the type of interface.
36  */
37 public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject>
38         extends FutureJVppCustomizer implements WriterCustomizer<D> {
39
40     protected AbstractInterfaceTypeCustomizer(final FutureJVppCore futureJVppCore) {
41         super(futureJVppCore);
42     }
43
44     private void checkProperInterfaceType(@Nonnull final WriteContext writeContext,
45                                           @Nonnull final InstanceIdentifier<D> id) {
46         final InstanceIdentifier<Interface> ifcTypeFromIid = id.firstIdentifierOf(Interface.class);
47         checkArgument(ifcTypeFromIid != null, "Instance identifier does not contain {} type", Interface.class);
48         checkArgument(id.firstKeyOf(Interface.class) != null, "Instance identifier does not contain keyed {} type",
49                 Interface.class);
50         final Optional<Interface> interfaceConfig = writeContext.readAfter(ifcTypeFromIid);
51         checkState(interfaceConfig.isPresent(),
52                 "Unable to get Interface configuration for an interface: %s currently being updated", ifcTypeFromIid);
53
54         IllegalInterfaceTypeException
55                 .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType());
56     }
57
58     protected abstract Class<? extends InterfaceType> getExpectedInterfaceType();
59
60     /**
61      * Validate expected interface type
62      */
63     @Override
64     public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter,
65                                              @Nonnull final WriteContext writeContext) throws WriteFailedException {
66         checkProperInterfaceType(writeContext, id);
67         writeInterface(id, dataAfter, writeContext);
68     }
69
70     protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter,
71                                            final WriteContext writeContext)
72             throws WriteFailedException;
73
74     // Validation for update and delete is not necessary
75
76     /**
77      * Indicates unexpected interface type
78      */
79     protected static final class IllegalInterfaceTypeException extends IllegalArgumentException {
80
81         private IllegalInterfaceTypeException(final String msg) {
82             super(msg);
83         }
84
85         /**
86          * Check the type of interface equals expected type
87          *
88          * @throws IllegalInterfaceTypeException if type of interface is null or not expected
89          */
90         static void checkInterfaceType(@Nonnull final Interface ifc,
91                                        @Nonnull final Class<? extends InterfaceType> expectedType) {
92             if (ifc.getType() == null || !expectedType.equals(ifc.getType())) {
93                 throw new IllegalInterfaceTypeException(String.format(
94                         "Unexpected interface type: %s for interface: %s. Expected interface is: %s", ifc.getType(),
95                         ifc.getName(), expectedType));
96             }
97         }
98
99     }
100 }