c176cb2cf3e50ba3b878e5923efeafe108059851
[controller.git] / opendaylight / topologymanager / src / test / java / org / opendaylight / controller / topologymanager / internal / TopologyManagerImplTest.java
1 /*
2  * Copyright (c) 2013 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.controller.topologymanager.internal;
10
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;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentMap;
20
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;
40
41 public class TopologyManagerImplTest {
42
43     /*
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
48      * 
49      * @param topoManagerImpl
50      * 
51      * @throws ConstructionException
52      */
53     public void setNodeEdges(TopologyManagerImpl topoManagerImpl)
54             throws ConstructionException {
55         topoManagerImpl.nonClusterObjectCreate();
56
57         State state;
58         Bandwidth bw;
59         Latency l;
60
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);
65         props.add(state);
66         props.add(bw);
67         props.add(l);
68
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,
78                     UpdateType.ADDED);
79             topoedgeupdateList.add(teu1);
80
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,
87                     UpdateType.ADDED);
88             topoedgeupdateList.add(teu2);
89             topoManagerImpl.edgeUpdate(topoedgeupdateList);
90
91         }
92
93     }
94
95     @Test
96     public void testGetNodeEdges() throws ConstructionException {
97         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
98         setNodeEdges(topoManagerImpl);
99
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)));
107
108             Set<Edge> edges = entry.getValue();
109             for (Edge edge : edges) {
110                 Long headNcId = ((Short) edge.getHeadNodeConnector().getID())
111                         .longValue();
112                 Long tailNcId = ((Short) edge.getTailNodeConnector().getID())
113                         .longValue();
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
118                                     .equals(nodeId))
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
129                                     .equals(nodeId + 1))
130                             || (headNcId.equals(nodeId + 1) && tailNcId
131                                     .equals(nodeId - 9)));
132                 }
133             }
134             i.remove();
135         }
136         Assert.assertTrue(nodeEdgeMap.isEmpty());
137     }
138
139     @Test
140     public void testGetEdges() throws ConstructionException {
141         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
142         setNodeEdges(topoManagerImpl);
143
144         Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
145
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();
152
153             Long headNodeId = (Long) headnc.getNode().getID();
154
155             Long headNcId = ((Short) headnc.getID()).longValue();
156             Long tailNcId = ((Short) tailnc.getID()).longValue();
157
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
162                                 .equals(headNodeId))
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)));
176             }
177
178             Set<Property> prop = entry.getValue();
179             for (Property p : prop) {
180                 String pName;
181                 long pValue;
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);
188                     continue;
189                 }
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);
196                     continue;
197                 }
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);
204                     continue;
205                 }
206             }
207             i.remove();
208         }
209         Assert.assertTrue(edgeProperty.isEmpty());
210     }
211
212     @Test
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");
222
223         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
224         topoManagerImpl.nonClusterObjectCreate();
225
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());
230
231         Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
232         Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
233                 .isSuccess());
234         Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
235                 .isSuccess());
236         Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
237
238     }
239
240     @Test
241     public void testGetUserLink() {
242         TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
243         TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
244         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
245         topoManagerImpl.nonClusterObjectCreate();
246
247         String name = null;
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;
256
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);
262
263         srcSwitchId = "1";
264         link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
265                 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
266                 dstNodeConnectorIDType, dstPort);
267         Assert.assertTrue(link[0].isValid() == false);
268
269         dstSwitchId = "2";
270         link[0] = new TopologyUserLinkConfig(name, srcNodeIDType, srcSwitchId,
271                 srcNodeConnectorIDType, srcPort, dstNodeIDType, dstSwitchId,
272                 dstNodeConnectorIDType, dstPort);
273         Assert.assertTrue(link[0].isValid() == false);
274
275         Integer i;
276
277         for (i = 0; i < 5; i++) {
278             link[i] = new TopologyUserLinkConfig(name, srcNodeIDType,
279                     srcSwitchId, srcNodeConnectorIDType, srcPort,
280                     dstNodeIDType, dstSwitchId, dstNodeConnectorIDType, dstPort);
281
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);
287
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);
293
294             Assert.assertTrue(link[i].isValid() == false);
295
296             link[i].setSrcNodeIDType("OF");
297             link[i].setSrcNodeConnectorIDType("OF");
298
299             Assert.assertTrue(link[i].isValid() == false);
300
301             link[i].setDstNodeIDType("OF");
302             link[i].setDstNodeConnectorIDType("OF");
303
304             Assert.assertTrue(link[i].isValid() == true);
305
306             reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeIDType,
307                     dstSwitchId, dstNodeConnectorIDType, dstPort,
308                     srcNodeIDType, srcSwitchId, srcNodeConnectorIDType, srcPort);
309
310             topoManagerImpl.addUserLink(link[i]);
311         }
312         ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
313                 .getUserLinks();
314         TopologyUserLinkConfig resultLink;
315
316         for (i = 0; i < 5; i++) {
317             resultLink = userLinks.get(((Integer) (i + 1)).toString());
318
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()));
329         }
330     }
331
332     @Test
333     public void testHostLinkMethods() throws ConstructionException,
334             UnknownHostException {
335         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
336         topoManagerImpl.nonClusterObjectCreate();
337         int hostCounter = 0;
338
339         State state;
340         Bandwidth bw;
341         Latency l;
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);
346         props.add(state);
347         props.add(bw);
348         props.add(l);
349
350         EthernetAddress ea;
351         InetAddress ip;
352         Host[] h = new Host[5];
353         NodeConnector[] nc = new NodeConnector[5];
354
355         /*
356          * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
357          * added for i = 4
358          */
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);
368             } else {
369                 h[hostCounter] = null;
370             }
371             hostCounter++;
372             nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
373                     NodeCreator.createOFNode((long) (i + 1)));
374             topoManagerImpl
375                     .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
376         }
377
378         for (int i = 0; i < 5; i++) {
379             Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
380             if (i == 4)
381                 Assert.assertTrue(host == null);
382             else
383                 Assert.assertTrue(host.equals(h[i]));
384         }
385
386         Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
387         for (int i = 0; i < 5; i++) {
388             Assert.assertTrue(ncSet.remove(nc[i]));
389         }
390         Assert.assertTrue(ncSet.isEmpty());
391     }
392
393     @Test
394     public void testGetNodesWithNodeConnectorHost()
395             throws ConstructionException, UnknownHostException {
396         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
397         topoManagerImpl.nonClusterObjectCreate();
398         int hostCounter = 0;
399
400         State state;
401         Bandwidth bw;
402         Latency l;
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);
407         props.add(state);
408         props.add(bw);
409         props.add(l);
410
411         EthernetAddress ea;
412         InetAddress ip;
413         Host[] h = new Host[5];
414         NodeConnector[] nc = new NodeConnector[5];
415
416         /*
417          * Adding host, nodeconnector, properties of edge to hostsDB for the
418          * first three nodes only
419          */
420         for (int i = 1; i < 6; i++) {
421             if (i < 4) {
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);
429             } else {
430                 h[hostCounter] = null;
431             }
432             hostCounter++;
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);
437         }
438
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());
445
446             Assert.assertTrue(ncSet == nodeNCmap.remove(node));
447         }
448
449         Assert.assertTrue(nodeNCmap.isEmpty());
450     }
451 }