From 0578156b721fa01c8c645b8f9625ecebdb6449e4 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Wed, 27 Jul 2016 11:05:51 +0200 Subject: HONEYCOMB-130: Separate v3po plugin from HC infra Creating folders: - common/ - infra/ - v3po/ - vpp-common/ Change-Id: I2c39e1b17e39e7c0f0628f44aa5fe08563fa06e4 Signed-off-by: Maros Marsalek --- .../v3po/util/AbstractInterfaceTypeCustomizer.java | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java (limited to 'vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java') diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java new file mode 100644 index 000000000..1f2fbd838 --- /dev/null +++ b/vpp-common/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.WriterCustomizer; +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 + extends FutureJVppCustomizer implements WriterCustomizer { + + protected AbstractInterfaceTypeCustomizer(final FutureJVpp futureJvpp) { + super(futureJvpp); + } + + private void checkProperInterfaceType(@Nonnull final WriteContext writeContext, + @Nonnull final InstanceIdentifier id) { + final InstanceIdentifier 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 interfaceConfig = writeContext.readAfter(ifcTypeFromIid); + checkState(interfaceConfig.isPresent(), + "Unable to get Interface configuration for an interface: %s currently being updated", ifcTypeFromIid); + + IllegalInterfaceTypeException + .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType()); + } + + protected abstract Class getExpectedInterfaceType(); + + /** + * Validate expected interface type + */ + @Override + public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + checkProperInterfaceType(writeContext, id); + writeInterface(id, dataAfter, writeContext); + } + + protected abstract void writeInterface(final InstanceIdentifier id, final D dataAfter, + final WriteContext writeContext) + throws WriteFailedException; + + // 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 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)); + } + } + + } +} -- cgit 1.2.3-korg