diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-05-13 10:04:11 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-05-17 17:13:08 +0200 |
commit | 02b2183a113214039611373f84352da4a5f57e2c (patch) | |
tree | 8091ad507e54de75366698c0860be9a0d7980d9b /v3po/vpp-translate-utils | |
parent | 7ebd4ac29fb72b80af0b481f8ea604085ded1bc8 (diff) |
Check expected interface type in sub-Interface nodes
+ Pass full WriteContext to the write customizers, making
them more flexible
Change-Id: I75c55aed02f9300eee20eabb4a3e84e294ed1e0f
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'v3po/vpp-translate-utils')
-rw-r--r-- | v3po/vpp-translate-utils/pom.xml | 4 | ||||
-rw-r--r-- | v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java | 103 |
2 files changed, 107 insertions, 0 deletions
diff --git a/v3po/vpp-translate-utils/pom.xml b/v3po/vpp-translate-utils/pom.xml index 51b4c0274..e9717de04 100644 --- a/v3po/vpp-translate-utils/pom.xml +++ b/v3po/vpp-translate-utils/pom.xml @@ -39,6 +39,10 @@ <artifactId>jvpp</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>org.opendaylight.mdsal.model</groupId> + <artifactId>ietf-interfaces</artifactId> + </dependency> <!-- Testing Dependencies --> <dependency> diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java new file mode 100644 index 000000000..45ac193cf --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer; +import io.fd.honeycomb.v3po.translate.write.WriteContext; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.future.FutureJVpp; + +/** + * Validation WriteCustomizers for Interface subnodes. + * Validates the type of interface. + * + * TODO this should be validated on model/DataTree level. However DataTree does not enforce When conditions + * Delete this class when DataTree handles when constraints properly + */ +public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject> + extends FutureJVppCustomizer implements ChildWriterCustomizer<D> { + + protected AbstractInterfaceTypeCustomizer(final FutureJVpp futureJvpp) { + super(futureJvpp); + } + + private void checkProperInterfaceType(@Nonnull final WriteContext writeContext, + @Nonnull final InstanceIdentifier<D> id) { + final InstanceIdentifier<Interface> ifcTypeFromIid = id.firstIdentifierOf(Interface.class); + checkArgument(ifcTypeFromIid != null, "Instance identifier does not contain {} type", Interface.class); + checkArgument(id.firstKeyOf(Interface.class) != null, "Instance identifier does not contain keyed {} type", + Interface.class); + final Optional<DataObject> interfaceConfigOperational = writeContext.readAfter(ifcTypeFromIid); + checkState(interfaceConfigOperational.isPresent(), + "Unable to get Interface configuration for an interface being updated under ID"); + + IllegalInterfaceTypeException + .checkInterfaceType((Interface) interfaceConfigOperational.get(), getExpectedInterfaceType()); + } + + protected abstract Class<? extends InterfaceType> getExpectedInterfaceType(); + + /** + * Validate expected interface type + */ + @Override + public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException.CreateFailedException { + checkProperInterfaceType(writeContext, id); + writeInterface(id, dataAfter, writeContext); + } + + protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter, + final WriteContext writeContext) + throws WriteFailedException.CreateFailedException; + + // Validation for update and delete is not necessary + + /** + * Indicates unexpected interface type + */ + protected static final class IllegalInterfaceTypeException extends IllegalArgumentException { + + private IllegalInterfaceTypeException(final String msg) { + super(msg); + } + + /** + * Check the type of interface equals expected type + * + * @throws IllegalInterfaceTypeException if type of interface is null or not expected + */ + static void checkInterfaceType(@Nonnull final Interface ifc, + @Nonnull final Class<? extends InterfaceType> expectedType) { + if (ifc.getType() == null || !expectedType.equals(ifc.getType())) { + throw new IllegalInterfaceTypeException(String.format( + "Unexpected interface type: %s for interface: %s. Expected interface is: %s", ifc.getType(), + ifc.getName(), expectedType)); + } + } + + } +} |