2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.topology.singleton.impl;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.actor.PoisonPill;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.AskTimeoutException;
15 import akka.pattern.Patterns;
16 import akka.util.Timeout;
17 import java.util.Collection;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.checkerframework.checker.lock.qual.Holding;
20 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.mdsal.binding.api.DataTreeModification;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.netconf.topology.singleton.api.NetconfTopologySingletonService;
28 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
29 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
30 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
31 import org.opendaylight.netconf.topology.singleton.messages.AskForMasterMountPoint;
32 import org.opendaylight.netconf.topology.singleton.messages.RefreshSlaveActor;
33 import org.opendaylight.netconf.topology.singleton.messages.UnregisterSlaveMountPoint;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Managing and reacting on data tree changes in specific netconf node when master writes status to the operational
46 * data store (e.g. handling lifecycle of slave mount point).
48 class NetconfNodeManager
49 implements ClusteredDataTreeChangeListener<Node>, NetconfTopologySingletonService, AutoCloseable {
51 private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManager.class);
53 private final Timeout actorResponseWaitTime;
54 private final DOMMountPointService mountPointService;
56 private volatile NetconfTopologySetup setup;
57 private volatile ListenerRegistration<NetconfNodeManager> dataChangeListenerRegistration;
58 private volatile RemoteDeviceId id;
61 private ActorRef slaveActorRef;
64 private boolean closed;
67 private int lastUpdateCount;
69 NetconfNodeManager(final NetconfTopologySetup setup,
70 final RemoteDeviceId id, final Timeout actorResponseWaitTime,
71 final DOMMountPointService mountPointService) {
74 this.actorResponseWaitTime = actorResponseWaitTime;
75 this.mountPointService = mountPointService;
79 public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
80 for (final DataTreeModification<Node> change : changes) {
81 final DataObjectModification<Node> rootNode = change.getRootNode();
82 final NodeId nodeId = NetconfTopologyUtils.getNodeId(rootNode.getIdentifier());
83 switch (rootNode.getModificationType()) {
84 case SUBTREE_MODIFIED:
85 LOG.debug("{}: Operational state for node {} - subtree modified from {} to {}",
86 id, nodeId, rootNode.getDataBefore(), rootNode.getDataAfter());
87 handleSlaveMountPoint(rootNode);
90 if (rootNode.getDataBefore() != null) {
91 LOG.debug("{}: Operational state for node {} updated from {} to {}",
92 id, nodeId, rootNode.getDataBefore(), rootNode.getDataAfter());
94 LOG.debug("{}: Operational state for node {} created: {}",
95 id, nodeId, rootNode.getDataAfter());
97 handleSlaveMountPoint(rootNode);
100 LOG.debug("{}: Operational state for node {} deleted.", id, nodeId);
101 unregisterSlaveMountpoint();
104 LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
110 public synchronized void close() {
117 if (dataChangeListenerRegistration != null) {
118 dataChangeListenerRegistration.close();
119 dataChangeListenerRegistration = null;
124 private void closeActor() {
125 if (slaveActorRef != null) {
126 LOG.debug("{}: Sending poison pill to {}", id, slaveActorRef);
127 slaveActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
128 slaveActorRef = null;
132 private synchronized void unregisterSlaveMountpoint() {
134 if (slaveActorRef != null) {
135 LOG.debug("{}: Sending message to unregister slave mountpoint to {}", id, slaveActorRef);
136 slaveActorRef.tell(new UnregisterSlaveMountPoint(), ActorRef.noSender());
140 void registerDataTreeChangeListener(final String topologyId, final NodeKey key) {
141 final InstanceIdentifier<Node> path = NetconfTopologyUtils.createTopologyNodeListPath(key, topologyId);
142 LOG.debug("{}: Registering data tree change listener on path {}", id, path);
143 dataChangeListenerRegistration = setup.getDataBroker().registerDataTreeChangeListener(
144 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path), this);
147 private synchronized void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
152 @SuppressWarnings("ConstantConditions")
153 final NetconfNode netconfNodeAfter = rootNode.getDataAfter().augmentation(NetconfNode.class);
155 if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
157 createOrUpdateActorRef();
159 final String masterAddress = netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode();
160 final String masterActorPath = NetconfTopologyUtils.createActorPath(masterAddress,
161 NetconfTopologyUtils.createMasterActorName(id.getName(),
162 netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode()));
164 final AskForMasterMountPoint askForMasterMountPoint = new AskForMasterMountPoint(slaveActorRef);
165 final ActorSelection masterActor = setup.getActorSystem().actorSelection(masterActorPath);
167 LOG.debug("{}: Sending {} message to master {}", id, askForMasterMountPoint, masterActor);
169 sendAskForMasterMountPointWithRetries(askForMasterMountPoint, masterActor, 1, lastUpdateCount);
171 unregisterSlaveMountpoint();
176 private void sendAskForMasterMountPointWithRetries(final AskForMasterMountPoint askForMasterMountPoint,
177 final ActorSelection masterActor, final int tries, final int updateCount) {
178 Patterns.ask(masterActor, askForMasterMountPoint, actorResponseWaitTime).onComplete(new OnComplete<>() {
180 public void onComplete(final Throwable failure, final Object response) {
181 synchronized (this) {
182 // Ignore the response if we were since closed or another notification update occurred.
183 if (closed || updateCount != lastUpdateCount) {
187 if (failure instanceof AskTimeoutException) {
188 if (tries <= 5 || tries % 10 == 0) {
189 LOG.warn("{}: Failed to send message to {} - retrying...", id, masterActor, failure);
191 sendAskForMasterMountPointWithRetries(askForMasterMountPoint, masterActor, tries + 1,
193 } else if (failure != null) {
194 LOG.error("{}: Failed to send message {} to {}. Slave mount point could not be created",
195 id, askForMasterMountPoint, masterActor, failure);
197 LOG.debug("{}: {} message to {} succeeded", id, askForMasterMountPoint, masterActor);
201 }, setup.getActorSystem().dispatcher());
205 private void createOrUpdateActorRef() {
206 if (slaveActorRef == null) {
207 slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, actorResponseWaitTime,
209 LOG.debug("{}: Slave actor created with name {}", id, slaveActorRef);
211 slaveActorRef.tell(new RefreshSlaveActor(setup, id, actorResponseWaitTime), ActorRef.noSender());
215 void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
216 setup = netconfTopologyDeviceSetup;