From 81c9cdfa5a9e91b91d73a6a2b20cff8b612d7de3 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 --- vpp-common/features/pom.xml | 89 ++++++++ vpp-common/features/src/main/features/features.xml | 33 +++ vpp-common/naming-context-api/pom.xml | 42 ++++ .../src/main/yang/naming-context.yang | 42 ++++ vpp-common/naming-context-impl/pom.xml | 53 +++++ .../src/main/config/default-config.xml | 62 ++++++ .../impl/rev141210/ContextReaderModule.java | 49 +++++ .../impl/rev141210/ContextReaderModuleFactory.java | 13 ++ .../src/main/yang/context-impl.yang | 39 ++++ vpp-common/pom.xml | 60 ++++++ vpp-common/vpp-jvpp-cfg/pom.xml | 39 ++++ .../src/main/config/default-config.xml | 46 +++++ .../vpp/jvpp/cfg/rev160406/VppJvppImplModule.java | 45 +++++ .../cfg/rev160406/VppJvppImplModuleFactory.java | 13 ++ .../vpp-jvpp-cfg/src/main/yang/vpp-jvpp-cfg.yang | 43 ++++ vpp-common/vpp-translate-utils/Readme.adoc | 3 + vpp-common/vpp-translate-utils/pom.xml | 87 ++++++++ .../v3po/util/cache/DumpCacheManager.java | 124 ++++++++++++ .../v3po/util/cache/EntityDumpExecutor.java | 41 ++++ .../v3po/util/cache/EntityDumpNonEmptyCheck.java | 33 +++ .../cache/EntityDumpPostProcessingFunction.java | 36 ++++ .../exceptions/check/DumpCheckFailedException.java | 31 +++ .../exceptions/check/i/DumpEmptyException.java | 40 ++++ .../execution/DumpExecutionFailedException.java | 27 +++ .../execution/i/DumpCallFailedException.java | 34 ++++ .../execution/i/DumpTimeoutException.java | 35 ++++ .../cache/noop/NoopDumpPostProcessingFunction.java | 33 +++ .../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 | 26 +++ .../rev160406/NamingContextImplModuleFactory.java | 13 ++ .../src/main/yang/vpp-util.yang | 37 ++++ .../v3po/util/cache/DumpCacheManagerTest.java | 143 +++++++++++++ .../translate/v3po/util/SubinterfaceUtilsTest.java | 32 +++ .../translate/v3po/util/TranslateUtilsTest.java | 137 +++++++++++++ 41 files changed, 2321 insertions(+) create mode 100644 vpp-common/features/pom.xml create mode 100644 vpp-common/features/src/main/features/features.xml create mode 100644 vpp-common/naming-context-api/pom.xml create mode 100644 vpp-common/naming-context-api/src/main/yang/naming-context.yang create mode 100644 vpp-common/naming-context-impl/pom.xml create mode 100644 vpp-common/naming-context-impl/src/main/config/default-config.xml create mode 100644 vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModule.java create mode 100644 vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModuleFactory.java create mode 100644 vpp-common/naming-context-impl/src/main/yang/context-impl.yang create mode 100644 vpp-common/pom.xml create mode 100644 vpp-common/vpp-jvpp-cfg/pom.xml create mode 100644 vpp-common/vpp-jvpp-cfg/src/main/config/default-config.xml create mode 100644 vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModule.java create mode 100644 vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModuleFactory.java create mode 100644 vpp-common/vpp-jvpp-cfg/src/main/yang/vpp-jvpp-cfg.yang create mode 100644 vpp-common/vpp-translate-utils/Readme.adoc create mode 100644 vpp-common/vpp-translate-utils/pom.xml create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java create 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/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModule.java create mode 100644 vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModuleFactory.java create mode 100644 vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang create mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java create mode 100644 vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/SubinterfaceUtilsTest.java create 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/features/pom.xml b/vpp-common/features/pom.xml new file mode 100644 index 000000000..e0ad56d56 --- /dev/null +++ b/vpp-common/features/pom.xml @@ -0,0 +1,89 @@ + + + + + io.fd.honeycomb.common + features-parent + 1.0.0-SNAPSHOT + ../../common/features-parent + + io.fd.honeycomb.vpp + vpp-common-features + 1.0.0-SNAPSHOT + ${project.artifactId} + 4.0.0 + + 3.1.1 + + + 0.8.2-Beryllium-SR2 + 1.3.2-Beryllium-SR2 + 1.3.2-Beryllium-SR2 + 1.0.2-Beryllium-SR2 + 0.8.2-Beryllium-SR2 + 0.3.2-Beryllium-SR2 + etc/opendaylight/karaf + + + + io.fd.honeycomb + honeycomb-features + features + ${project.version} + xml + runtime + + + ${project.groupId} + naming-context-api + ${project.version} + + + ${project.groupId} + naming-context-impl + ${project.version} + + + ${project.groupId} + naming-context-impl + ${project.version} + xml + config + + + ${project.groupId} + vpp-jvpp-cfg + ${project.version} + + + ${project.groupId} + vpp-translate-utils + ${project.version} + + + ${project.groupId} + vpp-jvpp-cfg + ${project.version} + xml + config + + + io.fd.vpp + jvpp + 16.09-SNAPSHOT + + + diff --git a/vpp-common/features/src/main/features/features.xml b/vpp-common/features/src/main/features/features.xml new file mode 100644 index 000000000..3983481dd --- /dev/null +++ b/vpp-common/features/src/main/features/features.xml @@ -0,0 +1,33 @@ + + + + + mvn:io.fd.honeycomb/honeycomb-features/${project.version}/xml/features + + + odl-honeycomb + mvn:io.fd.honeycomb.vpp/naming-context-api/{{VERSION}} + mvn:io.fd.honeycomb.vpp/naming-context-impl/{{VERSION}} + wrap:mvn:io.fd.vpp/jvpp/{{VERSION}} + mvn:io.fd.honeycomb.vpp/vpp-jvpp-cfg/{{VERSION}} + mvn:io.fd.honeycomb.vpp/vpp-translate-utils/{{VERSION}} + mvn:io.fd.honeycomb.vpp/vpp-jvpp-cfg/{{VERSION}}/xml/config + mvn:io.fd.honeycomb.vpp/naming-context-impl/{{VERSION}}/xml/config + + + diff --git a/vpp-common/naming-context-api/pom.xml b/vpp-common/naming-context-api/pom.xml new file mode 100644 index 000000000..9fd1cba0e --- /dev/null +++ b/vpp-common/naming-context-api/pom.xml @@ -0,0 +1,42 @@ + + + + + io.fd.honeycomb.common + api-parent + 1.0.0-SNAPSHOT + ../../common/api-parent + + + 4.0.0 + io.fd.honeycomb.vpp + naming-context-api + 1.0.0-SNAPSHOT + bundle + + + + + org.opendaylight.mdsal.model + mdsal-model-artifacts + 0.8.2-Beryllium-SR2 + pom + import + + + + + diff --git a/vpp-common/naming-context-api/src/main/yang/naming-context.yang b/vpp-common/naming-context-api/src/main/yang/naming-context.yang new file mode 100644 index 000000000..fa44bdd12 --- /dev/null +++ b/vpp-common/naming-context-api/src/main/yang/naming-context.yang @@ -0,0 +1,42 @@ +module naming-context { + yang-version 1; + namespace "urn:honeycomb:params:xml:ns:yang:naming:context"; + prefix "nc"; + + description + "This module contains data definition for naming mapping context"; + + revision "2016-05-13" { + description + "Initial revision."; + } + + container contexts { + + list naming-context { + + key "name"; + + leaf name { + type string; + } + + container mappings { + list mapping { + + key "name"; + unique "index"; + + leaf name { + type string; + } + + leaf index { + type int32; + } + } + } + } + } + +} \ No newline at end of file diff --git a/vpp-common/naming-context-impl/pom.xml b/vpp-common/naming-context-impl/pom.xml new file mode 100644 index 000000000..f8f2fbfb8 --- /dev/null +++ b/vpp-common/naming-context-impl/pom.xml @@ -0,0 +1,53 @@ + + + + + + + io.fd.honeycomb.common + impl-parent + 1.0.0-SNAPSHOT + ../../common/impl-parent + + + 4.0.0 + io.fd.honeycomb.vpp + naming-context-impl + 1.0.0-SNAPSHOT + + + + io.fd.honeycomb + translate-api + ${project.version} + + + io.fd.honeycomb + translate-utils + ${project.version} + + + ${project.groupId} + naming-context-api + ${project.version} + + + + bundle + + + + diff --git a/vpp-common/naming-context-impl/src/main/config/default-config.xml b/vpp-common/naming-context-impl/src/main/config/default-config.xml new file mode 100644 index 000000000..585375844 --- /dev/null +++ b/vpp-common/naming-context-impl/src/main/config/default-config.xml @@ -0,0 +1,62 @@ + + + + + + urn:opendaylight:params:xml:ns:yang:context:impl?module=context-impl&revision=2014-12-10 + urn:honeycomb:params:xml:ns:yang:translate:utils?module=translate-utils&revision=2016-04-06 + urn:honeycomb:params:xml:ns:yang:vpp:jvpp:cfg?module=vpp-jvpp-cfg&revision=2016-04-06 + urn:honeycomb:params:xml:ns:yang:data:api?module=data-api&revision=2016-04-11 + urn:honeycomb:params:xml:ns:yang:data:impl?module=data-impl&revision=2016-04-11 + urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding?module=opendaylight-md-sal-binding&revision=2013-10-28 + + + + + + + + prefix:context-reader + context-reader + + binding:binding-async-data-broker + honeycomb-context-binding-data-broker + + + + prefix:delegating-reader-registry + read-registry + + prefix:honeycomb-reader-factory + context-reader + + + + + + + + prefix:honeycomb-reader-factory + + context-reader + /modules/module[type='context-reader'][name='context-reader'] + + + + + + + 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 new file mode 100644 index 000000000..9be762014 --- /dev/null +++ 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 @@ -0,0 +1,49 @@ +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 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; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.ContextsBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** +* A reader to provide naming context related data +*/ +public class ContextReaderModule extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.context.impl.rev141210.AbstractContextReaderModule { + public ContextReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + super(identifier, dependencyResolver); + } + + public ContextReaderModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.context.impl.rev141210.ContextReaderModule oldModule, java.lang.AutoCloseable oldInstance) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + return new ReaderFactory(getContextBindingBrokerDependency()); + } + + private static final class ReaderFactory implements AutoCloseable, io.fd.honeycomb.v3po.translate.read.ReaderFactory { + + private final DataBroker contextBindingBrokerDependency; + + public ReaderFactory(final DataBroker contextBindingBrokerDependency) { + this.contextBindingBrokerDependency = contextBindingBrokerDependency; + } + + @Override + public void init(final ModifiableReaderRegistryBuilder registry) { + registry.add(new BindingBrokerReader<>(InstanceIdentifier.create(Contexts.class), + contextBindingBrokerDependency, + LogicalDatastoreType.OPERATIONAL, ContextsBuilder.class)); + } + } + +} 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/ContextReaderModuleFactory.java b/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModuleFactory.java new file mode 100644 index 000000000..aaafbc3e3 --- /dev/null +++ b/vpp-common/naming-context-impl/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/context/impl/rev141210/ContextReaderModuleFactory.java @@ -0,0 +1,13 @@ +/* +* Generated file +* +* Generated from: yang module name: context-impl yang module local name: context-reader +* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator +* Generated at: Fri Jul 29 11:05:42 CEST 2016 +* +* Do not modify this file unless it is present under src/main directory +*/ +package org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.context.impl.rev141210; +public class ContextReaderModuleFactory extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.context.impl.rev141210.AbstractContextReaderModuleFactory { + +} diff --git a/vpp-common/naming-context-impl/src/main/yang/context-impl.yang b/vpp-common/naming-context-impl/src/main/yang/context-impl.yang new file mode 100644 index 000000000..12861be68 --- /dev/null +++ b/vpp-common/naming-context-impl/src/main/yang/context-impl.yang @@ -0,0 +1,39 @@ +module context-impl { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:context:impl"; + prefix "v3po-impl"; + + import config { prefix config; revision-date 2013-04-05; } + import opendaylight-md-sal-binding { prefix md-sal-binding; revision-date 2013-10-28;} + import translate-api { prefix tapi; revision-date 2016-04-06; } + + description + "Service definition for v3po project"; + + revision "2014-12-10" { + description + "Initial revision"; + } + + identity context-reader { + base config:module-type; + config:provided-service tapi:honeycomb-reader-factory; + description "A reader to provide naming context related data"; + } + + augment "/config:modules/config:module/config:configuration" { + case context-reader { + when "/config:modules/config:module/config:type = 'context-reader'"; + + container context-binding-broker { + uses config:service-ref { + refine type { + mandatory true; + config:required-identity md-sal-binding:binding-async-data-broker; + } + } + } + + } + } +} diff --git a/vpp-common/pom.xml b/vpp-common/pom.xml new file mode 100644 index 000000000..ca4029b81 --- /dev/null +++ b/vpp-common/pom.xml @@ -0,0 +1,60 @@ + + + + + + org.opendaylight.odlparent + odlparent + 1.6.2-Beryllium-SR2 + + + + io.fd.honeycomb.vpp + vpp-common-aggregator + 1.0.0-SNAPSHOT + v3po + pom + 4.0.0 + + 3.1.1 + + + vpp-translate-utils + vpp-jvpp-cfg + naming-context-api + naming-context-impl + features + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.apache.maven.plugins + maven-install-plugin + + true + + + + + diff --git a/vpp-common/vpp-jvpp-cfg/pom.xml b/vpp-common/vpp-jvpp-cfg/pom.xml new file mode 100644 index 000000000..79956ef62 --- /dev/null +++ b/vpp-common/vpp-jvpp-cfg/pom.xml @@ -0,0 +1,39 @@ + + + + + io.fd.honeycomb.common + impl-parent + 1.0.0-SNAPSHOT + ../../common/impl-parent + + + 4.0.0 + io.fd.honeycomb.vpp + vpp-jvpp-cfg + 1.0.0-SNAPSHOT + bundle + + + + io.fd.vpp + jvpp + 16.09-SNAPSHOT + + + + diff --git a/vpp-common/vpp-jvpp-cfg/src/main/config/default-config.xml b/vpp-common/vpp-jvpp-cfg/src/main/config/default-config.xml new file mode 100644 index 000000000..cbb614e69 --- /dev/null +++ b/vpp-common/vpp-jvpp-cfg/src/main/config/default-config.xml @@ -0,0 +1,46 @@ + + + + + + + + urn:honeycomb:params:xml:ns:yang:vpp:jvpp:cfg?module=vpp-jvpp-cfg&revision=2016-04-06 + + + + + + + prefix:vpp-jvpp-impl + vpp-jvpp + + + + + + prefix:vpp-jvpp + + vpp-jvpp + /modules/module[type='vpp-jvpp-impl'][name='vpp-jvpp'] + + + + + + + diff --git a/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModule.java b/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModule.java new file mode 100644 index 000000000..8eb504a03 --- /dev/null +++ b/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModule.java @@ -0,0 +1,45 @@ +package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406; + +import java.io.IOException; +import org.openvpp.jvpp.JVppImpl; +import org.openvpp.jvpp.VppJNIConnection; +import org.openvpp.jvpp.future.FutureJVppFacade; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class VppJvppImplModule extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.AbstractVppJvppImplModule { + + private static final Logger LOG = LoggerFactory.getLogger(VppJvppImplModule.class); + + public VppJvppImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + super(identifier, dependencyResolver); + } + + public VppJvppImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.VppJvppImplModule oldModule, java.lang.AutoCloseable oldInstance) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + try { + final JVppImpl jVpp = new JVppImpl(new VppJNIConnection(getName())); + LOG.info("JVpp connection opened successfully as: {}", getName()); + return new FutureJVppFacade(jVpp) { + @Override + public void close() throws Exception { + super.close(); + LOG.info("Closing JVpp connection: {}", getName()); + jVpp.close(); + } + }; + } catch (IOException e) { + throw new IllegalStateException("Unable to open VPP management connection", e); + } + } + +} diff --git a/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModuleFactory.java b/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModuleFactory.java new file mode 100644 index 000000000..71151092d --- /dev/null +++ b/vpp-common/vpp-jvpp-cfg/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/jvpp/cfg/rev160406/VppJvppImplModuleFactory.java @@ -0,0 +1,13 @@ +/* +* Generated file +* +* Generated from: yang module name: vpp-jvpp-cfg yang module local name: vpp-jvpp-impl +* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator +* Generated at: Fri Apr 29 11:20:56 CEST 2016 +* +* Do not modify this file unless it is present under src/main directory +*/ +package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406; +public class VppJvppImplModuleFactory extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.jvpp.cfg.rev160406.AbstractVppJvppImplModuleFactory { + +} diff --git a/vpp-common/vpp-jvpp-cfg/src/main/yang/vpp-jvpp-cfg.yang b/vpp-common/vpp-jvpp-cfg/src/main/yang/vpp-jvpp-cfg.yang new file mode 100644 index 000000000..01a0bf339 --- /dev/null +++ b/vpp-common/vpp-jvpp-cfg/src/main/yang/vpp-jvpp-cfg.yang @@ -0,0 +1,43 @@ +module vpp-jvpp-cfg { + yang-version 1; + namespace "urn:honeycomb:params:xml:ns:yang:vpp:jvpp:cfg"; + prefix "vjvppc"; + + import config { prefix config; revision-date 2013-04-05; } + + description + "This module contains the base YANG definitions for + readers/writers plugged into a honeycomb"; + + revision "2016-04-06" { + description + "Initial revision."; + } + + identity vpp-jvpp { + base "config:service-type"; + config:java-class org.openvpp.jvpp.future.FutureJVpp; + } + + identity vpp-jvpp-impl { + base config:module-type; + config:provided-service vpp-jvpp; + } + + augment "/config:modules/config:module/config:configuration" { + case vpp-jvpp-impl { + when "/config:modules/config:module/config:type = 'vpp-jvpp-impl'"; + + leaf name { + type string; + default "v3poODL"; + } + + leaf description { + type string; + description "Artificial leaf just to trigger reinitialization of JVpp from HC"; + } + } + } + +} \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/Readme.adoc b/vpp-common/vpp-translate-utils/Readme.adoc new file mode 100644 index 000000000..1aaaea181 --- /dev/null +++ b/vpp-common/vpp-translate-utils/Readme.adoc @@ -0,0 +1,3 @@ += Honeycomb. VPP translation layer utils + +Provides utility classes useful in translation layer implementation for the VPP. \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/pom.xml b/vpp-common/vpp-translate-utils/pom.xml new file mode 100644 index 000000000..613fc0bff --- /dev/null +++ b/vpp-common/vpp-translate-utils/pom.xml @@ -0,0 +1,87 @@ + + + + + io.fd.honeycomb.common + impl-parent + 1.0.0-SNAPSHOT + ../../common/impl-parent + + + 4.0.0 + io.fd.honeycomb.vpp + vpp-translate-utils + 1.0.0-SNAPSHOT + bundle + + + + + org.opendaylight.mdsal.model + mdsal-model-artifacts + 0.8.2-Beryllium-SR2 + pom + import + + + + + + + io.fd.honeycomb + translate-utils + ${project.version} + + + ${project.groupId} + naming-context-api + ${project.version} + + + io.fd.vpp + jvpp + 16.09-SNAPSHOT + + + org.opendaylight.mdsal.model + ietf-inet-types-2013-07-15 + + + org.opendaylight.mdsal.model + ietf-interfaces + + + io.fd.honeycomb + data-api + ${project.version} + + + + + junit + junit + test + + + org.mockito + mockito-all + test + + + + + 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 new file mode 100644 index 000000000..e5adb54ad --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java @@ -0,0 +1,124 @@ +/* + * 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.cache; + +import static com.google.common.base.Preconditions.checkNotNull; + +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 javax.annotation.Nonnull; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager responsible for returning Data object dumps
either from cache or by invoking specified {@link + * EntityDumpExecutor} + */ +public final class DumpCacheManager { + + private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class); + + private final EntityDumpExecutor dumpExecutor; + private final EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private final EntityDumpPostProcessingFunction postProcessor; + + private DumpCacheManager(DumpCacheManagerBuilder builder) { + this.dumpExecutor = builder.dumpExecutor; + this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck; + this.postProcessor = builder.postProcessingFunction; + } + + /** + * Returns {@link Optional} of dump + */ + public Optional getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache) + throws DumpExecutionFailedException { + + //this key binding to every log has its logic ,because every customizer have its own cache manager and if + //there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs + LOG.debug("Loading dump for KEY[{}]", entityKey); + + T dump = (T) cache.get(entityKey); + + if (dump == null) { + LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey); + + dump = dumpExecutor.executeDump(); + + //this is not a critical exception, so its only logged here + try { + dumpNonEmptyCheck.assertNotEmpty(dump); + } catch (DumpCheckFailedException e) { + LOG.warn("Dump for KEY[{}] has been resolved as empty", entityKey, e); + return Optional.absent(); + } + + //no need to check if post processor active,if wasnt set,default no-op will be used + LOG.debug("Post-processing dump for KEY[{}]", entityKey); + dump = postProcessor.apply(dump); + + LOG.debug("Caching dump for KEY[{}]", entityKey); + cache.put(entityKey, dump); + return Optional.of(dump); + } else { + return Optional.of(dump); + } + } + + public static final class DumpCacheManagerBuilder { + + private EntityDumpExecutor dumpExecutor; + private EntityDumpNonEmptyCheck dumpNonEmptyCheck; + private EntityDumpPostProcessingFunction postProcessingFunction; + + public DumpCacheManagerBuilder() { + //for cases when user does not set specific post-processor + postProcessingFunction = new NoopDumpPostProcessingFunction(); + } + + public DumpCacheManagerBuilder withExecutor(@Nonnull EntityDumpExecutor executor) { + this.dumpExecutor = executor; + return this; + } + + public DumpCacheManagerBuilder withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck check) { + this.dumpNonEmptyCheck = check; + return this; + } + + public DumpCacheManagerBuilder withPostProcessingFunction( + EntityDumpPostProcessingFunction postProcessingFunction) { + this.postProcessingFunction = postProcessingFunction; + return this; + } + + public DumpCacheManager build() { + checkNotNull(dumpExecutor, "Dump executor cannot be null"); + checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null"); + checkNotNull(postProcessingFunction, + "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + + return new DumpCacheManager<>(this); + } + } +} + + diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java new file mode 100644 index 000000000..b25c59a8a --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java @@ -0,0 +1,41 @@ +/* + * 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.cache; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that return dumps for Data objects + */ +public interface EntityDumpExecutor { + + /** + * Performs dump on {link T} entity + * + * @return dump of specified {@link T} entity + * @throws DumpExecutionFailedException when dump fails + */ + public T executeDump() throws DumpExecutionFailedException; + + /** + * Bind dumping params for executor + */ + default public void bindDumpParams(U params) { + throw new UnsupportedOperationException("You should override this method if you need to bind dumping params"); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java new file mode 100644 index 000000000..d64e312fe --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.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.cache; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that verifies if dump of data object is non-empty + */ +public interface EntityDumpNonEmptyCheck { + + /** + * Verifies if data are non-empty,if not throws {@link DumpEmptyException} + * @throws DumpEmptyException + */ + public void assertNotEmpty(T data) throws DumpCheckFailedException; +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java new file mode 100644 index 000000000..2bc2446e9 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java @@ -0,0 +1,36 @@ +/* + * 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.cache; + +import java.util.function.Function; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for class that are post-processing data dumped from vpp + */ +@FunctionalInterface +public interface EntityDumpPostProcessingFunction extends Function { + + + /** + * Performs postprocessing on dumped data + * + * @return Post-processed data + */ + @Override + T apply(T t); +} \ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java new file mode 100644 index 000000000..39dabb62f --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java @@ -0,0 +1,31 @@ +/* + * 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.cache.exceptions.check; + +/** + * Abstract parent of exceptions thrown while checking if dump is not empty + */ +public abstract class DumpCheckFailedException extends Exception { + + public DumpCheckFailedException(String message) { + super(message); + } + + public DumpCheckFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java new file mode 100644 index 000000000..8acaf1c9f --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java @@ -0,0 +1,40 @@ +/* + * 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.cache.exceptions.check.i; + +import io.fd.honeycomb.translate.v3po.util.cache.EntityDumpNonEmptyCheck; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; + +/** + * This exception occurs when dump is resolved as empty by {@link EntityDumpNonEmptyCheck} + */ +public class DumpEmptyException extends DumpCheckFailedException { + + /** + * Creates {@link DumpEmptyException} with specified reason + */ + public DumpEmptyException(String reason) { + super(reason); + } + + /** + * Creates {@link DumpEmptyException} with specified reason and sub-exception + */ + public DumpEmptyException(String reason, Exception e) { + super(reason, e); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java new file mode 100644 index 000000000..ba249b6c0 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java @@ -0,0 +1,27 @@ +/* + * 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.cache.exceptions.execution; + +/** + * Default parent for all exceptions connected to dumping of VPP data + */ +public abstract class DumpExecutionFailedException extends Exception { + + public DumpExecutionFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java new file mode 100644 index 000000000..02b8722ab --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java @@ -0,0 +1,34 @@ +/* + * 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.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.VppBaseCallException; + +/** + * Wrapper exception for {@link org.openvpp.jvpp.VppBaseCallException} during dumping + */ +public class DumpCallFailedException extends DumpExecutionFailedException { + + public DumpCallFailedException(String message, VppBaseCallException cause) { + super(message, cause); + } + + public static final DumpCallFailedException wrapFailedCallException(String message, VppBaseCallException cause) { + return new DumpCallFailedException(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java new file mode 100644 index 000000000..4bc825408 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java @@ -0,0 +1,35 @@ +/* + * 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.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import java.util.concurrent.TimeoutException; + +/** + * Exception thrown when dump call ends in timeout + */ +public class DumpTimeoutException extends DumpExecutionFailedException { + + public DumpTimeoutException(String message, TimeoutException cause) { + super(message, cause); + + } + + public static final DumpTimeoutException wrapTimeoutException(String message, TimeoutException cause) { + return new DumpTimeoutException(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java new file mode 100644 index 000000000..58bfe6f0b --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.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.cache.noop; + +import io.fd.honeycomb.translate.v3po.util.cache.EntityDumpPostProcessingFunction; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NoopDumpPostProcessingFunction implements EntityDumpPostProcessingFunction { + + private static final Logger LOG = LoggerFactory.getLogger(NoopDumpPostProcessingFunction.class); + + @Override + public T apply(final T t) { + LOG.debug("Default post processing function called for {}", t); + return t; + } +} 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)); + } + } + + } +} 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 new file mode 100644 index 000000000..3f4f1f5ce --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..f6e0e6d48 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..654b13825 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..1df82af73 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..316d1f8e2 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..1a6a50133 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..9516459b2 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..60a816d89 --- /dev/null +++ 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 @@ -0,0 +1,26 @@ +package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406; + +import io.fd.honeycomb.v3po.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) { + super(identifier, dependencyResolver); + } + + public NamingContextImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406.NamingContextImplModule oldModule, java.lang.AutoCloseable oldInstance) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + return new NamingContext( + getArtificialNamePrefix(), + getIdentifier().getInstanceName()); + } + +} 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/NamingContextImplModuleFactory.java b/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModuleFactory.java new file mode 100644 index 000000000..c777dafa1 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/org/opendaylight/yang/gen/v1/urn/honeycomb/params/xml/ns/yang/vpp/util/rev160406/NamingContextImplModuleFactory.java @@ -0,0 +1,13 @@ +/* +* Generated file +* +* Generated from: yang module name: vpp-util yang module local name: naming-context-impl +* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator +* Generated at: Mon May 02 10:57:32 CEST 2016 +* +* Do not modify this file unless it is present under src/main directory +*/ +package org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406; +public class NamingContextImplModuleFactory extends org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.util.rev160406.AbstractNamingContextImplModuleFactory { + +} 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 new file mode 100644 index 000000000..23615d09e --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/yang/vpp-util.yang @@ -0,0 +1,37 @@ +module vpp-util { + yang-version 1; + namespace "urn:honeycomb:params:xml:ns:yang:vpp:util"; + prefix "vpp-u"; + + import config { prefix config; revision-date 2013-04-05; } + import data-api { prefix dapi; revision-date 2016-04-11; } + + description + "This module contains utilities for vpp readers/writers"; + + revision "2016-04-06" { + description + "Initial revision."; + } + + identity naming-context { + base "config:service-type"; + config:java-class io.fd.honeycomb.v3po.translate.v3po.util.NamingContext; + } + + identity naming-context-impl { + base config:module-type; + config:provided-service naming-context; + } + + augment "/config:modules/config:module/config:configuration" { + case naming-context-impl { + when "/config:modules/config:module/config:type = 'naming-context-impl'"; + + leaf artificial-name-prefix { + type string; + } + } + } + +} \ 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 new file mode 100644 index 000000000..5b9391305 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManagerTest.java @@ -0,0 +1,143 @@ +/* + * 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.cache; + +import static org.junit.Assert.assertEquals; +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 org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openvpp.jvpp.dto.IpDetails; +import org.openvpp.jvpp.dto.IpDetailsReplyDump; + +public class DumpCacheManagerTest { + + private static final String KEY = "cacheKey"; + + @Mock + private EntityDumpExecutor executor; + + private DumpCacheManager managerPositive; + private DumpCacheManager managerPositiveWithPostProcessing; + private DumpCacheManager managerNegative; + private ModificationCache cache; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + managerPositive = + new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .build(); + + managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .withPostProcessingFunction(createPostProcessor()) + .build(); + + managerNegative = + new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor) + .withNonEmptyPredicate(createNegativePredicate()) + .build(); + + cache = new ModificationCache(); + } + + /** + * This test verify full dump-caching cycle + */ + @Test + public void testCaching() throws DumpExecutionFailedException { + + + Optional stage1Optional = managerNegative.getDump(KEY, cache); + + //this is first call so instance should be from executor + assertEquals(false, stage1Optional.isPresent()); + assertEquals(false, cache.containsKey(KEY)); + + //rebind executor with other data + IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage2LoadedDump); + + Optional stage2Optional = managerPositive.getDump(KEY, cache); + + assertEquals(true, stage2Optional.isPresent()); + assertEquals(stage2LoadedDump, stage2Optional.get()); + + //rebind executor with other data + IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage3LoadedDump); + + Optional stage3Optional = managerPositive.getDump(KEY, cache); + assertEquals(true, stage3Optional.isPresent()); + //check if it returns instance cached from previous stage + assertEquals(stage2LoadedDump, stage3Optional.get()); + } + + @Test + public void testPostprocessing() throws DumpExecutionFailedException { + IpDetailsReplyDump dump = new IpDetailsReplyDump(); + IpDetails details = new IpDetails(); + details.swIfIndex = 2; + dump.ipDetails.add(details); + + when(executor.executeDump()).thenReturn(dump); + + Optional optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache); + + assertEquals(true, optionalDump.isPresent()); + assertEquals(1, optionalDump.get().ipDetails.size()); + assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex); + } + + private EntityDumpNonEmptyCheck createNegativePredicate() { + return data -> { + throw new DumpEmptyException("Empty dump", new IllegalArgumentException()); + }; + } + + private EntityDumpNonEmptyCheck createPositivePredicate() { + return data -> { + //DO NOTHING + }; + } + + private EntityDumpPostProcessingFunction createPostProcessor() { + return ipDetailsReplyDump -> { + IpDetailsReplyDump modified = new IpDetailsReplyDump(); + + for (IpDetails detail : ipDetailsReplyDump.ipDetails) { + IpDetails modifiedDetail = new IpDetails(); + modifiedDetail.swIfIndex = detail.swIfIndex + 5; + + modified.ipDetails.add(modifiedDetail); + } + + return modified; + }; + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..a61dfea90 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/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.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 new file mode 100644 index 000000000..334887430 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtilsTest.java @@ -0,0 +1,137 @@ +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