Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / Activator.java
index 83029c44e665ec77bc5796535353277fe481ea43..5d01b8decddf5884654a2e1a7e35c4e81390e4a8 100644 (file)
@@ -8,48 +8,68 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
 
-import org.opendaylight.controller.config.yang.store.api.YangStoreService;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
+import org.opendaylight.controller.netconf.api.util.NetconfConstants;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
 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;
+public class Activator implements BundleActivator {
 
-import static com.google.common.base.Preconditions.checkState;
+    private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
 
-public class Activator implements BundleActivator, YangStoreServiceTracker.YangStoreTrackerListener {
+    private ServiceRegistration<?> osgiRegistration;
 
-    private static final Logger logger = LoggerFactory.getLogger(Activator.class);
+    @Override
+    public void start(final BundleContext context) throws Exception {
+        ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory>() {
 
-    private BundleContext context;
-    ServiceRegistration osgiRegistration;
+            @Override
+            public ConfigSubsystemFacadeFactory addingService(ServiceReference<ConfigSubsystemFacadeFactory> reference) {
+                LOG.debug("Got addingService(SchemaContextProvider) event");
+                // Yang store service should not be registered multiple times
+                ConfigSubsystemFacadeFactory configSubsystemFacade = reference.getBundle().getBundleContext().getService(reference);
+                osgiRegistration = startNetconfServiceFactory(configSubsystemFacade, context);
+                return configSubsystemFacade;
+            }
 
-    @Override
-    public void start(BundleContext context) throws Exception {
-        this.context = context;
-        YangStoreServiceTracker tracker = new YangStoreServiceTracker(context, this);
-        tracker.open();
-    }
+            @Override
+            public void modifiedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
+                LOG.warn("Config manager facade was modified unexpectedly");
+            }
 
-    @Override
-    public void stop(BundleContext context) throws Exception {
+            @Override
+            public void removedService(ServiceReference<ConfigSubsystemFacadeFactory> reference, ConfigSubsystemFacadeFactory service) {
+                LOG.warn("Config manager facade was removed unexpectedly");
+            }
+        };
+
+        ServiceTracker<ConfigSubsystemFacadeFactory, ConfigSubsystemFacadeFactory> schemaContextProviderServiceTracker =
+                new ServiceTracker<>(context, ConfigSubsystemFacadeFactory.class, schemaServiceTrackerCustomizer);
+        schemaContextProviderServiceTracker.open();
     }
 
     @Override
-    public synchronized void onYangStoreAdded(YangStoreService yangStoreService) {
-        checkState(osgiRegistration == null, "More than one onYangStoreAdded received");
-        NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
-        logger.debug("Registering into OSGi");
-        osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
-                new Hashtable<String, Object>());
+    public void stop(final BundleContext bundleContext) throws Exception {
+        if (osgiRegistration != null) {
+            osgiRegistration.unregister();
+        }
     }
 
-    @Override
-    public synchronized void onYangStoreRemoved() {
-        osgiRegistration.unregister();
-        osgiRegistration = null;
+    private ServiceRegistration<NetconfOperationServiceFactory> startNetconfServiceFactory(final ConfigSubsystemFacadeFactory configSubsystemFacade, final BundleContext context) {
+        final NetconfOperationServiceFactoryImpl netconfOperationServiceFactory = new NetconfOperationServiceFactoryImpl(configSubsystemFacade);
+        // Add properties to autowire with netconf-impl instance for cfg subsystem
+        final Dictionary<String, String> properties = new Hashtable<>();
+        properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
+        return context.registerService(NetconfOperationServiceFactory.class, netconfOperationServiceFactory, properties);
     }
+
 }