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.BundleEvent;
23 import org.osgi.util.tracker.BundleTracker;
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 AutoCloseable,
32 BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
34 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
36 public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
37 private static final String YANG_MODULE_INFO_SERVICE_PATH = MODULE_INFO_PROVIDER_PATH_PREFIX
38 + YangModelBindingProvider.class.getName();
40 private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
42 private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
43 private boolean starting;
45 public ModuleInfoBundleTracker(final RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
46 this.moduleInfoRegistry = moduleInfoRegistry;
49 public void open(final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
50 LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
52 if (bundleTracker != null) {
53 this.bundleTracker = bundleTracker;
58 moduleInfoRegistry.updateService();
63 LOG.debug("ModuleInfoBundleTracker open complete");
68 if (bundleTracker != null) {
69 bundleTracker.close();
75 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
76 URL resource = bundle.getEntry(YANG_MODULE_INFO_SERVICE_PATH);
77 LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
78 if (resource == null) {
79 return Collections.emptyList();
81 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
84 for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
85 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
86 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
87 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
91 moduleInfoRegistry.updateService();
93 } catch (IOException e) {
94 LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
95 } catch (RuntimeException e) {
96 LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
99 LOG.trace("Got following registrations {}", registrations);
100 return registrations;
104 public void modifiedBundle(final Bundle bundle, final BundleEvent event,
105 final Collection<ObjectRegistration<YangModuleInfo>> object) {
109 public void removedBundle(final Bundle bundle, final BundleEvent event,
110 final Collection<ObjectRegistration<YangModuleInfo>> regs) {
115 for (ObjectRegistration<YangModuleInfo> reg : regs) {
118 } catch (Exception e) {
119 LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
124 private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
126 Class<?> clazz = loadClass(moduleInfoClass, bundle);
128 if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
129 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
130 YangModelBindingProvider.class, bundle);
131 throw new IllegalStateException(errorMessage);
133 final YangModelBindingProvider instance;
135 Object instanceObj = clazz.newInstance();
136 instance = YangModelBindingProvider.class.cast(instanceObj);
137 } catch (InstantiationException e) {
138 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
139 throw new IllegalStateException(errorMessage, e);
140 } catch (IllegalAccessException e) {
141 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
142 moduleInfoClass, bundle, e);
143 throw new IllegalStateException(errorMessage, e);
147 return instance.getModuleInfo();
148 } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
149 throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
153 private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
155 return bundle.loadClass(moduleInfoClass);
156 } catch (ClassNotFoundException e) {
157 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass,
159 throw new IllegalStateException(errorMessage);
163 public static String logMessage(final String slfMessage, final Object... params) {
164 LOG.info(slfMessage, params);
165 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
166 return String.format(formatMessage, params);