a33953291f60c333369efbbe1adb47bcc0f25bbe
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / NetconfNodeManager.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.singleton.impl;
10
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.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.NetconfTopologySingletonService;
25 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
26 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
27 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
28 import org.opendaylight.netconf.topology.singleton.messages.AskForMasterMountPoint;
29 import org.opendaylight.netconf.topology.singleton.messages.UnregisterSlaveMountPoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
37 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Managing and reacting on data tree changes in specific netconf node when master writes status to the operational
43  * data store (e.g. handling lifecycle of slave mount point).
44  */
45 class NetconfNodeManager
46         implements ClusteredDataTreeChangeListener<Node>, NetconfTopologySingletonService, AutoCloseable {
47
48     private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManager.class);
49
50     private final Timeout actorResponseWaitTime;
51     private final DOMMountPointService mountPointService;
52     private final SchemaSourceRegistry schemaRegistry;
53     private final SchemaRepository schemaRepository;
54
55     private NetconfTopologySetup setup;
56     private ListenerRegistration<NetconfNodeManager> dataChangeListenerRegistration;
57     private RemoteDeviceId id;
58     private ActorRef slaveActorRef;
59
60
61     NetconfNodeManager(final NetconfTopologySetup setup,
62                        final RemoteDeviceId id, final Timeout actorResponseWaitTime,
63                        final DOMMountPointService mountPointService) {
64         this.setup = setup;
65         this.id = id;
66         this.schemaRegistry = setup.getSchemaResourcesDTO().getSchemaRegistry();
67         this.schemaRepository = setup.getSchemaResourcesDTO().getSchemaRepository();
68         this.actorResponseWaitTime = actorResponseWaitTime;
69         this.mountPointService = mountPointService;
70     }
71
72     @Override
73     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> changes) {
74         for (final DataTreeModification<Node> change : changes) {
75             final DataObjectModification<Node> rootNode = change.getRootNode();
76             final NodeId nodeId = NetconfTopologyUtils.getNodeId(rootNode.getIdentifier());
77             switch (rootNode.getModificationType()) {
78                 case SUBTREE_MODIFIED:
79                     LOG.debug("{}: Operational for node {} updated. Trying to register slave mount point", id, nodeId);
80                     handleSlaveMountPoint(rootNode);
81                     break;
82                 case WRITE:
83                     if (rootNode.getDataBefore() != null) {
84                         LOG.debug("{}: Operational for node {} rewrited. Trying to register slave mount point", id, nodeId);
85                     } else {
86                         LOG.debug("{}: Operational for node {} created. Trying to register slave mount point", id, nodeId);
87                     }
88                     handleSlaveMountPoint(rootNode);
89                     break;
90                 case DELETE:
91                     LOG.debug("{}: Operational for node {} deleted. Trying to remove slave mount point", id, nodeId);
92                     closeActor();
93                     break;
94                 default:
95                     LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
96             }
97         }
98     }
99
100     @Override
101     public void close() {
102         closeActor();
103
104         if (dataChangeListenerRegistration != null) {
105             dataChangeListenerRegistration.close();
106             dataChangeListenerRegistration = null;
107         }
108     }
109
110     private void closeActor() {
111         if (slaveActorRef != null) {
112             slaveActorRef.tell(new UnregisterSlaveMountPoint(), ActorRef.noSender());
113             slaveActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
114             slaveActorRef = null;
115         }
116     }
117
118     void registerDataTreeChangeListener(final String topologyId, final NodeKey key) {
119         LOG.debug("{}: Registering data tree change listener on node {}", id, key);
120         dataChangeListenerRegistration = setup.getDataBroker().registerDataTreeChangeListener(
121                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
122                         NetconfTopologyUtils.createTopologyNodeListPath(key, topologyId)), this);
123     }
124
125     private void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
126         @SuppressWarnings("ConstantConditions")
127         final NetconfNode netconfNodeAfter = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
128
129         if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
130             createActorRef();
131             final String masterAddress = netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode();
132             final String path = NetconfTopologyUtils.createActorPath(masterAddress,
133                     NetconfTopologyUtils.createMasterActorName(id.getName(),
134                             netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode()));
135             setup.getActorSystem().actorSelection(path).tell(new AskForMasterMountPoint(), slaveActorRef);
136         } else {
137             closeActor();
138         }
139     }
140
141     private void createActorRef() {
142         if (slaveActorRef == null) {
143             slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, schemaRegistry,
144                     schemaRepository, actorResponseWaitTime, mountPointService), id.getName());
145         }
146     }
147
148     void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
149         setup = netconfTopologyDeviceSetup;
150         id = remoteDeviceId;
151     }
152 }