2 * Copyright (c) 2013, 2017 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
30 * into ModuleInfoRegistry.
32 public final class ModuleInfoBundleTracker
33 implements AutoCloseable, 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/";
38 private static final String YANG_MODULE_INFO_SERVICE_PATH = MODULE_INFO_PROVIDER_PATH_PREFIX
39 + YangModelBindingProvider.class.getName();
41 private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
43 private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> tracker;
44 private boolean starting;
46 public ModuleInfoBundleTracker(final RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
47 this.moduleInfoRegistry = moduleInfoRegistry;
50 public void open(final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
51 LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
53 if (bundleTracker != null) {
54 this.tracker = bundleTracker;
59 moduleInfoRegistry.updateService();
64 LOG.debug("ModuleInfoBundleTracker open complete");
69 if (tracker != null) {
76 @SuppressWarnings("IllegalCatch")
77 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
78 URL resource = bundle.getEntry(YANG_MODULE_INFO_SERVICE_PATH);
79 LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
80 if (resource == null) {
81 return Collections.emptyList();
83 List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
86 for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
87 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
88 YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
89 registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
93 moduleInfoRegistry.updateService();
95 } catch (final IOException e) {
96 LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
97 } catch (final RuntimeException e) {
98 LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
101 LOG.trace("Got following registrations {}", registrations);
102 return registrations;
106 public void modifiedBundle(final Bundle bundle, final BundleEvent event,
107 final Collection<ObjectRegistration<YangModuleInfo>> object) {
111 @SuppressWarnings("IllegalCatch")
112 public void removedBundle(final Bundle bundle, final BundleEvent event,
113 final Collection<ObjectRegistration<YangModuleInfo>> regs) {
118 for (ObjectRegistration<YangModuleInfo> reg : regs) {
121 } catch (final Exception e) {
122 LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
127 private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
129 Class<?> clazz = loadClass(moduleInfoClass, bundle);
131 if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
132 errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
133 YangModelBindingProvider.class, bundle);
134 throw new IllegalStateException(errorMessage);
136 final YangModelBindingProvider instance;
138 Object instanceObj = clazz.newInstance();
139 instance = YangModelBindingProvider.class.cast(instanceObj);
140 } catch (final InstantiationException e) {
141 errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
142 throw new IllegalStateException(errorMessage, e);
143 } catch (final IllegalAccessException e) {
144 errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
145 moduleInfoClass, bundle, e);
146 throw new IllegalStateException(errorMessage, e);
150 return instance.getModuleInfo();
151 } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
152 throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
156 private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
158 return bundle.loadClass(moduleInfoClass);
159 } catch (final ClassNotFoundException e) {
160 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle,
162 throw new IllegalStateException(errorMessage);
166 public static String logMessage(final String slfMessage, final Object... params) {
167 LOG.info(slfMessage, params);
168 String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
169 return String.format(formatMessage, params);