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", "1", "2", "1", "2");
177 TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1", "10", "20", "10", "20");
178 TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2", "1", "2", "1", "2");
179 TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20", "10", "20", "10", "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 srcSwitchId = null;
205 String srcPort = null;
206 String dstSwitchId = null;
207 String dstPort = null;
209 /*Creating userlinks and checking for their validity*/
210 link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort);
211 Assert.assertTrue(link[0].isValid() == false);
214 link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort);
215 Assert.assertTrue(link[0].isValid() == false);
218 link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort);
219 Assert.assertTrue(link[0].isValid() == false);
224 for (i = 0; i < 5; i++) {
225 link[i] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort);
227 name = Integer.toString(i + 1);
228 srcSwitchId = Integer.toString(i + 1);
229 srcPort = Integer.toString(i + 1);
230 dstSwitchId = Integer.toString((i + 1)*10);
231 dstPort = Integer.toString((i + 1)*10);
233 link[i].setName(name);
234 link[i].setSrcSwitchId(srcSwitchId);
235 link[i].setSrcPort(srcPort);
236 link[i].setDstSwitchId(dstSwitchId);
237 link[i].setDstPort(dstPort);
239 Assert.assertTrue(link[i].isValid() == true);
241 reverseLink[i] = new TopologyUserLinkConfig(name, dstSwitchId, dstPort, srcSwitchId, srcPort);
243 topoManagerImpl.addUserLink(link[i]);
245 ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl.getUserLinks();
246 TopologyUserLinkConfig resultLink;
248 for (i = 0; i < 5; i++) {
249 resultLink = userLinks.get(((Integer)(i + 1)).toString());
251 Assert.assertTrue(resultLink.getName().equals(reverseLink[i].getName()));
252 Assert.assertTrue(resultLink.getDstSwitchId().equals(reverseLink[i].getSrcSwitchId()));
253 Assert.assertTrue(resultLink.getDstPort().equals(reverseLink[i].getSrcPort()));
254 Assert.assertTrue(resultLink.getSrcSwitchId().equals(reverseLink[i].getDstSwitchId()));
255 Assert.assertTrue(resultLink.getSrcPort().equals(reverseLink[i].getDstPort()));
260 public void testHostLinkMethods() throws ConstructionException, UnknownHostException {
261 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
262 topoManagerImpl.nonClusterObjectCreate();
268 Set<Property> props = new HashSet<Property>();
269 state = new State(State.EDGE_UP);
270 bw = new Bandwidth(Bandwidth.BW100Gbps);
271 l = new Latency(Latency.LATENCY100ns);
278 Host[] h = new Host[5];
279 NodeConnector[] nc = new NodeConnector[5];
281 /* Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
284 for (int i = 0; i < 5; i++) {
285 if (hostCounter < 4) {
286 ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0,
287 (byte)0x0, (byte)0x0,
288 (byte)0x0, (byte)i});
289 String stringIP = new StringBuilder().append(i + 1).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString();
290 ip = InetAddress.getByName(stringIP);
291 h[hostCounter] = new Host(ea, ip);
293 h[hostCounter] = null;
296 nc[i] = NodeConnectorCreator.createOFNodeConnector((short)(i + 1), NodeCreator.createOFNode((long)(i + 1)));
297 topoManagerImpl.updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
300 for (int i = 0; i < 5; i++) {
301 Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
303 Assert.assertTrue(host == null);
305 Assert.assertTrue(host.equals(h[i]));
308 Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
309 for (int i = 0; i < 5; i++) {
310 Assert.assertTrue(ncSet.remove(nc[i]));
312 Assert.assertTrue(ncSet.isEmpty());
316 public void testGetNodesWithNodeConnectorHost() throws ConstructionException, UnknownHostException {
317 TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
318 topoManagerImpl.nonClusterObjectCreate();
324 Set<Property> props = new HashSet<Property>();
325 state = new State(State.EDGE_UP);
326 bw = new Bandwidth(Bandwidth.BW100Gbps);
327 l = new Latency(Latency.LATENCY100ns);
334 Host[] h = new Host[5];
335 NodeConnector[] nc = new NodeConnector[5];
337 /*Adding host, nodeconnector, properties of edge to hostsDB for the first three nodes only*/
338 for (int i = 1; i < 6; i++) {
340 ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0,
341 (byte)0x0, (byte)0x0,
342 (byte)0x0, (byte)i});
343 String stringIP = new StringBuilder().append(i).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString();
344 ip = InetAddress.getByName(stringIP);
345 h[hostCounter] = new Host(ea, ip);
348 h[hostCounter] = null;
351 nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short)i, NodeCreator.createOFNode((long)i));
352 topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1], UpdateType.ADDED, props);
355 /*Get the nodes which have host connected to its nodeConnector*/
356 Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl.getNodesWithNodeConnectorHost();
357 for (int i = 1; i < 6; i++) {
358 Node node = nc[i - 1].getNode();
359 Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
361 Assert.assertTrue(ncSet == nodeNCmap.remove(node));
364 Assert.assertTrue(nodeNCmap.isEmpty());