Merge "Bug 8364: Set bundle context when module reloads"
[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.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;
39
40 /**
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).
43  */
44 class NetconfNodeManager
45         implements ClusteredDataTreeChangeListener<Node>, NetconfTopologySingletonService, AutoCloseable {
46
47     private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeManager.class);
48
49     private final Timeout actorResponseWaitTime;
50     private final DOMMountPointService mountPointService;
51     private final SchemaSourceRegistry schemaRegistry;
52     private final SchemaRepository schemaRepository;
53
54     private NetconfTopologySetup setup;
55     private ListenerRegistration<NetconfNodeManager> dataChangeListenerRegistration;
56     private RemoteDeviceId id;
57     private ActorRef slaveActorRef;
58
59
60     NetconfNodeManager(final NetconfTopologySetup setup,
61                        final RemoteDeviceId id, final Timeout actorResponseWaitTime,
62                        final DOMMountPointService mountPointService) {
63         this.setup = setup;
64         this.id = id;
65         this.schemaRegistry = setup.getSchemaResourcesDTO().getSchemaRegistry();
66         this.schemaRepository = setup.getSchemaResourcesDTO().getSchemaRepository();
67         this.actorResponseWaitTime = actorResponseWaitTime;
68         this.mountPointService = mountPointService;
69     }
70
71     @Override
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);
80                     break;
81                 case WRITE:
82                     if (rootNode.getDataBefore() != null) {
83                         LOG.debug("{}: Operational for node {} rewrited. Trying to register slave mount point",
84                                 id, nodeId);
85                     } else {
86                         LOG.debug("{}: Operational for node {} created. Trying to register slave mount point",
87                                 id, nodeId);
88                     }
89                     handleSlaveMountPoint(rootNode);
90                     break;
91                 case DELETE:
92                     LOG.debug("{}: Operational for node {} deleted. Trying to remove slave mount point", id, nodeId);
93                     closeActor();
94                     break;
95                 default:
96                     LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
97             }
98         }
99     }
100
101     @Override
102     public void close() {
103         closeActor();
104
105         if (dataChangeListenerRegistration != null) {
106             dataChangeListenerRegistration.close();
107             dataChangeListenerRegistration = null;
108         }
109     }
110
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;
116         }
117     }
118
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);
124     }
125
126     private void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
127         @SuppressWarnings("ConstantConditions")
128         final NetconfNode netconfNodeAfter = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
129
130         if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
131             createActorRef();
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);
137         } else {
138             closeActor();
139         }
140     }
141
142     private void createActorRef() {
143         if (slaveActorRef == null) {
144             slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, schemaRegistry,
145                     schemaRepository, actorResponseWaitTime, mountPointService), id.getName());
146         }
147     }
148
149     void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
150         setup = netconfTopologyDeviceSetup;
151         id = remoteDeviceId;
152     }
153 }