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

©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.