Bug 8153: Enforce check-style rules for netconf - sal-netconf-connector
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalProvider.java
index 3fe01bbbfeb8992b14212289a7847375959e7fc9..4ad1dde5fefac226d5ee04bb4853ec132edf51c5 100644 (file)
@@ -8,8 +8,6 @@
 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;
@@ -21,45 +19,54 @@ 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 org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
+public class NetconfDeviceSalProvider implements AutoCloseable {
 
-    private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
 
     private final RemoteDeviceId id;
-    private MountInstance mountInstance;
+    private final MountInstance mountInstance;
+    private final DataBroker dataBroker;
 
     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);
+        public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
+                                             final AsyncTransaction<?, ?> transaction, final Throwable cause) {
+            LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
             chain.close();
             resetTransactionChainForAdapaters();
             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
         }
 
         @Override
-        public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
-            logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
+        public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
+            LOG.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
         }
     };
 
-    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
+    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
+                                    final DataBroker dataBroker) {
         this.id = deviceId;
+        mountInstance = new MountInstance(mountService, id);
+        this.dataBroker = dataBroker;
+        txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
+
+        topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
+    }
+
+    public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
+        this.id = deviceId;
+        mountInstance = new MountInstance(mountService, id);
+        this.dataBroker = null;
     }
 
     public MountInstance getMountInstance() {
@@ -74,50 +81,29 @@ public class NetconfDeviceSalProvider implements AutoCloseable, Provider, Bindin
         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);
+        LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
 
     }
 
     public void close() throws Exception {
         mountInstance.close();
-        topologyDatastoreAdapter.close();
+        if (topologyDatastoreAdapter != null) {
+            topologyDatastoreAdapter.close();
+        }
         topologyDatastoreAdapter = null;
-        txChain.close();
+        if (txChain != null) {
+            txChain.close();
+        }
     }
 
     public static final class MountInstance implements AutoCloseable {
 
-        private DOMMountPointService mountService;
+        private final DOMMountPointService mountService;
         private final RemoteDeviceId id;
         private NetconfDeviceNotificationService notificationService;
 
@@ -129,13 +115,13 @@ public class NetconfDeviceSalProvider implements AutoCloseable, Provider, Bindin
         }
 
         public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
-                                                    final DOMDataBroker broker, final DOMRpcService rpc,
-                                                    final NetconfDeviceNotificationService notificationService) {
-
+                                                           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());
+            final DOMMountPointService.DOMMountPointBuilder mountBuilder =
+                    mountService.createMountPoint(id.getTopologyPath());
             mountBuilder.addInitialSchemaContext(initialCtx);
 
             mountBuilder.addService(DOMDataBroker.class, broker);
@@ -144,13 +130,14 @@ public class NetconfDeviceSalProvider implements AutoCloseable, Provider, Bindin
             this.notificationService = notificationService;
 
             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;
             }
 
@@ -158,9 +145,9 @@ 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;
             }
         }
@@ -168,11 +155,11 @@ public class NetconfDeviceSalProvider implements AutoCloseable, Provider, Bindin
         @Override
         public synchronized void close() throws Exception {
             onTopologyDeviceDisconnected();
-            mountService = null;
         }
 
         public synchronized void publish(final DOMNotification domNotification) {
-            Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
+            Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
+                    domNotification);
             notificationService.publishNotification(domNotification);
         }
     }