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.v3po.translate.v3po.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.v3po.translate.spi.write.WriterCustomizer;
24 import io.fd.honeycomb.v3po.translate.write.WriteContext;
25 import io.fd.honeycomb.v3po.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;
34 * Validation WriteCustomizers for Interface subnodes.
35 * Validates the type of interface.
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
40 public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject>
41 extends FutureJVppCustomizer implements WriterCustomizer<D> {
43 protected AbstractInterfaceTypeCustomizer(final FutureJVpp futureJvpp) {
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",
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);
57 IllegalInterfaceTypeException
58 .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType());
61 protected abstract Class<? extends InterfaceType> getExpectedInterfaceType();
64 * Validate expected interface type
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);
73 protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter,
74 final WriteContext writeContext)
75 throws WriteFailedException;
77 // Validation for update and delete is not necessary
80 * Indicates unexpected interface type
82 protected static final class IllegalInterfaceTypeException extends IllegalArgumentException {
84 private IllegalInterfaceTypeException(final String msg) {
89 * Check the type of interface equals expected type
91 * @throws IllegalInterfaceTypeException if type of interface is null or not expected
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));