RemoteDeviceDataBroker proxy
[netconf.git] / opendaylight / netconf / abstract-topology / src / main / java / org / opendaylight / netconf / topology / util / BaseNodeManager.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.util;
10
11 import akka.actor.ActorContext;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.TypedActor;
15 import akka.actor.TypedProps;
16 import akka.japi.Creator;
17 import com.google.common.base.Preconditions;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
22 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
23 import org.opendaylight.netconf.topology.NodeManager;
24 import org.opendaylight.netconf.topology.NodeManagerCallback;
25 import org.opendaylight.netconf.topology.NodeManagerCallback.NodeManagerCallbackFactory;
26 import org.opendaylight.netconf.topology.RoleChangeStrategy;
27 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
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.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import scala.concurrent.Future;
34
35 public final class BaseNodeManager implements NodeManager {
36
37     private static final Logger LOG = LoggerFactory.getLogger(BaseNodeManager.class);
38
39     private final String nodeId;
40     private final String topologyId;
41     private final ActorSystem actorSystem;
42
43     private boolean isMaster = false;
44     private NodeManagerCallback delegate;
45
46     private BaseNodeManager(final String nodeId,
47                             final String topologyId,
48                             final ActorSystem actorSystem,
49                             final NodeManagerCallbackFactory delegateFactory,
50                             final RoleChangeStrategy roleChangeStrategy) {
51         LOG.debug("Creating BaseNodeManager, id: {}, {}", topologyId, nodeId );
52         this.nodeId = nodeId;
53         this.topologyId = topologyId;
54         this.actorSystem = actorSystem;
55         this.delegate = delegateFactory.create(nodeId, topologyId, actorSystem);
56         // if we want to override the place election happens,
57         // we need to override this with noop election strategy and implement election in callback
58         // cannot leak "this" here! have to use TypedActor.self()
59         roleChangeStrategy.registerRoleCandidate((NodeManager) TypedActor.self());
60     }
61
62     @Nonnull @Override public Node getInitialState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
63         LOG.trace("Retrieving Node {} initial state", nodeId);
64         return delegate.getInitialState(nodeId, configNode);
65     }
66
67     @Nonnull @Override public Node getFailedState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
68         LOG.trace("Retrieving Node {} failed state", nodeId);
69         return delegate.getFailedState(nodeId, configNode);
70     }
71
72     @Nonnull @Override public ListenableFuture<Node> onNodeCreated(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
73         LOG.debug("Creating Node {}, with configuration: {}", nodeId.getValue(), configNode);
74         return delegate.onNodeCreated(nodeId, configNode);
75     }
76
77     @Nonnull @Override public ListenableFuture<Node> onNodeUpdated(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
78         LOG.debug("Updating Node {}, with configuration: {}", nodeId.getValue(), configNode);
79         return delegate.onNodeUpdated(nodeId, configNode);
80     }
81
82     @Nonnull @Override public ListenableFuture<Void> onNodeDeleted(@Nonnull final NodeId nodeId) {
83         LOG.debug("Deleting Node {}", nodeId.getValue());
84         return delegate.onNodeDeleted(nodeId);
85     }
86
87     @Nonnull
88     @Override
89     public ListenableFuture<Node> getCurrentStatusForNode(@Nonnull NodeId nodeId) {
90         LOG.debug("Getting current status for node: {}", nodeId.getValue());
91         return delegate.getCurrentStatusForNode(nodeId);
92     }
93
94     @Override
95     public void onRoleChanged(RoleChangeDTO roleChangeDTO) {
96         LOG.debug("Node {} role has changed from: {} to {}", nodeId,
97                 (roleChangeDTO.wasOwner() ? "master" : "slave"),
98                 (roleChangeDTO.isOwner() ? "master" : "slave"));
99
100         isMaster = roleChangeDTO.isOwner();
101         delegate.onRoleChanged(roleChangeDTO);
102     }
103
104     @Override
105     public void onReceive(Object o, ActorRef actorRef) {
106         delegate.onReceive(o, actorRef);
107     }
108
109     @Override
110     public Future<NormalizedNodeMessage> onRemoteNodeCreated(final NormalizedNodeMessage message) {
111         return null;
112     }
113
114     @Override
115     public Future<NormalizedNodeMessage> onRemoteNodeUpdated(final NormalizedNodeMessage message) {
116         return null;
117     }
118
119     @Override
120     public Future<Void> onRemoteNodeDeleted(final NodeId nodeId) {
121         return null;
122     }
123
124     @Override
125     public Future<NormalizedNodeMessage> remoteGetCurrentStatusForNode(final NodeId nodeId) {
126         return null;
127     }
128
129     @Override
130     public void onDeviceConnected(SchemaContext remoteSchemaContext, NetconfSessionPreferences netconfSessionPreferences, DOMRpcService deviceRpc) {
131         delegate.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc);
132     }
133
134     @Override
135     public void onDeviceDisconnected() {
136         delegate.onDeviceDisconnected();
137     }
138
139     @Override
140     public void onDeviceFailed(Throwable throwable) {
141         delegate.onDeviceFailed(throwable);
142     }
143
144     @Override
145     public void onNotification(DOMNotification domNotification) {
146         delegate.onNotification(domNotification);
147     }
148
149     @Override
150     public void close() {
151         // NOOP
152     }
153
154     /**
155      * Builder of BaseNodeManager instances that are proxied as TypedActors
156      */
157     public static class BaseNodeManagerBuilder {
158         private String nodeId;
159         private String topologyId;
160         private NodeManagerCallbackFactory delegateFactory;
161         private RoleChangeStrategy roleChangeStrategy;
162         private ActorContext actorContext;
163
164
165         public BaseNodeManagerBuilder setNodeId(final String nodeId) {
166             this.nodeId = nodeId;
167             return this;
168         }
169
170         public BaseNodeManagerBuilder setTopologyId(final String topologyId) {
171             this.topologyId = topologyId;
172             return this;
173         }
174
175         public BaseNodeManagerBuilder setDelegateFactory(final NodeManagerCallbackFactory delegateFactory) {
176             this.delegateFactory = delegateFactory;
177             return this;
178         }
179
180         public BaseNodeManagerBuilder setRoleChangeStrategy(final RoleChangeStrategy roleChangeStrategy) {
181             this.roleChangeStrategy = roleChangeStrategy;
182             return this;
183         }
184
185         public BaseNodeManagerBuilder setActorContext(final ActorContext actorContext) {
186             this.actorContext = actorContext;
187             return this;
188         }
189
190         public NodeManager build() {
191             Preconditions.checkNotNull(nodeId);
192             Preconditions.checkNotNull(topologyId);
193             Preconditions.checkNotNull(delegateFactory);
194             Preconditions.checkNotNull(roleChangeStrategy);
195             Preconditions.checkNotNull(actorContext);
196             LOG.debug("Creating typed actor with id: {}", nodeId);
197
198             return TypedActor.get(actorContext).typedActorOf(new TypedProps<>(NodeManager.class, new Creator<BaseNodeManager>() {
199                 @Override
200                 public BaseNodeManager create() throws Exception {
201                     return new BaseNodeManager(nodeId, topologyId, actorContext.system(), delegateFactory, roleChangeStrategy);
202                 }
203             }), nodeId);
204         }
205     }
206 }