RemoteDeviceDataBroker proxy
[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 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;
18 import java.util.Map;
19 import java.util.Set;
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;
32
33 public class TestingTopologyDispatcher implements NetconfTopology{
34
35     private static final Logger LOG = LoggerFactory.getLogger(TestingTopologyDispatcher.class);
36
37     private final String topologyId;
38
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<>();
42
43
44     public TestingTopologyDispatcher(final String topologyId) {
45
46         this.topologyId = topologyId;
47     }
48
49     @Override
50     public String getTopologyId() {
51         return topologyId;
52     }
53
54     @Override
55     public DataBroker getDataBroker() {
56         return null;
57     }
58
59     // log the current connection attempt and return a successful future asynchronously
60     @Override
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() {
68             @Override
69             public void run() {
70                 try {
71                     Thread.sleep(4000);
72                     executorService.submit(new Runnable() {
73                         @Override
74                         public void run() {
75                             future.set(new NetconfDeviceCapabilities());
76                         }
77                     });
78                 } catch (InterruptedException e) {
79                     LOG.error("Cannot sleep thread", e);
80                 }
81             }
82         });
83         return future;
84     }
85
86     @Override
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() {
92             @Override
93             public void run() {
94                 try {
95                     Thread.sleep(4000);
96                     executorService.submit(new Runnable() {
97                         @Override
98                         public void run() {
99                             connected.remove(nodeId);
100                             future.set(null);
101                         }
102                     });
103                 } catch (InterruptedException e) {
104                     LOG.error("Cannot sleep thread", e);
105                 }
106
107             }
108         });
109         return future;
110     }
111
112     @Override
113     public void registerMountPoint(ActorContext context, NodeId nodeId) {
114         LOG.debug("Registering mount point for node {}", nodeId.getValue());
115     }
116
117     @Override
118     public void registerMountPoint(ActorContext context, NodeId nodeId, ActorRef masterRef) {
119         LOG.debug("Registering mount point for node {}", nodeId.getValue());
120     }
121
122     @Override
123     public void unregisterMountPoint(NodeId nodeId) {
124         LOG.debug("Unregistering mount point for node {}", nodeId.getValue());
125     }
126
127     @Override
128     public ConnectionStatusListenerRegistration registerConnectionStatusListener(final NodeId node, final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
129         Preconditions.checkState(connected.contains(node), "Node is not connected yet");
130
131         LOG.debug("Registering a connection status listener for node {}", node.getValue());
132         listeners.put(node, listener);
133         executorService.submit(new Runnable() {
134             @Override
135             public void run() {
136                 try {
137                     Thread.sleep(10000);
138
139                     boolean up = false;
140                     for (int i = 0; i < 20; i++) {
141                         if (up) {
142                             LOG.debug("Device has connected {}", node.getValue());
143                             listener.onDeviceConnected(null, null, null);
144                             up = false;
145                         } else {
146                             LOG.debug("Device has diconnected {}", node.getValue());
147                             listener.onDeviceDisconnected();
148                             up = true;
149                         }
150                         try {
151                             Thread.sleep(5000);
152                         } catch (InterruptedException e) {
153                             LOG.error("Cannot sleep thread", e);
154                         }
155                     }
156                 } catch (InterruptedException e) {
157                     e.printStackTrace();
158                 }
159
160             }
161         });
162
163         return null;
164     }
165 }