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.ObjectRegistration;
12 import org.opendaylight.yangtools.concepts.Registration;
13 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
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, which is then fed into ModuleInfoRegistry
33 public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
35 private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
37 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
40 private final ModuleInfoRegistry moduleInfoRegistry;
42 public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
43 this.moduleInfoRegistry = moduleInfoRegistry;
47 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
48 URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
49 logger.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
53 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
55 try (InputStream inputStream = resource.openStream()) {
56 List<String> lines = IOUtils.readLines(inputStream);
57 for (String moduleInfoName : lines) {
58 logger.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
59 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
60 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
63 } catch (Exception e) {
64 logger.error("Error while reading {}", resource, e);
65 throw new RuntimeException(e);
67 logger.trace("Got following registrations {}", registrations);
72 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
76 public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
81 for (Registration<YangModuleInfo> reg : regs) {
84 } catch (Exception e) {
85 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
90 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
92 Class<?> clazz = loadClass(moduleInfoClass, bundle);
94 if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
95 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
96 throw new IllegalStateException(errorMessage);
98 YangModelBindingProvider instance;
100 Object instanceObj = clazz.newInstance();
101 instance = YangModelBindingProvider.class.cast(instanceObj);
102 } catch (InstantiationException e) {
103 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
104 throw new IllegalStateException(errorMessage, e);
105 } catch (IllegalAccessException e) {
106 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
107 moduleInfoClass, bundle, e);
108 throw new IllegalStateException(errorMessage, e);
111 return instance.getModuleInfo();
112 } catch (NoClassDefFoundError e) {
115 logger.error("Error while executing getModuleInfo on {}", instance, e);
120 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
122 return bundle.loadClass(moduleInfoClass);
123 } catch (ClassNotFoundException e) {
124 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
125 throw new IllegalStateException(errorMessage);
129 public static String logMessage(String slfMessage, Object... params) {
130 logger.info(slfMessage, params);
131 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
132 return format(formatMessage, params);