blob: 280c33656bacc8834534b2285087874112a2fc99 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.topology;
2
3import net.floodlightcontroller.core.IFloodlightProviderService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08004import net.floodlightcontroller.core.module.FloodlightModuleContext;
5import net.floodlightcontroller.core.test.MockThreadPoolService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08006import net.floodlightcontroller.test.FloodlightTestCase;
7import net.floodlightcontroller.threadpool.IThreadPoolService;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -07008import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08009
10import org.junit.Before;
11import org.junit.Test;
12import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14
15public class TopologyManagerTest extends FloodlightTestCase {
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070016 protected final static Logger log = LoggerFactory.getLogger(TopologyManagerTest.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017 TopologyManager tm;
18 FloodlightModuleContext fmc;
19
20 @Before
21 public void setUp() throws Exception {
22 super.setUp();
23 fmc = new FloodlightModuleContext();
24 fmc.addService(IFloodlightProviderService.class, getMockFloodlightProvider());
25 MockThreadPoolService tp = new MockThreadPoolService();
26 fmc.addService(IThreadPoolService.class, tp);
27 tm = new TopologyManager();
28 tp.init(fmc);
29 tm.init(fmc);
30 tp.startUp(fmc);
31 }
32
33 @Test
34 public void testBasic1() throws Exception {
35 tm.addOrUpdateLink((long)1, (short)1, (long)2, (short)1, ILinkDiscovery.LinkType.DIRECT_LINK);
36 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
37 assertTrue(tm.getSwitchPorts().get((long)1).size()==1);
38 assertTrue(tm.getSwitchPorts().get((long)2).size()==1);
39 assertTrue(tm.getSwitchPortLinks().size()==2);
40 assertTrue(tm.getPortBroadcastDomainLinks().size()==0);
41 assertTrue(tm.getTunnelPorts().size()==0);
42
43 tm.addOrUpdateLink((long)1, (short)2, (long)2, (short)2, ILinkDiscovery.LinkType.MULTIHOP_LINK);
44 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
45 assertTrue(tm.getSwitchPorts().get((long)1).size()==2);
46 assertTrue(tm.getSwitchPorts().get((long)2).size()==2);
47 assertTrue(tm.getSwitchPortLinks().size()==4);
48 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
49 assertTrue(tm.getTunnelPorts().size()==0);
50
51 tm.addOrUpdateLink((long)1, (short)3, (long)2, (short)3, ILinkDiscovery.LinkType.TUNNEL);
52 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
53 assertTrue(tm.getSwitchPorts().get((long)1).size()==3);
54 assertTrue(tm.getSwitchPorts().get((long)2).size()==3);
55 assertTrue(tm.getSwitchPortLinks().size()==6);
56 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
57 assertTrue(tm.getTunnelPorts().size()==2);
58
59 tm.removeLink((long)1, (short)2, (long)2, (short)2);
60 log.info("# of switchports. {}", tm.getSwitchPorts().get((long)1).size());
61 assertTrue(tm.getSwitchPorts().get((long)1).size()==2);
62 assertTrue(tm.getSwitchPorts().get((long)2).size()==2);
63 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
64 assertTrue(tm.getSwitchPortLinks().size()==4);
65 assertTrue(tm.getPortBroadcastDomainLinks().size()==0);
66 assertTrue(tm.getTunnelPorts().size()==2);
67
68 tm.removeLink((long)1, (short)1, (long)2, (short)1);
69 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
70 assertTrue(tm.getSwitchPorts().get((long)1).size()==1);
71 assertTrue(tm.getSwitchPorts().get((long)2).size()==1);
72 assertTrue(tm.getSwitchPortLinks().size()==2);
73 assertTrue(tm.getPortBroadcastDomainLinks().size()==0);
74 assertTrue(tm.getTunnelPorts().size()==2);
75
76 tm.removeLink((long)1, (short)3, (long)2, (short)3);
77 assertTrue(tm.getSwitchPorts().size() == 0);
78 assertTrue(tm.getSwitchPortLinks().size()==0);
79 assertTrue(tm.getPortBroadcastDomainLinks().size()==0);
80 assertTrue(tm.getTunnelPorts().size()==0);
81 }
82
83 @Test
84 public void testBasic2() throws Exception {
85 tm.addOrUpdateLink((long)1, (short)1, (long)2, (short)1, ILinkDiscovery.LinkType.DIRECT_LINK);
86 tm.addOrUpdateLink((long)2, (short)2, (long)3, (short)1, ILinkDiscovery.LinkType.MULTIHOP_LINK);
87 tm.addOrUpdateLink((long)3, (short)2, (long)1, (short)2, ILinkDiscovery.LinkType.TUNNEL);
88 assertTrue(tm.getSwitchPorts().size() == 3); // for two nodes.
89 assertTrue(tm.getSwitchPorts().get((long)1).size()==2);
90 assertTrue(tm.getSwitchPorts().get((long)2).size()==2);
91 assertTrue(tm.getSwitchPorts().get((long)3).size()==2);
92 assertTrue(tm.getSwitchPortLinks().size()==6);
93 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
94 assertTrue(tm.getTunnelPorts().size()==2);
95
96 tm.removeLink((long)1, (short)1, (long)2, (short)1);
97 assertTrue(tm.getSwitchPorts().size() == 3); // for two nodes.
98 assertTrue(tm.getSwitchPorts().get((long)1).size()==1);
99 assertTrue(tm.getSwitchPorts().get((long)2).size()==1);
100 assertTrue(tm.getSwitchPorts().get((long)3).size()==2);
101 assertTrue(tm.getSwitchPortLinks().size()==4);
102 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
103 assertTrue(tm.getTunnelPorts().size()==2);
104
105 // nonexistent link // no null pointer exceptions.
106 tm.removeLink((long)3, (short)1, (long)2, (short)2);
107 assertTrue(tm.getSwitchPorts().size() == 3); // for two nodes.
108 assertTrue(tm.getSwitchPorts().get((long)1).size()==1);
109 assertTrue(tm.getSwitchPorts().get((long)2).size()==1);
110 assertTrue(tm.getSwitchPorts().get((long)3).size()==2);
111 assertTrue(tm.getSwitchPortLinks().size()==4);
112 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
113 assertTrue(tm.getTunnelPorts().size()==2);
114
115 tm.removeLink((long)3, (short)2, (long)1, (short)2);
116 assertTrue(tm.getSwitchPorts().size() == 2); // for two nodes.
117 assertTrue(tm.getSwitchPorts().get((long)1)==null);
118 assertTrue(tm.getSwitchPorts().get((long)2).size()==1);
119 assertTrue(tm.getSwitchPorts().get((long)3).size()==1);
120 assertTrue(tm.getSwitchPortLinks().size()==2);
121 assertTrue(tm.getPortBroadcastDomainLinks().size()==2);
122 assertTrue(tm.getTunnelPorts().size()==0);
123
124 tm.removeLink((long)2, (short)2, (long)3, (short)1);
125 assertTrue(tm.getSwitchPorts().size() == 0); // for two nodes.
126 assertTrue(tm.getSwitchPortLinks().size()==0);
127 assertTrue(tm.getPortBroadcastDomainLinks().size()==0);
128 assertTrue(tm.getTunnelPorts().size()==0);
129 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800130}