2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.vpp.util;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkState;
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.core.future.FutureJVppCore;
34 * Validation WriteCustomizers for Interface subnodes.
35 * Validates the type of interface.
37 public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject>
38 extends FutureJVppCustomizer implements WriterCustomizer<D> {
40 protected AbstractInterfaceTypeCustomizer(final FutureJVppCore futureJVppCore) {
41 super(futureJVppCore);
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",
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);
54 IllegalInterfaceTypeException
55 .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType());
58 protected abstract Class<? extends InterfaceType> getExpectedInterfaceType();
61 * Validate expected interface type
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);
70 protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter,
71 final WriteContext writeContext)
72 throws WriteFailedException;
74 // Validation for update and delete is not necessary
77 * Indicates unexpected interface type
79 protected static final class IllegalInterfaceTypeException extends IllegalArgumentException {
81 private IllegalInterfaceTypeException(final String msg) {
86 * Check the type of interface equals expected type
88 * @throws IllegalInterfaceTypeException if type of interface is null or not expected
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));