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 com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.HashMap;
15 import java.util.HashSet;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
22 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
23 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
24 import org.opendaylight.netconf.topology.pipeline.TopologyMountPointFacade.ConnectionStatusListenerRegistration;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class TestingTopologyDispatcher implements NetconfTopology{
33 private static final Logger LOG = LoggerFactory.getLogger(TestingTopologyDispatcher.class);
35 private final String topologyId;
37 private final ExecutorService executorService = Executors.newSingleThreadExecutor();
38 private final Set<NodeId> connected = new HashSet<>();
39 private final Map<NodeId, RemoteDeviceHandler<NetconfSessionPreferences>> listeners = new HashMap<>();
42 public TestingTopologyDispatcher(final String topologyId) {
44 this.topologyId = topologyId;
48 public String getTopologyId() {
53 public DataBroker getDataBroker() {
57 // log the current connection attempt and return a successful future asynchronously
59 public ListenableFuture<NetconfDeviceCapabilities> connectNode(final NodeId nodeId, final Node configNode) {
60 final NetconfNode augmentation = configNode.getAugmentation(NetconfNode.class);
61 LOG.debug("Connecting node {}, with config: {} ", nodeId.getValue(),
62 augmentation.getHost().getIpAddress().toString() + ":" + augmentation.getPort());
63 connected.add(nodeId);
64 final SettableFuture<NetconfDeviceCapabilities> future = SettableFuture.create();
65 executorService.submit(new Runnable() {
70 executorService.submit(new Runnable() {
73 future.set(new NetconfDeviceCapabilities());
76 } catch (InterruptedException e) {
77 LOG.error("Cannot sleep thread", e);
85 public ListenableFuture<Void> disconnectNode(final NodeId nodeId) {
86 Preconditions.checkState(connected.contains(nodeId), "Node is not connected yet");
87 LOG.debug("Disconnecting node {}", nodeId.getValue());
88 final SettableFuture<Void> future = SettableFuture.create();
89 executorService.submit(new Runnable() {
94 executorService.submit(new Runnable() {
97 connected.remove(nodeId);
101 } catch (InterruptedException e) {
102 LOG.error("Cannot sleep thread", e);
111 public void registerMountPoint(NodeId nodeId) {
112 LOG.debug("Registering mount point for node {}", nodeId.getValue());
117 public void unregisterMountPoint(NodeId nodeId) {
118 LOG.debug("Unregistering mount point for node {}", nodeId.getValue());
122 public ConnectionStatusListenerRegistration registerConnectionStatusListener(final NodeId node, final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
123 Preconditions.checkState(connected.contains(node), "Node is not connected yet");
125 LOG.debug("Registering a connection status listener for node {}", node.getValue());
126 listeners.put(node, listener);
127 executorService.submit(new Runnable() {
134 for (int i = 0; i < 20; i++) {
136 LOG.debug("Device has connected {}", node.getValue());
137 listener.onDeviceConnected(null, null, null);
140 LOG.debug("Device has diconnected {}", node.getValue());
141 listener.onDeviceDisconnected();
146 } catch (InterruptedException e) {
147 LOG.error("Cannot sleep thread", e);
150 } catch (InterruptedException e) {