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