Add restart-dependents-on-update blueprint extension
[mdsal.git] / src / main / java / org / opendaylight / controller / blueprint / BlueprintBundleTracker.java
index 98e31e826b464741f5f0fe36a7f6a2c423245af3..5727c13ba1e888825d94b96bcc1c330bdf39fa1e 100644 (file)
@@ -12,7 +12,10 @@ import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.List;
+import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.services.BlueprintExtenderService;
+import org.apache.aries.util.AriesFrameworkUtil;
+import org.opendaylight.controller.blueprint.ext.OpendaylightNamespaceHandler;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -45,7 +48,9 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus
     private ServiceTracker<BlueprintExtenderService, BlueprintExtenderService> serviceTracker;
     private BundleTracker<Bundle> bundleTracker;
     private volatile BlueprintExtenderService blueprintExtenderService;
+    private volatile ServiceRegistration<?> blueprintContainerRestartReg;
     private ServiceRegistration<?> eventHandlerReg;
+    private ServiceRegistration<?> namespaceReg;
 
     /**
      * Implemented from BundleActivator.
@@ -54,11 +59,9 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus
     public void start(BundleContext context) {
         LOG.info("Starting {}", getClass().getSimpleName());
 
-        // Register EventHandler for blueprint events
+        registerBlueprintEventHandler(context);
 
-        Dictionary<String, Object> props = new Hashtable<>();
-        props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC, EventConstants.TOPIC_CREATED);
-        eventHandlerReg = context.registerService(EventHandler.class.getName(), this, props);
+        registerNamespaceHandler(context);
 
         bundleTracker = new BundleTracker<>(context, Bundle.ACTIVE, this);
 
@@ -72,6 +75,10 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus
 
                         LOG.debug("Got BlueprintExtenderService");
 
+                        blueprintContainerRestartReg = context.registerService(
+                                BlueprintContainerRestartService.class.getName(),
+                                new BlueprintContainerRestartServiceImpl(blueprintExtenderService), new Hashtable<>());
+
                         return blueprintExtenderService;
                     }
 
@@ -88,6 +95,19 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus
         serviceTracker.open();
     }
 
+    private void registerNamespaceHandler(BundleContext context) {
+        Dictionary<String, Object> props = new Hashtable<>();
+        props.put("osgi.service.blueprint.namespace", OpendaylightNamespaceHandler.NAMESPACE_1_0_0);
+        namespaceReg = context.registerService(NamespaceHandler.class.getName(),
+                new OpendaylightNamespaceHandler(), props);
+    }
+
+    private void registerBlueprintEventHandler(BundleContext context) {
+        Dictionary<String, Object> props = new Hashtable<>();
+        props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC, EventConstants.TOPIC_CREATED);
+        eventHandlerReg = context.registerService(EventHandler.class.getName(), this, props);
+    }
+
     /**
      * Implemented from BundleActivator.
      */
@@ -95,7 +115,10 @@ public class BlueprintBundleTracker implements BundleActivator, BundleTrackerCus
     public void stop(BundleContext context) {
         bundleTracker.close();
         serviceTracker.close();
-        eventHandlerReg.unregister();
+
+        AriesFrameworkUtil.safeUnregisterService(eventHandlerReg);
+        AriesFrameworkUtil.safeUnregisterService(namespaceReg);
+        AriesFrameworkUtil.safeUnregisterService(blueprintContainerRestartReg);
     }
 
     /**