package org.opendaylight.controller.config.manager.impl.osgi.mapping;
import org.apache.commons.io.IOUtils;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
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 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.slf4j.LoggerFactory;
import java.io.InputStream;
-import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Collection;
import java.util.LinkedList;
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<Collection<Registration<YangModuleInfo>>> {
+public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<ObjectRegistration<YangModuleInfo>>> {
private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
- public static final String GET_MODULE_INFO_METHOD = "getModuleInfo";
public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
- private ModuleInfoBackedContext moduleInfoLoadingStrategy = ModuleInfoBackedContext.create();
- public GeneratedClassLoadingStrategy getModuleInfoLoadingStrategy() {
- return moduleInfoLoadingStrategy;
+ private final ModuleInfoRegistry moduleInfoRegistry;
+
+ public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
+ this.moduleInfoRegistry = moduleInfoRegistry;
}
@Override
- public Collection<Registration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
+ 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;
}
-
- List<Registration<YangModuleInfo>> registrations = new LinkedList<>();
+ 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);
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);
}
-
+ logger.trace("Got following registrations {}", registrations);
return registrations;
}
@Override
- public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> object) {
- // NOOP
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> object) {
}
@Override
- public void removedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> regs) {
+ public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
if(regs == null) {
return;
}
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);
}
+ try{
+ return instance.getModuleInfo();
+ } catch (NoClassDefFoundError e) {
- throw new IllegalStateException(errorMessage);
+
+ logger.error("Error while executing getModuleInfo on {}", instance, e);
+ throw 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);
}
}