2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.mdsal.binding.dom.codec.osgi.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.io.Resources;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.IOException;
17 import java.nio.charset.StandardCharsets;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.regex.Pattern;
22 import org.opendaylight.yangtools.concepts.ObjectRegistration;
23 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
24 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
25 import org.osgi.framework.Bundle;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.BundleEvent;
28 import org.osgi.util.tracker.BundleTracker;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry.
35 final class ModuleInfoBundleTracker extends BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> {
36 private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
37 // FIXME: this should be in a place shared with maven-sal-api-gen-plugin
38 private static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
40 private static final String YANG_MODLE_BINDING_PROVIDER_SERVICE = MODULE_INFO_PROVIDER_PATH_PREFIX
41 + YangModelBindingProvider.class.getName();
43 private static final Pattern BRACE_PATTERN = Pattern.compile("{}", Pattern.LITERAL);
45 private final OsgiModuleInfoRegistry moduleInfoRegistry;
47 private volatile boolean deferUpdates = true;
49 ModuleInfoBundleTracker(final BundleContext context, final OsgiModuleInfoRegistry moduleInfoRegistry) {
50 super(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE, null);
51 this.moduleInfoRegistry = requireNonNull(moduleInfoRegistry);
58 moduleInfoRegistry.updateService();
68 @SuppressWarnings("checkstyle:illegalCatch")
69 public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
70 final URL resource = bundle.getEntry(YANG_MODLE_BINDING_PROVIDER_SERVICE);
71 if (resource == null) {
72 LOG.debug("Bundle {} does not have an entry for {}", bundle, YANG_MODLE_BINDING_PROVIDER_SERVICE);
73 return ImmutableList.of();
76 LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
77 final List<String> lines;
79 lines = Resources.readLines(resource, StandardCharsets.UTF_8);
80 } catch (IOException e) {
81 LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
82 return ImmutableList.of();
85 if (lines.isEmpty()) {
86 LOG.debug("Bundle {} has empty services for {}", bundle, YANG_MODLE_BINDING_PROVIDER_SERVICE);
87 return ImmutableList.of();
90 final List<ObjectRegistration<YangModuleInfo>> registrations = new ArrayList<>(lines.size());
91 for (String moduleInfoName : lines) {
92 LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
93 final YangModuleInfo moduleInfo;
95 moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
96 } catch (RuntimeException e) {
97 LOG.warn("Failed to acquire {} from bundle {}, ignoring it", moduleInfoName, bundle, e);
101 registrations.add(moduleInfoRegistry.registerInfo(moduleInfo));
105 moduleInfoRegistry.updateService();
108 LOG.trace("Bundle {} resulted in registrations {}", bundle, registrations);
109 return registrations;
113 public void modifiedBundle(final Bundle bundle, final BundleEvent event,
114 final Collection<ObjectRegistration<YangModuleInfo>> object) {
119 @SuppressWarnings("checkstyle:illegalCatch")
120 public void removedBundle(final Bundle bundle, final BundleEvent event,
121 final Collection<ObjectRegistration<YangModuleInfo>> regs) {
127 regs.forEach(reg -> {
130 } catch (Exception e) {
131 LOG.warn("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
136 moduleInfoRegistry.updateService();
141 private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
142 final Class<?> clazz = loadClass(moduleInfoClass, bundle);
143 if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
144 String errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
145 YangModelBindingProvider.class, bundle);
146 throw new IllegalStateException(errorMessage);
149 final YangModelBindingProvider instance;
151 Object instanceObj = clazz.newInstance();
152 instance = YangModelBindingProvider.class.cast(instanceObj);
153 } catch (InstantiationException e) {
154 String errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass,
156 throw new IllegalStateException(errorMessage, e);
157 } catch (IllegalAccessException e) {
158 String errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
159 moduleInfoClass, bundle, e);
160 throw new IllegalStateException(errorMessage, e);
164 return instance.getModuleInfo();
165 } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
166 throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
170 private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
172 return bundle.loadClass(moduleInfoClass);
173 } catch (ClassNotFoundException e) {
174 String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle,
176 throw new IllegalStateException(errorMessage);
180 @SuppressFBWarnings("SLF4J_UNKNOWN_ARRAY")
181 private static String logMessage(final String slfMessage, final Object... params) {
182 LOG.info(slfMessage, params);
183 return String.format(BRACE_PATTERN.matcher(slfMessage).replaceAll("%s"), params);