From ef852789b2c156196a847b211066ae456c2683f5 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Tue, 27 Jun 2017 09:25:04 +0200 Subject: HONEYCOMB-358 - Activation module Provides module that provides set of distribution started modules Change-Id: I54287cc17f3af7d51a47a7342e5b8496e5ade00e Signed-off-by: Jan Srnicek --- .../io/fd/honeycomb/infra/bgp/distro/Main.java | 13 +- .../config/activation.json | 3 + .../infra/bgp/distro/BgpDistributionTest.java | 36 +---- .../src/test/resources/activation.json | 3 + .../base-distro-test-modules/base-modules | 14 ++ .../infra/distro/ActiveModuleProvider.java | 154 --------------------- .../java/io/fd/honeycomb/infra/distro/Main.java | 22 +-- .../infra/distro/activation/ActivationConfig.java | 33 +++++ .../infra/distro/activation/ActivationModule.java | 32 +++++ .../distro/activation/ActiveModuleProvider.java | 150 ++++++++++++++++++++ .../infra/distro/activation/ActiveModules.java | 57 ++++++++ .../distro/schema/YangBindingProviderModule.java | 1 + .../config/activation.json | 3 + .../infra/distro/ActiveModuleProviderTest.java | 91 ------------ .../infra/distro/BaseMinimalDistributionTest.java | 25 +--- .../activation/ActiveModuleProviderTest.java | 91 ++++++++++++ .../src/test/resources/activation.json | 3 + .../base-distro-test-modules/base-modules | 9 ++ 18 files changed, 418 insertions(+), 322 deletions(-) create mode 100644 infra/bgp-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json create mode 100644 infra/bgp-distribution/src/test/resources/activation.json create mode 100644 infra/bgp-distribution/src/test/resources/base-distro-test-modules/base-modules delete mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/ActiveModuleProvider.java create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java create mode 100644 infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java create mode 100644 infra/minimal-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json delete mode 100644 infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/ActiveModuleProviderTest.java create mode 100644 infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProviderTest.java create mode 100644 infra/minimal-distribution/src/test/resources/activation.json create mode 100644 infra/minimal-distribution/src/test/resources/base-distro-test-modules/base-modules (limited to 'infra') diff --git a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java index bf6725a09..8994f0415 100644 --- a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java +++ b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java @@ -16,18 +16,12 @@ package io.fd.honeycomb.infra.bgp.distro; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.STANDARD_MODULES_RELATIVE_PATH; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.aggregateResources; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.loadActiveModules; - import com.google.inject.ConfigurationException; import com.google.inject.CreationException; import com.google.inject.Injector; -import com.google.inject.Module; import com.google.inject.ProvisionException; import io.fd.honeycomb.infra.bgp.BgpConfiguration; import io.fd.honeycomb.infra.bgp.BgpServerProvider; -import java.util.Set; import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighbors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,16 +34,15 @@ public final class Main { } public static void main(String[] args) { - final ClassLoader classLoader = Main.class.getClassLoader(); - init(loadActiveModules(aggregateResources(STANDARD_MODULES_RELATIVE_PATH, classLoader))); + init(); } /** * Initialize the Honeycomb with provided modules */ - public static Injector init(final Set modules) { + public static Injector init() { try { - Injector injector = io.fd.honeycomb.infra.distro.Main.init(modules); + Injector injector = io.fd.honeycomb.infra.distro.Main.init(); final BgpConfiguration bgpAttributes = injector.getInstance(BgpConfiguration.class); if (bgpAttributes.isBgpEnabled()) { diff --git a/infra/bgp-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json b/infra/bgp-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json new file mode 100644 index 000000000..7b6f44a20 --- /dev/null +++ b/infra/bgp-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json @@ -0,0 +1,3 @@ +{ + "modules-resource-path": "../modules/" +} \ No newline at end of file diff --git a/infra/bgp-distribution/src/test/java/io/fd/honeycomb/infra/bgp/distro/BgpDistributionTest.java b/infra/bgp-distribution/src/test/java/io/fd/honeycomb/infra/bgp/distro/BgpDistributionTest.java index d205b95a8..be927a64e 100644 --- a/infra/bgp-distribution/src/test/java/io/fd/honeycomb/infra/bgp/distro/BgpDistributionTest.java +++ b/infra/bgp-distribution/src/test/java/io/fd/honeycomb/infra/bgp/distro/BgpDistributionTest.java @@ -16,30 +16,12 @@ package io.fd.honeycomb.infra.bgp.distro; -import static com.google.common.collect.ImmutableSet.of; - import com.google.common.io.ByteStreams; -import com.google.inject.Module; import com.mashape.unirest.http.Unirest; -import io.fd.honeycomb.infra.bgp.BgpConfigurationModule; -import io.fd.honeycomb.infra.bgp.BgpExtensionsModule; -import io.fd.honeycomb.infra.bgp.BgpModule; -import io.fd.honeycomb.infra.bgp.BgpReadersModule; -import io.fd.honeycomb.infra.bgp.BgpWritersModule; -import io.fd.honeycomb.infra.distro.cfgattrs.CfgAttrsModule; -import io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule; -import io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule; -import io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule; -import io.fd.honeycomb.infra.distro.netconf.NetconfModule; -import io.fd.honeycomb.infra.distro.netconf.NetconfReadersModule; -import io.fd.honeycomb.infra.distro.restconf.RestconfModule; -import io.fd.honeycomb.infra.distro.schema.SchemaModule; -import io.fd.honeycomb.infra.distro.schema.YangBindingProviderModule; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.Socket; -import java.util.Set; import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; @@ -62,22 +44,6 @@ public class BgpDistributionTest { private static final byte BGP_OPEN_MSG_TYPE = 1; private static final int BGP_PORT = 1790; - public static final Set BASE_MODULES = of( - new YangBindingProviderModule(), - new SchemaModule(), - new ConfigAndOperationalPipelineModule(), - new ContextPipelineModule(), - new InitializerPipelineModule(), - new NetconfModule(), - new NetconfReadersModule(), - new RestconfModule(), - new CfgAttrsModule(), - new BgpModule(), - new BgpExtensionsModule(), - new BgpReadersModule(), - new BgpWritersModule(), - new BgpConfigurationModule()); - @Before public void setUp() throws Exception { SSLContext sslcontext = SSLContexts.custom() @@ -94,7 +60,7 @@ public class BgpDistributionTest { @Test(timeout = 120000) public void test() throws Exception { - io.fd.honeycomb.infra.bgp.distro.Main.init(BASE_MODULES); + io.fd.honeycomb.infra.bgp.distro.Main.init(); LOG.info("Testing Honeycomb BGP distribution"); assertBgp(); } diff --git a/infra/bgp-distribution/src/test/resources/activation.json b/infra/bgp-distribution/src/test/resources/activation.json new file mode 100644 index 000000000..7e2015123 --- /dev/null +++ b/infra/bgp-distribution/src/test/resources/activation.json @@ -0,0 +1,3 @@ +{ + "modules-resource-path": "base-distro-test-modules" +} \ No newline at end of file diff --git a/infra/bgp-distribution/src/test/resources/base-distro-test-modules/base-modules b/infra/bgp-distribution/src/test/resources/base-distro-test-modules/base-modules new file mode 100644 index 000000000..18602a0ad --- /dev/null +++ b/infra/bgp-distribution/src/test/resources/base-distro-test-modules/base-modules @@ -0,0 +1,14 @@ +io.fd.honeycomb.infra.bgp.BgpConfigurationModule +io.fd.honeycomb.infra.bgp.BgpExtensionsModule +io.fd.honeycomb.infra.bgp.BgpModule +io.fd.honeycomb.infra.bgp.BgpReadersModule +io.fd.honeycomb.infra.bgp.BgpWritersModule +io.fd.honeycomb.infra.distro.cfgattrs.CfgAttrsModule +io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule +io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule +io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule +io.fd.honeycomb.infra.distro.netconf.NetconfModule +io.fd.honeycomb.infra.distro.netconf.NetconfReadersModule +io.fd.honeycomb.infra.distro.restconf.RestconfModule +io.fd.honeycomb.infra.distro.schema.SchemaModule +io.fd.honeycomb.infra.distro.schema.YangBindingProviderModule \ No newline at end of file diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/ActiveModuleProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/ActiveModuleProvider.java deleted file mode 100644 index 03c56969d..000000000 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/ActiveModuleProvider.java +++ /dev/null @@ -1,154 +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.infra.distro; - -import com.google.common.collect.ImmutableList; -import com.google.inject.Module; -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import javax.annotation.Nonnull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Provides list of active modules for distribution - */ -public class ActiveModuleProvider { - - public static final String STANDARD_MODULES_RELATIVE_PATH = "../modules/"; - private static final Logger LOG = LoggerFactory.getLogger(ActiveModuleProvider.class); - - /** - * Provide unique set of active modules filtered from provided resources - */ - public static Set loadActiveModules(@Nonnull final List moduleNames) { - final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader(); - LOG.info("Reading active modules configuration for distribution"); - - // process resources to resource modules - return moduleNames.stream() - .map(String::trim) - .filter(trimmedLine -> trimmedLine.length() != 0) - // filter out commented lines - .filter(nonEmptyLine -> !nonEmptyLine.startsWith("//")) - // filter duplicates - .distinct() - .map(validLine -> nameToClass(validLine, classLoader)) - // filters out classes that are not modules - .filter(ActiveModuleProvider::filterNonModules) - .map(ActiveModuleProvider::classToInstance) - .collect(Collectors.toSet()); - } - - /** - * Aggregate all resources from provided relative path into a {@code List} - */ - public static List aggregateResources(final String relativePath, final ClassLoader classLoader) { - try { - return Collections.list(classLoader.getResources(relativePath)).stream() - .map(ActiveModuleProvider::toURI) - .flatMap(ActiveModuleProvider::folderToFile) - .map(File::toURI) - .map(Paths::get) - // readAll lines and add them to iteration - .flatMap(ActiveModuleProvider::readLines) - .collect(Collectors.toList()); - } catch (IOException e) { - LOG.error("Unable to load resources from relative path {}", relativePath, e); - throw new IllegalStateException("Unable to load resources from relative path " + relativePath, e); - } - } - - private static Stream folderToFile(final URI uri) { - final File[] files = new File(uri).listFiles(File::isFile); - - return files != null - ? ImmutableList.copyOf(files).stream() - : Collections.emptyList().stream(); - } - - private static boolean filterNonModules(final Class clazz) { - final boolean isModule = Module.class.isAssignableFrom(clazz); - if (!isModule) { - LOG.warn("Class {} is provided in modules configuration, but is not a Module and will be ignored", clazz); - } - return isModule; - } - - /** - * Read lines from {@code Path} - */ - private static Stream readLines(final Path path) { - try { - return Files.readAllLines(path).stream(); - } catch (IOException e) { - LOG.error("Unable to read content of {}", path, e); - throw new IllegalStateException("Unable to read content of " + path, e); - } - } - - /** - * Converts {@code URL} to {@code URI} - */ - private static URI toURI(final URL url) { - try { - return url.toURI(); - } catch (URISyntaxException e) { - LOG.error("Unable to convert {} to uri", url); - throw new IllegalStateException("Unable to convert " + url + " to uri", e); - } - } - - /** - * Loads class by provided name - */ - private static Class nameToClass(final String name, - final ClassLoader classLoader) { - try { - LOG.info("Loading module class {}", name); - return classLoader.loadClass(name); - } catch (ClassNotFoundException e) { - LOG.error("Unable to convert {} to class, make sure you've provided sources to classpath", name); - throw new IllegalStateException( - "Unable to convert " + name + " to class, make sure you've provided sources to classpath", e); - } - } - - /** - * Creates instance of module class - */ - private static Module classToInstance(final Class moduleClass) { - try { - LOG.info("Creating instance for module {}", moduleClass); - return Module.class.cast(moduleClass.newInstance()); - } catch (InstantiationException | IllegalAccessException e) { - LOG.error("Unable to create instance for class {}", moduleClass, e); - throw new IllegalStateException("Unable to create instance for class" + moduleClass, e); - } - } -} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java index 4769b570d..265740bcc 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java @@ -16,11 +16,10 @@ package io.fd.honeycomb.infra.distro; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.STANDARD_MODULES_RELATIVE_PATH; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.aggregateResources; -import static io.fd.honeycomb.infra.distro.ActiveModuleProvider.loadActiveModules; +import static com.google.inject.Guice.createInjector; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; import com.google.inject.ConfigurationException; import com.google.inject.CreationException; import com.google.inject.Guice; @@ -31,6 +30,8 @@ import com.google.inject.ProvisionException; import com.google.inject.name.Names; import io.fd.honeycomb.data.init.DataTreeInitializer; import io.fd.honeycomb.data.init.InitializerRegistry; +import io.fd.honeycomb.infra.distro.activation.ActivationModule; +import io.fd.honeycomb.infra.distro.activation.ActiveModules; import io.fd.honeycomb.infra.distro.cfgattrs.HoneycombConfiguration; import io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule; import io.fd.honeycomb.infra.distro.netconf.HoneycombNotification2NetconfProvider; @@ -38,7 +39,6 @@ import io.fd.honeycomb.infra.distro.netconf.NetconfModule; import io.fd.honeycomb.infra.distro.netconf.NetconfSshServerProvider; import io.fd.honeycomb.infra.distro.netconf.NetconfTcpServerProvider; import io.fd.honeycomb.infra.distro.restconf.RestconfModule; -import java.util.Set; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory; @@ -54,17 +54,23 @@ public final class Main { } public static void main(String[] args) { - final ClassLoader classLoader = Main.class.getClassLoader(); - init(loadActiveModules(aggregateResources(STANDARD_MODULES_RELATIVE_PATH, classLoader))); + init(); } /** * Initialize the Honeycomb with provided modules */ - public static Injector init(final Set modules) { + public static Injector init() { try { LOG.info("Starting honeycomb"); - Injector injector = Guice.createInjector(modules); + final ActivationModule activationModule = new ActivationModule(); + // creating child injector does not work in this case, so just create injector, and does not store ref + // to it, or its active modules instance + Injector injector = createInjector(ImmutableSet.builder() + .add(activationModule) + .addAll(createInjector(activationModule).getInstance(ActiveModules.class).createModuleInstances()) + .build()); + LOG.info("Honeycomb configuration: {}", injector.getInstance(HoneycombConfiguration.class)); // Log all bindings diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java new file mode 100644 index 000000000..8777c32a2 --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.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.infra.distro.activation; + +import java.util.Optional; +import net.jmob.guice.conf.core.BindConfig; +import net.jmob.guice.conf.core.InjectConfig; +import net.jmob.guice.conf.core.Syntax; + +@BindConfig(value = "activation", syntax = Syntax.JSON) +public class ActivationConfig { + + @InjectConfig("modules-resource-path") + private String modulesResourcePath; + + public String getModulesResourcePath() { + return Optional.ofNullable(modulesResourcePath).orElse("../modules/"); + } +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java new file mode 100644 index 000000000..00f77a66d --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.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.infra.distro.activation; + +import com.google.inject.AbstractModule; +import net.jmob.guice.conf.core.ConfigurationModule; + +/** + * Module that provides set of modules activated by distribution and binds this set to be available + */ +public class ActivationModule extends AbstractModule { + @Override + protected void configure() { + install(ConfigurationModule.create()); + requestInjection(ActivationConfig.class); + bind(ActiveModules.class).toProvider(ActiveModuleProvider.class).asEagerSingleton(); + } +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java new file mode 100644 index 000000000..21d108082 --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2017 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.infra.distro.activation; + +import com.google.common.collect.ImmutableList; +import com.google.inject.Inject; +import com.google.inject.Module; +import com.google.inject.Provider; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.annotation.Nonnull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides list of active modules for distribution + */ +class ActiveModuleProvider implements Provider { + + private static final Logger LOG = LoggerFactory.getLogger(ActiveModuleProvider.class); + + @Inject + private ActivationConfig config; + + @Override + public ActiveModules get() { + return new ActiveModules(loadActiveModules( + aggregateResources(config.getModulesResourcePath(), Thread.currentThread().getContextClassLoader()))); + } + + /** + * Provide unique set of active modules filtered from provided resources + */ + static Set> loadActiveModules(@Nonnull final List moduleNames) { + final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader(); + LOG.info("Reading active modules configuration for distribution"); + + // process resources to resource modules + return moduleNames.stream() + .map(String::trim) + .filter(trimmedLine -> trimmedLine.length() != 0) + // filter out commented lines + .filter(nonEmptyLine -> !nonEmptyLine.startsWith("//")) + // filter duplicates + .distinct() + .map(validLine -> moduleNameToClass(validLine, classLoader)) + // filters out classes that are not modules + .filter(ActiveModuleProvider::filterNonModules) + .collect(Collectors.toSet()); + } + + /** + * Aggregate all resources from provided relative path into a {@code List} + */ + public static List aggregateResources(final String relativePath, final ClassLoader classLoader) { + try { + return Collections.list(classLoader.getResources(relativePath)).stream() + .map(ActiveModuleProvider::toURI) + .flatMap(ActiveModuleProvider::folderToFile) + .map(File::toURI) + .map(Paths::get) + // readAll lines and add them to iteration + .flatMap(ActiveModuleProvider::readLines) + .collect(Collectors.toList()); + } catch (IOException e) { + LOG.error("Unable to load resources from relative path {}", relativePath, e); + throw new IllegalStateException("Unable to load resources from relative path " + relativePath, e); + } + } + + private static Stream folderToFile(final URI uri) { + final File[] files = new File(uri).listFiles(File::isFile); + + return files != null + ? ImmutableList.copyOf(files).stream() + : Collections.emptyList().stream(); + } + + private static boolean filterNonModules(final Class clazz) { + final boolean isModule = Module.class.isAssignableFrom(clazz); + if (!isModule) { + LOG.warn("Class {} is provided in modules configuration, but is not a Module and will be ignored", clazz); + } + return isModule; + } + + /** + * Read lines from {@code Path} + */ + private static Stream readLines(final Path path) { + try { + return Files.readAllLines(path).stream(); + } catch (IOException e) { + LOG.error("Unable to read content of {}", path, e); + throw new IllegalStateException("Unable to read content of " + path, e); + } + } + + /** + * Converts {@code URL} to {@code URI} + */ + private static URI toURI(final URL url) { + try { + return url.toURI(); + } catch (URISyntaxException e) { + LOG.error("Unable to convert {} to uri", url); + throw new IllegalStateException("Unable to convert " + url + " to uri", e); + } + } + + /** + * Loads class by provided name + */ + private static Class moduleNameToClass(final String name, + final ClassLoader classLoader) { + try { + LOG.info("Loading module class {}", name); + return (Class) classLoader.loadClass(name); + } catch (ClassNotFoundException e) { + LOG.error("Unable to convert {} to class, make sure you've provided sources to classpath", name); + throw new IllegalStateException( + "Unable to convert " + name + " to class, make sure you've provided sources to classpath", e); + } + } +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java new file mode 100644 index 000000000..f729f00b9 --- /dev/null +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java @@ -0,0 +1,57 @@ +/* + * 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.infra.distro.activation; + +import static java.lang.String.format; + +import com.google.inject.Module; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Provides static set of active activeModulesClasses + */ +public class ActiveModules { + private final Set> activeModulesClasses; + + public ActiveModules(final Set> activeModulesClasses) { + this.activeModulesClasses = activeModulesClasses; + } + + public Set> getActiveModulesClasses() { + return activeModulesClasses; + } + + public Set createModuleInstances() { + return activeModulesClasses.stream() + .map(moduleClass -> { + try { + return moduleClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalStateException(format("Unable to create instance of module %s", moduleClass), + e); + } + }).collect(Collectors.toSet()); + } + + @Override + public String toString() { + return "ActiveModules{" + + "activeModulesClasses=" + activeModulesClasses + + '}'; + } +} diff --git a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/schema/YangBindingProviderModule.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/schema/YangBindingProviderModule.java index 4348333a9..8e402808b 100644 --- a/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/schema/YangBindingProviderModule.java +++ b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/schema/YangBindingProviderModule.java @@ -56,6 +56,7 @@ public class YangBindingProviderModule extends AbstractModule { .map(YangBindingProviderModule::urlToString) .flatMap(content -> Lists.newArrayList(content.split("\n")).stream()) .filter(line -> !Strings.isNullOrEmpty(line.trim())) + .distinct() .map(YangBindingProviderModule::loadClass) .forEach(providerClass -> { LOG.debug("ModuleProvider found for {}", providerClass); diff --git a/infra/minimal-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json b/infra/minimal-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json new file mode 100644 index 000000000..7b6f44a20 --- /dev/null +++ b/infra/minimal-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json @@ -0,0 +1,3 @@ +{ + "modules-resource-path": "../modules/" +} \ No newline at end of file diff --git a/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/ActiveModuleProviderTest.java b/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/ActiveModuleProviderTest.java deleted file mode 100644 index bdadc5bd8..000000000 --- a/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/ActiveModuleProviderTest.java +++ /dev/null @@ -1,91 +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.infra.distro; - - -import static com.google.common.collect.ImmutableList.of; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.isA; -import static org.hamcrest.core.Is.is; - -import com.google.common.collect.ImmutableList; -import com.google.inject.Module; -import java.util.List; -import java.util.Set; -import org.junit.Test; - -public class ActiveModuleProviderTest { - - @Test - public void testLoadActiveModulesSuccessfull() { - final ImmutableList rawResources = of( - "// this should be skipped", - "// io.fd.honeycomb.infra.distro.Modules$ChildModule1", - " io.fd.honeycomb.infra.distro.Modules$ChildModule2", - "io.fd.honeycomb.infra.distro.Modules$ChildModule3 ", - "io.fd.honeycomb.infra.distro.Modules$ChildModule3", - "io.fd.honeycomb.infra.distro.Modules$NonModule" - ); - - final Set activeModules = ActiveModuleProvider.loadActiveModules(rawResources); - - // first and second line should be ignored due to comment - // second,third,and fourth are valid,but should reduce module count to 2,because of duplicity - // last one does is not ancestor of Module, so it should be ignored/skipped - assertThat(activeModules, hasSize(2)); - //hasItems or containsInAnyOrder does not have/is deprecated in variant with matcher - assertThat(activeModules, hasItem(isA(io.fd.honeycomb.infra.distro.Modules.ChildModule2.class))); - assertThat(activeModules, hasItem(isA(io.fd.honeycomb.infra.distro.Modules.ChildModule3.class))); - } - - @Test(expected = IllegalStateException.class) - public void testLoadActiveModulesFailed() { - final ImmutableList rawResources = of( - "// this should be skipped", - "// io.fd.honeycomb.infra.distro.Modules$ChildModule1", - " io.fd.honeycomb.infra.distro.Modules$ChildModule2", - "### io.fd.honeycomb.infra.distro.Modules$ChildModule3 ",// it should fail because of this - "io.fd.honeycomb.infra.distro.Modules$ChildModule3", - "io.fd.honeycomb.infra.distro.Modules$NonModule" - ); - - ActiveModuleProvider.loadActiveModules(rawResources); - } - - @Test - public void testAggregateResourcesNonEmpty() { - final List aggregatedResources = - ActiveModuleProvider.aggregateResources("./modules", this.getClass().getClassLoader()); - assertThat(aggregatedResources, hasSize(5)); - assertThat(aggregatedResources, hasItems(" Non-commented non-trimmed", - "//Commented", - "// Commented non-trimmed", - "Not commented", - "// Line from second file")); - } - - @Test - public void testAggregateResourcesEmpty() { - assertThat(ActiveModuleProvider.aggregateResources("/non-existing-folder", this.getClass().getClassLoader()), - is(empty())); - } - -} diff --git a/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/BaseMinimalDistributionTest.java b/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/BaseMinimalDistributionTest.java index d8e06042a..e39dba8a0 100644 --- a/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/BaseMinimalDistributionTest.java +++ b/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/BaseMinimalDistributionTest.java @@ -16,34 +16,22 @@ package io.fd.honeycomb.infra.distro; -import static com.google.common.collect.ImmutableSet.of; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import com.google.common.base.Charsets; import com.google.common.io.ByteStreams; -import com.google.inject.Module; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSubsystem; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; -import io.fd.honeycomb.infra.distro.cfgattrs.CfgAttrsModule; -import io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule; -import io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule; -import io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule; -import io.fd.honeycomb.infra.distro.netconf.NetconfModule; -import io.fd.honeycomb.infra.distro.netconf.NetconfReadersModule; -import io.fd.honeycomb.infra.distro.restconf.RestconfModule; -import io.fd.honeycomb.infra.distro.schema.SchemaModule; -import io.fd.honeycomb.infra.distro.schema.YangBindingProviderModule; import java.io.IOException; import java.io.InputStream; import java.net.Socket; import java.util.Properties; -import java.util.Set; import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; @@ -69,17 +57,6 @@ public class BaseMinimalDistributionTest { private static final String NETCONF_NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0"; private static final int HELLO_WAIT = 2500; - public static final Set BASE_MODULES = of( - new YangBindingProviderModule(), - new SchemaModule(), - new ConfigAndOperationalPipelineModule(), - new ContextPipelineModule(), - new InitializerPipelineModule(), - new NetconfModule(), - new NetconfReadersModule(), - new RestconfModule(), - new CfgAttrsModule()); - @Before public void setUp() throws Exception { SSLContext sslcontext = SSLContexts.custom() @@ -99,7 +76,7 @@ public class BaseMinimalDistributionTest { */ @Test(timeout = 120000) public void test() throws Exception { - Main.init(BASE_MODULES); + Main.init(); LOG.info("Testing Honeycomb base distribution"); LOG.info("Testing NETCONF TCP"); diff --git a/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProviderTest.java b/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProviderTest.java new file mode 100644 index 000000000..fd2c6c860 --- /dev/null +++ b/infra/minimal-distribution/src/test/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProviderTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017 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.infra.distro.activation; + + +import static com.google.common.collect.ImmutableList.of; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.isA; +import static org.hamcrest.core.Is.is; + +import com.google.common.collect.ImmutableList; +import com.google.inject.Module; +import java.util.List; +import java.util.Set; +import org.junit.Test; + +public class ActiveModuleProviderTest { + + @Test + public void testLoadActiveModulesSuccessfull() { + final ImmutableList rawResources = of( + "// this should be skipped", + "// io.fd.honeycomb.infra.distro.Modules$ChildModule1", + " io.fd.honeycomb.infra.distro.Modules$ChildModule2", + "io.fd.honeycomb.infra.distro.Modules$ChildModule3 ", + "io.fd.honeycomb.infra.distro.Modules$ChildModule3", + "io.fd.honeycomb.infra.distro.Modules$NonModule" + ); + // have to be without wildcard, otherwise mockito has problem with it + final Set activeModules = (Set) new ActiveModules(ActiveModuleProvider.loadActiveModules(rawResources)).createModuleInstances(); + + // first and second line should be ignored due to comment + // second,third,and fourth are valid,but should reduce module count to 2,because of duplicity + // last one does is not ancestor of Module, so it should be ignored/skipped + assertThat(activeModules, hasSize(2)); + //hasItems or containsInAnyOrder does not have/is deprecated in variant with matcher + assertThat(activeModules, hasItem(isA(io.fd.honeycomb.infra.distro.Modules.ChildModule2.class))); + assertThat(activeModules, hasItem(isA(io.fd.honeycomb.infra.distro.Modules.ChildModule3.class))); + } + + @Test(expected = IllegalStateException.class) + public void testLoadActiveModulesFailed() { + final ImmutableList rawResources = of( + "// this should be skipped", + "// io.fd.honeycomb.infra.distro.Modules$ChildModule1", + " io.fd.honeycomb.infra.distro.Modules$ChildModule2", + "### io.fd.honeycomb.infra.distro.Modules$ChildModule3 ",// it should fail because of this + "io.fd.honeycomb.infra.distro.Modules$ChildModule3", + "io.fd.honeycomb.infra.distro.Modules$NonModule" + ); + + ActiveModuleProvider.loadActiveModules(rawResources); + } + + @Test + public void testAggregateResourcesNonEmpty() { + final List aggregatedResources = + ActiveModuleProvider.aggregateResources("modules", this.getClass().getClassLoader()); + assertThat(aggregatedResources, hasSize(5)); + assertThat(aggregatedResources, hasItems(" Non-commented non-trimmed", + "//Commented", + "// Commented non-trimmed", + "Not commented", + "// Line from second file")); + } + + @Test + public void testAggregateResourcesEmpty() { + assertThat(ActiveModuleProvider.aggregateResources("/non-existing-folder", this.getClass().getClassLoader()), + is(empty())); + } + +} diff --git a/infra/minimal-distribution/src/test/resources/activation.json b/infra/minimal-distribution/src/test/resources/activation.json new file mode 100644 index 000000000..7e2015123 --- /dev/null +++ b/infra/minimal-distribution/src/test/resources/activation.json @@ -0,0 +1,3 @@ +{ + "modules-resource-path": "base-distro-test-modules" +} \ No newline at end of file diff --git a/infra/minimal-distribution/src/test/resources/base-distro-test-modules/base-modules b/infra/minimal-distribution/src/test/resources/base-distro-test-modules/base-modules new file mode 100644 index 000000000..28b28446c --- /dev/null +++ b/infra/minimal-distribution/src/test/resources/base-distro-test-modules/base-modules @@ -0,0 +1,9 @@ +io.fd.honeycomb.infra.distro.cfgattrs.CfgAttrsModule +io.fd.honeycomb.infra.distro.data.ConfigAndOperationalPipelineModule +io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule +io.fd.honeycomb.infra.distro.initializer.InitializerPipelineModule +io.fd.honeycomb.infra.distro.netconf.NetconfModule +io.fd.honeycomb.infra.distro.netconf.NetconfReadersModule +io.fd.honeycomb.infra.distro.restconf.RestconfModule +io.fd.honeycomb.infra.distro.schema.SchemaModule +io.fd.honeycomb.infra.distro.schema.YangBindingProviderModule \ No newline at end of file -- cgit 1.2.3-korg