Use normal identify messages first
[netconf.git] / opendaylight / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / impl / NetconfTopologyManagerCallback.java
1 /*
2  * Copyright (c) 2015 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.impl;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.actor.TypedActor;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.HashMap;
18 import java.util.Map;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.netconf.topology.NodeManager;
22 import org.opendaylight.netconf.topology.NodeManagerCallback.NodeManagerCallbackFactory;
23 import org.opendaylight.netconf.topology.TopologyManagerCallback;
24 import org.opendaylight.netconf.topology.util.BaseNodeManager.BaseNodeManagerBuilder;
25 import org.opendaylight.netconf.topology.util.NodeWriter;
26 import org.opendaylight.netconf.topology.util.NoopRoleChangeStrategy;
27 import org.opendaylight.netconf.topology.util.SalNodeWriter;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NetconfTopologyManagerCallback implements TopologyManagerCallback {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManagerCallback.class);
36
37     private final ActorSystem actorSystem;
38     private boolean isMaster;
39
40     private final String topologyId;
41     private final NodeWriter naSalNodeWriter;
42     private final Map<NodeId, NodeManager> nodes = new HashMap<>();
43     private final NodeManagerCallbackFactory nodeHandlerFactory;
44
45     public NetconfTopologyManagerCallback(final ActorSystem actorSystem,
46                                           final DataBroker dataBroker,
47                                           final String topologyId,
48                                           final NodeManagerCallbackFactory nodeHandlerFactory) {
49         this(actorSystem, topologyId, nodeHandlerFactory, new SalNodeWriter(dataBroker, topologyId));
50     }
51
52     public NetconfTopologyManagerCallback(final ActorSystem actorSystem,
53                                           final String topologyId,
54                                           final NodeManagerCallbackFactory nodeHandlerFactory,
55                                           final NodeWriter naSalNodeWriter) {
56         this(actorSystem, topologyId, nodeHandlerFactory, naSalNodeWriter, false);
57
58     }
59
60     public NetconfTopologyManagerCallback(final ActorSystem actorSystem,
61                                           final String topologyId,
62                                           final NodeManagerCallbackFactory nodeHandlerFactory,
63                                           final NodeWriter naSalNodeWriter,
64                                           boolean isMaster) {
65         this.actorSystem = actorSystem;
66         this.topologyId = topologyId;
67         this.nodeHandlerFactory = nodeHandlerFactory;
68         this.naSalNodeWriter = naSalNodeWriter;
69
70         this.isMaster = isMaster;
71     }
72
73     @Override
74     public ListenableFuture<Node> onNodeCreated(final NodeId nodeId, final Node node) {
75
76         // if this node was already configured, and whole config was pushed again, reinit with update
77         if (nodes.containsKey(nodeId)) {
78             return onNodeUpdated(nodeId, node);
79         }
80
81         // Init node admin
82         final NodeManager naBaseNodeManager =
83                 createNodeManager(nodeId);
84         nodes.put(nodeId, naBaseNodeManager);
85
86         // only master should put initial state into datastore
87         if (isMaster) {
88             naSalNodeWriter.init(nodeId, naBaseNodeManager.getInitialState(nodeId, node));
89         }
90
91         // trigger connect on this node
92         return naBaseNodeManager.onNodeCreated(nodeId, node);
93     }
94
95     @Override
96     public ListenableFuture<Node> onNodeUpdated(final NodeId nodeId, final Node node) {
97         // only master should put initial state into datastore
98         if (isMaster) {
99             naSalNodeWriter.init(nodeId, nodes.get(nodeId).getInitialState(nodeId, node));
100         }
101
102         // Trigger onNodeUpdated only on this node
103         return nodes.get(nodeId).onNodeUpdated(nodeId, node);
104     }
105
106     @Override
107     public ListenableFuture<Void> onNodeDeleted(final NodeId nodeId) {
108         // Trigger delete only on this node
109         final ListenableFuture<Void> future = nodes.get(nodeId).onNodeDeleted(nodeId);
110         Futures.addCallback(future, new FutureCallback<Void>() {
111             @Override
112             public void onSuccess(Void result) {
113                 // remove proxy from node list and stop the actor
114                 LOG.debug("Stopping node actor for node : {}", nodeId.getValue());
115                 final NodeManager remove = nodes.remove(nodeId);
116                 TypedActor.get(actorSystem).stop(remove);
117             }
118
119             @Override
120             public void onFailure(Throwable t) {
121                 // NOOP will be handled on higher level
122             }
123         });
124         return future;
125     }
126
127     @Nonnull
128     @Override
129     public ListenableFuture<Node> getCurrentStatusForNode(@Nonnull NodeId nodeId) {
130         if (!nodes.containsKey(nodeId)) {
131             nodes.put(nodeId, createNodeManager(nodeId));
132         }
133         return nodes.get(nodeId).getCurrentStatusForNode(nodeId);
134     }
135
136     @Override
137     public void onRoleChanged(RoleChangeDTO roleChangeDTO) {
138         isMaster = roleChangeDTO.isOwner();
139         // our post-election logic
140     }
141
142     private NodeManager createNodeManager(NodeId nodeId) {
143         return new BaseNodeManagerBuilder().setNodeId(nodeId.getValue())
144                 .setActorContext(TypedActor.context())
145                 .setDelegateFactory(nodeHandlerFactory)
146                 .setRoleChangeStrategy(new NoopRoleChangeStrategy())
147                 .setTopologyId(topologyId)
148                 .build();
149     }
150
151     @Override
152     public void onReceive(Object o, ActorRef actorRef) {
153
154     }
155
156     @Nonnull
157     @Override
158     public Node getInitialState(@Nonnull NodeId nodeId, @Nonnull Node configNode) {
159         return nodes.get(nodeId).getInitialState(nodeId, configNode);
160     }
161
162     @Nonnull
163     @Override
164     public Node getFailedState(@Nonnull NodeId nodeId, @Nonnull Node configNode) {
165         return nodes.get(nodeId).getFailedState(nodeId, configNode);
166     }
167 }