3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.topologymanager.internal;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.HashSet;
15 import java.util.Iterator;
18 import java.util.concurrent.ConcurrentMap;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.core.Bandwidth;
23 import org.opendaylight.controller.sal.core.ConstructionException;
24 import org.opendaylight.controller.sal.core.Edge;
25 import org.opendaylight.controller.sal.core.Host;
26 import org.opendaylight.controller.sal.core.Latency;
27 import org.opendaylight.controller.sal.core.Node;
28 import org.opendaylight.controller.sal.core.Node.NodeIDType;
29 import org.opendaylight.controller.sal.core.NodeConnector;
30 import org.opendaylight.controller.sal.core.Property;
31 import org.opendaylight.controller.sal.core.State;
32 import org.opendaylight.controller.sal.core.UpdateType;
33 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
34 import org.opendaylight.controller.sal.utils.StatusCode;
35 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
36 import org.opendaylight.controller.sal.utils.NodeCreator;
37 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
39 public class TopologyManagerImplTest {
42 * Sets the node, edges and properties for edges here:
43 * Edge <SwitchId : NodeConnectorId> :
44 * <1:1>--><11:11>; <1:2>--><11:12>;
45 * <3:3>--><13:13>; <3:4>--><13:14>;
46 * <5:5>--><15:15>; <5:6>--><15:16>;
47 * Method used by two tests: testGetNodeEdges and testGetEdges
48 * @param topoManagerImpl
49 * @throws ConstructionException
51 public void setNodeEdges(TopologyManagerImpl topoManagerImpl) throws ConstructionException {
52 topoManagerImpl.nonClusterObjectCreate();
58 Set<Property> props = new HashSet<Property>();
59 state = new State(State.EDGE_UP);
60 bw = new Bandwidth(Bandwidth.BW100Gbps);
61 l = new Latency(Latency.LATENCY100ns);
66 for (short i = 1; i < 6; i=(short) (i+2)) {
67 NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(i, NodeCreator.createOFNode((long)i));
68 NodeConnector tailnc1 = NodeConnectorCreator.createOFNodeConnector((short)(i+10), NodeCreator.createOFNode((long)(i+10)));
69 Edge e1 = new Edge(headnc1, tailnc1);
70 topoManagerImpl.edgeUpdate(e1, UpdateType.ADDED, props);
72 NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector((short) (i+1), headnc1.getNode());
73 NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector((short)(i+11), tailnc1.getNode());
74 Edge e2 = new Edge(headnc2, tailnc2);
75 topoManagerImpl.edgeUpdate(e2, UpdateType.ADDED, props);
80 public void testGetNodeEdges() throws ConstructionException {
81 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
82 setNodeEdges(topoManagerImpl);
84 Map<Node, Set<Edge>> nodeEdgeMap = topoManagerImpl.getNodeEdges();
85 for (Iterator<Map.Entry<Node,Set<Edge>>> i = nodeEdgeMap.entrySet().iterator(); i.hasNext();) {
86 Map.Entry<Node, Set<Edge>> entry = i.next();
87 Node node = entry.getKey();
88 Long nodeId = ((Long) node.getID()).longValue();
89 Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW)));
91 Set<Edge> edges = entry.getValue();
92 for (Edge edge : edges) {
93 Long headNcId = ((Short)edge.getHeadNodeConnector().getID()).longValue();
94 Long tailNcId = ((Short) edge.getTailNodeConnector().getID()).longValue();
95 if (nodeId == 1 || nodeId == 3 || nodeId == 5) {
96 Assert.assertTrue((headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10)) ||
97 (headNcId.equals(nodeId + 10) && tailNcId.equals(nodeId)) ||
98 (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId + 11)) ||
99 (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1)));
100 } else if (nodeId == 11 || nodeId == 13 || nodeId == 15) {
101 Assert.assertTrue((headNcId.equals(nodeId) && tailNcId.equals(nodeId - 10)) ||
102 (headNcId.equals(nodeId) && tailNcId.equals(nodeId - 10)) ||
103 (headNcId.equals(nodeId - 9) && tailNcId.equals(nodeId + 1)) ||
104 (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9)));
109 Assert.assertTrue(nodeEdgeMap.isEmpty());
113 public void testGetEdges() throws ConstructionException {
114 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
115 setNodeEdges(topoManagerImpl);
117 Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
119 for (Iterator <Map.Entry<Edge, Set<Property>>> i = edgeProperty.entrySet().iterator() ; i.hasNext();) {
120 Map.Entry<Edge, Set<Property>> entry = i.next();
121 Edge e = entry.getKey();
122 NodeConnector headnc = e.getHeadNodeConnector();
123 NodeConnector tailnc = e.getTailNodeConnector();
125 Long headNodeId = (Long) headnc.getNode().getID();
127 Long headNcId = ((Short)headnc.getID()).longValue();
128 Long tailNcId = ((Short)tailnc.getID()).longValue();
130 if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
131 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId.equals(headNodeId + 10)) ||
132 (headNcId.equals(headNodeId + 10) && tailNcId.equals(headNodeId)) ||
133 (headNcId.equals(headNodeId + 1) && tailNcId.equals(headNodeId + 11)) ||
134 (headNcId.equals(headNodeId + 11) && tailNcId.equals(headNodeId + 1)));
135 } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
136 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId.equals(headNodeId - 10)) ||
137 (headNcId.equals(headNodeId) && tailNcId.equals(headNodeId - 10)) ||
138 (headNcId.equals(headNodeId - 9) && tailNcId.equals(headNodeId + 1)) ||
139 (headNcId.equals(headNodeId + 1) && tailNcId.equals(headNodeId - 9)));
142 Set<Property> prop = entry.getValue();
143 for (Property p : prop) {
146 if (p instanceof Bandwidth) {
147 Bandwidth b = (Bandwidth)p;
148 pName = Bandwidth.BandwidthPropName;
149 pValue = b.getValue();
150 Assert.assertTrue(pName.equals(p.getName()) && pValue == Bandwidth.BW100Gbps );
153 if (p instanceof Latency) {
154 Latency l = (Latency)p;
155 pName = Latency.LatencyPropName;
156 pValue = l.getValue();
157 Assert.assertTrue(pName.equals(p.getName()) && pValue == Latency.LATENCY100ns);
160 if (p instanceof State) {
161 State state = (State)p;
162 pName = State.StatePropName;
163 pValue = state.getValue();
164 Assert.assertTrue(pName.equals(p.getName()) && pValue == State.EDGE_UP);
170 Assert.assertTrue(edgeProperty.isEmpty());
175 public void testAddDeleteUserLink () {
176 TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1", "OF", "1", "OF", "2", "OF", "1", "OF", "2");
177 TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1", "OF", "10", "OF", "20", "OF", "10", "OF", "20");
178 TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2", "OF", "1", "OF", "2", "OF", "1", "OF", "2");
179 TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20", "OF", "10", "OF", "20", "OF", "10", "OF", "20");
181 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
182 topoManagerImpl.nonClusterObjectCreate();
184 Assert.assertTrue (topoManagerImpl.addUserLink(link1).isSuccess());
185 Assert.assertTrue (topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
186 Assert.assertTrue (topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
187 Assert.assertTrue (topoManagerImpl.addUserLink(link4).isSuccess());
189 Assert.assertTrue (topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
190 Assert.assertTrue (topoManagerImpl.deleteUserLink(link1.getName()).isSuccess());
191 Assert.assertTrue (topoManagerImpl.deleteUserLink(link4.getName()).isSuccess());
192 Assert.assertTrue (topoManagerImpl.getUserLinks().isEmpty());
197 public void testGetUserLink () {
198 TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
199 TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
200 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
201 topoManagerImpl.nonClusterObjectCreate();
204 String srcNodeIDType = null;
205 String srcSwitchId = null;
206 String srcNodeConnectorIDType = null;
207 String srcPort = null;
208 String dstNodeIDType = null;
209 String dstSwitchId = null;
210 String dstNodeConnectorIDType = null;
211 String dstPort = null;
213 /*Creating userlinks and checking for their validity*/
214 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
215 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
216 dstNodeConnectorIDType, dstPort);
217 Assert.assertTrue(link[0].isValid() == false);
220 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
221 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
222 dstNodeConnectorIDType, dstPort);
223 Assert.assertTrue(link[0].isValid() == false);
226 link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
227 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
228 dstNodeConnectorIDType, dstPort);
229 Assert.assertTrue(link[0].isValid() == false);
234 for (i = 0; i < 5; i++) {
235 link[i] = new TopologyUserLinkConfig(name, srcNodeIDType,
236 srcSwitchId, srcNodeConnectorIDType, srcPort,
237 dstNodeIDType, dstSwitchId, dstNodeConnectorIDType, dstPort);
239 name = Integer.toString(i + 1);
240 srcSwitchId = Integer.toString(i + 1);
241 srcPort = Integer.toString(i + 1);
242 dstSwitchId = Integer.toString((i + 1)*10);
243 dstPort = Integer.toString((i + 1)*10);
245 link[i].setName(name);
246 link[i].setSrcSwitchId(srcSwitchId);
247 link[i].setSrcPort(srcPort);
248 link[i].setDstSwitchId(dstSwitchId);
249 link[i].setDstPort(dstPort);
251 Assert.assertTrue(link[i].isValid() == false);
253 link[i].setSrcNodeIDType("OF");
254 link[i].setSrcNodeConnectorIDType("OF");
256 Assert.assertTrue(link[i].isValid() == false);
258 link[i].setDstNodeIDType("OF");
259 link[i].setDstNodeConnectorIDType("OF");
261 Assert.assertTrue(link[i].isValid() == true);
263 reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeIDType,
264 dstSwitchId, dstNodeConnectorIDType, dstPort,
265 srcNodeIDType, srcSwitchId, srcNodeConnectorIDType, srcPort);
267 topoManagerImpl.addUserLink(link[i]);
269 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl.getUserLinks();
270 TopologyUserLinkConfig resultLink;
272 for (i = 0; i < 5; i++) {
273 resultLink = userLinks.get(((Integer)(i + 1)).toString());
275 Assert.assertTrue(resultLink.getName().equals(reverseLink[i].getName()));
276 Assert.assertTrue(resultLink.getDstSwitchId().equals(reverseLink[i].getSrcSwitchId()));
277 Assert.assertTrue(resultLink.getDstPort().equals(reverseLink[i].getSrcPort()));
278 Assert.assertTrue(resultLink.getSrcSwitchId().equals(reverseLink[i].getDstSwitchId()));
279 Assert.assertTrue(resultLink.getSrcPort().equals(reverseLink[i].getDstPort()));
284 public void testHostLinkMethods() throws ConstructionException, UnknownHostException {
285 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
286 topoManagerImpl.nonClusterObjectCreate();
292 Set<Property> props = new HashSet<Property>();
293 state = new State(State.EDGE_UP);
294 bw = new Bandwidth(Bandwidth.BW100Gbps);
295 l = new Latency(Latency.LATENCY100ns);
302 Host[] h = new Host[5];
303 NodeConnector[] nc = new NodeConnector[5];
305 /* Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
308 for (int i = 0; i < 5; i++) {
309 if (hostCounter < 4) {
310 ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0,
311 (byte)0x0, (byte)0x0,
312 (byte)0x0, (byte)i});
313 String stringIP = new StringBuilder().append(i + 1).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString();
314 ip = InetAddress.getByName(stringIP);
315 h[hostCounter] = new Host(ea, ip);
317 h[hostCounter] = null;
320 nc[i] = NodeConnectorCreator.createOFNodeConnector((short)(i + 1), NodeCreator.createOFNode((long)(i + 1)));
321 topoManagerImpl.updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
324 for (int i = 0; i < 5; i++) {
325 Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
327 Assert.assertTrue(host == null);
329 Assert.assertTrue(host.equals(h[i]));
332 Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
333 for (int i = 0; i < 5; i++) {
334 Assert.assertTrue(ncSet.remove(nc[i]));
336 Assert.assertTrue(ncSet.isEmpty());
340 public void testGetNodesWithNodeConnectorHost() throws ConstructionException, UnknownHostException {
341 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
342 topoManagerImpl.nonClusterObjectCreate();
348 Set<Property> props = new HashSet<Property>();
349 state = new State(State.EDGE_UP);
350 bw = new Bandwidth(Bandwidth.BW100Gbps);
351 l = new Latency(Latency.LATENCY100ns);
358 Host[] h = new Host[5];
359 NodeConnector[] nc = new NodeConnector[5];
361 /*Adding host, nodeconnector, properties of edge to hostsDB for the first three nodes only*/
362 for (int i = 1; i < 6; i++) {
364 ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0,
365 (byte)0x0, (byte)0x0,
366 (byte)0x0, (byte)i});
367 String stringIP = new StringBuilder().append(i).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString();
368 ip = InetAddress.getByName(stringIP);
369 h[hostCounter] = new Host(ea, ip);
372 h[hostCounter] = null;
375 nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short)i, NodeCreator.createOFNode((long)i));
376 topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1], UpdateType.ADDED, props);
379 /*Get the nodes which have host connected to its nodeConnector*/
380 Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl.getNodesWithNodeConnectorHost();
381 for (int i = 1; i < 6; i++) {
382 Node node = nc[i - 1].getNode();
383 Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
385 Assert.assertTrue(ncSet == nodeNCmap.remove(node));
388 Assert.assertTrue(nodeNCmap.isEmpty());