From e3c31cee916480b2d9d169c1f5afb1c42efaabe1 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Fri, 29 Jul 2016 16:27:12 +0200 Subject: HONEYCOMB-130: Rename infra packages(remove vpp/v3po) Change-Id: Ic5b90e397e3743623d01b206bc60bc5c7df6b981 Signed-off-by: Maros Marsalek --- .../impl/rev141210/ContextReaderModule.java | 6 +- .../v3po/util/AbstractInterfaceTypeCustomizer.java | 103 ++++++++++ .../translate/v3po/util/FutureJVppCustomizer.java | 45 +++++ .../translate/v3po/util/NamingContext.java | 188 +++++++++++++++++ .../translate/v3po/util/ReadTimeoutException.java | 33 +++ .../translate/v3po/util/SubInterfaceUtils.java | 28 +++ .../translate/v3po/util/TagRewriteOperation.java | 86 ++++++++ .../translate/v3po/util/TranslateUtils.java | 225 +++++++++++++++++++++ .../translate/v3po/util/WriteTimeoutException.java | 33 +++ .../v3po/util/cache/DumpCacheManager.java | 2 +- .../v3po/util/AbstractInterfaceTypeCustomizer.java | 103 ---------- .../translate/v3po/util/FutureJVppCustomizer.java | 45 ----- .../v3po/translate/v3po/util/NamingContext.java | 188 ----------------- .../translate/v3po/util/ReadTimeoutException.java | 33 --- .../translate/v3po/util/SubInterfaceUtils.java | 28 --- .../translate/v3po/util/TagRewriteOperation.java | 86 -------- .../v3po/translate/v3po/util/TranslateUtils.java | 225 --------------------- .../translate/v3po/util/WriteTimeoutException.java | 33 --- .../util/rev160406/NamingContextImplModule.java | 2 +- .../src/main/yang/vpp-util.yang | 2 +- .../translate/v3po/util/SubinterfaceUtilsTest.java | 32 +++ .../translate/v3po/util/TranslateUtilsTest.java | 137 +++++++++++++ .../v3po/util/cache/DumpCacheManagerTest.java | 2 +- .../translate/v3po/util/SubinterfaceUtilsTest.java | 32 --- .../translate/v3po/util/TranslateUtilsTest.java | 137 ------------- 25 files changed, 917 insertions(+), 917 deletions(-) create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/AbstractInterfaceTypeCustomizer.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/FutureJVppCustomizer.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/NamingContext.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ReadTimeoutException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/SubInterfaceUtils.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TagRewriteOperation.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TranslateUtils.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/WriteTimeoutException.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java delete mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java create mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/SubinterfaceUtilsTest.java create mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/TranslateUtilsTest.java delete mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/SubinterfaceUtilsTest.java delete mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java (limited to 'vpp-common') diff --git a/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModule.java b/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModule.java index 9be762014..da7ab832d 100644 --- a/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModule.java +++ b/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModule.java @@ -1,7 +1,7 @@ package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.context.impl.rev141210; -import io.fd.honeycomb.v3po.translate.read.registry.ModifiableReaderRegistryBuilder; -import io.fd.honeycomb.v3po.translate.util.read.BindingBrokerReader; +import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; +import io.fd.honeycomb.translate.util.read.BindingBrokerReader; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts; @@ -30,7 +30,7 @@ public class ContextReaderModule extends org.opendaylight.yang.gen.v1.urn.openda return new ReaderFactory(getContextBindingBrokerDependency()); } - private static final class ReaderFactory implements AutoCloseable, io.fd.honeycomb.v3po.translate.read.ReaderFactory { + private static final class ReaderFactory implements AutoCloseable, io.fd.honeycomb.translate.read.ReaderFactory { private final DataBroker contextBindingBrokerDependency; diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/AbstractInterfaceTypeCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/AbstractInterfaceTypeCustomizer.java new file mode 100644 index 000000000..7f3422fb8 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/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.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.translate.spi.write.WriterCustomizer; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.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)); + } + } + + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/FutureJVppCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/FutureJVppCustomizer.java new file mode 100644 index 000000000..45453259f --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/FutureJVppCustomizer.java @@ -0,0 +1,45 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import org.openvpp.jvpp.future.FutureJVpp; + +import javax.annotation.Nonnull; + +/** + * Abstract utility to hold the vppApi reference. + */ +@Beta +public abstract class FutureJVppCustomizer { + + private final FutureJVpp futureJvpp; + + public FutureJVppCustomizer(@Nonnull final FutureJVpp futureJvpp) { + this.futureJvpp = Preconditions.checkNotNull(futureJvpp, "futureJvpp should not be null"); + } + + /** + * Get vppApi reference + * + * @return vppApi reference + */ + public FutureJVpp getFutureJVpp() { + return futureJvpp; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/NamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/NamingContext.java new file mode 100644 index 000000000..b7d3afa3c --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/NamingContext.java @@ -0,0 +1,188 @@ +/* + * 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.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.translate.util.RWUtils; +import io.fd.honeycomb.translate.MappingContext; +import java.util.stream.Collector; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility adapter on top of {@link MappingContext} storing integer to string mappings according to naming-context yang + * model + */ +public final class NamingContext implements AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(NamingContext.class); + + private final String artificialNamePrefix; + private final KeyedInstanceIdentifier + namingContextIid; + + private static final Collector SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); + + /** + * Create new naming context + * + * @param artificialNamePrefix artificial name to be used for items without a name in VPP (or not provided) + * @param instanceName name of this context instance. Will be used as list item identifier within context data tree + */ + public NamingContext(@Nonnull final String artificialNamePrefix, @Nonnull final String instanceName) { + this.artificialNamePrefix = artificialNamePrefix; + namingContextIid = InstanceIdentifier.create(Contexts.class).child( + org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext.class, + new NamingContextKey(instanceName)); + } + + /** + * Retrieve name for mapping stored provided mappingContext instance. If not present, artificial name will be + * generated. + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return name mapped to provided index + */ + @Nonnull + public synchronized String getName(final int index, @Nonnull final MappingContext mappingContext) { + if (!containsName(index, mappingContext)) { + final String artificialName = getArtificialName(index); + addName(index, artificialName, mappingContext); + } + + final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); + checkState(read.isPresent(), "Mapping for index: %s is not present. But should be", index); + + return read.get().getMapping().stream() + .filter(mapping -> mapping.getIndex().equals(index)) + .collect(SINGLE_ITEM_COLLECTOR).getName(); + } + + /** + * Retrieve name for mapping stored provided mappingContext instance. if present + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return name mapped to provided index + */ + @Nonnull + public synchronized Optional getNameIfPresent(final int index, + @Nonnull final MappingContext mappingContext) { + final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); + + return read.isPresent() + ? Optional.of(read.get().getMapping().stream() + .filter(mapping -> mapping.getIndex().equals(index)) + .collect(SINGLE_ITEM_COLLECTOR) + .getName()) + : Optional.absent(); + } + + /** + * Check whether mapping is present for index. + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return true if present, false otherwise + */ + public synchronized boolean containsName(final int index, @Nonnull final MappingContext mappingContext) { + final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); + return read.isPresent() + ? read.get().getMapping().stream().anyMatch(mapping -> mapping.getIndex().equals(index)) + : false; + } + + + /** + * Add mapping to current context + * + * @param index index of a mapped item + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + */ + public synchronized void addName(final int index, final String name, final MappingContext mappingContext) { + final KeyedInstanceIdentifier mappingIid = getMappingIid(name); + mappingContext.put(mappingIid, new MappingBuilder().setIndex(index).setName(name).build()); + } + + private KeyedInstanceIdentifier getMappingIid(final String name) { + return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(name)); + } + + /** + * Remove mapping from current context + * + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + */ + public synchronized void removeName(final String name, final MappingContext mappingContext) { + mappingContext.delete(getMappingIid(name)); + } + + /** + * Returns index value associated with the given name. + * + * @param name the name whose associated index value is to be returned + * @param mappingContext mapping context providing context data for current transaction + * + * @return integer index value matching supplied name + * @throws IllegalArgumentException if name was not found + */ + public synchronized int getIndex(final String name, final MappingContext mappingContext) { + final Optional read = mappingContext.read(getMappingIid(name)); + checkArgument(read.isPresent(), "No mapping stored for name: %s", name); + return read.get().getIndex(); + + } + + /** + * Check whether mapping is present for name. + * + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return true if present, false otherwise + */ + public synchronized boolean containsIndex(final String name, final MappingContext mappingContext) { + return mappingContext.read(getMappingIid(name)).isPresent(); + } + + private String getArtificialName(final int index) { + return artificialNamePrefix + index; + } + + @Override + public void close() throws Exception { + /// Not removing the mapping from backing storage + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ReadTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ReadTimeoutException.java new file mode 100644 index 000000000..10babaedc --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ReadTimeoutException.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.translate.read.ReadFailedException; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Thrown when read method invocation times out. + */ +@Beta +public class ReadTimeoutException extends ReadFailedException { + + public ReadTimeoutException(final InstanceIdentifier id, final Throwable cause) { + super(id, cause); + } + +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/SubInterfaceUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/SubInterfaceUtils.java new file mode 100644 index 000000000..b325f7cc4 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/SubInterfaceUtils.java @@ -0,0 +1,28 @@ +/* + * 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.translate.v3po.util; + +public final class SubInterfaceUtils { + + private SubInterfaceUtils() { + throw new UnsupportedOperationException("Utility class cannot be instantiated."); + } + + public static String getSubInterfaceName(final String superIfName, final int subIfaceId) { + return String.format("%s.%d", superIfName, subIfaceId); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TagRewriteOperation.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TagRewriteOperation.java new file mode 100644 index 000000000..7958aff06 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TagRewriteOperation.java @@ -0,0 +1,86 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.base.Preconditions; +import com.google.common.primitives.UnsignedBytes; +import javax.annotation.Nonnegative; +import javax.annotation.Nullable; + +/** + * Defines vlan tag rewrite config options for VPP + * + * TODO corresponding enum (defined in l2_vtr.h) should be defined in vpe.api + * (does vpp's IDL support enum type definition?) + * which would allow to generate this class in jvpp + */ +public enum TagRewriteOperation { + disabled(0), + push_1(0), + push_2(0), + pop_1(1), + pop_2(2), + translate_1_to_1(1), + translate_1_to_2(1), + translate_2_to_1(2), + translate_2_to_2(2); + + private final static int MAX_INDEX = 3; + private final int code; + private final byte popTags; + + TagRewriteOperation(final int popTags) { + this.code = this.ordinal(); + this.popTags = UnsignedBytes.checkedCast(popTags); + } + + private static TagRewriteOperation[][] translation = new TagRewriteOperation[][] { + {disabled, push_1, push_2}, + {pop_1, translate_1_to_1, translate_1_to_2}, + {pop_2, translate_2_to_1, translate_2_to_2} + }; + + /** + * Returns VPP tag rewrite operation for given number of tags to pop and tags to push. + * @param toPop number of tags to pop (0..2) + * @param toPush number of tags to push (0..2) + * @return vpp tag rewrite operation for given input parameters + */ + public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) { + Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop"); + Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push"); + return translation[toPop][toPush]; + } + + /** + * Returns VPP tag rewrite operation for given operation code. + * @param code VPP tag rewrite operation code + * @return vpp tag rewrite operation for given input parameter + */ + @Nullable + public static TagRewriteOperation get(@Nonnegative final int code) { + for (TagRewriteOperation operation : TagRewriteOperation.values()) { + if (code == operation.code){ + return operation; + } + } + return null; + } + + public byte getPopTags() { + return popTags; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TranslateUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TranslateUtils.java new file mode 100644 index 000000000..4ecf8ff91 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/TranslateUtils.java @@ -0,0 +1,225 @@ +/* + * 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.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.base.Splitter; +import com.google.common.net.InetAddresses; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppBaseCallException; +import org.openvpp.jvpp.dto.JVppReply; + +public final class TranslateUtils { + + public static final Splitter COLON_SPLITTER = Splitter.on(':'); + public static final int DEFAULT_TIMEOUT_IN_SECONDS = 5; + + private TranslateUtils() { + } + + public static > REP getReplyForWrite(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType) + throws VppBaseCallException, WriteTimeoutException { + return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static > REP getReplyForWrite(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, WriteTimeoutException { + try { + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new WriteTimeoutException(replyType, e); + } + } + + public static > REP getReplyForRead(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType) + throws VppBaseCallException, ReadTimeoutException { + return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static > REP getReplyForRead(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, ReadTimeoutException { + try { + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new ReadTimeoutException(replyType, e); + } + } + + public static > REP getReply(@Nonnull Future future) + throws TimeoutException, VppBaseCallException { + return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static > REP getReply(@Nonnull Future future, + @Nonnegative final int timeoutInSeconds) + throws TimeoutException, VppBaseCallException { + try { + checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0"); + return future.get(timeoutInSeconds, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted", e); + } catch (ExecutionException e) { + // Execution exception could generally contains any exception + // when using exceptions instead of return codes just rethrow it for processing on corresponding place + if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) { + throw (VppBaseCallException) (e.getCause()); + } + throw new IllegalStateException(e); + } + } + + /** + * Transform Ipv4 address to a byte array acceptable by VPP. VPP expects incoming byte array to be in the same order + * as the address. + * + * @return byte array with address bytes + */ + public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { + byte[] retval = new byte[4]; + String[] dots = ipv4Addr.getValue().split("\\."); + + for (int d = 0; d < 4; d++) { + retval[d] = (byte) (Short.parseShort(dots[d]) & 0xff); + } + return retval; + } + + /** + * Parse byte array returned by VPP representing an Ipv4 address. Vpp returns IP byte arrays in reversed order. + * + * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No + * change in order. + */ + @Nonnull + public static Ipv4AddressNoZone arrayToIpv4AddressNoZone(@Nonnull byte[] ip) { + // VPP sends ipv4 in a 16 byte array + if (ip.length == 16) { + ip = Arrays.copyOfRange(ip, 0, 4); + } + try { + // Not reversing the byte array here!! because the IP coming from VPP is in reversed byte order + // compared to byte order it was submitted + return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddresses.fromLittleEndianByteArray(ip))); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Unable to parse ipv4", e); + } + } + + /** + * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from + * vpp contain \u0000. + */ + public static String toString(final byte[] cString) { + return new String(cString).replaceAll("\\u0000", "").intern(); + } + + /** + * Parse string represented mac address (using ":" as separator) into a byte array + */ + @Nonnull + public static byte[] parseMac(@Nonnull final String macAddress) { + final List parts = COLON_SPLITTER.splitToList(macAddress); + checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress); + return parseMacLikeString(parts); + } + + private static byte[] parseMacLikeString(final List strings) { + return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect( + () -> new byte[strings.size()], + new BiConsumer() { + + private int i = -1; + + @Override + public void accept(final byte[] bytes, final Byte aByte) { + bytes[++i] = aByte; + } + }, + (bytes, bytes2) -> { + throw new UnsupportedOperationException("Parallel collect not supported"); + }); + } + + public static byte parseHexByte(final String aByte) { + return (byte) Integer.parseInt(aByte, 16); + } + + /** + * Returns 0 if argument is null or false, 1 otherwise. + * + * @param value Boolean value to be converted + * @return byte value equal to 0 or 1 + */ + public static byte booleanToByte(@Nullable final Boolean value) { + return value != null && value + ? (byte) 1 + : (byte) 0; + } + + /** + * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise. + * + * @param value byte value to be converted + * @return Boolean value + * @throws IllegalArgumentException if argument is neither 0 nor 1 + */ + @Nonnull + public static Boolean byteToBoolean(final byte value) { + if (value == 0) { + return Boolean.FALSE; + } else if (value == 1) { + return Boolean.TRUE; + } + throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value)); + } + + /** + * Reverses bytes in the byte array + * + * @param bytes input array + * @return reversed array + */ + public static byte[] reverseBytes(final byte[] bytes) { + final byte[] reversed = new byte[bytes.length]; + int i = 1; + for (byte aByte : bytes) { + reversed[bytes.length - i++] = aByte; + } + + return reversed; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/WriteTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/WriteTimeoutException.java new file mode 100644 index 000000000..7179f705d --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/WriteTimeoutException.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.translate.write.WriteFailedException; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Thrown when write method invocation times out. + */ +@Beta +public class WriteTimeoutException extends WriteFailedException { + + public WriteTimeoutException(final InstanceIdentifier id, final Throwable cause) { + super(id, cause); + } + +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java index e5adb54ad..1acd9de4b 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java @@ -22,7 +22,7 @@ import com.google.common.base.Optional; import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; import io.fd.honeycomb.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction; -import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.translate.ModificationCache; import javax.annotation.Nonnull; import org.openvpp.jvpp.dto.JVppReplyDump; import org.slf4j.Logger; 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 deleted file mode 100644 index 1f2fbd838..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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)); - } - } - - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java deleted file mode 100644 index 3f4f1f5ce..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 com.google.common.annotations.Beta; -import com.google.common.base.Preconditions; -import org.openvpp.jvpp.future.FutureJVpp; - -import javax.annotation.Nonnull; - -/** - * Abstract utility to hold the vppApi reference. - */ -@Beta -public abstract class FutureJVppCustomizer { - - private final FutureJVpp futureJvpp; - - public FutureJVppCustomizer(@Nonnull final FutureJVpp futureJvpp) { - this.futureJvpp = Preconditions.checkNotNull(futureJvpp, "futureJvpp should not be null"); - } - - /** - * Get vppApi reference - * - * @return vppApi reference - */ - public FutureJVpp getFutureJVpp() { - return futureJvpp; - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java deleted file mode 100644 index f6e0e6d48..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * 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.MappingContext; -import io.fd.honeycomb.v3po.translate.util.RWUtils; -import java.util.stream.Collector; -import javax.annotation.Nonnull; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder; -import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Utility adapter on top of {@link MappingContext} storing integer to string mappings according to naming-context yang - * model - */ -public final class NamingContext implements AutoCloseable { - - private static final Logger LOG = LoggerFactory.getLogger(NamingContext.class); - - private final String artificialNamePrefix; - private final KeyedInstanceIdentifier - namingContextIid; - - private static final Collector SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); - - /** - * Create new naming context - * - * @param artificialNamePrefix artificial name to be used for items without a name in VPP (or not provided) - * @param instanceName name of this context instance. Will be used as list item identifier within context data tree - */ - public NamingContext(@Nonnull final String artificialNamePrefix, @Nonnull final String instanceName) { - this.artificialNamePrefix = artificialNamePrefix; - namingContextIid = InstanceIdentifier.create(Contexts.class).child( - org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext.class, - new NamingContextKey(instanceName)); - } - - /** - * Retrieve name for mapping stored provided mappingContext instance. If not present, artificial name will be - * generated. - * - * @param index index of a mapped item - * @param mappingContext mapping context providing context data for current transaction - * - * @return name mapped to provided index - */ - @Nonnull - public synchronized String getName(final int index, @Nonnull final MappingContext mappingContext) { - if (!containsName(index, mappingContext)) { - final String artificialName = getArtificialName(index); - addName(index, artificialName, mappingContext); - } - - final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); - checkState(read.isPresent(), "Mapping for index: %s is not present. But should be", index); - - return read.get().getMapping().stream() - .filter(mapping -> mapping.getIndex().equals(index)) - .collect(SINGLE_ITEM_COLLECTOR).getName(); - } - - /** - * Retrieve name for mapping stored provided mappingContext instance. if present - * - * @param index index of a mapped item - * @param mappingContext mapping context providing context data for current transaction - * - * @return name mapped to provided index - */ - @Nonnull - public synchronized Optional getNameIfPresent(final int index, - @Nonnull final MappingContext mappingContext) { - final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); - - return read.isPresent() - ? Optional.of(read.get().getMapping().stream() - .filter(mapping -> mapping.getIndex().equals(index)) - .collect(SINGLE_ITEM_COLLECTOR) - .getName()) - : Optional.absent(); - } - - /** - * Check whether mapping is present for index. - * - * @param index index of a mapped item - * @param mappingContext mapping context providing context data for current transaction - * - * @return true if present, false otherwise - */ - public synchronized boolean containsName(final int index, @Nonnull final MappingContext mappingContext) { - final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); - return read.isPresent() - ? read.get().getMapping().stream().anyMatch(mapping -> mapping.getIndex().equals(index)) - : false; - } - - - /** - * Add mapping to current context - * - * @param index index of a mapped item - * @param name name of a mapped item - * @param mappingContext mapping context providing context data for current transaction - */ - public synchronized void addName(final int index, final String name, final MappingContext mappingContext) { - final KeyedInstanceIdentifier mappingIid = getMappingIid(name); - mappingContext.put(mappingIid, new MappingBuilder().setIndex(index).setName(name).build()); - } - - private KeyedInstanceIdentifier getMappingIid(final String name) { - return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(name)); - } - - /** - * Remove mapping from current context - * - * @param name name of a mapped item - * @param mappingContext mapping context providing context data for current transaction - */ - public synchronized void removeName(final String name, final MappingContext mappingContext) { - mappingContext.delete(getMappingIid(name)); - } - - /** - * Returns index value associated with the given name. - * - * @param name the name whose associated index value is to be returned - * @param mappingContext mapping context providing context data for current transaction - * - * @return integer index value matching supplied name - * @throws IllegalArgumentException if name was not found - */ - public synchronized int getIndex(final String name, final MappingContext mappingContext) { - final Optional read = mappingContext.read(getMappingIid(name)); - checkArgument(read.isPresent(), "No mapping stored for name: %s", name); - return read.get().getIndex(); - - } - - /** - * Check whether mapping is present for name. - * - * @param name name of a mapped item - * @param mappingContext mapping context providing context data for current transaction - * - * @return true if present, false otherwise - */ - public synchronized boolean containsIndex(final String name, final MappingContext mappingContext) { - return mappingContext.read(getMappingIid(name)).isPresent(); - } - - private String getArtificialName(final int index) { - return artificialNamePrefix + index; - } - - @Override - public void close() throws Exception { - /// Not removing the mapping from backing storage - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java deleted file mode 100644 index 654b13825..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 com.google.common.annotations.Beta; -import io.fd.honeycomb.v3po.translate.read.ReadFailedException; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Thrown when read method invocation times out. - */ -@Beta -public class ReadTimeoutException extends ReadFailedException { - - public ReadTimeoutException(final InstanceIdentifier id, final Throwable cause) { - super(id, cause); - } - -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java deleted file mode 100644 index 1df82af73..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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; - -public final class SubInterfaceUtils { - - private SubInterfaceUtils() { - throw new UnsupportedOperationException("Utility class cannot be instantiated."); - } - - public static String getSubInterfaceName(final String superIfName, final int subIfaceId) { - return String.format("%s.%d", superIfName, subIfaceId); - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java deleted file mode 100644 index 316d1f8e2..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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 com.google.common.base.Preconditions; -import com.google.common.primitives.UnsignedBytes; -import javax.annotation.Nonnegative; -import javax.annotation.Nullable; - -/** - * Defines vlan tag rewrite config options for VPP - * - * TODO corresponding enum (defined in l2_vtr.h) should be defined in vpe.api - * (does vpp's IDL support enum type definition?) - * which would allow to generate this class in jvpp - */ -public enum TagRewriteOperation { - disabled(0), - push_1(0), - push_2(0), - pop_1(1), - pop_2(2), - translate_1_to_1(1), - translate_1_to_2(1), - translate_2_to_1(2), - translate_2_to_2(2); - - private final static int MAX_INDEX = 3; - private final int code; - private final byte popTags; - - TagRewriteOperation(final int popTags) { - this.code = this.ordinal(); - this.popTags = UnsignedBytes.checkedCast(popTags); - } - - private static TagRewriteOperation[][] translation = new TagRewriteOperation[][] { - {disabled, push_1, push_2}, - {pop_1, translate_1_to_1, translate_1_to_2}, - {pop_2, translate_2_to_1, translate_2_to_2} - }; - - /** - * Returns VPP tag rewrite operation for given number of tags to pop and tags to push. - * @param toPop number of tags to pop (0..2) - * @param toPush number of tags to push (0..2) - * @return vpp tag rewrite operation for given input parameters - */ - public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) { - Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop"); - Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push"); - return translation[toPop][toPush]; - } - - /** - * Returns VPP tag rewrite operation for given operation code. - * @param code VPP tag rewrite operation code - * @return vpp tag rewrite operation for given input parameter - */ - @Nullable - public static TagRewriteOperation get(@Nonnegative final int code) { - for (TagRewriteOperation operation : TagRewriteOperation.values()) { - if (code == operation.code){ - return operation; - } - } - return null; - } - - public byte getPopTags() { - return popTags; - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java deleted file mode 100644 index 1a6a50133..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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 com.google.common.base.Splitter; -import com.google.common.net.InetAddresses; -import java.net.UnknownHostException; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.function.BiConsumer; -import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.openvpp.jvpp.VppBaseCallException; -import org.openvpp.jvpp.dto.JVppReply; - -public final class TranslateUtils { - - public static final Splitter COLON_SPLITTER = Splitter.on(':'); - public static final int DEFAULT_TIMEOUT_IN_SECONDS = 5; - - private TranslateUtils() { - } - - public static > REP getReplyForWrite(@Nonnull Future future, - @Nonnull final InstanceIdentifier replyType) - throws VppBaseCallException, WriteTimeoutException { - return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); - } - - public static > REP getReplyForWrite(@Nonnull Future future, - @Nonnull final InstanceIdentifier replyType, - @Nonnegative final int timeoutInSeconds) - throws VppBaseCallException, WriteTimeoutException { - try { - return getReply(future, timeoutInSeconds); - } catch (TimeoutException e) { - throw new WriteTimeoutException(replyType, e); - } - } - - public static > REP getReplyForRead(@Nonnull Future future, - @Nonnull final InstanceIdentifier replyType) - throws VppBaseCallException, ReadTimeoutException { - return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); - } - - public static > REP getReplyForRead(@Nonnull Future future, - @Nonnull final InstanceIdentifier replyType, - @Nonnegative final int timeoutInSeconds) - throws VppBaseCallException, ReadTimeoutException { - try { - return getReply(future, timeoutInSeconds); - } catch (TimeoutException e) { - throw new ReadTimeoutException(replyType, e); - } - } - - public static > REP getReply(@Nonnull Future future) - throws TimeoutException, VppBaseCallException { - return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS); - } - - public static > REP getReply(@Nonnull Future future, - @Nonnegative final int timeoutInSeconds) - throws TimeoutException, VppBaseCallException { - try { - checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0"); - return future.get(timeoutInSeconds, TimeUnit.SECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IllegalStateException("Interrupted", e); - } catch (ExecutionException e) { - // Execution exception could generally contains any exception - // when using exceptions instead of return codes just rethrow it for processing on corresponding place - if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) { - throw (VppBaseCallException) (e.getCause()); - } - throw new IllegalStateException(e); - } - } - - /** - * Transform Ipv4 address to a byte array acceptable by VPP. VPP expects incoming byte array to be in the same order - * as the address. - * - * @return byte array with address bytes - */ - public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { - byte[] retval = new byte[4]; - String[] dots = ipv4Addr.getValue().split("\\."); - - for (int d = 0; d < 4; d++) { - retval[d] = (byte) (Short.parseShort(dots[d]) & 0xff); - } - return retval; - } - - /** - * Parse byte array returned by VPP representing an Ipv4 address. Vpp returns IP byte arrays in reversed order. - * - * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No - * change in order. - */ - @Nonnull - public static Ipv4AddressNoZone arrayToIpv4AddressNoZone(@Nonnull byte[] ip) { - // VPP sends ipv4 in a 16 byte array - if (ip.length == 16) { - ip = Arrays.copyOfRange(ip, 0, 4); - } - try { - // Not reversing the byte array here!! because the IP coming from VPP is in reversed byte order - // compared to byte order it was submitted - return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddresses.fromLittleEndianByteArray(ip))); - } catch (UnknownHostException e) { - throw new IllegalArgumentException("Unable to parse ipv4", e); - } - } - - /** - * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from - * vpp contain \u0000. - */ - public static String toString(final byte[] cString) { - return new String(cString).replaceAll("\\u0000", "").intern(); - } - - /** - * Parse string represented mac address (using ":" as separator) into a byte array - */ - @Nonnull - public static byte[] parseMac(@Nonnull final String macAddress) { - final List parts = COLON_SPLITTER.splitToList(macAddress); - checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress); - return parseMacLikeString(parts); - } - - private static byte[] parseMacLikeString(final List strings) { - return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect( - () -> new byte[strings.size()], - new BiConsumer() { - - private int i = -1; - - @Override - public void accept(final byte[] bytes, final Byte aByte) { - bytes[++i] = aByte; - } - }, - (bytes, bytes2) -> { - throw new UnsupportedOperationException("Parallel collect not supported"); - }); - } - - public static byte parseHexByte(final String aByte) { - return (byte) Integer.parseInt(aByte, 16); - } - - /** - * Returns 0 if argument is null or false, 1 otherwise. - * - * @param value Boolean value to be converted - * @return byte value equal to 0 or 1 - */ - public static byte booleanToByte(@Nullable final Boolean value) { - return value != null && value - ? (byte) 1 - : (byte) 0; - } - - /** - * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise. - * - * @param value byte value to be converted - * @return Boolean value - * @throws IllegalArgumentException if argument is neither 0 nor 1 - */ - @Nonnull - public static Boolean byteToBoolean(final byte value) { - if (value == 0) { - return Boolean.FALSE; - } else if (value == 1) { - return Boolean.TRUE; - } - throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value)); - } - - /** - * Reverses bytes in the byte array - * - * @param bytes input array - * @return reversed array - */ - public static byte[] reverseBytes(final byte[] bytes) { - final byte[] reversed = new byte[bytes.length]; - int i = 1; - for (byte aByte : bytes) { - reversed[bytes.length - i++] = aByte; - } - - return reversed; - } -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java deleted file mode 100644 index 9516459b2..000000000 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 com.google.common.annotations.Beta; -import io.fd.honeycomb.v3po.translate.write.WriteFailedException; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Thrown when write method invocation times out. - */ -@Beta -public class WriteTimeoutException extends WriteFailedException { - - public WriteTimeoutException(final InstanceIdentifier id, final Throwable cause) { - super(id, cause); - } - -} diff --git a/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModule.java b/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModule.java index 60a816d89..bff1a3a49 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModule.java +++ b/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModule.java @@ -1,6 +1,6 @@ package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406; -import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; +import io.fd.honeycomb.translate.v3po.util.NamingContext; public class NamingContextImplModule extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406.AbstractNamingContextImplModule { public NamingContextImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { diff --git a/vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang b/vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang index 23615d09e..f9d090c03 100644 --- a/vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang +++ b/vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang @@ -16,7 +16,7 @@ module vpp-util { identity naming-context { base "config:service-type"; - config:java-class io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; + config:java-class io.fd.honeycomb.translate.v3po.util.NamingContext; } identity naming-context-impl { diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/SubinterfaceUtilsTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/SubinterfaceUtilsTest.java new file mode 100644 index 000000000..fb8a8066e --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/SubinterfaceUtilsTest.java @@ -0,0 +1,32 @@ +/* + * 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.translate.v3po.util; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class SubinterfaceUtilsTest { + + @Test + public void testGetSubInterfaceName() throws Exception { + final String superIfName = "GigabitEthernet0/9/0"; + final int subIfaceId = 123; + final String expectedSubIfaceName = "GigabitEthernet0/9/0.123"; + assertEquals(expectedSubIfaceName, SubInterfaceUtils.getSubInterfaceName(superIfName, subIfaceId)); + } +} \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/TranslateUtilsTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/TranslateUtilsTest.java new file mode 100644 index 000000000..d9cb52f3a --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/TranslateUtilsTest.java @@ -0,0 +1,137 @@ +package io.fd.honeycomb.translate.v3po.util; + +import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yangtools.yang.binding.DataContainer; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.dto.JVppReply; + +public class TranslateUtilsTest { + + private static class AnDataObject implements DataObject { + @Override + public Class getImplementedInterface() { + return null; + } + } + + @Test + public void testGetReplyForWriteTimeout() throws Exception { + final Future> future = mock(Future.class); + when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class); + final InstanceIdentifier replyType = InstanceIdentifier.create(AnDataObject.class); + try { + TranslateUtils.getReplyForWrite(future, replyType); + } catch (WriteTimeoutException e) { + assertTrue(e.getCause() instanceof TimeoutException); + assertEquals(replyType, e.getFailedId()); + return; + } + fail("WriteTimeoutException was expected"); + } + + @Test + public void testGetReplyForReadTimeout() throws Exception { + final Future> future = mock(Future.class); + final InstanceIdentifier replyType = InstanceIdentifier.create(AnDataObject.class); + when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class); + try { + TranslateUtils.getReplyForRead(future, replyType); + } catch (ReadTimeoutException e) { + assertTrue(e.getCause() instanceof TimeoutException); + assertEquals(replyType, e.getFailedId()); + return; + } + fail("ReadTimeoutException was expected"); + } + + @Test + public void testIpv4NoZone() throws Exception { + final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1"); + byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr); + assertEquals((byte)192, bytes[0]); + // Simulating the magic of VPP + bytes = reverseBytes(bytes); + final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes); + assertEquals(ipv4Addr, ipv4AddressNoZone); + } + + @Test + public void testToString() { + final byte[] expected = "test".getBytes(); + final byte[] cString = new byte[expected.length + 10]; + System.arraycopy(expected, 0, cString, 0, expected.length); + final String jString = TranslateUtils.toString(cString); + assertArrayEquals(expected, jString.getBytes()); + } + + @Test + public void testParseMac() throws Exception { + byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9"); + assertMac(bytes); + } + + private void assertMac(final byte[] bytes) { + assertEquals(6, bytes.length); + assertEquals((byte) 0, bytes[0]); + assertEquals((byte) 255, bytes[1]); + assertEquals((byte) 127, bytes[2]); + assertEquals((byte) 21, bytes[3]); + assertEquals((byte) 94, bytes[4]); + assertEquals((byte) 169, bytes[5]); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacLonger() throws Exception { + byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77"); + assertMac(bytes); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseMacShorter() throws Exception { + TranslateUtils.parseMac("00:fF:7f"); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseRandomString() throws Exception { + TranslateUtils.parseMac("random{}}@$*&*!"); + } + + @Test(expected = NumberFormatException.class) + public void testParseMacNumberFormatEx() throws Exception { + TranslateUtils.parseMac("00:XX:7f:15:5e:77\""); + } + + @Test + public void testBooleanToByte() { + assertEquals(0, TranslateUtils.booleanToByte(null)); + assertEquals(0, TranslateUtils.booleanToByte(false)); + assertEquals(1, TranslateUtils.booleanToByte(true)); + } + + @Test + public void testByteToBoolean() { + assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0)); + assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testByteToBooleanFailed() { + TranslateUtils.byteToBoolean((byte) 123); + } + +} \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java index 5b9391305..9eaa024e3 100644 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.when; import com.google.common.base.Optional; import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; -import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.translate.ModificationCache; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/SubinterfaceUtilsTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/SubinterfaceUtilsTest.java deleted file mode 100644 index a61dfea90..000000000 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/SubinterfaceUtilsTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class SubinterfaceUtilsTest { - - @Test - public void testGetSubInterfaceName() throws Exception { - final String superIfName = "GigabitEthernet0/9/0"; - final int subIfaceId = 123; - final String expectedSubIfaceName = "GigabitEthernet0/9/0.123"; - assertEquals(expectedSubIfaceName, SubInterfaceUtils.getSubInterfaceName(superIfName, subIfaceId)); - } -} \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java deleted file mode 100644 index 334887430..000000000 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package io.fd.honeycomb.v3po.translate.v3po.util; - -import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.reverseBytes; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.junit.Test; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yangtools.yang.binding.DataContainer; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.openvpp.jvpp.dto.JVppReply; - -public class TranslateUtilsTest { - - private static class AnDataObject implements DataObject { - @Override - public Class getImplementedInterface() { - return null; - } - } - - @Test - public void testGetReplyForWriteTimeout() throws Exception { - final Future> future = mock(Future.class); - when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class); - final InstanceIdentifier replyType = InstanceIdentifier.create(AnDataObject.class); - try { - TranslateUtils.getReplyForWrite(future, replyType); - } catch (WriteTimeoutException e) { - assertTrue(e.getCause() instanceof TimeoutException); - assertEquals(replyType, e.getFailedId()); - return; - } - fail("WriteTimeoutException was expected"); - } - - @Test - public void testGetReplyForReadTimeout() throws Exception { - final Future> future = mock(Future.class); - final InstanceIdentifier replyType = InstanceIdentifier.create(AnDataObject.class); - when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class); - try { - TranslateUtils.getReplyForRead(future, replyType); - } catch (ReadTimeoutException e) { - assertTrue(e.getCause() instanceof TimeoutException); - assertEquals(replyType, e.getFailedId()); - return; - } - fail("ReadTimeoutException was expected"); - } - - @Test - public void testIpv4NoZone() throws Exception { - final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1"); - byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr); - assertEquals((byte)192, bytes[0]); - // Simulating the magic of VPP - bytes = reverseBytes(bytes); - final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes); - assertEquals(ipv4Addr, ipv4AddressNoZone); - } - - @Test - public void testToString() { - final byte[] expected = "test".getBytes(); - final byte[] cString = new byte[expected.length + 10]; - System.arraycopy(expected, 0, cString, 0, expected.length); - final String jString = TranslateUtils.toString(cString); - assertArrayEquals(expected, jString.getBytes()); - } - - @Test - public void testParseMac() throws Exception { - byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9"); - assertMac(bytes); - } - - private void assertMac(final byte[] bytes) { - assertEquals(6, bytes.length); - assertEquals((byte) 0, bytes[0]); - assertEquals((byte) 255, bytes[1]); - assertEquals((byte) 127, bytes[2]); - assertEquals((byte) 21, bytes[3]); - assertEquals((byte) 94, bytes[4]); - assertEquals((byte) 169, bytes[5]); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseMacLonger() throws Exception { - byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77"); - assertMac(bytes); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseMacShorter() throws Exception { - TranslateUtils.parseMac("00:fF:7f"); - } - - @Test(expected = IllegalArgumentException.class) - public void testParseRandomString() throws Exception { - TranslateUtils.parseMac("random{}}@$*&*!"); - } - - @Test(expected = NumberFormatException.class) - public void testParseMacNumberFormatEx() throws Exception { - TranslateUtils.parseMac("00:XX:7f:15:5e:77\""); - } - - @Test - public void testBooleanToByte() { - assertEquals(0, TranslateUtils.booleanToByte(null)); - assertEquals(0, TranslateUtils.booleanToByte(false)); - assertEquals(1, TranslateUtils.booleanToByte(true)); - } - - @Test - public void testByteToBoolean() { - assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0)); - assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1)); - } - - @Test(expected = IllegalArgumentException.class) - public void testByteToBooleanFailed() { - TranslateUtils.byteToBoolean((byte) 123); - } - -} \ No newline at end of file -- cgit 1.2.3-korg