X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fosgi%2Fmapping%2FModuleInfoBundleTracker.java;h=57740477a1f6c6ec643d94776e7e62ef6e961753;hb=refs%2Fchanges%2F73%2F46573%2F5;hp=8ba290f30632e10101cc2185367844d29ee73f19;hpb=df2aabb0c61b9be04623c2a660c5a1592ff051f7;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java index 8ba290f306..57740477a1 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java @@ -7,85 +7,112 @@ */ package org.opendaylight.controller.config.manager.impl.osgi.mapping; -import org.apache.commons.io.IOUtils; -import org.opendaylight.yangtools.concepts.Registration; -import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy; -import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext; +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.opendaylight.yangtools.concepts.ObjectRegistration; 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; -import java.io.InputStream; -import java.lang.reflect.InvocationTargetException; -import java.net.URL; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import static java.lang.String.format; - /** - * Tracks bundles and attempts to retrieve YangModuleInfo. + * Tracks bundles and attempts to retrieve YangModuleInfo, which is then fed into ModuleInfoRegistry */ -public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer>> { +public final class ModuleInfoBundleTracker implements AutoCloseable, + BundleTrackerCustomizer>> { - private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class); - public static final String GET_MODULE_INFO_METHOD = "getModuleInfo"; + private static final Logger LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class); public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/"; - private ModuleInfoBackedContext moduleInfoLoadingStrategy = ModuleInfoBackedContext.create(); - public GeneratedClassLoadingStrategy getModuleInfoLoadingStrategy() { - return moduleInfoLoadingStrategy; + private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry; + private BundleTracker>> bundleTracker; + private boolean starting; + + public ModuleInfoBundleTracker(BundleContext context, RefreshingSCPModuleInfoRegistry moduleInfoRegistry) { + this.moduleInfoRegistry = moduleInfoRegistry; } - @Override - public Collection> addingBundle(Bundle bundle, BundleEvent event) { - URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName()); + public void open(BundleTracker>> 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"); + } - if(resource==null) { - return null; + @Override + public void close() { + if(bundleTracker != null) { + bundleTracker.close(); } + } - List> registrations = new LinkedList<>(); + @Override + public Collection> addingBundle(Bundle bundle, BundleEvent event) { + URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName()); + LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource); + if(resource == null) { + return Collections.emptyList(); + } + List> registrations = new LinkedList<>(); - try (InputStream inputStream = resource.openStream()) { - List lines = IOUtils.readLines(inputStream); - for (String moduleInfoName : lines) { + try { + for (String moduleInfoName : Resources.readLines(resource, StandardCharsets.UTF_8)) { + LOG.trace("Retrieve ModuleInfo({}, {})", moduleInfoName, bundle); YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle); - registrations.add(moduleInfoLoadingStrategy.registerModuleInfo(moduleInfo)); + 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); } + LOG.trace("Got following registrations {}", registrations); return registrations; } @Override - public void modifiedBundle(Bundle bundle, BundleEvent event, Collection> object) { - // NOOP + public void modifiedBundle(Bundle bundle, BundleEvent event, Collection> object) { } @Override - public void removedBundle(Bundle bundle, BundleEvent event, Collection> regs) { + public void removedBundle(Bundle bundle, BundleEvent event, Collection> regs) { if(regs == null) { return; } - for (Registration reg : regs) { + for (ObjectRegistration reg : regs) { 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); } } } @@ -94,49 +121,42 @@ public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer 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); } - + YangModelBindingProvider instance; try { - Object instance = clazz.newInstance(); - Object result = clazz.getMethod(GET_MODULE_INFO_METHOD).invoke(instance); - - if (YangModuleInfo.class.isAssignableFrom(result.getClass()) == false) { - errorMessage = logMessage("Error invoking method not found {} in bundle {}, reason {}", - GET_MODULE_INFO_METHOD, bundle, "Not assignable from " + YangModuleInfo.class); - } else { - return (YangModuleInfo) result; - } - + Object instanceObj = clazz.newInstance(); + instance = YangModelBindingProvider.class.cast(instanceObj); } catch (InstantiationException e) { errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e); + throw new IllegalStateException(errorMessage, e); } catch (IllegalAccessException e) { - errorMessage = logMessage("Illegal access during instatiation of class {} in bundle {}, reason {}", + errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}", moduleInfoClass, bundle, e); - } catch (NoSuchMethodException e) { - errorMessage = logMessage("Method not found {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD, bundle, e); - } catch (InvocationTargetException e) { - errorMessage = logMessage("Error invoking method {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD, - bundle, e); + throw new IllegalStateException(errorMessage, e); } - throw new IllegalStateException(errorMessage); + try{ + return instance.getModuleInfo(); + } catch (NoClassDefFoundError | ExceptionInInitializerError e) { + throw new IllegalStateException("Error while executing getModuleInfo on " + instance, e); + } } private static Class loadClass(String moduleInfoClass, Bundle bundle) { try { return bundle.loadClass(moduleInfoClass); } catch (ClassNotFoundException e) { - String errorMessage = logMessage("Could not find class {} in bunde {}, reason {}", moduleInfoClass, bundle, e); + String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e); throw new IllegalStateException(errorMessage); } } 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); } }