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
9 package org.opendaylight.netconf.topology.singleton.impl;
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.util.Timeout;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.netconf.topology.singleton.api.NetconfTopologySingletonService;
24 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
25 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
26 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
27 import org.opendaylight.netconf.topology.singleton.messages.AskForMasterMountPoint;
28 import org.opendaylight.netconf.topology.singleton.messages.UnregisterSlaveMountPoint;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
36 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Managing and reacting on data tree changes in specific netconf node when master writes status to the operational
42 * data store (e.g. handling lifecycle of slave mount point).
44 class NetconfNodeManager
45 implements ClusteredDataTreeChangeListener<Node>, NetconfTopologySingletonService, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManager.class);
49 private final Timeout actorResponseWaitTime;
50 private final DOMMountPointService mountPointService;
51 private final SchemaSourceRegistry schemaRegistry;
52 private final SchemaRepository schemaRepository;
54 private NetconfTopologySetup setup;
55 private ListenerRegistration<NetconfNodeManager> dataChangeListenerRegistration;
56 private RemoteDeviceId id;
57 private ActorRef slaveActorRef;
60 NetconfNodeManager(final NetconfTopologySetup setup,
61 final RemoteDeviceId id, final Timeout actorResponseWaitTime,
62 final DOMMountPointService mountPointService) {
65 this.schemaRegistry = setup.getSchemaResourcesDTO().getSchemaRegistry();
66 this.schemaRepository = setup.getSchemaResourcesDTO().getSchemaRepository();
67 this.actorResponseWaitTime = actorResponseWaitTime;
68 this.mountPointService = mountPointService;
72 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> changes) {
73 for (final DataTreeModification<Node> change : changes) {
74 final DataObjectModification<Node> rootNode = change.getRootNode();
75 final NodeId nodeId = NetconfTopologyUtils.getNodeId(rootNode.getIdentifier());
76 switch (rootNode.getModificationType()) {
77 case SUBTREE_MODIFIED:
78 LOG.debug("{}: Operational for node {} updated. Trying to register slave mount point", id, nodeId);
79 handleSlaveMountPoint(rootNode);
82 if (rootNode.getDataBefore() != null) {
83 LOG.debug("{}: Operational for node {} rewrited. Trying to register slave mount point",
86 LOG.debug("{}: Operational for node {} created. Trying to register slave mount point",
89 handleSlaveMountPoint(rootNode);
92 LOG.debug("{}: Operational for node {} deleted. Trying to remove slave mount point", id, nodeId);
96 LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
102 public void close() {
105 if (dataChangeListenerRegistration != null) {
106 dataChangeListenerRegistration.close();
107 dataChangeListenerRegistration = null;
111 private void closeActor() {
112 if (slaveActorRef != null) {
113 slaveActorRef.tell(new UnregisterSlaveMountPoint(), ActorRef.noSender());
114 slaveActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
115 slaveActorRef = null;
119 void registerDataTreeChangeListener(final String topologyId, final NodeKey key) {
120 LOG.debug("{}: Registering data tree change listener on node {}", id, key);
121 dataChangeListenerRegistration = setup.getDataBroker().registerDataTreeChangeListener(
122 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
123 NetconfTopologyUtils.createTopologyNodeListPath(key, topologyId)), this);
126 private void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
127 @SuppressWarnings("ConstantConditions")
128 final NetconfNode netconfNodeAfter = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
130 if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
132 final String masterAddress = netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode();
133 final String path = NetconfTopologyUtils.createActorPath(masterAddress,
134 NetconfTopologyUtils.createMasterActorName(id.getName(),
135 netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode()));
136 setup.getActorSystem().actorSelection(path).tell(new AskForMasterMountPoint(), slaveActorRef);
142 private void createActorRef() {
143 if (slaveActorRef == null) {
144 slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, schemaRegistry,
145 schemaRepository, actorResponseWaitTime, mountPointService), id.getName());
149 void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
150 setup = netconfTopologyDeviceSetup;