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;
11 import akka.actor.ActorContext;
12 import akka.actor.ActorRef;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.HashMap;
17 import java.util.HashSet;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
24 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
25 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
26 import org.opendaylight.netconf.topology.pipeline.TopologyMountPointFacade.ConnectionStatusListenerRegistration;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class TestingTopologyDispatcher implements NetconfTopology{
35 private static final Logger LOG = LoggerFactory.getLogger(TestingTopologyDispatcher.class);
37 private final String topologyId;
39 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
40 private final Set<NodeId> connected = new HashSet<>();
41 private final Map<NodeId, RemoteDeviceHandler<NetconfSessionPreferences>> listeners = new HashMap<>();
44 public TestingTopologyDispatcher(final String topologyId) {
46 this.topologyId = topologyId;
50 public String getTopologyId() {
55 public DataBroker getDataBroker() {
59 // log the current connection attempt and return a successful future asynchronously
61 public ListenableFuture<NetconfDeviceCapabilities> connectNode(final NodeId nodeId, final Node configNode) {
62 final NetconfNode augmentation = configNode.getAugmentation(NetconfNode.class);
63 LOG.debug("Connecting node {}, with config: {} ", nodeId.getValue(),
64 augmentation.getHost().getIpAddress().toString() + ":" + augmentation.getPort());
65 connected.add(nodeId);
66 final SettableFuture<NetconfDeviceCapabilities> future = SettableFuture.create();
67 executorService.submit(new Runnable() {
72 executorService.submit(new Runnable() {
75 future.set(new NetconfDeviceCapabilities());
78 } catch (InterruptedException e) {
79 LOG.error("Cannot sleep thread", e);
87 public ListenableFuture<Void> disconnectNode(final NodeId nodeId) {
88 Preconditions.checkState(connected.contains(nodeId), "Node is not connected yet");
89 LOG.debug("Disconnecting node {}", nodeId.getValue());
90 final SettableFuture<Void> future = SettableFuture.create();
91 executorService.submit(new Runnable() {
96 executorService.submit(new Runnable() {
99 connected.remove(nodeId);
103 } catch (InterruptedException e) {
104 LOG.error("Cannot sleep thread", e);
113 public void registerMountPoint(ActorContext context, NodeId nodeId) {
114 LOG.debug("Registering mount point for node {}", nodeId.getValue());
118 public void registerMountPoint(ActorContext context, NodeId nodeId, ActorRef masterRef) {
119 LOG.debug("Registering mount point for node {}", nodeId.getValue());
123 public void unregisterMountPoint(NodeId nodeId) {
124 LOG.debug("Unregistering mount point for node {}", nodeId.getValue());
128 public ConnectionStatusListenerRegistration registerConnectionStatusListener(final NodeId node, final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
129 Preconditions.checkState(connected.contains(node), "Node is not connected yet");
131 LOG.debug("Registering a connection status listener for node {}", node.getValue());
132 listeners.put(node, listener);
133 executorService.submit(new Runnable() {
140 for (int i = 0; i < 20; i++) {
142 LOG.debug("Device has connected {}", node.getValue());
143 listener.onDeviceConnected(null, null, null);
146 LOG.debug("Device has diconnected {}", node.getValue());
147 listener.onDeviceDisconnected();
152 } catch (InterruptedException e) {
153 LOG.error("Cannot sleep thread", e);
156 } catch (InterruptedException e) {