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 static java.lang.String.format;
12 import java.io.InputStream;
14 import java.util.Collection;
15 import java.util.LinkedList;
16 import java.util.List;
18 import org.apache.commons.io.IOUtils;
19 import org.opendaylight.yangtools.concepts.ObjectRegistration;
20 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
21 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
22 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleEvent;
25 import org.osgi.util.tracker.BundleTrackerCustomizer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry
32 public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
34 private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
36 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
39 private final ModuleInfoRegistry moduleInfoRegistry;
41 public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
42 this.moduleInfoRegistry = moduleInfoRegistry;
46 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
47 URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
48 logger.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
52 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
54 try (InputStream inputStream = resource.openStream()) {
55 List<String> lines = IOUtils.readLines(inputStream);
56 for (String moduleInfoName : lines) {
57 logger.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
58 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
59 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
62 } catch (Exception e) {
63 logger.error("Error while reading {}", resource, e);
64 throw new RuntimeException(e);
66 logger.trace("Got following registrations {}", registrations);
71 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
75 public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
80 for (ObjectRegistration<YangModuleInfo> reg : regs) {
83 } catch (Exception e) {
84 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
89 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
91 Class<?> clazz = loadClass(moduleInfoClass, bundle);
93 if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
94 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
95 throw new IllegalStateException(errorMessage);
97 YangModelBindingProvider instance;
99 Object instanceObj = clazz.newInstance();
100 instance = YangModelBindingProvider.class.cast(instanceObj);
101 } catch (InstantiationException e) {
102 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
103 throw new IllegalStateException(errorMessage, e);
104 } catch (IllegalAccessException e) {
105 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
106 moduleInfoClass, bundle, e);
107 throw new IllegalStateException(errorMessage, e);
110 return instance.getModuleInfo();
111 } catch (NoClassDefFoundError e) {
114 logger.error("Error while executing getModuleInfo on {}", instance, e);
119 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
121 return bundle.loadClass(moduleInfoClass);
122 } catch (ClassNotFoundException e) {
123 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
124 throw new IllegalStateException(errorMessage);
128 public static String logMessage(String slfMessage, Object... params) {
129 logger.info(slfMessage, params);
130 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
131 return format(formatMessage, params);