Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / NetconfOperationServiceFactoryImpl.java
index 2db3c9d4f1d538893da1b9de420c696ef555e242..6ea628d0190e8a531166c823efa4eff8bf6e5173 100644 (file)
@@ -8,60 +8,56 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
 
-import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
-import org.opendaylight.controller.config.yang.store.api.YangStoreException;
-import org.opendaylight.controller.config.yang.store.api.YangStoreService;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Sets;
+import java.util.Set;
+import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
+import org.opendaylight.controller.config.util.capability.Capability;
+import org.opendaylight.controller.config.util.capability.ModuleListener;
+import org.opendaylight.controller.config.util.capability.YangModuleCapability;
+import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.management.MBeanServer;
-import java.lang.management.ManagementFactory;
+import org.opendaylight.yangtools.yang.model.api.Module;
 
 public class NetconfOperationServiceFactoryImpl implements NetconfOperationServiceFactory {
 
-    public static final int ATTEMPT_TIMEOUT_MS = 1000;
-
-    private final YangStoreService yangStoreService;
-    private final ConfigRegistryJMXClient jmxClient;
+    private final ConfigSubsystemFacadeFactory configFacadeFactory;
 
-    private static final Logger logger = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);
-
-    public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
-        this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
+    public NetconfOperationServiceFactoryImpl(ConfigSubsystemFacadeFactory configFacadeFactory) {
+        this.configFacadeFactory = configFacadeFactory;
     }
 
-    public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
-        this.yangStoreService = yangStoreService;
+    @Override
+    public NetconfOperationServiceImpl createService(String netconfSessionIdForReporting) {
+        return new NetconfOperationServiceImpl(configFacadeFactory.createFacade(netconfSessionIdForReporting), netconfSessionIdForReporting);
+    }
 
-        // Config registry might not be present yet, but will be eventually
-        while(true) {
+    @Override
+    public Set<Capability> getCapabilities() {
+        return configFacadeFactory.getCurrentCapabilities();
+    }
 
-            final ConfigRegistryJMXClient configRegistryJMXClient;
-            try {
-                configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
-            } catch (IllegalStateException e) {
-                logger.debug("Jmx client could not be created, reattempting");
-                try {
-                    Thread.sleep(ATTEMPT_TIMEOUT_MS);
-                } catch (InterruptedException e1) {
-                    Thread.currentThread().interrupt();
-                    throw new RuntimeException(e1);
-                }
-                continue;
+    @Override
+    public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+        return configFacadeFactory.getYangStoreService().registerModuleListener(new ModuleListener() {
+            @Override
+            public void onCapabilitiesChanged(Set<Module> added, Set<Module> removed) {
+                listener.onCapabilitiesChanged(
+                        transformModulesToCapabilities(added), transformModulesToCapabilities(removed));
             }
-
-            jmxClient = configRegistryJMXClient;
-            break;
-        }
+        });
     }
 
-    @Override
-    public NetconfOperationServiceImpl createService(long netconfSessionId, String netconfSessionIdForReporting) {
-        try {
-            return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
-        } catch (YangStoreException e) {
-            throw new IllegalStateException(e);
+    private static final Function<Module, Capability> MODULE_TO_CAPABILITY = new Function<Module, Capability>() {
+        @Override
+        public Capability apply(final Module module) {
+            return new YangModuleCapability(module, module.getSource());
         }
+    };
+
+    public static Set<Capability> transformModulesToCapabilities(Set<Module> modules) {
+        return Sets.newHashSet(Collections2.transform(modules, MODULE_TO_CAPABILITY));
     }
+
 }