2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.infra.distro.schema;
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;
26 import javax.annotation.Nonnull;
27 import java.util.Collection;
29 import java.util.stream.Collectors;
31 import static java.lang.String.format;
34 * Loads active yang modules
35 * Relying on generate yang-module-index
37 public class YangModulesProvider implements Provider<YangModulesProvider.YangModules> {
40 private ActiveModules activeModules;
43 private ActivationConfig config;
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());
50 return new YangModules(activeModules.getActiveModulesClasses().stream()
52 .map(index::getByModuleName)
53 .flatMap(Collection::stream)
54 .map(YangModulesProvider::loadClass)
55 .map(aClass -> (Class<? extends YangModelBindingProvider>) aClass)
56 .collect(Collectors.toSet()));
59 static class YangModules {
60 private final Set<Class<? extends YangModelBindingProvider>> yangBindings;
62 YangModules(final Set<Class<? extends YangModelBindingProvider>> yangBindings) {
63 this.yangBindings = yangBindings;
66 Set<YangModelBindingProvider> getYangBindings() {
67 return yangBindings.stream()
68 .map(providerClass -> {
70 return providerClass.newInstance();
71 } catch (InstantiationException | IllegalAccessException e) {
72 throw new IllegalStateException(format("Unable to create instance of %s", providerClass),
75 }).collect(Collectors.toSet());
79 private static Class<?> loadClass(@Nonnull final String className) {
81 return Class.forName(className);
82 } catch (ClassNotFoundException e) {
83 throw new IllegalArgumentException("Unable to load class: " + className, e);