Fix logging arguments
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / NetconfTopologyContext.java
index 31a7f64d6cc021e8d99a5c5fdb9883bc91f17f54..09e0fefc685871052a4c3e436258fec286a3293a 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.topology.singleton.impl;
 
 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
@@ -18,8 +17,9 @@ import akka.util.Timeout;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.atomic.AtomicBoolean;
 import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -33,7 +33,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import scala.concurrent.Future;
 
-class NetconfTopologyContext implements ClusterSingletonService {
+class NetconfTopologyContext implements ClusterSingletonService, AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyContext.class);
 
@@ -46,9 +46,9 @@ class NetconfTopologyContext implements ClusterSingletonService {
     private RemoteDeviceConnector remoteDeviceConnector;
     private NetconfNodeManager netconfNodeManager;
     private ActorRef masterActorRef;
-    private boolean finalClose = false;
-    private boolean closed = false;
-    private boolean isMaster;
+    private final AtomicBoolean closed = new AtomicBoolean(false);
+    private final AtomicBoolean stopped = new AtomicBoolean(false);
+    private volatile boolean isMaster;
 
     NetconfTopologyContext(final NetconfTopologySetup netconfTopologyDeviceSetup,
                            final ServiceGroupIdentifier serviceGroupIdent,
@@ -59,10 +59,9 @@ class NetconfTopologyContext implements ClusterSingletonService {
         this.mountService = mountService;
 
         remoteDeviceId = NetconfTopologyUtils.createRemoteDeviceId(netconfTopologyDeviceSetup.getNode().getNodeId(),
-                netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class));
+                netconfTopologyDeviceSetup.getNode().augmentation(NetconfNode.class));
 
-        remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId,
-                actorResponseWaitTime, mountService);
+        remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId);
 
         netconfNodeManager = createNodeDeviceManager();
     }
@@ -79,14 +78,15 @@ class NetconfTopologyContext implements ClusterSingletonService {
             netconfNodeManager = null;
         }
 
-        if (!finalClose) {
-            final String masterAddress = Cluster.get(netconfTopologyDeviceSetup.getActorSystem()).selfAddress().toString();
+        if (!closed.get()) {
+            final String masterAddress =
+                    Cluster.get(netconfTopologyDeviceSetup.getActorSystem()).selfAddress().toString();
             masterActorRef = netconfTopologyDeviceSetup.getActorSystem().actorOf(NetconfNodeActor.props(
                     netconfTopologyDeviceSetup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY,
                     actorResponseWaitTime, mountService),
                     NetconfTopologyUtils.createMasterActorName(remoteDeviceId.getName(), masterAddress));
 
-            remoteDeviceConnector.startRemoteDeviceConnection(masterActorRef);
+            remoteDeviceConnector.startRemoteDeviceConnection(newMasterSalFacade());
         }
 
     }
@@ -95,13 +95,13 @@ class NetconfTopologyContext implements ClusterSingletonService {
     @Override
     public ListenableFuture<Void> closeServiceInstance() {
 
-        if (!finalClose) {
+        if (!closed.get()) {
             // in case that master changes role to slave, new NodeDeviceManager must be created and listener registered
             netconfNodeManager = createNodeDeviceManager();
         }
         stopDeviceConnectorAndActor();
 
-        return Futures.immediateCheckedFuture(null);
+        return Futures.immediateFuture(null);
     }
 
     @Override
@@ -113,13 +113,16 @@ class NetconfTopologyContext implements ClusterSingletonService {
         final NetconfNodeManager ndm =
                 new NetconfNodeManager(netconfTopologyDeviceSetup, remoteDeviceId, actorResponseWaitTime, mountService);
         ndm.registerDataTreeChangeListener(netconfTopologyDeviceSetup.getTopologyId(),
-                netconfTopologyDeviceSetup.getNode().getKey());
+                netconfTopologyDeviceSetup.getNode().key());
 
         return ndm;
     }
 
-    void closeFinal() throws Exception {
-        finalClose = true;
+    @Override
+    public void close() {
+        if (!closed.compareAndSet(false, true)) {
+            return;
+        }
 
         if (netconfNodeManager != null) {
             netconfNodeManager.close();
@@ -129,13 +132,13 @@ class NetconfTopologyContext implements ClusterSingletonService {
     }
 
     /**
-     * If configuration data was changed
+     * Refresh, if configuration data was changed.
      * @param setup new setup
      */
     void refresh(@Nonnull final NetconfTopologySetup setup) {
         netconfTopologyDeviceSetup = Preconditions.checkNotNull(setup);
         remoteDeviceId = NetconfTopologyUtils.createRemoteDeviceId(netconfTopologyDeviceSetup.getNode().getNodeId(),
-                netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class));
+                netconfTopologyDeviceSetup.getNode().augmentation(NetconfNode.class));
 
         if (isMaster) {
             remoteDeviceConnector.stopRemoteDeviceConnection();
@@ -143,8 +146,7 @@ class NetconfTopologyContext implements ClusterSingletonService {
         if (!isMaster) {
             netconfNodeManager.refreshDevice(netconfTopologyDeviceSetup, remoteDeviceId);
         }
-        remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId,
-                actorResponseWaitTime, mountService);
+        remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId);
 
         if (isMaster) {
             final Future<Object> future = Patterns.ask(masterActorRef, new RefreshSetupMasterActorData(
@@ -152,19 +154,19 @@ class NetconfTopologyContext implements ClusterSingletonService {
 
             future.onComplete(new OnComplete<Object>() {
                 @Override
-                public void onComplete(final Throwable failure, final Object success) throws Throwable {
+                public void onComplete(final Throwable failure, final Object success) {
                     if (failure != null) {
-                        LOG.error("Failed to refresh master actor data: {}", failure);
+                        LOG.error("Failed to refresh master actor data", failure);
                         return;
                     }
-                    remoteDeviceConnector.startRemoteDeviceConnection(masterActorRef);
+                    remoteDeviceConnector.startRemoteDeviceConnection(newMasterSalFacade());
                 }
             }, netconfTopologyDeviceSetup.getActorSystem().dispatcher());
         }
     }
 
-    private synchronized void stopDeviceConnectorAndActor() {
-        if (closed) {
+    private void stopDeviceConnectorAndActor() {
+        if (!stopped.compareAndSet(false, true)) {
             return;
         }
         if (remoteDeviceConnector != null) {
@@ -175,6 +177,10 @@ class NetconfTopologyContext implements ClusterSingletonService {
             netconfTopologyDeviceSetup.getActorSystem().stop(masterActorRef);
             masterActorRef = null;
         }
-        closed = true;
+    }
+
+    protected MasterSalFacade newMasterSalFacade() {
+        return new MasterSalFacade(remoteDeviceId, netconfTopologyDeviceSetup.getActorSystem(), masterActorRef,
+                actorResponseWaitTime, mountService, netconfTopologyDeviceSetup.getDataBroker());
     }
 }