Add Honeynode emulator for device221
[transportpce.git] / tests / honeynode221 / minimal-distribution-core / src / main / java / io / fd / honeycomb / infra / distro / activation / ActiveModuleProvider.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.distro.activation;
18
19 import com.google.inject.Inject;
20 import com.google.inject.Module;
21 import com.google.inject.Provider;
22 import io.fd.honeycomb.infra.distro.schema.ResourceLoader;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import javax.annotation.Nonnull;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Provides list of active modules for distribution
33  */
34 public class ActiveModuleProvider implements Provider<ActiveModules>, ResourceLoader {
35
36     private static final Logger LOG = LoggerFactory.getLogger(ActiveModuleProvider.class);
37
38     @Inject
39     private ActivationConfig config;
40
41     @Override
42     public ActiveModules get() {
43         return new ActiveModules(loadActiveModules(
44                 aggregateResources(config.getModulesResourcePath())));
45     }
46
47     /**
48      * Provide unique set of active modules filtered from provided resources
49      */
50     static Set<Class<? extends Module>> loadActiveModules(@Nonnull final List<String> moduleNames) {
51         final ClassLoader classLoader = ActiveModuleProvider.class.getClassLoader();
52         LOG.info("Reading active modules configuration for distribution");
53
54         // process resources to resource modules
55         return moduleNames.stream()
56                 .map(String::trim)
57                 .filter(trimmedLine -> trimmedLine.length() != 0)
58                 // filter out commented lines
59                 .filter(nonEmptyLine -> !nonEmptyLine.startsWith("//"))
60                 // filter duplicates
61                 .distinct()
62                 .map(validLine -> moduleNameToClass(validLine, classLoader))
63                 // filters out classes that are not modules
64                 .filter(ActiveModuleProvider::filterNonModules)
65                 .collect(Collectors.toSet());
66     }
67
68     /**
69      * Aggregate all resources from provided relative path into a {@code List<String>}
70      */
71     public List<String> aggregateResources(final String relativePath) {
72         // must use universal approach of loading from folder/jar
73         // because of memory footprint benchmark
74         return new ArrayList<>(loadResourceContentsOnPath(relativePath));
75     }
76
77     private static boolean filterNonModules(final Class<?> clazz) {
78         final boolean isModule = Module.class.isAssignableFrom(clazz);
79         if (!isModule) {
80             LOG.warn("Class {} is provided in modules configuration, but is not a Module and will be ignored", clazz);
81         }
82         return isModule;
83     }
84
85     /**
86      * Loads class by provided name
87      */
88     private static Class<? extends Module> moduleNameToClass(final String name,
89                                                              final ClassLoader classLoader) {
90         try {
91             LOG.debug("Loading module class {}", name);
92             return (Class<? extends Module>) classLoader.loadClass(name);
93         } catch (ClassNotFoundException e) {
94             LOG.error("Unable to convert {} to class, make sure you've provided sources to classpath", name);
95             throw new IllegalStateException(
96                     "Unable to convert " + name + " to class, make sure you've provided sources to classpath", e);
97         }
98     }
99 }