2 * Copyright (c) 2013 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.controller.topologymanager.internal;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
19 import java.util.concurrent.ConcurrentMap;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.controller.sal.core.Bandwidth;
24 import org.opendaylight.controller.sal.core.ConstructionException;
25 import org.opendaylight.controller.sal.core.Edge;
26 import org.opendaylight.controller.sal.core.Host;
27 import org.opendaylight.controller.sal.core.Latency;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.core.Node.NodeIDType;
30 import org.opendaylight.controller.sal.core.NodeConnector;
31 import org.opendaylight.controller.sal.core.Property;
32 import org.opendaylight.controller.sal.core.State;
33 import org.opendaylight.controller.sal.core.UpdateType;
34 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
35 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
36 import org.opendaylight.controller.sal.utils.StatusCode;
37 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
38 import org.opendaylight.controller.sal.utils.NodeCreator;
39 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
41 public class TopologyManagerImplTest {
44 * Sets the node, edges and properties for edges here: Edge <SwitchId :
45 * NodeConnectorId> : <1:1>--><11:11>; <1:2>--><11:12>; <3:3>--><13:13>;
46 * <3:4>--><13:14>; <5:5>--><15:15>; <5:6>--><15:16>; Method used by two
47 * tests: testGetNodeEdges and testGetEdges
49 * @param topoManagerImpl
51 * @throws ConstructionException
53 public void setNodeEdges(TopologyManagerImpl topoManagerImpl)
54 throws ConstructionException {
55 topoManagerImpl.nonClusterObjectCreate();
61 Set<Property> props = new HashSet<Property>();
62 state = new State(State.EDGE_UP);
63 bw = new Bandwidth(Bandwidth.BW100Gbps);
64 l = new Latency(Latency.LATENCY100ns);
69 for (short i = 1; i < 6; i = (short) (i + 2)) {
70 List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
71 NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(
72 i, NodeCreator.createOFNode((long) i));
73 NodeConnector tailnc1 = NodeConnectorCreator
74 .createOFNodeConnector((short) (i + 10),
75 NodeCreator.createOFNode((long) (i + 10)));
76 Edge e1 = new Edge(headnc1, tailnc1);
77 TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props,
79 topoedgeupdateList.add(teu1);
81 NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector(
82 (short) (i + 1), headnc1.getNode());
83 NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector(
84 (short) (i + 11), tailnc1.getNode());
85 Edge e2 = new Edge(headnc2, tailnc2);
86 TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props,
88 topoedgeupdateList.add(teu2);
89 topoManagerImpl.edgeUpdate(topoedgeupdateList);
96 public void testGetNodeEdges() throws ConstructionException {
97 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
98 setNodeEdges(topoManagerImpl);
100 Map<Node, Set<Edge>> nodeEdgeMap = topoManagerImpl.getNodeEdges();
101 for (Iterator<Map.Entry<Node, Set<Edge>>> i = nodeEdgeMap.entrySet()
102 .iterator(); i.hasNext();) {
103 Map.Entry<Node, Set<Edge>> entry = i.next();
104 Node node = entry.getKey();
105 Long nodeId = ((Long) node.getID()).longValue();
106 Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW)));
108 Set<Edge> edges = entry.getValue();
109 for (Edge edge : edges) {
110 Long headNcId = ((Short) edge.getHeadNodeConnector().getID())
112 Long tailNcId = ((Short) edge.getTailNodeConnector().getID())
114 if (nodeId == 1 || nodeId == 3 || nodeId == 5) {
115 Assert.assertTrue((headNcId.equals(nodeId) && tailNcId
116 .equals(nodeId + 10))
117 || (headNcId.equals(nodeId + 10) && tailNcId
119 || (headNcId.equals(nodeId + 1) && tailNcId
120 .equals(nodeId + 11))
121 || (headNcId.equals(nodeId + 11) && tailNcId
122 .equals(nodeId + 1)));
123 } else if (nodeId == 11 || nodeId == 13 || nodeId == 15) {
124 Assert.assertTrue((headNcId.equals(nodeId) && tailNcId
125 .equals(nodeId - 10))
126 || (headNcId.equals(nodeId) && tailNcId
127 .equals(nodeId - 10))
128 || (headNcId.equals(nodeId - 9) && tailNcId
130 || (headNcId.equals(nodeId + 1) && tailNcId
131 .equals(nodeId - 9)));
136 Assert.assertTrue(nodeEdgeMap.isEmpty());
140 public void testGetEdges() throws ConstructionException {
141 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
142 setNodeEdges(topoManagerImpl);
144 Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
146 for (Iterator<Map.Entry<Edge, Set<Property>>> i = edgeProperty
147 .entrySet().iterator(); i.hasNext();) {
148 Map.Entry<Edge, Set<Property>> entry = i.next();
149 Edge e = entry.getKey();
150 NodeConnector headnc = e.getHeadNodeConnector();
151 NodeConnector tailnc = e.getTailNodeConnector();
153 Long headNodeId = (Long) headnc.getNode().getID();
155 Long headNcId = ((Short) headnc.getID()).longValue();
156 Long tailNcId = ((Short) tailnc.getID()).longValue();
158 if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
159 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
160 .equals(headNodeId + 10))
161 || (headNcId.equals(headNodeId + 10) && tailNcId
163 || (headNcId.equals(headNodeId + 1) && tailNcId
164 .equals(headNodeId + 11))
165 || (headNcId.equals(headNodeId + 11) && tailNcId
166 .equals(headNodeId + 1)));
167 } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
168 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
169 .equals(headNodeId - 10))
170 || (headNcId.equals(headNodeId) && tailNcId
171 .equals(headNodeId - 10))
172 || (headNcId.equals(headNodeId - 9) && tailNcId
173 .equals(headNodeId + 1))
174 || (headNcId.equals(headNodeId + 1) && tailNcId
175 .equals(headNodeId - 9)));
178 Set<Property> prop = entry.getValue();
179 for (Property p : prop) {
182 if (p instanceof Bandwidth) {
183 Bandwidth b = (Bandwidth) p;
184 pName = Bandwidth.BandwidthPropName;
185 pValue = b.getValue();
186 Assert.assertTrue(pName.equals(p.getName())
187 && pValue == Bandwidth.BW100Gbps);
190 if (p instanceof Latency) {
191 Latency l = (Latency) p;
192 pName = Latency.LatencyPropName;
193 pValue = l.getValue();
194 Assert.assertTrue(pName.equals(p.getName())
195 && pValue == Latency.LATENCY100ns);
198 if (p instanceof State) {
199 State state = (State) p;
200 pName = State.StatePropName;
201 pValue = state.getValue();
202 Assert.assertTrue(pName.equals(p.getName())
203 && pValue == State.EDGE_UP);
209 Assert.assertTrue(edgeProperty.isEmpty());
213 public void testAddDeleteUserLink() {
214 TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1",
215 "OF", "1", "OF", "2", "OF", "1", "OF", "2");
216 TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1",
217 "OF", "10", "OF", "20", "OF", "10", "OF", "20");
218 TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2",
219 "OF", "1", "OF", "2", "OF", "1", "OF", "2");
220 TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20",
221 "OF", "10", "OF", "20", "OF", "10", "OF", "20");
223 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
224 topoManagerImpl.nonClusterObjectCreate();
226 Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess());
227 Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
228 Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
229 Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess());
231 Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
232 Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
234 Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
236 Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
241 public void testGetUserLink() {
242 TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
243 TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
244 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
245 topoManagerImpl.nonClusterObjectCreate();
248 String srcNodeIDType = null;
249 String srcSwitchId = null;
250 String srcNodeConnectorIDType = null;
251 String srcPort = null;
252 String dstNodeIDType = null;
253 String dstSwitchId = null;
254 String dstNodeConnectorIDType = null;
255 String dstPort = null;
257 /* Creating userlinks and checking for their validity */
258 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
259 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
260 dstNodeConnectorIDType, dstPort);
261 Assert.assertTrue(link[0].isValid() == false);
264 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
265 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
266 dstNodeConnectorIDType, dstPort);
267 Assert.assertTrue(link[0].isValid() == false);
270 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
271 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
272 dstNodeConnectorIDType, dstPort);
273 Assert.assertTrue(link[0].isValid() == false);
277 for (i = 0; i < 5; i++) {
278 link[i] = new TopologyUserLinkConfig(name, srcNodeIDType,
279 srcSwitchId, srcNodeConnectorIDType, srcPort,
280 dstNodeIDType, dstSwitchId, dstNodeConnectorIDType, dstPort);
282 name = Integer.toString(i + 1);
283 srcSwitchId = Integer.toString(i + 1);
284 srcPort = Integer.toString(i + 1);
285 dstSwitchId = Integer.toString((i + 1) * 10);
286 dstPort = Integer.toString((i + 1) * 10);
288 link[i].setName(name);
289 link[i].setSrcSwitchId(srcSwitchId);
290 link[i].setSrcPort(srcPort);
291 link[i].setDstSwitchId(dstSwitchId);
292 link[i].setDstPort(dstPort);
294 Assert.assertTrue(link[i].isValid() == false);
296 link[i].setSrcNodeIDType("OF");
297 link[i].setSrcNodeConnectorIDType("OF");
299 Assert.assertTrue(link[i].isValid() == false);
301 link[i].setDstNodeIDType("OF");
302 link[i].setDstNodeConnectorIDType("OF");
304 Assert.assertTrue(link[i].isValid() == true);
306 reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeIDType,
307 dstSwitchId, dstNodeConnectorIDType, dstPort,
308 srcNodeIDType, srcSwitchId, srcNodeConnectorIDType, srcPort);
310 topoManagerImpl.addUserLink(link[i]);
312 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
314 TopologyUserLinkConfig resultLink;
316 for (i = 0; i < 5; i++) {
317 resultLink = userLinks.get(((Integer) (i + 1)).toString());
319 Assert.assertTrue(resultLink.getName().equals(
320 reverseLink[i].getName()));
321 Assert.assertTrue(resultLink.getDstSwitchId().equals(
322 reverseLink[i].getSrcSwitchId()));
323 Assert.assertTrue(resultLink.getDstPort().equals(
324 reverseLink[i].getSrcPort()));
325 Assert.assertTrue(resultLink.getSrcSwitchId().equals(
326 reverseLink[i].getDstSwitchId()));
327 Assert.assertTrue(resultLink.getSrcPort().equals(
328 reverseLink[i].getDstPort()));
333 public void testHostLinkMethods() throws ConstructionException,
334 UnknownHostException {
335 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
336 topoManagerImpl.nonClusterObjectCreate();
342 Set<Property> props = new HashSet<Property>();
343 state = new State(State.EDGE_UP);
344 bw = new Bandwidth(Bandwidth.BW100Gbps);
345 l = new Latency(Latency.LATENCY100ns);
352 Host[] h = new Host[5];
353 NodeConnector[] nc = new NodeConnector[5];
356 * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
359 for (int i = 0; i < 5; i++) {
360 if (hostCounter < 4) {
361 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
362 (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
363 String stringIP = new StringBuilder().append(i + 1).append(".")
364 .append(i + 10).append(".").append(i + 20).append(".")
365 .append(i + 30).toString();
366 ip = InetAddress.getByName(stringIP);
367 h[hostCounter] = new Host(ea, ip);
369 h[hostCounter] = null;
372 nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
373 NodeCreator.createOFNode((long) (i + 1)));
375 .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
378 for (int i = 0; i < 5; i++) {
379 Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
381 Assert.assertTrue(host == null);
383 Assert.assertTrue(host.equals(h[i]));
386 Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
387 for (int i = 0; i < 5; i++) {
388 Assert.assertTrue(ncSet.remove(nc[i]));
390 Assert.assertTrue(ncSet.isEmpty());
394 public void testGetNodesWithNodeConnectorHost()
395 throws ConstructionException, UnknownHostException {
396 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
397 topoManagerImpl.nonClusterObjectCreate();
403 Set<Property> props = new HashSet<Property>();
404 state = new State(State.EDGE_UP);
405 bw = new Bandwidth(Bandwidth.BW100Gbps);
406 l = new Latency(Latency.LATENCY100ns);
413 Host[] h = new Host[5];
414 NodeConnector[] nc = new NodeConnector[5];
417 * Adding host, nodeconnector, properties of edge to hostsDB for the
418 * first three nodes only
420 for (int i = 1; i < 6; i++) {
422 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
423 (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
424 String stringIP = new StringBuilder().append(i).append(".")
425 .append(i + 10).append(".").append(i + 20).append(".")
426 .append(i + 30).toString();
427 ip = InetAddress.getByName(stringIP);
428 h[hostCounter] = new Host(ea, ip);
430 h[hostCounter] = null;
433 nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i,
434 NodeCreator.createOFNode((long) i));
435 topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1],
436 UpdateType.ADDED, props);
439 /* Get the nodes which have host connected to its nodeConnector */
440 Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl
441 .getNodesWithNodeConnectorHost();
442 for (int i = 1; i < 6; i++) {
443 Node node = nc[i - 1].getNode();
444 Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
446 Assert.assertTrue(ncSet == nodeNCmap.remove(node));
449 Assert.assertTrue(nodeNCmap.isEmpty());