summaryrefslogtreecommitdiffstats
path: root/infra/minimal-distribution/src/main
diff options
context:
space:
mode:
authorJan Srnicek <jsrnicek@cisco.com>2017-06-27 09:25:04 +0200
committerMarek Gradzki <mgradzki@cisco.com>2017-06-30 11:16:29 +0000
commitef852789b2c156196a847b211066ae456c2683f5 (patch)
tree0ad11f02581de272fc7fc8a76b4f8e23c1b01354 /infra/minimal-distribution/src/main
parent74f4963ae3b0ccf3fdf67e9797000853cc9a068b (diff)
HONEYCOMB-358 - Activation module
Provides module that provides set of distribution started modules Change-Id: I54287cc17f3af7d51a47a7342e5b8496e5ade00e Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
Diffstat (limited to 'infra/minimal-distribution/src/main')
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/Main.java22
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationConfig.java33
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActivationModule.java32
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java (renamed from infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/ActiveModuleProvider.java)42
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModules.java57
-rw-r--r--infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/schema/YangBindingProviderModule.java1
-rw-r--r--infra/minimal-distribution/src/main/resources/honeycomb-minimal-resources/config/activation.json3
7 files changed, 159 insertions, 31 deletions
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<? extends Module> 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.<Module>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/ActiveModuleProvider.java b/infra/minimal-distribution/src/main/java/io/fd/honeycomb/infra/distro/activation/ActiveModuleProvider.java
index 03c56969d..21d108082 100644
--- 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/activation/ActiveModuleProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * 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.
@@ -14,10 +14,12 @@
* limitations under the License.
*/
-package io.fd.honeycomb.infra.distro;
+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;
@@ -38,15 +40,23 @@ import org.slf4j.LoggerFactory;
/**
* Provides list of active modules for distribution
*/
-public class ActiveModuleProvider {
+class ActiveModuleProvider implements Provider<ActiveModules> {
- public static final String STANDARD_MODULES_RELATIVE_PATH = "../modules/";
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
*/
- public static Set<Module> loadActiveModules(@Nonnull final List<String> moduleNames) {
+ static Set<Class<? extends Module>> loadActiveModules(@Nonnull final List<String> moduleNames) {
final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader();
LOG.info("Reading active modules configuration for distribution");
@@ -58,10 +68,9 @@ public class ActiveModuleProvider {
.filter(nonEmptyLine -> !nonEmptyLine.startsWith("//"))
// filter duplicates
.distinct()
- .map(validLine -> nameToClass(validLine, classLoader))
+ .map(validLine -> moduleNameToClass(validLine, classLoader))
// filters out classes that are not modules
.filter(ActiveModuleProvider::filterNonModules)
- .map(ActiveModuleProvider::classToInstance)
.collect(Collectors.toSet());
}
@@ -127,28 +136,15 @@ public class ActiveModuleProvider {
/**
* Loads class by provided name
*/
- private static Class<?> nameToClass(final String name,
- final ClassLoader classLoader) {
+ private static Class<? extends Module> moduleNameToClass(final String name,
+ final ClassLoader classLoader) {
try {
LOG.info("Loading module class {}", name);
- return classLoader.loadClass(name);
+ return (Class<? extends Module>) 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/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<Class<? extends Module>> activeModulesClasses;
+
+ public ActiveModules(final Set<Class<? extends Module>> activeModulesClasses) {
+ this.activeModulesClasses = activeModulesClasses;
+ }
+
+ public Set<Class<? extends Module>> getActiveModulesClasses() {
+ return activeModulesClasses;
+ }
+
+ public Set<? extends Module> 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