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 static final int SILENT_ATTEMPTS = 30;
-
- 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);
+ }
- ConfigRegistryJMXClient configRegistryJMXClient;
- int i = 0;
- // Config registry might not be present yet, but will be eventually
- while(true) {
+ @Override
+ public Set<Capability> getCapabilities() {
+ return configFacadeFactory.getCurrentCapabilities();
+ }
- try {
- configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
- break;
- } catch (IllegalStateException e) {
- ++i;
- if (i > SILENT_ATTEMPTS) {
- logger.info("JMX client not created after {} attempts, still trying", i, e);
- } else {
- logger.debug("JMX client could not be created, reattempting, try {}", i, e);
- }
- try {
- Thread.sleep(ATTEMPT_TIMEOUT_MS);
- } catch (InterruptedException e1) {
- Thread.currentThread().interrupt();
- throw new RuntimeException("Interrupted while reattempting connection", e1);
- }
+ @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;
- if (i > SILENT_ATTEMPTS) {
- logger.info("Created JMX client after {} attempts", i);
- } else {
- logger.debug("Created JMX client after {} attempts", i);
- }
+ });
}
- @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));
}
+
}