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.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;
35 public final class BaseNodeManager implements NodeManager {
37 private static final Logger LOG = LoggerFactory.getLogger(BaseNodeManager.class);
39 private final String nodeId;
40 private final 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.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());
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);
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);
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);
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);
76 @Nonnull @Override public ListenableFuture<Void> onNodeDeleted(@Nonnull final NodeId nodeId) {
77 LOG.debug("Deleting Node {}", nodeId.getValue());
78 return delegate.onNodeDeleted(nodeId);
83 public ListenableFuture<Node> getCurrentStatusForNode(@Nonnull NodeId nodeId) {
84 LOG.debug("Getting current status for node: {}", nodeId.getValue());
85 return delegate.getCurrentStatusForNode(nodeId);
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"));
94 delegate.onRoleChanged(roleChangeDTO);
98 public void onReceive(Object o, ActorRef actorRef) {
99 delegate.onReceive(o, actorRef);
103 public Future<NormalizedNodeMessage> onRemoteNodeCreated(final NormalizedNodeMessage message) {
108 public Future<NormalizedNodeMessage> onRemoteNodeUpdated(final NormalizedNodeMessage message) {
113 public Future<Void> onRemoteNodeDeleted(final NodeId nodeId) {
118 public Future<NormalizedNodeMessage> remoteGetCurrentStatusForNode(final NodeId nodeId) {
123 public void onDeviceConnected(SchemaContext remoteSchemaContext, NetconfSessionPreferences netconfSessionPreferences, DOMRpcService deviceRpc) {
124 delegate.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc);
128 public void onDeviceDisconnected() {
129 delegate.onDeviceDisconnected();
133 public void onDeviceFailed(Throwable throwable) {
134 delegate.onDeviceFailed(throwable);
138 public void onNotification(DOMNotification domNotification) {
139 delegate.onNotification(domNotification);
143 public void close() {
148 * Builder of BaseNodeManager instances that are proxied as TypedActors
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;
158 public BaseNodeManagerBuilder setNodeId(final String nodeId) {
159 this.nodeId = nodeId;
163 public BaseNodeManagerBuilder setTopologyId(final String topologyId) {
164 this.topologyId = topologyId;
168 public BaseNodeManagerBuilder setDelegateFactory(final NodeManagerCallbackFactory delegateFactory) {
169 this.delegateFactory = delegateFactory;
173 public BaseNodeManagerBuilder setRoleChangeStrategy(final RoleChangeStrategy roleChangeStrategy) {
174 this.roleChangeStrategy = roleChangeStrategy;
178 public BaseNodeManagerBuilder setActorContext(final ActorContext actorContext) {
179 this.actorContext = actorContext;
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);
191 return TypedActor.get(actorContext).typedActorOf(new TypedProps<>(NodeManager.class, new Creator<BaseNodeManager>() {
193 public BaseNodeManager create() throws Exception {
194 return new BaseNodeManager(nodeId, topologyId, actorContext.system(), delegateFactory, roleChangeStrategy);