7931bb8c392e7136744002269dd14583419cc663
[netconf.git] / opendaylight / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / TestingTopologyDispatcher.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology;
10
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;
16 import java.util.Map;
17 import java.util.Set;
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;
30
31 public class TestingTopologyDispatcher implements NetconfTopology{
32
33     private static final Logger LOG = LoggerFactory.getLogger(TestingTopologyDispatcher.class);
34
35     private final String topologyId;
36
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<>();
40
41
42     public TestingTopologyDispatcher(final String topologyId) {
43
44         this.topologyId = topologyId;
45     }
46
47     @Override
48     public String getTopologyId() {
49         return topologyId;
50     }
51
52     @Override
53     public DataBroker getDataBroker() {
54         return null;
55     }
56
57     // log the current connection attempt and return a successful future asynchronously
58     @Override
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() {
66             @Override
67             public void run() {
68                 try {
69                     Thread.sleep(4000);
70                     executorService.submit(new Runnable() {
71                         @Override
72                         public void run() {
73                             future.set(new NetconfDeviceCapabilities());
74                         }
75                     });
76                 } catch (InterruptedException e) {
77                     LOG.error("Cannot sleep thread", e);
78                 }
79             }
80         });
81         return future;
82     }
83
84     @Override
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() {
90             @Override
91             public void run() {
92                 try {
93                     Thread.sleep(4000);
94                     executorService.submit(new Runnable() {
95                         @Override
96                         public void run() {
97                             connected.remove(nodeId);
98                             future.set(null);
99                         }
100                     });
101                 } catch (InterruptedException e) {
102                     LOG.error("Cannot sleep thread", e);
103                 }
104
105             }
106         });
107         return future;
108     }
109
110     @Override
111     public void registerMountPoint(NodeId nodeId) {
112         LOG.debug("Registering mount point for node {}", nodeId.getValue());
113
114     }
115
116     @Override
117     public void unregisterMountPoint(NodeId nodeId) {
118         LOG.debug("Unregistering mount point for node {}", nodeId.getValue());
119     }
120
121     @Override
122     public ConnectionStatusListenerRegistration registerConnectionStatusListener(final NodeId node, final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
123         Preconditions.checkState(connected.contains(node), "Node is not connected yet");
124
125         LOG.debug("Registering a connection status listener for node {}", node.getValue());
126         listeners.put(node, listener);
127         executorService.submit(new Runnable() {
128             @Override
129             public void run() {
130                 try {
131                     Thread.sleep(10000);
132
133                     boolean up = false;
134                     for (int i = 0; i < 20; i++) {
135                         if (up) {
136                             LOG.debug("Device has connected {}", node.getValue());
137                             listener.onDeviceConnected(null, null, null);
138                             up = false;
139                         } else {
140                             LOG.debug("Device has diconnected {}", node.getValue());
141                             listener.onDeviceDisconnected();
142                             up = true;
143                         }
144                         try {
145                             Thread.sleep(5000);
146                         } catch (InterruptedException e) {
147                             LOG.error("Cannot sleep thread", e);
148                         }
149                     }
150                 } catch (InterruptedException e) {
151                     e.printStackTrace();
152                 }
153
154             }
155         });
156
157         return null;
158     }
159 }