Merge changes Ic434bf4a,I05a3fb18,I47a3783d,I8234bbfd
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / Activator.java
index 5642cc7188788fd74a7aeccf7ca8ffa97abd009e..faaa17d5280f289ed6a004fd1f6df95165a02eda 100644 (file)
@@ -8,61 +8,80 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
 
-import org.opendaylight.controller.config.yang.store.api.YangStoreService;
+import static com.google.common.base.Preconditions.checkState;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Hashtable;
-
-import static com.google.common.base.Preconditions.checkState;
+public class Activator implements BundleActivator {
 
-public class Activator implements BundleActivator, YangStoreServiceTracker.YangStoreTrackerListener {
-
-    private static final Logger logger = LoggerFactory.getLogger(Activator.class);
+    private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
 
     private BundleContext context;
-    private ServiceRegistration osgiRegistration;
+    private ServiceRegistration<?> osgiRegistration;
     private ConfigRegistryLookupThread configRegistryLookup = null;
 
     @Override
-    public void start(BundleContext context) throws Exception {
+    public void start(final BundleContext context) throws Exception {
         this.context = context;
-        YangStoreServiceTracker tracker = new YangStoreServiceTracker(context, this);
-        tracker.open();
-    }
 
-    @Override
-    public void stop(BundleContext context) throws Exception {
-        if (configRegistryLookup != null) {
-            configRegistryLookup.interrupt();
-        }
-    }
+        ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
+            @Override
+            public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
+                LOG.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
+                checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
 
-    @Override
-    public synchronized void onYangStoreAdded(YangStoreService yangStoreService) {
-        checkState(configRegistryLookup  == null, "More than one onYangStoreAdded received");
-        configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
-        configRegistryLookup.start();
+                SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
+
+                YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(schemaContextProvider);
+                configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
+                configRegistryLookup.start();
+                return configRegistryLookup;
+            }
+
+            @Override
+            public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
+                LOG.debug("Got modifiedService(SchemaContextProvider) event");
+                configRegistryLookup.yangStoreService.refresh();
+
+            }
+
+            @Override
+            public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
+                configRegistryLookup.interrupt();
+                if (osgiRegistration != null) {
+                    osgiRegistration.unregister();
+                }
+                osgiRegistration = null;
+                Activator.this.configRegistryLookup = null;
+            }
+        };
+
+        ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
+        listenerTracker.open();
     }
 
     @Override
-    public synchronized void onYangStoreRemoved() {
-        configRegistryLookup.interrupt();
-        if (osgiRegistration != null) {
-            osgiRegistration.unregister();
+    public void stop(BundleContext context) {
+        if (configRegistryLookup != null) {
+            configRegistryLookup.interrupt();
         }
-        osgiRegistration = null;
-        configRegistryLookup = null;
     }
 
     private class ConfigRegistryLookupThread extends Thread {
-        private final YangStoreService yangStoreService;
+        private final YangStoreServiceImpl yangStoreService;
 
-        private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
+        private ConfigRegistryLookupThread(YangStoreServiceImpl yangStoreService) {
             super("config-registry-lookup");
             this.yangStoreService = yangStoreService;
         }
@@ -70,9 +89,10 @@ public class Activator implements BundleActivator, YangStoreServiceTracker.YangS
         @Override
         public void run() {
             NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
-            logger.debug("Registering into OSGi");
-            osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
-                    new Hashtable<String, Object>());
+            LOG.debug("Registering into OSGi");
+            Dictionary<String, String> properties = new Hashtable<>();
+            properties.put("name", "config-netconf-connector");
+            osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);
         }
     }
 }