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

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