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 com.google.common.io.Resources;
11 import java.io.IOException;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.LinkedList;
17 import java.util.List;
18 import org.opendaylight.yangtools.concepts.ObjectRegistration;
19 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
20 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.BundleEvent;
24 import org.osgi.util.tracker.BundleTracker;
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 AutoCloseable,
33 BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
35 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
37 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
40 private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
41 private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
42 private boolean starting;
44 public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
45 this.moduleInfoRegistry = moduleInfoRegistry;
48 public void open(BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
49 LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
51 if(bundleTracker != null) {
52 this.bundleTracker = bundleTracker;
57 moduleInfoRegistry.updateService();
62 LOG.debug("ModuleInfoBundleTracker open complete");
67 if(bundleTracker != null) {
68 bundleTracker.close();
73 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
74 URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
75 LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
76 if(resource == null) {
77 return Collections.emptyList();
79 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
82 for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
83 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
84 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
85 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
89 moduleInfoRegistry.updateService();
91 } catch (IOException e) {
92 LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
93 } catch (RuntimeException e) {
94 LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
97 LOG.trace("Got following registrations {}", registrations);
102 public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
106 public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
111 for (ObjectRegistration<YangModuleInfo> reg : regs) {
114 } catch (Exception e) {
115 LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
120 private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
122 Class<?> clazz = loadClass(moduleInfoClass, bundle);
124 if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
125 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
126 throw new IllegalStateException(errorMessage);
128 YangModelBindingProvider instance;
130 Object instanceObj = clazz.newInstance();
131 instance = YangModelBindingProvider.class.cast(instanceObj);
132 } catch (InstantiationException e) {
133 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
134 throw new IllegalStateException(errorMessage, e);
135 } catch (IllegalAccessException e) {
136 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
137 moduleInfoClass, bundle, e);
138 throw new IllegalStateException(errorMessage, e);
142 return instance.getModuleInfo();
143 } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
144 throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
148 private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
150 return bundle.loadClass(moduleInfoClass);
151 } catch (ClassNotFoundException e) {
152 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
153 throw new IllegalStateException(errorMessage);
157 public static String logMessage(String slfMessage, Object... params) {
158 LOG.info(slfMessage, params);
159 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
160 return String.format(formatMessage, params);