Merge "BUG 2854 : Do not add empty read write transactions to the replicable journal"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceSalFacade.java
index dbef290197b91ea2e7eb55e19fbb9363cadace16..11f82c02bfb9b9e8e65925cd0a7d0b802737267b 100644 (file)
@@ -7,38 +7,25 @@
  */
 package org.opendaylight.controller.sal.connect.netconf.sal;
 
+import com.google.common.collect.Lists;
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ExecutorService;
-
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
-import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
+import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCapabilities;
+import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.controller.sal.core.api.Broker;
-import org.opendaylight.controller.sal.core.api.RpcImplementation;
-import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
-import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
-import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
-import org.opendaylight.controller.sal.dom.broker.impl.NotificationRouterImpl;
-import org.opendaylight.controller.sal.dom.broker.impl.SchemaAwareRpcBroker;
-import org.opendaylight.controller.sal.dom.broker.spi.NotificationRouter;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-
-public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionCapabilities> {
+public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
 
     private static final Logger logger= LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
 
@@ -47,9 +34,9 @@ public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDevice
 
     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
 
-    public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext, final ExecutorService executor) {
+    public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker, final BundleContext bundleContext) {
         this.id = id;
-        this.salProvider = new NetconfDeviceSalProvider(id, executor);
+        this.salProvider = new NetconfDeviceSalProvider(id);
         registerToSal(domBroker, bindingBroker, bundleContext);
     }
 
@@ -59,77 +46,41 @@ public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDevice
     }
 
     @Override
-    public synchronized void onNotification(final CompositeNode domNotification) {
+    public synchronized void onNotification(final ContainerNode domNotification) {
         salProvider.getMountInstance().publish(domNotification);
     }
 
     @Override
-    public synchronized void onDeviceConnected(final SchemaContextProvider remoteSchemaContextProvider,
-                                               final NetconfSessionCapabilities netconfSessionPreferences, final RpcImplementation deviceRpc) {
-        final SchemaContext schemaContext = remoteSchemaContextProvider.getSchemaContext();
-
-        // TODO remove deprecated SchemaContextProvider from SchemaAwareRpcBroker
-        // TODO move SchemaAwareRpcBroker from sal-broker-impl, now we have depend on the whole sal-broker-impl
-        final RpcProvisionRegistry rpcRegistry = new SchemaAwareRpcBroker(id.getPath().toString(), new org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider() {
-            @Override
-            public SchemaContext getSchemaContext() {
-                return schemaContext;
-            }
-        });
-        registerRpcsToSal(schemaContext, rpcRegistry, deviceRpc);
-        final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, deviceRpc, schemaContext, netconfSessionPreferences);
+    public synchronized void onDeviceConnected(final SchemaContext schemaContext,
+                                               final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
 
-        // TODO NotificationPublishService and NotificationRouter have the same interface
-        final NotificationPublishService notificationService = new NotificationPublishService() {
+        final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
 
-            private final NotificationRouter innerRouter = new NotificationRouterImpl();
+        final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
 
-            @Override
-            public void publish(final CompositeNode notification) {
-                innerRouter.publish(notification);
-            }
-
-            @Override
-            public ListenerRegistration<NotificationListener> addNotificationListener(final QName notification, final NotificationListener listener) {
-                return innerRouter.addNotificationListener(notification, listener);
-            }
-        };
-
-        salProvider.getMountInstance().onDeviceConnected(schemaContext, domBroker, rpcRegistry, notificationService);
+        salProvider.getMountInstance().onDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
         salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
+        salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
+        salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
     }
 
     @Override
-    public void onDeviceDisconnected() {
+    public synchronized void onDeviceDisconnected() {
         salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
+        salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
         salProvider.getMountInstance().onDeviceDisconnected();
+        salProvider.getMountInstance().onTopologyDeviceDisconnected();
     }
 
-    private void registerRpcsToSal(final SchemaContext schemaContext, final RpcProvisionRegistry rpcRegistry, final RpcImplementation deviceRpc) {
-        final Map<QName, String> failedRpcs = Maps.newHashMap();
-        for (final RpcDefinition rpcDef : schemaContext.getOperations()) {
-            try {
-                salRegistrations.add(rpcRegistry.addRpcImplementation(rpcDef.getQName(), deviceRpc));
-                logger.debug("{}: Rpc {} from netconf registered successfully", id, rpcDef.getQName());
-            } catch (final Exception e) {
-                // Only debug per rpc, warn for all of them at the end to pollute log a little less (e.g. routed rpcs)
-                logger.debug("{}: Unable to register rpc {} from netconf device. This rpc will not be available", id,
-                        rpcDef.getQName(), e);
-                failedRpcs.put(rpcDef.getQName(), e.getClass() + ":" + e.getMessage());
-            }
-        }
-
-        if (failedRpcs.isEmpty() == false) {
-            if (logger.isDebugEnabled()) {
-                logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs);
-            } else {
-                logger.warn("{}: Some rpcs from netconf device were not registered: {}", id, failedRpcs.keySet());
-            }
-        }
+    @Override
+    public synchronized void onDeviceFailed(final Throwable throwable) {
+        salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
+        salProvider.getMountInstance().onDeviceDisconnected();
+        salProvider.getMountInstance().onTopologyDeviceDisconnected();
     }
 
     @Override
-    public void close() {
+    public synchronized void close() {
         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
             closeGracefully(reg);
         }
@@ -145,4 +96,5 @@ public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDevice
             }
         }
     }
+
 }