Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / ModuleInfoBundleTracker.java
index 2a2a7784ebed7a48f298b607f957dc1921393768..cfbd95a39e8d22256c5474cdb4f409c6cba2b2d4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  */
 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.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
+ * 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 LOG = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
 
     public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
+    private static final String YANG_MODULE_INFO_SERVICE_PATH = MODULE_INFO_PROVIDER_PATH_PREFIX
+            + YangModelBindingProvider.class.getName();
 
+    private final RefreshingSCPModuleInfoRegistry moduleInfoRegistry;
 
-    private final ModuleInfoRegistry moduleInfoRegistry;
+    private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> tracker;
+    private boolean starting;
 
-    public ModuleInfoBundleTracker(ModuleInfoRegistry moduleInfoRegistry) {
+    public ModuleInfoBundleTracker(final RefreshingSCPModuleInfoRegistry moduleInfoRegistry) {
         this.moduleInfoRegistry = moduleInfoRegistry;
     }
 
+    public void open(final BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> bundleTracker) {
+        LOG.debug("ModuleInfoBundleTracker open starting with bundleTracker {}", bundleTracker);
+
+        if (bundleTracker != null) {
+            this.tracker = bundleTracker;
+            starting = true;
+            bundleTracker.open();
+
+            starting = false;
+            moduleInfoRegistry.updateService();
+        } else {
+            starting = false;
+        }
+
+        LOG.debug("ModuleInfoBundleTracker open complete");
+    }
+
+    @Override
+    public void close() {
+        if (tracker != null) {
+            tracker.close();
+            tracker = null;
+        }
+    }
+
     @Override
-    public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
-        URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
+    @SuppressWarnings("IllegalCatch")
+    public Collection<ObjectRegistration<YangModuleInfo>> addingBundle(final Bundle bundle, final BundleEvent event) {
+        URL resource = bundle.getEntry(YANG_MODULE_INFO_SERVICE_PATH);
         LOG.debug("Got addingBundle({}) with YangModelBindingProvider resource {}", bundle, resource);
-        if(resource==null) {
-            return null;
+        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) {
+        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) {
-            LOG.error("Error while reading {}", resource, e);
-            throw new RuntimeException(e);
+            if (!starting) {
+                moduleInfoRegistry.updateService();
+            }
+        } catch (final IOException e) {
+            LOG.error("Error while reading {} from bundle {}", resource, bundle, e);
+        } catch (final 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<ObjectRegistration<YangModuleInfo>> object) {
+    public void modifiedBundle(final Bundle bundle, final BundleEvent event,
+            final Collection<ObjectRegistration<YangModuleInfo>> object) {
     }
 
     @Override
-    public void removedBundle(Bundle bundle, BundleEvent event, Collection<ObjectRegistration<YangModuleInfo>> regs) {
-        if(regs == null) {
+    @SuppressWarnings("IllegalCatch")
+    public void removedBundle(final Bundle bundle, final BundleEvent event,
+            final Collection<ObjectRegistration<YangModuleInfo>> regs) {
+        if (regs == null) {
             return;
         }
 
         for (ObjectRegistration<YangModuleInfo> reg : regs) {
             try {
                 reg.close();
-            } catch (Exception e) {
-                throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
+            } catch (final Exception e) {
+                LOG.error("Unable to unregister YangModuleInfo {}", reg.getInstance(), e);
             }
         }
     }
 
-    private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
+    private static YangModuleInfo retrieveModuleInfo(final String moduleInfoClass, final Bundle bundle) {
         String errorMessage;
         Class<?> clazz = loadClass(moduleInfoClass, bundle);
 
-        if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
-            errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
+        if (!YangModelBindingProvider.class.isAssignableFrom(clazz)) {
+            errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz,
+                    YangModelBindingProvider.class, bundle);
             throw new IllegalStateException(errorMessage);
         }
-        YangModelBindingProvider instance;
+        final YangModelBindingProvider instance;
         try {
             Object instanceObj = clazz.newInstance();
             instance = YangModelBindingProvider.class.cast(instanceObj);
-        } catch (InstantiationException e) {
+        } catch (final InstantiationException e) {
             errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
             throw new IllegalStateException(errorMessage, e);
-        } catch (IllegalAccessException e) {
+        } catch (final IllegalAccessException e) {
             errorMessage = logMessage("Illegal access during instantiation of class {} in bundle {}, reason {}",
                     moduleInfoClass, bundle, e);
             throw new IllegalStateException(errorMessage, e);
         }
-        try{
-            return instance.getModuleInfo();
-        } catch (NoClassDefFoundError e) {
-
 
-            LOG.error("Error while executing getModuleInfo on {}", instance, e);
-            throw e;
+        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) {
+    private static Class<?> loadClass(final String moduleInfoClass, final Bundle bundle) {
         try {
             return bundle.loadClass(moduleInfoClass);
-        } catch (ClassNotFoundException e) {
-            String errorMessage = logMessage("Could not find class {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
+        } catch (final ClassNotFoundException 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) {
+    public static String logMessage(final String slfMessage, final Object... params) {
         LOG.info(slfMessage, params);
         String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
-        return format(formatMessage, params);
+        return String.format(formatMessage, params);
     }
 }