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