Merge "BUG 2676 : Use notification-dispatcher for DataChangeListener actors"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModule.java
index f73d9cc72f893aa0260e6adc7bf0f257eb18252e..44b2435da2284358b4a2ed13cbb6e699e23f2115 100644 (file)
@@ -10,15 +10,14 @@ package org.opendaylight.controller.config.yang.md.sal.connector.netconf;
 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition;
 import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull;
 
-import java.io.File;
-import java.io.InputStream;
-import java.net.InetAddress;
+import com.google.common.base.Optional;
+import io.netty.util.concurrent.EventExecutor;
+import java.math.BigDecimal;
 import java.net.InetSocketAddress;
+import java.util.List;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
+import org.opendaylight.controller.config.api.JmxAttributeValidationException;
 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
-import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfiguration;
 import org.opendaylight.controller.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
@@ -26,26 +25,24 @@ import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
+import org.opendaylight.controller.sal.connect.netconf.NetconfStateSchemas;
 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
+import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceSalFacade;
+import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.controller.sal.core.api.Broker;
 import org.opendaylight.protocol.framework.ReconnectStrategy;
 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
-import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
-import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
+import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-import com.google.common.net.InetAddresses;
-import io.netty.util.HashedWheelTimer;
-
 /**
  *
  */
@@ -53,8 +50,10 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
 {
     private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class);
 
-    private static AbstractCachingSchemaSourceProvider<String, InputStream> GLOBAL_NETCONF_SOURCE_PROVIDER = null;
     private BundleContext bundleContext;
+    private Optional<NetconfSessionPreferences> userCapabilities;
+    private SchemaSourceRegistry schemaRegistry;
+    private SchemaContextFactory schemaContextFactory;
 
     public NetconfConnectorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
@@ -67,6 +66,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
     @Override
     protected void customValidation() {
         checkNotNull(getAddress(), addressJmxAttribute);
+        checkCondition(isHostAddressPresent(getAddress()), "Host address not present in " + getAddress(), addressJmxAttribute);
         checkNotNull(getPort(), portJmxAttribute);
         checkNotNull(getDomRegistry(), portJmxAttribute);
         checkNotNull(getDomRegistry(), domRegistryJmxAttribute);
@@ -77,11 +77,9 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
         checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute);
         checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute);
 
-        // FIXME BUG-944 remove backwards compatibility
-        if(getClientDispatcher() == null) {
-            checkCondition(getBossThreadGroup() != null, "Client dispatcher was not set, thread groups have to be set instead", bossThreadGroupJmxAttribute);
-            checkCondition(getWorkerThreadGroup() != null, "Client dispatcher was not set, thread groups have to be set instead", workerThreadGroupJmxAttribute);
-        }
+        checkNotNull(getClientDispatcher(), clientDispatcherJmxAttribute);
+        checkNotNull(getBindingRegistry(), bindingRegistryJmxAttribute);
+        checkNotNull(getProcessingExecutor(), processingExecutorJmxAttribute);
 
         // Check username + password in case of ssh
         if(getTcpOnly() == false) {
@@ -89,96 +87,61 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
             checkNotNull(getPassword(), passwordJmxAttribute);
         }
 
-        // FIXME BUG 944 remove this warning
-        if(getBindingRegistry() == null) {
-            logger.warn("Configuration property: \"binding-registry\" not set for sal-netconf-connector (" + getIdentifier() + "). " +
-                    "Netconf-connector now requires a dependency on \"binding-broker-osgi-registry\". " +
-                    "The dependency is optional for now to preserve backwards compatibility, but will be mandatory in the future. " +
-                    "Please set the property as in \"01-netconf-connector\" initial config file. " +
-                    "The service will be retrieved from OSGi service registry now.");
-        }
+        userCapabilities = getUserCapabilities();
+    }
 
-        // FIXME BUG 944 remove this warning
-        if(getProcessingExecutor() == null) {
-            logger.warn("Configuration property: \"processing-executor\" not set for sal-netconf-connector (" + getIdentifier() + "). " +
-                    "Netconf-connector now requires a dependency on \"threadpool\". " +
-                    "The dependency is optional for now to preserve backwards compatibility, but will be mandatory in the future. " +
-                    "Please set the property as in \"01-netconf-connector\" initial config file. " +
-                    "New instance will be created for the executor.");
-        }
+    private boolean isHostAddressPresent(final Host address) {
+        return address.getDomainName() != null ||
+               address.getIpAddress() != null && (address.getIpAddress().getIpv4Address() != null || address.getIpAddress().getIpv6Address() != null);
     }
 
     @Override
     public java.lang.AutoCloseable createInstance() {
-        final RemoteDeviceId id = new RemoteDeviceId(getIdentifier());
+        final RemoteDeviceId id = new RemoteDeviceId(getIdentifier(), getSocketAddress());
 
-        final ExecutorService globalProcessingExecutor = getGlobalProcessingExecutor();
+        final ExecutorService globalProcessingExecutor = getProcessingExecutorDependency().getExecutor();
 
         final Broker domBroker = getDomRegistryDependency();
-        final BindingAwareBroker bindingBroker = getBindingRegistryBackwards();
+        final BindingAwareBroker bindingBroker = getBindingRegistryDependency();
+
+        final RemoteDeviceHandler<NetconfSessionPreferences> salFacade
+                = new NetconfDeviceSalFacade(id, domBroker, bindingBroker, bundleContext, globalProcessingExecutor);
+
+        final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
+                new NetconfDevice.SchemaResourcesDTO(schemaRegistry, schemaContextFactory, new NetconfStateSchemas.NetconfStateSchemasResolverImpl());
 
-        final RemoteDeviceHandler salFacade = new NetconfDeviceSalFacade(id, domBroker, bindingBroker, bundleContext, globalProcessingExecutor);
         final NetconfDevice device =
-                NetconfDevice.createNetconfDevice(id, getGlobalNetconfSchemaProvider(), globalProcessingExecutor, salFacade);
-        final NetconfDeviceCommunicator listener = new NetconfDeviceCommunicator(id, device);
+                new NetconfDevice(schemaResourcesDTO, id, salFacade, globalProcessingExecutor, new NetconfMessageTransformer(), getReconnectOnChangedSchema());
+
+        final NetconfDeviceCommunicator listener = userCapabilities.isPresent() ?
+                new NetconfDeviceCommunicator(id, device, userCapabilities.get()) : new NetconfDeviceCommunicator(id, device);
+
         final NetconfReconnectingClientConfiguration clientConfig = getClientConfig(listener);
+        final NetconfClientDispatcher dispatcher = getClientDispatcherDependency();
 
-        // FIXME BUG-944 remove backwards compatibility
-        final NetconfClientDispatcher dispatcher = getClientDispatcher() == null ? createDispatcher() : getClientDispatcherDependency();
         listener.initializeRemoteConnection(dispatcher, clientConfig);
 
-        return new AutoCloseable() {
-            @Override
-            public void close() throws Exception {
-                listener.close();
-                salFacade.close();
-            }
-        };
+        return new SalConnectorCloseable(listener, salFacade);
     }
 
-    private BindingAwareBroker getBindingRegistryBackwards() {
-        if(getBindingRegistry() != null) {
-            return getBindingRegistryDependency();
-        } else {
-            // FIXME BUG 944 remove backwards compatibility
-            final ServiceReference<BindingAwareBroker> serviceReference = bundleContext.getServiceReference(BindingAwareBroker.class);
-            Preconditions
-                    .checkNotNull(
-                            serviceReference,
-                            "Unable to retrieve %s from OSGi service registry, use binding-registry config property to inject %s with config subsystem",
-                            BindingAwareBroker.class, BindingAwareBroker.class);
-            return bundleContext.getService(serviceReference);
+    private Optional<NetconfSessionPreferences> getUserCapabilities() {
+        if(getYangModuleCapabilities() == null) {
+            return Optional.absent();
         }
-    }
 
-    private ExecutorService getGlobalProcessingExecutor() {
-        if(getProcessingExecutor() != null) {
-            return getProcessingExecutorDependency().getExecutor();
-        } else {
-            // FIXME BUG 944 remove backwards compatibility
-            return Executors.newCachedThreadPool();
+        final List<String> capabilities = getYangModuleCapabilities().getCapability();
+        if(capabilities == null || capabilities.isEmpty()) {
+            return Optional.absent();
         }
-    }
 
-    private synchronized AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider() {
-        if(GLOBAL_NETCONF_SOURCE_PROVIDER == null) {
-            final String storageFile = "cache/schema";
-            //            File directory = bundleContext.getDataFile(storageFile);
-            final File directory = new File(storageFile);
-            final SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
-            GLOBAL_NETCONF_SOURCE_PROVIDER = FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
-        }
-        return GLOBAL_NETCONF_SOURCE_PROVIDER;
-    }
+        final NetconfSessionPreferences parsedOverrideCapabilities = NetconfSessionPreferences.fromStrings(capabilities);
+        JmxAttributeValidationException.checkCondition(
+                parsedOverrideCapabilities.getNonModuleCaps().isEmpty(),
+                "Capabilities to override can only contain module based capabilities, non-module capabilities will be retrieved from the device," +
+                        " configured non-module capabilities: " + parsedOverrideCapabilities.getNonModuleCaps(),
+                yangModuleCapabilitiesJmxAttribute);
 
-    // FIXME BUG-944 remove backwards compatibility
-    /**
-     * @deprecated Use getClientDispatcherDependency method instead to retrieve injected dispatcher.
-     * This one creates new instance of NetconfClientDispatcher and will be removed in near future.
-     */
-    @Deprecated
-    private NetconfClientDispatcher createDispatcher() {
-        return new NetconfClientDispatcherImpl(getBossThreadGroupDependency(), getWorkerThreadGroupDependency(), new HashedWheelTimer());
+        return Optional.of(parsedOverrideCapabilities);
     }
 
     public void setBundleContext(final BundleContext bundleContext) {
@@ -187,54 +150,86 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co
 
     public NetconfReconnectingClientConfiguration getClientConfig(final NetconfDeviceCommunicator listener) {
         final InetSocketAddress socketAddress = getSocketAddress();
-        final ReconnectStrategy strategy = getReconnectStrategy();
         final long clientConnectionTimeoutMillis = getConnectionTimeoutMillis();
 
+        final ReconnectStrategyFactory sf = new TimedReconnectStrategyFactory(
+            getEventExecutorDependency(), getMaxConnectionAttempts(), getBetweenAttemptsTimeoutMillis(), getSleepFactor());
+        final ReconnectStrategy strategy = sf.createReconnectStrategy();
+
         return NetconfReconnectingClientConfigurationBuilder.create()
         .withAddress(socketAddress)
         .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
         .withReconnectStrategy(strategy)
-        .withSessionListener(listener)
         .withAuthHandler(new LoginPassword(getUsername(),getPassword()))
         .withProtocol(getTcpOnly() ?
                 NetconfClientConfiguration.NetconfClientProtocol.TCP :
                 NetconfClientConfiguration.NetconfClientProtocol.SSH)
-        .withConnectStrategyFactory(new ReconnectStrategyFactory() {
-            @Override
-            public ReconnectStrategy createReconnectStrategy() {
-                return getReconnectStrategy();
-            }
-        })
+        .withConnectStrategyFactory(sf)
+        .withSessionListener(listener)
         .build();
     }
 
-    private ReconnectStrategy getReconnectStrategy() {
-        final Long connectionAttempts;
-        if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) {
-            connectionAttempts = getMaxConnectionAttempts();
-        } else {
-            logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
-            connectionAttempts = null;
+    private static final class SalConnectorCloseable implements AutoCloseable {
+        private final RemoteDeviceHandler<NetconfSessionPreferences> salFacade;
+        private final NetconfDeviceCommunicator listener;
+
+        public SalConnectorCloseable(final NetconfDeviceCommunicator listener,
+                                     final RemoteDeviceHandler<NetconfSessionPreferences> salFacade) {
+            this.listener = listener;
+            this.salFacade = salFacade;
+        }
+
+        @Override
+        public void close() {
+            listener.close();
+            salFacade.close();
+        }
+    }
+
+    private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
+        private final Long connectionAttempts;
+        private final EventExecutor executor;
+        private final double sleepFactor;
+        private final int minSleep;
+
+        TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts, final int minSleep, final BigDecimal sleepFactor) {
+            if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
+                connectionAttempts = maxConnectionAttempts;
+            } else {
+                logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this);
+                connectionAttempts = null;
+            }
+
+            this.sleepFactor = sleepFactor.doubleValue();
+            this.executor = executor;
+            this.minSleep = minSleep;
         }
-        final double sleepFactor = getSleepFactor().doubleValue();
-        final int minSleep = getBetweenAttemptsTimeoutMillis();
-        final Long maxSleep = null;
-        final Long deadline = null;
 
-        return new TimedReconnectStrategy(getEventExecutorDependency(), getBetweenAttemptsTimeoutMillis(),
-                minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
+        @Override
+        public ReconnectStrategy createReconnectStrategy() {
+            final Long maxSleep = null;
+            final Long deadline = null;
+
+            return new TimedReconnectStrategy(executor, minSleep,
+                    minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);
+        }
     }
 
     private InetSocketAddress getSocketAddress() {
-        /*
-         * Uncomment after Switch to IP Address
-        if(getAddress().getIpv4Address() != null) {
-            addressValue = getAddress().getIpv4Address().getValue();
+        if(getAddress().getDomainName() != null) {
+            return new InetSocketAddress(getAddress().getDomainName().getValue(), getPort().getValue());
         } else {
-            addressValue = getAddress().getIpv6Address().getValue();
+            final IpAddress ipAddress = getAddress().getIpAddress();
+            final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue();
+            return new InetSocketAddress(ip, getPort().getValue());
         }
-         */
-        final InetAddress inetAddress = InetAddresses.forString(getAddress());
-        return new InetSocketAddress(inetAddress, getPort().intValue());
+    }
+
+    public void setSchemaRegistry(final SchemaSourceRegistry schemaRegistry) {
+        this.schemaRegistry = schemaRegistry;
+    }
+
+    public void setSchemaContextFactory(final SchemaContextFactory schemaContextFactory) {
+        this.schemaContextFactory = schemaContextFactory;
     }
 }