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(tailnc1, headnc1);
77 TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props,
79 topoedgeupdateList.add(teu1);
81 NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector(
82 (short) (i + 1), headnc1.getNode());
83 NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector(
84 (short) (i + 11), tailnc1.getNode());
85 Edge e2 = new Edge(tailnc2, headnc2);
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())
115 (headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10))
116 || (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1))
117 || (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9))
118 || (headNcId.equals(nodeId - 10) && tailNcId.equals(nodeId))
123 Assert.assertTrue(nodeEdgeMap.isEmpty());
127 public void testGetEdges() throws ConstructionException {
128 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
129 setNodeEdges(topoManagerImpl);
131 Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
133 for (Iterator<Map.Entry<Edge, Set<Property>>> i = edgeProperty
134 .entrySet().iterator(); i.hasNext();) {
135 Map.Entry<Edge, Set<Property>> entry = i.next();
136 Edge e = entry.getKey();
137 NodeConnector headnc = e.getHeadNodeConnector();
138 NodeConnector tailnc = e.getTailNodeConnector();
140 Long headNodeId = (Long) headnc.getNode().getID();
142 Long headNcId = ((Short) headnc.getID()).longValue();
143 Long tailNcId = ((Short) tailnc.getID()).longValue();
145 if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
146 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
147 .equals(headNodeId + 10))
148 || (headNcId.equals(headNodeId + 10) && tailNcId
150 || (headNcId.equals(headNodeId + 1) && tailNcId
151 .equals(headNodeId + 11))
152 || (headNcId.equals(headNodeId + 11) && tailNcId
153 .equals(headNodeId + 1)));
154 } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
155 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
156 .equals(headNodeId - 10))
157 || (headNcId.equals(headNodeId) && tailNcId
158 .equals(headNodeId - 10))
159 || (headNcId.equals(headNodeId - 9) && tailNcId
160 .equals(headNodeId + 1))
161 || (headNcId.equals(headNodeId + 1) && tailNcId
162 .equals(headNodeId - 9)));
165 Set<Property> prop = entry.getValue();
166 for (Property p : prop) {
169 if (p instanceof Bandwidth) {
170 Bandwidth b = (Bandwidth) p;
171 pName = Bandwidth.BandwidthPropName;
172 pValue = b.getValue();
173 Assert.assertTrue(pName.equals(p.getName())
174 && pValue == Bandwidth.BW100Gbps);
177 if (p instanceof Latency) {
178 Latency l = (Latency) p;
179 pName = Latency.LatencyPropName;
180 pValue = l.getValue();
181 Assert.assertTrue(pName.equals(p.getName())
182 && pValue == Latency.LATENCY100ns);
185 if (p instanceof State) {
186 State state = (State) p;
187 pName = State.StatePropName;
188 pValue = state.getValue();
189 Assert.assertTrue(pName.equals(p.getName())
190 && pValue == State.EDGE_UP);
196 Assert.assertTrue(edgeProperty.isEmpty());
200 public void testAddDeleteUserLink() {
201 TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1",
202 "OF|1@OF|2", "OF|1@OF|2");
203 TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1",
204 "OF|10@OF|20", "OF|10@OF|20");
205 TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2",
206 "OF|1@OF|2", "OF|1@OF|2");
207 TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20",
208 "OF|10@OF|20", "OF|10@OF|20");
210 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
211 topoManagerImpl.nonClusterObjectCreate();
213 Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess());
214 Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
215 Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
216 Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess());
218 Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
219 Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
221 Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
223 Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
228 public void testGetUserLink() {
229 TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
230 TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
231 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
232 topoManagerImpl.nonClusterObjectCreate();
234 String name = "Test";
235 String srcSwitchId = null;
236 String srcNodeConnectorIDType = null;
237 String srcPort = null;
238 String srcNodeIDType = null;
239 String dstNodeIDType = null;
240 String dstSwitchId = null;
241 String dstNodeConnectorIDType = null;
242 String dstPort = null;
243 String srcNodeConnector = null;
244 String dstNodeConnector = null;
246 /* Creating userlinks and checking for their validity */
247 link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
248 Assert.assertTrue(link[0].isValid() == false);
250 srcNodeConnector = "OF|1@OF|1";
251 link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
252 Assert.assertTrue(link[0].isValid() == false);
254 dstNodeConnector = "OF|1@OF|2";
255 link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
256 Assert.assertTrue(link[0].isValid() == true);
260 for (i = 0; i < 5; i++) {
261 link[i] = new TopologyUserLinkConfig();
263 name = Integer.toString(i + 1);
264 srcSwitchId = Integer.toString(i + 1);
265 srcPort = Integer.toString(i + 1);
266 dstSwitchId = Integer.toString((i + 1) * 10);
267 dstPort = Integer.toString((i + 1) * 10);
269 link[i].setName(name);
270 srcNodeConnectorIDType = dstNodeConnectorIDType = "INCORRECT";
271 srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
272 dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
274 link[i].setSrcNodeConnector(srcNodeConnector);
275 Assert.assertTrue(link[i].isValid() == false);
277 srcNodeConnectorIDType = "OF";
278 srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
279 link[i].setSrcNodeConnector(srcNodeConnector);
280 Assert.assertTrue(link[i].isValid() == false);
282 dstNodeConnectorIDType = "OF";
283 dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
284 link[i].setDstNodeConnector(dstNodeConnector);
285 Assert.assertTrue(link[i].isValid() == true);
287 reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeConnector, srcNodeConnector);
288 topoManagerImpl.addUserLink(link[i]);
291 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
293 TopologyUserLinkConfig resultLink;
295 for (i = 0; i < 5; i++) {
296 resultLink = userLinks.get(((Integer) (i + 1)).toString());
298 Assert.assertTrue(resultLink.getName().equals(
299 reverseLink[i].getName()));
300 Assert.assertTrue(resultLink.getDstNodeConnector().equals(
301 reverseLink[i].getSrcNodeConnector()));
302 Assert.assertTrue(resultLink.getSrcNodeConnector().equals(
303 reverseLink[i].getDstNodeConnector()));
308 public void testHostLinkMethods() throws ConstructionException,
309 UnknownHostException {
310 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
311 topoManagerImpl.nonClusterObjectCreate();
317 Set<Property> props = new HashSet<Property>();
318 state = new State(State.EDGE_UP);
319 bw = new Bandwidth(Bandwidth.BW100Gbps);
320 l = new Latency(Latency.LATENCY100ns);
327 Host[] h = new Host[5];
328 NodeConnector[] nc = new NodeConnector[5];
331 * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
334 for (int i = 0; i < 5; i++) {
335 if (hostCounter < 4) {
336 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
337 (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
338 String stringIP = new StringBuilder().append(i + 1).append(".")
339 .append(i + 10).append(".").append(i + 20).append(".")
340 .append(i + 30).toString();
341 ip = InetAddress.getByName(stringIP);
342 h[hostCounter] = new Host(ea, ip);
344 h[hostCounter] = null;
347 nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
348 NodeCreator.createOFNode((long) (i + 1)));
350 .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
353 for (int i = 0; i < 5; i++) {
354 Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
356 Assert.assertTrue(host == null);
358 Assert.assertTrue(host.equals(h[i]));
361 Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
362 for (int i = 0; i < 5; i++) {
363 Assert.assertTrue(ncSet.remove(nc[i]));
365 Assert.assertTrue(ncSet.isEmpty());
369 public void testGetNodesWithNodeConnectorHost()
370 throws ConstructionException, UnknownHostException {
371 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
372 topoManagerImpl.nonClusterObjectCreate();
378 Set<Property> props = new HashSet<Property>();
379 state = new State(State.EDGE_UP);
380 bw = new Bandwidth(Bandwidth.BW100Gbps);
381 l = new Latency(Latency.LATENCY100ns);
388 Host[] h = new Host[5];
389 NodeConnector[] nc = new NodeConnector[5];
392 * Adding host, nodeconnector, properties of edge to hostsDB for the
393 * first three nodes only
395 for (int i = 1; i < 6; i++) {
397 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
398 (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
399 String stringIP = new StringBuilder().append(i).append(".")
400 .append(i + 10).append(".").append(i + 20).append(".")
401 .append(i + 30).toString();
402 ip = InetAddress.getByName(stringIP);
403 h[hostCounter] = new Host(ea, ip);
405 h[hostCounter] = null;
408 nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i,
409 NodeCreator.createOFNode((long) i));
410 topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1],
411 UpdateType.ADDED, props);
414 /* Get the nodes which have host connected to its nodeConnector */
415 Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl
416 .getNodesWithNodeConnectorHost();
417 for (int i = 1; i < 6; i++) {
418 Node node = nc[i - 1].getNode();
419 Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
421 Assert.assertTrue(ncSet == nodeNCmap.remove(node));
424 Assert.assertTrue(nodeNCmap.isEmpty());