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;
11 import com.google.common.base.Charsets;
12 import com.google.common.io.Resources;
13 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.LinkedList;
17 import java.util.List;
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<>();
54 for (String moduleInfoName : Resources.readLines(resource, Charsets.UTF_8)) {
55 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
56 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
57 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
59 } catch (IOException e) {
60 LOG.error("Error while reading {}", resource, e);
61 throw new RuntimeException(e);
64 LOG.trace("Got following registrations {}", registrations);
69 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
73 public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
78 for (ObjectRegistration<YangModuleInfo> reg : regs) {
81 } catch (Exception e) {
82 throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
87 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
89 Class<?> clazz = loadClass(moduleInfoClass, bundle);
91 if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
92 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
93 throw new IllegalStateException(errorMessage);
95 YangModelBindingProvider instance;
97 Object instanceObj = clazz.newInstance();
98 instance = YangModelBindingProvider.class.cast(instanceObj);
99 } catch (InstantiationException e) {
100 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
101 throw new IllegalStateException(errorMessage, e);
102 } catch (IllegalAccessException e) {
103 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
104 moduleInfoClass, bundle, e);
105 throw new IllegalStateException(errorMessage, e);
108 return instance.getModuleInfo();
109 } catch (NoClassDefFoundError e) {
112 LOG.error("Error while executing getModuleInfo on {}", instance, e);
117 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
119 return bundle.loadClass(moduleInfoClass);
120 } catch (ClassNotFoundException e) {
121 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
122 throw new IllegalStateException(errorMessage);
126 public static String logMessage(String slfMessage, Object... params) {
127 LOG.info(slfMessage, params);
128 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
129 return format(formatMessage, params);