Fix test case mis-spelling.
[controller.git] / opendaylight / adsal / topologymanager / implementation / 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 org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.core.Bandwidth;
14 import org.opendaylight.controller.sal.core.ConstructionException;
15 import org.opendaylight.controller.sal.core.Description;
16 import org.opendaylight.controller.sal.core.Edge;
17 import org.opendaylight.controller.sal.core.Host;
18 import org.opendaylight.controller.sal.core.Latency;
19 import org.opendaylight.controller.sal.core.Node;
20 import org.opendaylight.controller.sal.core.Node.NodeIDType;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
23 import org.opendaylight.controller.sal.core.Property;
24 import org.opendaylight.controller.sal.core.State;
25 import org.opendaylight.controller.sal.core.TimeStamp;
26 import org.opendaylight.controller.sal.core.UpdateType;
27 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
28 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
29 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
30 import org.opendaylight.controller.sal.utils.NodeCreator;
31 import org.opendaylight.controller.sal.utils.Status;
32 import org.opendaylight.controller.sal.utils.StatusCode;
33 import org.opendaylight.controller.switchmanager.ISwitchManager;
34 import org.opendaylight.controller.switchmanager.SpanConfig;
35 import org.opendaylight.controller.switchmanager.Subnet;
36 import org.opendaylight.controller.switchmanager.SubnetConfig;
37 import org.opendaylight.controller.switchmanager.Switch;
38 import org.opendaylight.controller.switchmanager.SwitchConfig;
39 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
40
41 import java.net.InetAddress;
42 import java.net.UnknownHostException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Set;
50 import java.util.concurrent.ConcurrentMap;
51
52 public class TopologyManagerImplTest {
53     /**
54      * Mockup of switch manager that only maintains existence of node
55      * connector.
56      */
57     private final class TestSwitchManager implements ISwitchManager {
58         private final Set<Node>  nodeSet = new HashSet<Node>();
59         private final Set<NodeConnector> nodeConnectorSet =
60                 new HashSet<NodeConnector>();
61
62         private void addNodeConnectors(NodeConnector ... connectors) {
63             for (NodeConnector nc: connectors) {
64                 if (nc != null) {
65                     nodeSet.add(nc.getNode());
66                     nodeConnectorSet.add(nc);
67                 }
68             }
69         }
70
71         private void addNodeConnectors(TopologyUserLinkConfig ... links) {
72             for (TopologyUserLinkConfig link: links) {
73                 NodeConnector src =
74                         NodeConnector.fromString(link.getSrcNodeConnector());
75                 NodeConnector dst =
76                         NodeConnector.fromString(link.getDstNodeConnector());
77                 addNodeConnectors(src, dst);
78             }
79         }
80
81         @Override
82         public Status addSubnet(SubnetConfig configObject) {
83             return null;
84         }
85
86         @Override
87         public Status removeSubnet(SubnetConfig configObject) {
88             return null;
89         }
90
91         @Override
92         public Status modifySubnet(SubnetConfig configObject) {
93             return null;
94         }
95
96         @Override
97         public Status removeSubnet(String name) {
98             return null;
99         }
100
101         @Override
102         public List<Switch> getNetworkDevices() {
103             return null;
104         }
105
106         @Override
107         public List<SubnetConfig> getSubnetsConfigList() {
108             return null;
109         }
110
111         @Override
112         public SubnetConfig getSubnetConfig(String subnet) {
113             return null;
114         }
115
116         @Override
117         public Subnet getSubnetByNetworkAddress(InetAddress networkAddress) {
118             return null;
119         }
120
121         @Override
122         public Status saveSwitchConfig() {
123             return null;
124         }
125
126         @Override
127         public Status addSpanConfig(SpanConfig configObject) {
128             return null;
129         }
130
131         @Override
132         public Status removeSpanConfig(SpanConfig cfgObject) {
133             return null;
134         }
135
136         @Override
137         public List<SpanConfig> getSpanConfigList() {
138             return null;
139         }
140
141         @Override
142         public List<NodeConnector> getSpanPorts(Node node) {
143             return null;
144         }
145
146         @Override
147         public void updateSwitchConfig(SwitchConfig cfgObject) {
148         }
149
150         @Override
151         public Status updateNodeConfig(SwitchConfig switchConfig) {
152             return null;
153         }
154
155         @Override
156         public Status removeNodeConfig(String nodeId) {
157             return null;
158         }
159
160         @Override
161         public SwitchConfig getSwitchConfig(String nodeId) {
162             return null;
163         }
164
165         @Override
166         public Status addPortsToSubnet(String name, List<String> nodeConnectors) {
167             return null;
168         }
169
170         @Override
171         public Status removePortsFromSubnet(String name, List<String> nodeConnectors) {
172             return null;
173         }
174
175         @Override
176         public Set<Node> getNodes() {
177             return new HashSet<Node>(nodeSet);
178         }
179
180         @Override
181         public Map<String, Property> getNodeProps(Node node) {
182             return new HashMap<String, Property>();
183         }
184
185         @Override
186         public Property getNodeProp(Node node, String propName) {
187             return null;
188         }
189
190         @Override
191         public void setNodeProp(Node node, Property prop) {
192         }
193
194         @Override
195         public Status removeNodeProp(Node node, String propName) {
196             return null;
197         }
198
199         @Override
200         public Status removeNodeAllProps(Node node) {
201             return null;
202         }
203
204         @Override
205         public Set<NodeConnector> getUpNodeConnectors(Node node) {
206             return getNodeConnectors(node);
207         }
208
209         @Override
210         public Set<NodeConnector> getNodeConnectors(Node node) {
211             Set<NodeConnector> set = new HashSet<NodeConnector>();
212             for (NodeConnector nc: nodeConnectorSet) {
213                 if (nc.getNode().equals(node)) {
214                     set.add(nc);
215                 }
216             }
217
218             return set;
219         }
220
221         @Override
222         public Set<NodeConnector> getPhysicalNodeConnectors(Node node) {
223             return getNodeConnectors(node);
224         }
225
226         @Override
227         public Map<String, Property> getNodeConnectorProps(NodeConnector nodeConnector) {
228             return new HashMap<String, Property>();
229         }
230
231         @Override
232         public Property getNodeConnectorProp(NodeConnector nodeConnector, String propName) {
233             return null;
234         }
235
236         @Override
237         public Status addNodeConnectorProp(NodeConnector nodeConnector, Property prop) {
238             return null;
239         }
240
241         @Override
242         public Status removeNodeConnectorProp(NodeConnector nc, String propName) {
243             return null;
244         }
245
246         @Override
247         public Status removeNodeConnectorAllProps(NodeConnector nodeConnector) {
248             return null;
249         }
250
251         @Override
252         public NodeConnector getNodeConnector(Node node, String nodeConnectorName) {
253             return null;
254         }
255
256         @Override
257         public boolean isSpecial(NodeConnector p) {
258             String type = p.getType();
259             return (type.equals(NodeConnectorIDType.CONTROLLER)
260                     || type.equals(NodeConnectorIDType.ALL)
261                     || type.equals(NodeConnectorIDType.SWSTACK)
262                     || type.equals(NodeConnectorIDType.HWPATH));
263         }
264
265         @Override
266         public Boolean isNodeConnectorEnabled(NodeConnector nodeConnector) {
267             if (doesNodeConnectorExist(nodeConnector)) {
268                 return Boolean.TRUE;
269             }
270             return Boolean.FALSE;
271         }
272
273         @Override
274         public boolean doesNodeConnectorExist(NodeConnector nc) {
275             return (nc != null && nodeConnectorSet.contains(nc));
276         }
277
278         @Override
279         public byte[] getControllerMAC() {
280             return new byte[6];
281         }
282
283         @Override
284         public byte[] getNodeMAC(Node node) {
285             return new byte[6];
286         }
287
288         @Override
289         public Property createProperty(String propName, String propValue) {
290             return null;
291         }
292
293         @Override
294         public String getNodeDescription(Node node) {
295             return null;
296         }
297
298         @Override
299         public Status removeControllerProperty(String propertyName){
300             return null;
301         }
302
303         @Override
304         public Set<Switch> getConfiguredNotConnectedSwitches() {
305             // TODO Auto-generated method stub
306             return null;
307         }
308
309         @Override
310         public Map<String, Property> getControllerProperties() {
311             // TODO Auto-generated method stub
312             return null;
313         }
314
315         @Override
316         public Property getControllerProperty(String propertyName) {
317             // TODO Auto-generated method stub
318             return null;
319         }
320
321         @Override
322         public Status setControllerProperty(Property property) {
323             // TODO Auto-generated method stub
324             return null;
325         }
326     }
327
328     /*
329      * Sets the node, edges and properties for edges here: Edge <SwitchId :
330      * NodeConnectorId> : <1:1>--><11:11>; <1:2>--><11:12>; <3:3>--><13:13>;
331      * <3:4>--><13:14>; <5:5>--><15:15>; <5:6>--><15:16>; Method used by two
332      * tests: testGetNodeEdges and testGetEdges
333      *
334      * @param topoManagerImpl
335      *
336      * @throws ConstructionException
337      */
338     public void setNodeEdges(TopologyManagerImpl topoManagerImpl, TestSwitchManager swMgr)
339             throws ConstructionException {
340         topoManagerImpl.nonClusterObjectCreate();
341
342         State state;
343         Bandwidth bw;
344         Latency l;
345
346         Set<Property> props = new HashSet<Property>();
347         state = new State(State.EDGE_UP);
348         bw = new Bandwidth(Bandwidth.BW100Gbps);
349         l = new Latency(Latency.LATENCY100ns);
350         props.add(state);
351         props.add(bw);
352         props.add(l);
353
354         for (short i = 1; i < 6; i = (short) (i + 2)) {
355             List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
356             NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(
357                     i, NodeCreator.createOFNode((long) i));
358             NodeConnector tailnc1 = NodeConnectorCreator
359                     .createOFNodeConnector((short) (i + 10),
360                             NodeCreator.createOFNode((long) (i + 10)));
361             swMgr.addNodeConnectors(tailnc1, headnc1);
362             Edge e1 = new Edge(tailnc1, headnc1);
363             TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props,
364                     UpdateType.ADDED);
365             topoedgeupdateList.add(teu1);
366
367             NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector(
368                     (short) (i + 1), headnc1.getNode());
369             NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector(
370                     (short) (i + 11), tailnc1.getNode());
371             swMgr.addNodeConnectors(tailnc1, headnc2);
372             Edge e2 = new Edge(tailnc2, headnc2);
373             TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props,
374                     UpdateType.ADDED);
375             topoedgeupdateList.add(teu2);
376             topoManagerImpl.edgeUpdate(topoedgeupdateList);
377         }
378     }
379
380     @Test
381     public void testGetNodeEdges() throws ConstructionException {
382         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
383         TestSwitchManager swMgr = new TestSwitchManager();
384         topoManagerImpl.setSwitchManager(swMgr);
385         setNodeEdges(topoManagerImpl, swMgr);
386
387         Map<Node, Set<Edge>> nodeEdgeMap = topoManagerImpl.getNodeEdges();
388         for (Iterator<Map.Entry<Node, Set<Edge>>> i = nodeEdgeMap.entrySet()
389                 .iterator(); i.hasNext();) {
390             Map.Entry<Node, Set<Edge>> entry = i.next();
391             Node node = entry.getKey();
392             Long nodeId = ((Long) node.getID()).longValue();
393             Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW)));
394
395             Set<Edge> edges = entry.getValue();
396             for (Edge edge : edges) {
397                 Long headNcId = ((Short) edge.getHeadNodeConnector().getID())
398                         .longValue();
399                 Long tailNcId = ((Short) edge.getTailNodeConnector().getID())
400                         .longValue();
401                 Assert.assertTrue(
402                         (headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10))
403                         || (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1))
404                         || (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9))
405                         || (headNcId.equals(nodeId - 10) && tailNcId.equals(nodeId))
406                         );
407             }
408             i.remove();
409         }
410         Assert.assertTrue(nodeEdgeMap.isEmpty());
411     }
412
413     @Test
414     public void testGetEdges() throws ConstructionException {
415         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
416         TestSwitchManager swMgr = new TestSwitchManager();
417         topoManagerImpl.setSwitchManager(swMgr);
418         setNodeEdges(topoManagerImpl, swMgr);
419
420         Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
421
422         for (Iterator<Map.Entry<Edge, Set<Property>>> i = edgeProperty
423                 .entrySet().iterator(); i.hasNext();) {
424             Map.Entry<Edge, Set<Property>> entry = i.next();
425             Edge e = entry.getKey();
426             NodeConnector headnc = e.getHeadNodeConnector();
427             NodeConnector tailnc = e.getTailNodeConnector();
428
429             Long headNodeId = (Long) headnc.getNode().getID();
430
431             Long headNcId = ((Short) headnc.getID()).longValue();
432             Long tailNcId = ((Short) tailnc.getID()).longValue();
433
434             if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
435                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
436                         .equals(headNodeId + 10))
437                         || (headNcId.equals(headNodeId + 10) && tailNcId
438                                 .equals(headNodeId))
439                                 || (headNcId.equals(headNodeId + 1) && tailNcId
440                                         .equals(headNodeId + 11))
441                                         || (headNcId.equals(headNodeId + 11) && tailNcId
442                                                 .equals(headNodeId + 1)));
443             } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
444                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
445                         .equals(headNodeId - 10))
446                         || (headNcId.equals(headNodeId) && tailNcId
447                                 .equals(headNodeId - 10))
448                                 || (headNcId.equals(headNodeId - 9) && tailNcId
449                                         .equals(headNodeId + 1))
450                                         || (headNcId.equals(headNodeId + 1) && tailNcId
451                                                 .equals(headNodeId - 9)));
452             }
453
454             Set<Property> prop = entry.getValue();
455             for (Property p : prop) {
456                 String pName;
457                 long pValue;
458                 if (p instanceof Bandwidth) {
459                     Bandwidth b = (Bandwidth) p;
460                     pName = Bandwidth.BandwidthPropName;
461                     pValue = b.getValue();
462                     Assert.assertTrue(pName.equals(p.getName())
463                             && pValue == Bandwidth.BW100Gbps);
464                     continue;
465                 }
466                 if (p instanceof Latency) {
467                     Latency l = (Latency) p;
468                     pName = Latency.LatencyPropName;
469                     pValue = l.getValue();
470                     Assert.assertTrue(pName.equals(p.getName())
471                             && pValue == Latency.LATENCY100ns);
472                     continue;
473                 }
474                 if (p instanceof State) {
475                     State state = (State) p;
476                     pName = State.StatePropName;
477                     pValue = state.getValue();
478                     Assert.assertTrue(pName.equals(p.getName())
479                             && pValue == State.EDGE_UP);
480                     continue;
481                 }
482             }
483             i.remove();
484         }
485         Assert.assertTrue(edgeProperty.isEmpty());
486     }
487
488     @Test
489     public void testAddDeleteUserLink() {
490         TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1",
491                 "OF|1@OF|2", "OF|1@OF|3");
492         TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1",
493                 "OF|10@OF|20", "OF|10@OF|30");
494         TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2",
495                 "OF|1@OF|2", "OF|1@OF|3");
496         TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20",
497                 "OF|10@OF|20", "OF|10@OF|30");
498
499         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
500         TestSwitchManager swMgr = new TestSwitchManager();
501         topoManagerImpl.setSwitchManager(swMgr);
502         topoManagerImpl.nonClusterObjectCreate();
503
504         swMgr.addNodeConnectors(link1, link2, link3, link4);
505
506         Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess());
507         Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
508         Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
509         Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess());
510
511         Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
512         Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
513                 .isSuccess());
514         Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
515                 .isSuccess());
516         Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
517
518         TopologyUserLinkConfig badlink1 =
519                 new TopologyUserLinkConfig("bad1", "OF|1@OF|4", "OF|1@OF|5");
520         TopologyUserLinkConfig badlink2 =
521                 new TopologyUserLinkConfig("bad2", "OF|10@OF|7", "OF|7@OF|13");
522         Assert.assertEquals(StatusCode.NOTFOUND,
523                 topoManagerImpl.addUserLink(badlink1).getCode());
524         Assert.assertEquals(StatusCode.NOTFOUND,
525                 topoManagerImpl.addUserLink(badlink2).getCode());
526     }
527
528     @Test
529     public void testGetUserLink() {
530         TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
531         TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
532         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
533         TestSwitchManager swMgr = new TestSwitchManager();
534         topoManagerImpl.setSwitchManager(swMgr);
535         topoManagerImpl.nonClusterObjectCreate();
536
537         String name = "Test";
538         String srcSwitchId = null;
539         String srcNodeConnectorIDType = null;
540         String srcPort = null;
541         String srcNodeIDType = null;
542         String dstNodeIDType = null;
543         String dstSwitchId = null;
544         String dstNodeConnectorIDType = null;
545         String dstPort = null;
546         String srcNodeConnector = null;
547         String dstNodeConnector = null;
548
549         /* Creating userlinks and checking for their validity */
550         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
551         Assert.assertTrue(link[0].isValid() == false);
552
553         srcNodeConnector = "OF|1@OF|1";
554         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
555         Assert.assertTrue(link[0].isValid() == false);
556
557         dstNodeConnector = "OF|1@OF|2";
558         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
559         Assert.assertTrue(link[0].isValid() == true);
560
561         Integer i;
562
563         for (i = 0; i < 5; i++) {
564             link[i] = new TopologyUserLinkConfig();
565
566             name = Integer.toString(i + 1);
567             srcSwitchId = Integer.toString(i + 1);
568             srcPort = Integer.toString(i + 1);
569             dstSwitchId = Integer.toString((i + 1) * 10);
570             dstPort = Integer.toString((i + 1) * 10);
571
572             link[i].setName(name);
573             srcNodeConnectorIDType = dstNodeConnectorIDType = "INCORRECT";
574             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
575             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
576
577             link[i].setSrcNodeConnector(srcNodeConnector);
578             Assert.assertTrue(link[i].isValid() == false);
579
580             srcNodeConnectorIDType = "OF";
581             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
582             link[i].setSrcNodeConnector(srcNodeConnector);
583             Assert.assertTrue(link[i].isValid() == false);
584
585             dstNodeConnectorIDType = "OF";
586             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
587             link[i].setDstNodeConnector(dstNodeConnector);
588             Assert.assertTrue(link[i].isValid() == true);
589
590             reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeConnector, srcNodeConnector);
591
592             Assert.assertEquals(StatusCode.NOTFOUND,
593                     topoManagerImpl.addUserLink(link[i]).getCode());
594             swMgr.addNodeConnectors(link[i]);
595             Assert.assertTrue(topoManagerImpl.addUserLink(link[i]).isSuccess());
596         }
597
598         ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
599                 .getUserLinks();
600         TopologyUserLinkConfig resultLink;
601
602         for (i = 0; i < 5; i++) {
603             resultLink = userLinks.get(((Integer) (i + 1)).toString());
604
605             Assert.assertTrue(resultLink.getName().equals(
606                     reverseLink[i].getName()));
607             Assert.assertTrue(resultLink.getDstNodeConnector().equals(
608                     reverseLink[i].getSrcNodeConnector()));
609             Assert.assertTrue(resultLink.getSrcNodeConnector().equals(
610                     reverseLink[i].getDstNodeConnector()));
611         }
612     }
613
614     @Test
615     public void testHostLinkMethods() throws ConstructionException,
616     UnknownHostException {
617         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
618         TestSwitchManager swMgr = new TestSwitchManager();
619         topoManagerImpl.setSwitchManager(swMgr);
620         topoManagerImpl.nonClusterObjectCreate();
621         int hostCounter = 0;
622
623         State state;
624         Bandwidth bw;
625         Latency l;
626         Set<Property> props = new HashSet<Property>();
627         state = new State(State.EDGE_UP);
628         bw = new Bandwidth(Bandwidth.BW100Gbps);
629         l = new Latency(Latency.LATENCY100ns);
630         props.add(state);
631         props.add(bw);
632         props.add(l);
633
634         EthernetAddress ea;
635         InetAddress ip;
636         Host[] h = new Host[5];
637         NodeConnector[] nc = new NodeConnector[5];
638
639         /*
640          * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
641          * added for i = 4
642          */
643         for (int i = 0; i < 5; i++) {
644             if (hostCounter < 4) {
645                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
646                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
647                 String stringIP = new StringBuilder().append(i + 1).append(".")
648                         .append(i + 10).append(".").append(i + 20).append(".")
649                         .append(i + 30).toString();
650                 ip = InetAddress.getByName(stringIP);
651                 h[hostCounter] = new Host(ea, ip);
652             } else {
653                 h[hostCounter] = null;
654             }
655             hostCounter++;
656             nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
657                     NodeCreator.createOFNode((long) (i + 1)));
658             topoManagerImpl
659             .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
660         }
661
662         for (int i = 0; i < 5; i++) {
663             Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
664             if (i == 4) {
665                 Assert.assertTrue(host == null);
666             } else {
667                 Assert.assertTrue(host.equals(h[i]));
668             }
669         }
670
671         Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
672         for (int i = 0; i < 5; i++) {
673             Assert.assertTrue(ncSet.remove(nc[i]));
674         }
675         Assert.assertTrue(ncSet.isEmpty());
676     }
677
678     @Test
679     public void testGetNodesWithNodeConnectorHost()
680             throws ConstructionException, UnknownHostException {
681         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
682         TestSwitchManager swMgr = new TestSwitchManager();
683         topoManagerImpl.setSwitchManager(swMgr);
684         topoManagerImpl.nonClusterObjectCreate();
685         int hostCounter = 0;
686
687         State state;
688         Bandwidth bw;
689         Latency l;
690         Set<Property> props = new HashSet<Property>();
691         state = new State(State.EDGE_UP);
692         bw = new Bandwidth(Bandwidth.BW100Gbps);
693         l = new Latency(Latency.LATENCY100ns);
694         props.add(state);
695         props.add(bw);
696         props.add(l);
697
698         EthernetAddress ea;
699         InetAddress ip;
700         Host[] h = new Host[5];
701         NodeConnector[] nc = new NodeConnector[5];
702
703         /*
704          * Adding host, nodeconnector, properties of edge to hostsDB for the
705          * first three nodes only
706          */
707         for (int i = 1; i < 6; i++) {
708             if (i < 4) {
709                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
710                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
711                 String stringIP = new StringBuilder().append(i).append(".")
712                         .append(i + 10).append(".").append(i + 20).append(".")
713                         .append(i + 30).toString();
714                 ip = InetAddress.getByName(stringIP);
715                 h[hostCounter] = new Host(ea, ip);
716             } else {
717                 h[hostCounter] = null;
718             }
719             hostCounter++;
720             nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i,
721                     NodeCreator.createOFNode((long) i));
722             topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1],
723                     UpdateType.ADDED, props);
724         }
725
726         /* Get the nodes which have host connected to its nodeConnector */
727         Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl
728                 .getNodesWithNodeConnectorHost();
729         for (int i = 1; i < 6; i++) {
730             Node node = nc[i - 1].getNode();
731             Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
732
733             Assert.assertTrue(ncSet == nodeNCmap.remove(node));
734         }
735
736         Assert.assertTrue(nodeNCmap.isEmpty());
737     }
738
739     @Test
740     public void bug1348FixTest() throws ConstructionException {
741         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
742         TestSwitchManager swMgr = new TestSwitchManager();
743         topoManagerImpl.setSwitchManager(swMgr);
744         topoManagerImpl.nonClusterObjectCreate();
745
746         NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(
747                 (short) 1, NodeCreator.createOFNode(1000L));
748         NodeConnector tailnc1 = NodeConnectorCreator.createOFNodeConnector(
749                 (short) 2, NodeCreator.createOFNode(2000L));
750         Edge edge = new Edge(headnc1, tailnc1);
751         List<TopoEdgeUpdate> updatedEdges = new ArrayList<>();
752         Set<Property> edgeProps = new HashSet<>();
753         edgeProps.add(new TimeStamp(System.currentTimeMillis(), "creation"));
754         edgeProps.add(new Latency(Latency.LATENCY100ns));
755         edgeProps.add(new State(State.EDGE_UP));
756         edgeProps.add(new Bandwidth(Bandwidth.BW100Gbps));
757         edgeProps.add(new Description("Test edge"));
758         updatedEdges.add(new TopoEdgeUpdate(edge, edgeProps, UpdateType.CHANGED));
759
760         try {
761             topoManagerImpl.edgeUpdate(updatedEdges);
762         } catch (Exception e) {
763             Assert.fail("Exception was raised when trying to update edge properties: " + e.getMessage());
764         }
765
766         Assert.assertEquals(1, topoManagerImpl.getEdges().size());
767         Assert.assertNotNull(topoManagerImpl.getEdges().get(edge));
768     }
769 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.