2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.util;
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;
31 public final class BaseNodeManager implements NodeManager {
33 private static final Logger LOG = LoggerFactory.getLogger(BaseNodeManager.class);
35 private final String nodeId;
36 private final String topologyId;
37 private final ActorSystem actorSystem;
39 private boolean isMaster;
40 private NodeManagerCallback delegate;
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 );
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());
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);
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);
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);
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);
78 @Nonnull @Override public ListenableFuture<Void> onNodeDeleted(@Nonnull final NodeId nodeId) {
79 LOG.debug("Deleting Node {}", nodeId.getValue());
80 return delegate.onNodeDeleted(nodeId);
85 public ListenableFuture<Node> getCurrentStatusForNode(@Nonnull NodeId nodeId) {
86 LOG.debug("Getting current status for node: {}", nodeId.getValue());
87 return delegate.getCurrentStatusForNode(nodeId);
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"));
96 isMaster = roleChangeDTO.isOwner();
97 delegate.onRoleChanged(roleChangeDTO);
101 public void onReceive(Object o, ActorRef actorRef) {
106 public Future<NormalizedNodeMessage> onRemoteNodeCreated(final NormalizedNodeMessage message) {
111 public Future<NormalizedNodeMessage> onRemoteNodeUpdated(final NormalizedNodeMessage message) {
116 public Future<Void> onRemoteNodeDeleted(final NodeId nodeId) {
121 public Future<NormalizedNodeMessage> remoteGetCurrentStatusForNode(final NodeId nodeId) {
126 * Builder of BaseNodeManager instances that are proxied as TypedActors
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;
136 public BaseNodeManagerBuilder setNodeId(final String nodeId) {
137 this.nodeId = nodeId;
141 public BaseNodeManagerBuilder setTopologyId(final String topologyId) {
142 this.topologyId = topologyId;
146 public BaseNodeManagerBuilder setDelegateFactory(final NodeManagerCallbackFactory delegateFactory) {
147 this.delegateFactory = delegateFactory;
151 public BaseNodeManagerBuilder setRoleChangeStrategy(final RoleChangeStrategy roleChangeStrategy) {
152 this.roleChangeStrategy = roleChangeStrategy;
156 public BaseNodeManagerBuilder setActorContext(final ActorContext actorContext) {
157 this.actorContext = actorContext;
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);
169 return TypedActor.get(actorContext).typedActorOf(new TypedProps<>(NodeManager.class, new Creator<BaseNodeManager>() {
171 public BaseNodeManager create() throws Exception {
172 return new BaseNodeManager(nodeId, topologyId, actorContext.system(), delegateFactory, roleChangeStrategy);