Bug 8032 - Initialization in sal failed, disconnecting from device
[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", id, nodeId);
84                     } else {
85                         LOG.debug("{}: Operational for node {} created. Trying to register slave mount point", id, nodeId);
86                     }
87                     handleSlaveMountPoint(rootNode);
88                     break;
89                 case DELETE:
90                     LOG.debug("{}: Operational for node {} deleted. Trying to remove slave mount point", id, nodeId);
91                     closeActor();
92                     break;
93                 default:
94                     LOG.debug("{}: Uknown operation for node: {}", id, nodeId);
95             }
96         }
97     }
98
99     @Override
100     public void close() {
101         closeActor();
102
103         if (dataChangeListenerRegistration != null) {
104             dataChangeListenerRegistration.close();
105             dataChangeListenerRegistration = null;
106         }
107     }
108
109     private void closeActor() {
110         if (slaveActorRef != null) {
111             slaveActorRef.tell(new UnregisterSlaveMountPoint(), ActorRef.noSender());
112             slaveActorRef.tell(PoisonPill.getInstance(), ActorRef.noSender());
113             slaveActorRef = null;
114         }
115     }
116
117     void registerDataTreeChangeListener(final String topologyId, final NodeKey key) {
118         LOG.debug("{}: Registering data tree change listener on node {}", id, key);
119         dataChangeListenerRegistration = setup.getDataBroker().registerDataTreeChangeListener(
120                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
121                         NetconfTopologyUtils.createTopologyNodeListPath(key, topologyId)), this);
122     }
123
124     private void handleSlaveMountPoint(final DataObjectModification<Node> rootNode) {
125         @SuppressWarnings("ConstantConditions")
126         final NetconfNode netconfNodeAfter = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
127
128         if (NetconfNodeConnectionStatus.ConnectionStatus.Connected.equals(netconfNodeAfter.getConnectionStatus())) {
129             createActorRef();
130             final String masterAddress = netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode();
131             final String path = NetconfTopologyUtils.createActorPath(masterAddress,
132                     NetconfTopologyUtils.createMasterActorName(id.getName(),
133                             netconfNodeAfter.getClusteredConnectionStatus().getNetconfMasterNode()));
134             setup.getActorSystem().actorSelection(path).tell(new AskForMasterMountPoint(), slaveActorRef);
135         } else {
136             closeActor();
137         }
138     }
139
140     private void createActorRef() {
141         if (slaveActorRef == null) {
142             slaveActorRef = setup.getActorSystem().actorOf(NetconfNodeActor.props(setup, id, schemaRegistry,
143                     schemaRepository, actorResponseWaitTime, mountPointService), id.getName());
144         }
145     }
146
147     void refreshDevice(final NetconfTopologySetup netconfTopologyDeviceSetup, final RemoteDeviceId remoteDeviceId) {
148         setup = netconfTopologyDeviceSetup;
149         id = remoteDeviceId;
150     }
151 }