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;
17 import org.apache.commons.io.IOUtils;
18 import org.opendaylight.yangtools.concepts.ObjectRegistration;
19 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
20 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
21 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
22 import org.osgi.framework.Bundle;
23 import org.osgi.framework.BundleEvent;
24 import org.osgi.util.tracker.BundleTrackerCustomizer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry
31 public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
33 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
35 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
38 private final ModuleInfoRegistry moduleInfoRegistry;
40 public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
41 this.moduleInfoRegistry = moduleInfoRegistry;
45 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
46 URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
47 LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
51 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
53 try (InputStream inputStream = resource.openStream()) {
54 List<String> lines = IOUtils.readLines(inputStream);
55 for (String moduleInfoName : lines) {
56 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
57 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
58 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
61 } catch (Exception e) {
62 LOG.error("Error while reading {}", resource, e);
63 throw new RuntimeException(e);
65 LOG.trace("Got following registrations {}", registrations);
70 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
74 public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
79 for (ObjectRegistration<YangModuleInfo> reg : regs) {
82 } catch (Exception e) {
83 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
88 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
90 Class<?> clazz = loadClass(moduleInfoClass, bundle);
92 if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
93 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
94 throw new IllegalStateException(errorMessage);
96 YangModelBindingProvider instance;
98 Object instanceObj = clazz.newInstance();
99 instance = YangModelBindingProvider.class.cast(instanceObj);
100 } catch (InstantiationException e) {
101 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
102 throw new IllegalStateException(errorMessage, e);
103 } catch (IllegalAccessException e) {
104 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
105 moduleInfoClass, bundle, e);
106 throw new IllegalStateException(errorMessage, e);
109 return instance.getModuleInfo();
110 } catch (NoClassDefFoundError e) {
113 LOG.error("Error while executing getModuleInfo on {}", instance, e);
118 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
120 return bundle.loadClass(moduleInfoClass);
121 } catch (ClassNotFoundException e) {
122 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
123 throw new IllegalStateException(errorMessage);
127 public static String logMessage(String slfMessage, Object... params) {
128 LOG.info(slfMessage, params);
129 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
130 return format(formatMessage, params);