2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.osgi.mapping;
10 import org.apache.commons.io.IOUtils;
11 import org.opendaylight.yangtools.concepts.Registration;
12 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
13 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
14 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
15 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
16 import org.osgi.framework.Bundle;
17 import org.osgi.framework.BundleEvent;
18 import org.osgi.util.tracker.BundleTrackerCustomizer;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.io.InputStream;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.List;
28 import static java.lang.String.format;
31 * Tracks bundles and attempts to retrieve YangModuleInfo.
33 public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<Registration<YangModuleInfo>>> {
35 private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
36 public static final String GET_MODULE_INFO_METHOD = "getModuleInfo";
38 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
40 private ModuleInfoBackedContext moduleInfoLoadingStrategy = ModuleInfoBackedContext.create();
42 public GeneratedClassLoadingStrategy getModuleInfoLoadingStrategy() {
43 return moduleInfoLoadingStrategy;
47 public Collection<Registration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
48 URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
54 List<Registration<YangModuleInfo>> registrations = new LinkedList<>();
56 try (InputStream inputStream = resource.openStream()) {
57 List<String> lines = IOUtils.readLines(inputStream);
58 for (String moduleInfoName : lines) {
59 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
60 registrations.add(moduleInfoLoadingStrategy.registerModuleInfo(moduleInfo));
64 } catch (Exception e) {
65 logger.error("Error while reading {}", resource, e);
66 throw new RuntimeException(e);
73 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> object) {
78 public void removedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> regs) {
83 for (Registration<YangModuleInfo> reg : regs) {
86 } catch (Exception e) {
87 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
92 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
94 Class<?> clazz = loadClass(moduleInfoClass, bundle);
96 if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
97 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
98 throw new IllegalStateException(errorMessage);
100 YangModelBindingProvider instance;
102 Object instanceObj = clazz.newInstance();
103 instance = YangModelBindingProvider.class.cast(instanceObj);
104 } catch (InstantiationException e) {
105 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
106 throw new IllegalStateException(errorMessage, e);
107 } catch (IllegalAccessException e) {
108 errorMessage = logMessage("Illegal access during instatiation of class {} in bundle {}, reason {}",
109 moduleInfoClass, bundle, e);
110 throw new IllegalStateException(errorMessage, e);
113 return instance.getModuleInfo();
114 } catch (NoClassDefFoundError e) {
117 logger.error("Error while executing getModuleInfo on {}", instance, e);
122 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
124 return bundle.loadClass(moduleInfoClass);
125 } catch (ClassNotFoundException e) {
126 String errorMessage = logMessage("Could not find class {} in bunde {}, reason {}", moduleInfoClass, bundle, e);
127 throw new IllegalStateException(errorMessage);
131 public static String logMessage(String slfMessage, Object... params) {
132 logger.info(slfMessage, params);
133 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
134 return format(formatMessage, params);