*/
package org.opendaylight.controller.config.manager.impl.osgi.mapping;
-import static java.lang.String.format;
-
-import java.io.InputStream;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
-
-import org.apache.commons.io.IOUtils;
import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTracker;
import org.osgi.util.tracker.BundleTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry
*/
-public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
+public final class ModuleInfoBundleTracker implements AutoCloseable,
+ BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
- private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
+ private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
- private final ModuleInfoRegistry moduleInfoRegistry;
+ private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
+ private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker;
+ private boolean starting;
- public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
+ public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
this.moduleInfoRegistry = moduleInfoRegistry;
}
+ public void open(BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
+ LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
+
+ if(bundleTracker != null) {
+ this.bundleTracker = bundleTracker;
+ starting = true;
+ bundleTracker.open();
+
+ starting = false;
+ moduleInfoRegistry.updateService();
+ } else {
+ starting = false;
+ }
+
+ LOG.debug("ModuleInfoBundleTracker open complete");
+ }
+
+ @Override
+ public void close() {
+ if(bundleTracker != null) {
+ bundleTracker.close();
+ }
+ }
+
@Override
public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
- logger.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
- if(resource==null) {
- return null;
+ LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
+ if(resource == null) {
+ return Collections.emptyList();
}
List<ObjectRegistration<YangModuleInfo>> registrations = new LinkedList<>();
- try (InputStream inputStream = resource.openStream()) {
- List<String> lines = IOUtils.readLines(inputStream);
- for (String moduleInfoName : lines) {
- logger.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
+ try {
+ for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) {
+ LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle);
YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
registrations.add(moduleInfoRegistry.registerModuleInfo(moduleInfo));
}
- } catch (Exception e) {
- logger.error("Error while reading {}", resource, e);
- throw new RuntimeException(e);
+ if(!starting) {
+ moduleInfoRegistry.updateService();
+ }
+ } catch (IOException e) {
+ LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
+ } catch (RuntimeException e) {
+ LOG.error("Failed to process {} for bundle {}", resource, bundle, e);
}
- logger.trace("Got following registrations {}", registrations);
+
+ LOG.trace("Got following registrations {}", registrations);
return registrations;
}
try {
reg.close();
} catch (Exception e) {
- throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
+ LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
}
}
}
String errorMessage;
Class<?> clazz = loadClass(moduleInfoClass, bundle);
- if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
+ if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
throw new IllegalStateException(errorMessage);
}
moduleInfoClass, bundle, e);
throw new IllegalStateException(errorMessage, e);
}
+
try{
return instance.getModuleInfo();
- } catch (NoClassDefFoundError e) {
-
-
- logger.error("Error while executing getModuleInfo on {}", instance, e);
- throw e;
+ } catch (NoClassDefFoundError | ExceptionInInitializerError e) {
+ throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e);
}
}
}
public static String logMessage(String slfMessage, Object... params) {
- logger.info(slfMessage, params);
+ LOG.info(slfMessage, params);
String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
- return format(formatMessage, params);
+ return String.format(formatMessage, params);
}
}