2 * Copyright (c) 2016 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.singleton.impl;
11 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
13 import akka.actor.ActorRef;
14 import akka.cluster.Cluster;
15 import akka.dispatch.OnComplete;
16 import akka.pattern.Patterns;
17 import com.google.common.base.Preconditions;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
25 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
26 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
27 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
28 import org.opendaylight.netconf.topology.singleton.messages.RefreshSetupMasterActorData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
34 class NetconfTopologyContext implements ClusterSingletonService {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyContext.class);
38 private final ServiceGroupIdentifier serviceGroupIdent;
39 private NetconfTopologySetup netconfTopologyDeviceSetup;
40 private RemoteDeviceId remoteDeviceId;
41 private RemoteDeviceConnector remoteDeviceConnector;
42 private NetconfNodeManager netconfNodeManager;
43 private boolean finalClose = false;
44 private boolean isMaster;
46 private ActorRef masterActorRef;
48 NetconfTopologyContext(final NetconfTopologySetup netconfTopologyDeviceSetup,
49 final ServiceGroupIdentifier serviceGroupIdent) {
50 this.netconfTopologyDeviceSetup = Preconditions.checkNotNull(netconfTopologyDeviceSetup);
51 this.serviceGroupIdent = serviceGroupIdent;
53 remoteDeviceId = NetconfTopologyUtils.createRemoteDeviceId(netconfTopologyDeviceSetup.getNode().getNodeId(),
54 netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class));
56 remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId);
58 netconfNodeManager = createNodeDeviceManager();
63 public void instantiateServiceInstance() {
64 LOG.info("Master was selected: {}", remoteDeviceId.getHost().getIpAddress());
68 // master should not listen on netconf-node operational datastore
69 if (netconfNodeManager != null) {
70 netconfNodeManager.close();
71 netconfNodeManager = null;
75 final String masterAddress = Cluster.get(netconfTopologyDeviceSetup.getActorSystem()).selfAddress().toString();
76 masterActorRef = netconfTopologyDeviceSetup.getActorSystem().actorOf(NetconfNodeActor.props(
77 netconfTopologyDeviceSetup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY, DEFAULT_SCHEMA_REPOSITORY),
78 NetconfTopologyUtils.createMasterActorName(remoteDeviceId.getName(), masterAddress));
80 remoteDeviceConnector.startRemoteDeviceConnection(masterActorRef);
85 // called when master is down/changed to slave
87 public ListenableFuture<Void> closeServiceInstance() {
90 // in case that master changes role to slave, new NodeDeviceManager must be created and listener registered
91 netconfNodeManager = createNodeDeviceManager();
93 if (masterActorRef != null) {
94 netconfTopologyDeviceSetup.getActorSystem().stop(masterActorRef);
95 masterActorRef = null;
97 if (remoteDeviceConnector != null) {
98 remoteDeviceConnector.stopRemoteDeviceConnection();
101 return Futures.immediateCheckedFuture(null);
105 public ServiceGroupIdentifier getIdentifier() {
106 return serviceGroupIdent;
109 private NetconfNodeManager createNodeDeviceManager() {
110 final NetconfNodeManager ndm =
111 new NetconfNodeManager(netconfTopologyDeviceSetup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
112 DEFAULT_SCHEMA_REPOSITORY);
113 ndm.registerDataTreeChangeListener(netconfTopologyDeviceSetup.getTopologyId(),
114 netconfTopologyDeviceSetup.getNode().getKey());
119 void closeFinal() throws Exception {
122 if (netconfNodeManager != null) {
123 netconfNodeManager.close();
126 if (remoteDeviceConnector != null) {
127 remoteDeviceConnector.stopRemoteDeviceConnection();
130 if (masterActorRef != null) {
131 netconfTopologyDeviceSetup.getActorSystem().stop(masterActorRef);
132 masterActorRef = null;
137 * If configuration data was changed
138 * @param setup new setup
140 void refresh(@Nonnull final NetconfTopologySetup setup) {
141 netconfTopologyDeviceSetup = Preconditions.checkNotNull(setup);
142 remoteDeviceId = NetconfTopologyUtils.createRemoteDeviceId(netconfTopologyDeviceSetup.getNode().getNodeId(),
143 netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class));
146 remoteDeviceConnector.stopRemoteDeviceConnection();
149 netconfNodeManager.refreshDevice(netconfTopologyDeviceSetup, remoteDeviceId);
151 remoteDeviceConnector = new RemoteDeviceConnectorImpl(netconfTopologyDeviceSetup, remoteDeviceId);
154 final Future<Object> future = Patterns.ask(masterActorRef, new RefreshSetupMasterActorData(
155 netconfTopologyDeviceSetup, remoteDeviceId), NetconfTopologyUtils.TIMEOUT);
157 future.onComplete(new OnComplete<Object>() {
159 public void onComplete(final Throwable failure, final Object success) throws Throwable {
160 if (failure != null) {
161 LOG.error("Failed to refresh master actor data: {}", failure);
164 remoteDeviceConnector.startRemoteDeviceConnection(masterActorRef);
166 }, netconfTopologyDeviceSetup.getActorSystem().dispatcher());