Honeynode test tool
[transportpce.git] / tests / honeynode / minimal-distribution-core / src / main / java / io / fd / honeycomb / infra / distro / schema / YangModulesProvider.java
1 /*
2  * Copyright (c) 2016 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.schema;
18
19
20 import com.google.inject.Inject;
21 import com.google.inject.Provider;
22 import io.fd.honeycomb.infra.distro.activation.ActivationConfig;
23 import io.fd.honeycomb.infra.distro.activation.ActiveModules;
24 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
25
26 import javax.annotation.Nonnull;
27 import java.util.Collection;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30
31 import static java.lang.String.format;
32
33 /**
34  * Loads active yang modules
35  * Relying on generate yang-module-index
36  */
37 public class YangModulesProvider implements Provider<YangModulesProvider.YangModules> {
38
39     @Inject
40     private ActiveModules activeModules;
41
42     @Inject
43     private ActivationConfig config;
44
45     @Override
46     public YangModules get() {
47         // no need to bind this, pretty big map and its needed just here
48         final YangModuleMappingIndex index = new YangModuleMappingIndex(config.getYangModulesIndexPath());
49
50         return new YangModules(activeModules.getActiveModulesClasses().stream()
51                 .map(Class::getName)
52                 .map(index::getByModuleName)
53                 .flatMap(Collection::stream)
54                 .map(YangModulesProvider::loadClass)
55                 .map(aClass -> (Class<? extends YangModelBindingProvider>) aClass)
56                 .collect(Collectors.toSet()));
57     }
58
59     static class YangModules {
60         private final Set<Class<? extends YangModelBindingProvider>> yangBindings;
61
62         YangModules(final Set<Class<? extends YangModelBindingProvider>> yangBindings) {
63             this.yangBindings = yangBindings;
64         }
65
66         Set<YangModelBindingProvider> getYangBindings() {
67             return yangBindings.stream()
68                     .map(providerClass -> {
69                         try {
70                             return providerClass.newInstance();
71                         } catch (InstantiationException | IllegalAccessException e) {
72                             throw new IllegalStateException(format("Unable to create instance of %s", providerClass),
73                                     e);
74                         }
75                     }).collect(Collectors.toSet());
76         }
77     }
78
79     private static Class<?> loadClass(@Nonnull final String className) {
80         try {
81             return Class.forName(className);
82         } catch (ClassNotFoundException e) {
83             throw new IllegalArgumentException("Unable to load class: " + className, e);
84         }
85     }
86 }