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 javax.annotation.Nonnull;
19 import javax.annotation.concurrent.GuardedBy;
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.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
42 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import scala.concurrent.Future;
48 * Managing and reacting on data tree changes in specific netconf node when master writes status to the operational
49 * data store (e.g. handling lifecycle of slave mount point).
51 class NetconfNodeManager
52 implements ClusteredDataTreeChangeListener<Node>, NetconfTopologySingletonService, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManager.class);
56 private final Timeout actorResponseWaitTime;
57 private final DOMMountPointService mountPointService;
58 private final SchemaSourceRegistry schemaRegistry;
59 private final SchemaRepository schemaRepository;
61 private volatile NetconfTopologySetup setup;
62 private volatile ListenerRegistration<NetconfNodeManager> dataChangeListenerRegistration;
63 private volatile RemoteDeviceId id;
66 private ActorRef slaveActorRef;
69 private boolean closed;
72 private int lastUpdateCount;
74 NetconfNodeManager(final NetconfTopologySetup setup,
75 final RemoteDeviceId id, final Timeout actorResponseWaitTime,
76 final DOMMountPointService mountPointService) {
79 this.schemaRegistry = setup.getSchemaResourcesDTO().getSchemaRegistry();
80 this.schemaRepository = setup.getSchemaResourcesDTO().getSchemaRepository();
81 this.actorResponseWaitTime = actorResponseWaitTime;
82 this.mountPointService = mountPointService;
86 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> changes) {
87 for (final DataTreeModification<Node> change : changes) {
88 final DataObjectModification<Node> rootNode = change.getRootNode();
89 final NodeId nodeId = NetconfTopologyUtils.getNodeId(rootNode.getIdentifier());
90 switch (rootNode.getModificationType()) {
91 case SUBTREE_MODIFIED:
92 LOG.debug("{}: Operational state for node {} - subtree modified from {} to {}",
93 id, nodeId, rootNode.getDataBefore(), rootNode.getDataAfter());
94 handleSlaveMountPoint(rootNode);
97 if (rootNode.getDataBefore() != null) {
98 LOG.debug("{}: Operational state for node {} updated from {} to {}",
99 id, nodeId, rootNode.getDataBefore(), rootNode.getDataAfter());
101 LOG.debug("{}: Operational state for node {} created: {}",
102 id, nodeId, rootNode.getDataAfter());
104 handleSlaveMountPoint(rootNode);
107 LOG.debug("{}: Operational state for node {} deleted.", id, nodeId);
108 unregisterSlaveMountpoint();
111 LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
117 public synchronized void close() {
124 if (dataChangeListenerRegistration != null) {
125 dataChangeListenerRegistration.close();
126 dataChangeListenerRegistration = null;
131 private void closeActor() {
132 if (slaveActorRef != null) {
133 LOG.debug("{}: Sending poison pill to {}", id, slaveActorRef);
134 slaveActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
135 slaveActorRef = null;
139 private synchronized void unregisterSlaveMountpoint() {
141 if (slaveActorRef != null) {
142 LOG.debug("{}: Sending message to unregister slave mountpoint to {}", id, slaveActorRef);
143 slaveActorRef.tell(new UnregisterSlaveMountPoint(), ActorRef.noSender());
147 void registerDataTreeChangeListener(final String topologyId, final NodeKey key) {
148 final InstanceIdentifier<Node> path = NetconfTopologyUtils.createTopologyNodeListPath(key, topologyId);
149 LOG.debug("{}: Registering data tree change listener on path {}", id, path);
150 dataChangeListenerRegistration = setup.getDataBroker().registerDataTreeChangeListener(
151 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path), this);
154 private synchronized void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
159 @SuppressWarnings("ConstantConditions")
160 final NetconfNode netconfNodeAfter = rootNode.getDataAfter().augmentation(NetconfNode.class);
162 if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
164 createOrUpdateActorRef();
166 final String masterAddress = netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode();
167 final String masterActorPath = NetconfTopologyUtils.createActorPath(masterAddress,
168 NetconfTopologyUtils.createMasterActorName(id.getName(),
169 netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode()));
171 final AskForMasterMountPoint askForMasterMountPoint = new AskForMasterMountPoint(slaveActorRef);
172 final ActorSelection masterActor = setup.getActorSystem().actorSelection(masterActorPath);
174 LOG.debug("{}: Sending {} message to master {}", id, askForMasterMountPoint, masterActor);
176 sendAskForMasterMountPointWithRetries(askForMasterMountPoint, masterActor, 1, lastUpdateCount);
178 unregisterSlaveMountpoint();
183 private void sendAskForMasterMountPointWithRetries(final AskForMasterMountPoint askForMasterMountPoint,
184 final ActorSelection masterActor, final int tries, final int updateCount) {
185 final Future<Object> future = Patterns.ask(masterActor, askForMasterMountPoint, actorResponseWaitTime);
186 future.onComplete(new OnComplete<Object>() {
188 public void onComplete(final Throwable failure, final Object response) {
189 synchronized (this) {
190 // Ignore the response if we were since closed or another notification update occurred.
191 if (closed || updateCount != lastUpdateCount) {
195 if (failure instanceof AskTimeoutException) {
196 if (tries <= 5 || tries % 10 == 0) {
197 LOG.warn("{}: Failed to send message to {} - retrying...", id, masterActor, failure);
199 sendAskForMasterMountPointWithRetries(askForMasterMountPoint, masterActor, tries + 1,
201 } else if (failure != null) {
202 LOG.error("{}: Failed to send message {} to {}. Slave mount point could not be created",
203 id, askForMasterMountPoint, masterActor, failure);
205 LOG.debug("{}: {} message to {} succeeded", id, askForMasterMountPoint, masterActor);
209 }, setup.getActorSystem().dispatcher());
213 private void createOrUpdateActorRef() {
214 if (slaveActorRef == null) {
215 slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, schemaRegistry,
216 schemaRepository, actorResponseWaitTime, mountPointService));
217 LOG.debug("{}: Slave actor created with name {}", id, slaveActorRef);
220 .tell(new RefreshSlaveActor(setup, id, schemaRegistry, schemaRepository, actorResponseWaitTime),
221 ActorRef.noSender());
225 void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
226 setup = netconfTopologyDeviceSetup;