Move NetconfDeviceTopologyAdapter
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalProvider.java
index c0f066ffdc07f8a27dc099b7d15eeca65d0606d8..ff4af4649ea6141fe3f82384fdd37174b628fe46 100644 (file)
  */
 package org.opendaylight.netconf.sal.connect.netconf.sal;
 
-import com.google.common.base.Preconditions;
-import java.util.Collection;
-import java.util.Collections;
-import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
-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.DOMNotification;
-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.core.api.Broker;
-import org.opendaylight.controller.sal.core.api.Provider;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
+import org.opendaylight.mdsal.dom.api.DOMActionService;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMNotification;
+import org.opendaylight.mdsal.dom.api.DOMNotificationService;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
+import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
+import org.opendaylight.netconf.sal.connect.api.NetconfRpcService;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Actions;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
+import org.opendaylight.netconf.sal.connect.api.SchemalessRpcService;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
-
-    private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
+// FIXME: remove this class and promote MountInstance to a top-level construct
+// Non-final for mocking
+public class NetconfDeviceSalProvider implements AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
 
     private final RemoteDeviceId id;
-    private MountInstance mountInstance;
-
-    private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
-
-    private DataBroker dataBroker;
-    private BindingTransactionChain txChain;
-
-    private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
-        @Override
-        public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
-            logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
-            chain.close();
-            resetTransactionChainForAdapaters();
-            throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
-        }
+    private final MountInstance mountInstance;
 
-        @Override
-        public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
-            logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
-        }
-    };
-
-    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
-        this.id = deviceId;
+    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
+        id = requireNonNull(deviceId);
+        mountInstance = new MountInstance(mountService, id);
     }
 
     public MountInstance getMountInstance() {
-        Preconditions.checkState(mountInstance != null,
-                "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
+        checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
+                id);
         return mountInstance;
     }
 
-    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);
-
-        final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
-        if (mountService != null) {
-            mountInstance = new MountInstance(mountService, id);
-        }
-    }
-
     @Override
-    public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
-        return Collections.emptySet();
-    }
-
-    @Override
-    public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
-        logger.debug("{}: Session with sal established {}", id, session);
-
-        this.dataBroker = session.getSALService(DataBroker.class);
-        txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
-
-        topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
-    }
-
-    private void resetTransactionChainForAdapaters() {
-        txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
-
-        topologyDatastoreAdapter.setTxChain(txChain);
-
-        logger.trace("{}: Resetting TransactionChain {}", id, txChain);
-
-    }
-
-    public void close() throws Exception {
+    public void close() {
         mountInstance.close();
-        if (topologyDatastoreAdapter != null) {
-            topologyDatastoreAdapter.close();
-        }
-        topologyDatastoreAdapter = null;
-        if (txChain != null) {
-            txChain.close();
-        }
     }
 
-    public static final class MountInstance implements AutoCloseable {
+    public static class MountInstance implements AutoCloseable {
 
-        private DOMMountPointService mountService;
+        private final DOMMountPointService mountService;
         private final RemoteDeviceId id;
-        private NetconfDeviceNotificationService notificationService;
 
+        private NetconfDeviceNotificationService notificationService;
         private ObjectRegistration<DOMMountPoint> topologyRegistration;
 
-        public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
-            this.mountService = Preconditions.checkNotNull(mountService);
-            this.id = Preconditions.checkNotNull(id);
+        MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
+            this.mountService = requireNonNull(mountService);
+            this.id = requireNonNull(id);
         }
 
-        public 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");
+        public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
+                final RemoteDeviceServices services, final DOMDataBroker broker,
+                final NetconfDataTreeService dataTreeService) {
+            onTopologyDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
+                dataTreeService);
+        }
 
-            final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
-            mountBuilder.addInitialSchemaContext(initialCtx);
+        public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
+                final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
+                final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
+            requireNonNull(mountService, "Closed");
+            checkState(topologyRegistration == null, "Already initialized");
+
+            final var mountBuilder = mountService.createMountPoint(id.getTopologyPath());
+            mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
+
+            final var rpcs = services.rpcs();
+            mountBuilder.addService(NetconfRpcService.class, rpcs);
+            if (rpcs instanceof Rpcs.Normalized normalized) {
+                mountBuilder.addService(DOMRpcService.class, normalized);
+            } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
+                mountBuilder.addService(SchemalessRpcService.class, schemaless);
+            }
+            if (services.actions() instanceof Actions.Normalized normalized) {
+                mountBuilder.addService(DOMActionService.class, normalized);
+            }
 
-            mountBuilder.addService(DOMDataBroker.class, broker);
-            mountBuilder.addService(DOMRpcService.class, rpc);
-            mountBuilder.addService(DOMNotificationService.class, notificationService);
-            this.notificationService = notificationService;
+            if (broker != null) {
+                mountBuilder.addService(DOMDataBroker.class, broker);
+            }
+            if (dataTreeService != null) {
+                mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
+            }
+            mountBuilder.addService(DOMNotificationService.class, newNotificationService);
+            notificationService = newNotificationService;
 
             topologyRegistration = mountBuilder.register();
-            logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
-
+            LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
         }
 
+        @SuppressWarnings("checkstyle:IllegalCatch")
         public synchronized void onTopologyDeviceDisconnected() {
-            if(topologyRegistration == null) {
-                logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
+            if (topologyRegistration == null) {
+                LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
                 return;
             }
 
@@ -162,22 +120,21 @@ public class NetconfDeviceSalProvider implements AutoCloseable, Provider, Bindin
                 topologyRegistration.close();
             } catch (final Exception e) {
                 // Only log and ignore
-                logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
+                LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
             } finally {
-                logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
+                LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
                 topologyRegistration = null;
             }
         }
 
         @Override
-        public synchronized void close() throws Exception {
+        public synchronized void close() {
             onTopologyDeviceDisconnected();
-            mountService = null;
         }
 
         public synchronized void publish(final DOMNotification domNotification) {
-            Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
-            notificationService.publishNotification(domNotification);
+            checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
+                .publishNotification(domNotification);
         }
     }