03151505f72c3aa6aa1ab01af487dd618e9f9c24
[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.netconf.topology.NodeManager;
21 import org.opendaylight.netconf.topology.NodeManagerCallback;
22 import org.opendaylight.netconf.topology.NodeManagerCallback.NodeManagerCallbackFactory;
23 import org.opendaylight.netconf.topology.RoleChangeStrategy;
24 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30
31 public final class BaseNodeManager implements NodeManager {
32
33     private static final Logger LOG = LoggerFactory.getLogger(BaseNodeManager.class);
34
35     private final String nodeId;
36     private final String topologyId;
37     private final ActorSystem actorSystem;
38
39     private boolean isMaster;
40     private 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.topologyId = topologyId;
50         this.actorSystem = actorSystem;
51         this.delegate = delegateFactory.create(nodeId, topologyId, actorSystem);
52         // if we want to override the place election happens,
53         // we need to override this with noop election strategy and implement election in callback
54         // cannot leak "this" here! have to use TypedActor.self()
55         roleChangeStrategy.registerRoleCandidate((NodeManager) TypedActor.self());
56     }
57
58     @Nonnull @Override public Node getInitialState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
59         LOG.trace("Retrieving Node {} initial state", nodeId);
60         return delegate.getInitialState(nodeId, configNode);
61     }
62
63     @Nonnull @Override public Node getFailedState(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
64         LOG.trace("Retrieving Node {} failed state", nodeId);
65         return delegate.getFailedState(nodeId, configNode);
66     }
67
68     @Nonnull @Override public ListenableFuture<Node> onNodeCreated(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
69         LOG.debug("Creating Node {}, with configuration: {}", nodeId.getValue(), configNode);
70         return delegate.onNodeCreated(nodeId, configNode);
71     }
72
73     @Nonnull @Override public ListenableFuture<Node> onNodeUpdated(@Nonnull final NodeId nodeId, @Nonnull final Node configNode) {
74         LOG.debug("Updating Node {}, with configuration: {}", nodeId.getValue(), configNode);
75         return delegate.onNodeUpdated(nodeId, configNode);
76     }
77
78     @Nonnull @Override public ListenableFuture<Void> onNodeDeleted(@Nonnull final NodeId nodeId) {
79         LOG.debug("Deleting Node {}", nodeId.getValue());
80         return delegate.onNodeDeleted(nodeId);
81     }
82
83     @Nonnull
84     @Override
85     public ListenableFuture<Node> getCurrentStatusForNode(@Nonnull NodeId nodeId) {
86         LOG.debug("Getting current status for node: {}", nodeId.getValue());
87         return delegate.getCurrentStatusForNode(nodeId);
88     }
89
90     @Override
91     public void onRoleChanged(RoleChangeDTO roleChangeDTO) {
92         LOG.debug("Node {} role has changed from: {} to {}", nodeId,
93                 (roleChangeDTO.wasOwner() ? "master" : "slave"),
94                 (roleChangeDTO.isOwner() ? "master" : "slave"));
95
96         isMaster = roleChangeDTO.isOwner();
97         delegate.onRoleChanged(roleChangeDTO);
98     }
99
100     @Override
101     public void onReceive(Object o, ActorRef actorRef) {
102
103     }
104
105     @Override
106     public Future<NormalizedNodeMessage> onRemoteNodeCreated(final NormalizedNodeMessage message) {
107         return null;
108     }
109
110     @Override
111     public Future<NormalizedNodeMessage> onRemoteNodeUpdated(final NormalizedNodeMessage message) {
112         return null;
113     }
114
115     @Override
116     public Future<Void> onRemoteNodeDeleted(final NodeId nodeId) {
117         return null;
118     }
119
120     @Override
121     public Future<NormalizedNodeMessage> remoteGetCurrentStatusForNode(final NodeId nodeId) {
122         return null;
123     }
124
125     /**
126      * Builder of BaseNodeManager instances that are proxied as TypedActors
127      */
128     public static class BaseNodeManagerBuilder {
129         private String nodeId;
130         private String topologyId;
131         private NodeManagerCallbackFactory delegateFactory;
132         private RoleChangeStrategy roleChangeStrategy;
133         private ActorContext actorContext;
134
135
136         public BaseNodeManagerBuilder setNodeId(final String nodeId) {
137             this.nodeId = nodeId;
138             return this;
139         }
140
141         public BaseNodeManagerBuilder setTopologyId(final String topologyId) {
142             this.topologyId = topologyId;
143             return this;
144         }
145
146         public BaseNodeManagerBuilder setDelegateFactory(final NodeManagerCallbackFactory delegateFactory) {
147             this.delegateFactory = delegateFactory;
148             return this;
149         }
150
151         public BaseNodeManagerBuilder setRoleChangeStrategy(final RoleChangeStrategy roleChangeStrategy) {
152             this.roleChangeStrategy = roleChangeStrategy;
153             return this;
154         }
155
156         public BaseNodeManagerBuilder setActorContext(final ActorContext actorContext) {
157             this.actorContext = actorContext;
158             return this;
159         }
160
161         public NodeManager build() {
162             Preconditions.checkNotNull(nodeId);
163             Preconditions.checkNotNull(topologyId);
164             Preconditions.checkNotNull(delegateFactory);
165             Preconditions.checkNotNull(roleChangeStrategy);
166             Preconditions.checkNotNull(actorContext);
167             LOG.debug("Creating typed actor with id: {}", nodeId);
168
169             return TypedActor.get(actorContext).typedActorOf(new TypedProps<>(NodeManager.class, new Creator<BaseNodeManager>() {
170                 @Override
171                 public BaseNodeManager create() throws Exception {
172                     return new BaseNodeManager(nodeId, topologyId, actorContext.system(), delegateFactory, roleChangeStrategy);
173                 }
174             }), nodeId);
175         }
176     }
177 }