BUG-2314 Migrate netconf-connector to NormalizedNode
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceSalProvider.java
index cf9174dd50d707ef36c5e80deefce3ea8f8be20c..3246f519042802cedd396708ed17c0db69c79301 100644 (file)
@@ -10,21 +10,19 @@ package org.opendaylight.controller.sal.connect.netconf.sal;
 import com.google.common.base.Preconditions;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.concurrent.ExecutorService;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.controller.sal.core.api.Broker;
 import org.opendaylight.controller.sal.core.api.Provider;
-import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
-import org.opendaylight.controller.sal.core.api.notify.NotificationPublishService;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -34,13 +32,13 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
 
     private final RemoteDeviceId id;
-    private final ExecutorService executor;
     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
     private MountInstance mountInstance;
 
-    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
+    private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
+
+    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
         this.id = deviceId;
-        this.executor = executor;
     }
 
     public MountInstance getMountInstance() {
@@ -55,6 +53,12 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
         return datastoreAdapter;
     }
 
+    public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
+        Preconditions.checkState(topologyDatastoreAdapter != null,
+                "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
+        return topologyDatastoreAdapter;
+    }
+
     @Override
     public void onSessionInitiated(final Broker.ProviderSession session) {
         logger.debug("{}: (BI)Session with sal established {}", id, session);
@@ -70,31 +74,22 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
         return Collections.emptySet();
     }
 
-    @Override
-    public Collection<? extends RpcService> getImplementations() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public Collection<? extends BindingAwareProvider.ProviderFunctionality> getFunctionality() {
-        return Collections.emptySet();
-    }
-
     @Override
     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
         logger.debug("{}: Session with sal established {}", id, session);
 
         final DataBroker dataBroker = session.getSALService(DataBroker.class);
         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
-    }
 
-    @Override
-    public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {}
+        topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, dataBroker);
+    }
 
     public void close() throws Exception {
         mountInstance.close();
         datastoreAdapter.close();
         datastoreAdapter = null;
+        topologyDatastoreAdapter.close();
+        topologyDatastoreAdapter = null;
     }
 
     static final class MountInstance implements AutoCloseable {
@@ -102,16 +97,19 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
         private DOMMountPointService mountService;
         private final RemoteDeviceId id;
         private ObjectRegistration<DOMMountPoint> registration;
-        private NotificationPublishService notificationSerivce;
+        private NetconfDeviceNotificationService notificationService;
+
+        private ObjectRegistration<DOMMountPoint> topologyRegistration;
 
         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
             this.mountService = Preconditions.checkNotNull(mountService);
             this.id = Preconditions.checkNotNull(id);
         }
 
+        @Deprecated
         synchronized void onDeviceConnected(final SchemaContext initialCtx,
-                final DOMDataBroker broker, final RpcProvisionRegistry rpc,
-                final NotificationPublishService notificationSerivce) {
+                final DOMDataBroker broker, final DOMRpcService rpc,
+                final NetconfDeviceNotificationService notificationService) {
 
             Preconditions.checkNotNull(mountService, "Closed");
             Preconditions.checkState(registration == null, "Already initialized");
@@ -120,13 +118,14 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
             mountBuilder.addInitialSchemaContext(initialCtx);
 
             mountBuilder.addService(DOMDataBroker.class, broker);
-            mountBuilder.addService(RpcProvisionRegistry.class, rpc);
-            this.notificationSerivce = notificationSerivce;
-            mountBuilder.addService(NotificationPublishService.class, notificationSerivce);
+            mountBuilder.addService(DOMRpcService.class, rpc);
+            mountBuilder.addService(DOMNotificationService.class, notificationService);
+            this.notificationService = notificationService;
 
             registration = mountBuilder.register();
         }
 
+        @Deprecated
         synchronized void onDeviceDisconnected() {
             if(registration == null) {
                 return;
@@ -142,17 +141,50 @@ final class NetconfDeviceSalProvider implements AutoCloseable, Provider, Binding
             }
         }
 
+        synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
+                                                    final DOMDataBroker broker, final DOMRpcService rpc,
+                                                    final NetconfDeviceNotificationService notificationService) {
+
+            Preconditions.checkNotNull(mountService, "Closed");
+            Preconditions.checkState(topologyRegistration == null, "Already initialized");
+
+            final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
+            mountBuilder.addInitialSchemaContext(initialCtx);
+
+            mountBuilder.addService(DOMDataBroker.class, broker);
+            mountBuilder.addService(DOMRpcService.class, rpc);
+            mountBuilder.addService(DOMNotificationService.class, notificationService);
+
+            topologyRegistration = mountBuilder.register();
+        }
+
+        synchronized void onTopologyDeviceDisconnected() {
+            if(topologyRegistration == null) {
+                return;
+            }
+
+            try {
+                topologyRegistration.close();
+            } catch (final Exception e) {
+                // Only log and ignore
+                logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
+            } finally {
+                topologyRegistration = null;
+            }
+        }
+
         @Override
         synchronized public void close() throws Exception {
             if(registration != null) {
                 onDeviceDisconnected();
+                onTopologyDeviceDisconnected();
             }
             mountService = null;
         }
 
-        public synchronized void publish(final CompositeNode domNotification) {
-            Preconditions.checkNotNull(notificationSerivce, "Device not set up yet, cannot handle notification {}", domNotification);
-            notificationSerivce.publish(domNotification);
+        public synchronized void publish(final ContainerNode domNotification) {
+            Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
+            notificationService.publishNotification(domNotification);
         }
     }