blob: 67128db52047eed17051d68f730c3e1e70e85251 [file] [log] [blame]
Yi Tsengf4e13e32017-03-30 15:38:39 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.vpls;
17
18import com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.net.EncapsulationType;
23import org.onosproject.net.host.HostEvent;
24import org.onosproject.vpls.api.VplsData;
25import org.onosproject.vpls.api.VplsOperation;
26import org.onosproject.vpls.api.VplsOperationService;
27import org.onosproject.vpls.store.VplsStoreEvent;
28
29import java.util.Collection;
30
31import static org.junit.Assert.*;
32import static org.onosproject.net.EncapsulationType.*;
33import static org.onosproject.vpls.api.VplsData.VplsState.*;
34
35/**
36 * Test for {@link VplsManager}
37 */
38public class VplsManagerTest extends VplsTest {
39 private VplsManager vplsManager;
40 private TestVplsStore vplsStore = new TestVplsStore();
41 private TestHostService hostService = new TestHostService();
42 private TestInterfaceService interfaceService = new TestInterfaceService();
43 private TestVplsOperationService vplsOperationService = new TestVplsOperationService();
44
45 @Before
46 public void setup() {
47 vplsManager = new VplsManager();
48 vplsManager.hostService = hostService;
49 vplsManager.vplsStore = vplsStore;
50 vplsManager.operationService = vplsOperationService;
51 vplsManager.interfaceService = interfaceService;
52 vplsStore.clear();
53 vplsOperationService.clear();
54 vplsManager.activate();
55 }
56
57 @After
58 public void tearDown() {
59 vplsManager.deactivate();
60 }
61
62 /**
63 * Creates VPLS by given name and encapsulation type.
64 */
65 @Test
66 public void testCreateVpls() {
67 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
68 assertEquals(VPLS1, vplsData.name());
69 assertEquals(NONE, vplsData.encapsulationType());
70
71 vplsData = vplsStore.getVpls(VPLS1);
72 assertEquals(vplsData.state(), ADDING);
73 }
74
75 /**
76 * Gets VPLS by VPLS name.
77 */
78 @Test
79 public void testGetVpls() {
80 VplsData vplsData = VplsData.of(VPLS1);
81 vplsData.state(ADDED);
82 vplsStore.addVpls(vplsData);
83
84 VplsData result = vplsManager.getVpls(VPLS1);
85 assertEquals(vplsData, result);
86
87 result = vplsManager.getVpls(VPLS2);
88 assertNull(result);
89 }
90
91 /**
92 * Removes a VPLS.
93 */
94 @Test
95 public void testRemoveVpls() {
96 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
97 vplsManager.removeVpls(vplsData);
98 assertEquals(vplsData.state(), REMOVING);
99 vplsData = vplsStore.getVpls(VPLS1);
100 assertNull(vplsData);
101 Collection<VplsData> allVpls = vplsStore.getAllVpls();
102 assertEquals(0, allVpls.size());
103 }
104
105 /**
106 * Removes all VPLS.
107 */
108 @Test
109 public void testRemoveAllVpls() {
110 VplsData vplsData = VplsData.of(VPLS1);
111 vplsData.state(ADDED);
112 vplsStore.addVpls(vplsData);
113
114 vplsData = VplsData.of(VPLS2, VLAN);
115 vplsData.state(ADDED);
116 vplsStore.addVpls(vplsData);
117
118 vplsManager.removeAllVpls();
119 assertEquals(0, vplsStore.getAllVpls().size());
120 }
121
122 /**
123 * Adds network interfaces one by one to a VPLS.
124 */
125 @Test
126 public void testAddInterface() {
127 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
128 vplsManager.addInterface(vplsData, V100H1);
129 vplsManager.addInterface(vplsData, V100H2);
130 vplsData = vplsStore.getVpls(VPLS1);
131 assertNotNull(vplsData);
132 assertEquals(vplsData.state(), UPDATING);
133 assertEquals(2, vplsData.interfaces().size());
134 assertTrue(vplsData.interfaces().contains(V100H1));
135 assertTrue(vplsData.interfaces().contains(V100H2));
136 }
137
138 /**
139 * Adds network interfaces to a VPLS.
140 */
141 @Test
142 public void testAddInterfaces() {
143 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
144 vplsManager.addInterfaces(vplsData, ImmutableSet.of(V100H1, V100H2));
145 vplsData = vplsStore.getVpls(VPLS1);
146 assertNotNull(vplsData);
147 assertEquals(vplsData.state(), UPDATING);
148 assertEquals(2, vplsData.interfaces().size());
149 assertTrue(vplsData.interfaces().contains(V100H1));
150 assertTrue(vplsData.interfaces().contains(V100H2));
151 }
152
153 /**
154 * Removes network interfaces one by one from a VPLS.
155 */
156 @Test
157 public void testRemoveInterface() {
158 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
159 vplsManager.addInterface(vplsData, V100H1);
160 vplsManager.addInterface(vplsData, V100H2);
161 vplsManager.removeInterface(vplsData, V100H1);
162 vplsData = vplsStore.getVpls(VPLS1);
163 assertNotNull(vplsData);
164 assertEquals(vplsData.state(), UPDATING);
165 assertEquals(1, vplsData.interfaces().size());
166 assertTrue(vplsData.interfaces().contains(V100H2));
167 }
168
169 /**
170 * Removes network interfaces from a VPLS.
171 */
172 @Test
173 public void testRemoveInterfaces() {
174 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
175 vplsManager.addInterface(vplsData, V100H1);
176 vplsManager.addInterface(vplsData, V100H2);
177 vplsManager.removeInterfaces(vplsData, ImmutableSet.of(V100H1, V100H2));
178 vplsData = vplsStore.getVpls(VPLS1);
179 assertNotNull(vplsData);
180 assertEquals(vplsData.state(), UPDATING);
181 assertEquals(0, vplsData.interfaces().size());
182 }
183
184 /**
185 * Sets encapsulation type for a VPLS.
186 */
187 @Test
188 public void testSetEncapsulationType() {
189 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
190 vplsManager.setEncapsulationType(vplsData, EncapsulationType.VLAN);
191 vplsData = vplsStore.getVpls(VPLS1);
192 assertNotNull(vplsData);
193 assertEquals(vplsData.state(), UPDATING);
194 assertEquals(vplsData.encapsulationType(), EncapsulationType.VLAN);
195 }
196
197 /**
198 * Adds hosts to a VPLS.
199 */
200 @Test
201 public void testAddHost() {
202 VplsData vplsData = VplsData.of(VPLS1, NONE);
203 vplsData.addInterface(V100H1);
204 vplsData.state(ADDED);
205 vplsStore.addVpls(vplsData);
206
207 HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_ADDED, V100HOST1);
208 hostService.postHostEvent(hostEvent);
209
210 vplsData = vplsStore.getVpls(VPLS1);
211 assertNotNull(vplsData);
212
213 assertEquals(vplsData.state(), UPDATING);
214 }
215
216 /**
217 * Removes hosts from a VPLS.
218 */
219 @Test
220 public void testRemoveHost() {
221 VplsData vplsData = VplsData.of(VPLS1, NONE);
222 vplsData.addInterface(V100H1);
223 vplsData.state(ADDED);
224 vplsStore.addVpls(vplsData);
225
226 HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_REMOVED, V100HOST1);
227 hostService.postHostEvent(hostEvent);
228 vplsData = vplsStore.getVpls(VPLS1);
229 assertNotNull(vplsData);
230
231 assertEquals(vplsData.state(), UPDATING);
232 }
233
234 /**
235 * Pass different VPLS store event to store delegate.
236 * Include these cases:
237 * <ul>
238 * <li>VPLS added</li>
239 * <li>VPLS updated</li>
240 * <li>VPLS state updated</li>
241 * <li>VPLS removed</li>
242 * </ul>
243 */
244 @Test
245 public void testStoreDelegate() {
246 // Add
247 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
248 VplsStoreEvent event = new VplsStoreEvent(VplsStoreEvent.Type.ADD, vplsData);
249 vplsStore.delegate().notify(event);
250
251 VplsOperation vplsOperation = vplsOperationService.operation();
252 assertEquals(vplsOperation.op(), VplsOperation.Operation.ADD);
253 assertEquals(vplsOperation.vpls(), vplsData);
254 vplsOperationService.clear();
255
256 // Update info
257 vplsData.encapsulationType(EncapsulationType.VLAN);
258 vplsData.state(UPDATING);
259 event = new VplsStoreEvent(VplsStoreEvent.Type.UPDATE, vplsData);
260 vplsStore.delegate().notify(event);
261 vplsOperation = vplsOperationService.operation();
262 assertEquals(vplsOperation.op(), VplsOperation.Operation.UPDATE);
263 assertEquals(vplsOperation.vpls(), vplsData);
264 vplsOperationService.clear();
265
266 // Update state (no operation)
267 vplsData.state(VplsData.VplsState.ADDED);
268 event = new VplsStoreEvent(VplsStoreEvent.Type.UPDATE, vplsData);
269 vplsStore.delegate().notify(event);
270 vplsOperation = vplsOperationService.operation();
271 assertNull(vplsOperation);
272 vplsOperationService.clear();
273
274 // Remove
275 event = new VplsStoreEvent(VplsStoreEvent.Type.REMOVE, vplsData);
276 vplsStore.delegate().notify(event);
277 vplsOperation = vplsOperationService.operation();
278 assertEquals(vplsOperation.op(), VplsOperation.Operation.REMOVE);
279 assertEquals(vplsOperation.vpls(), vplsData);
280 vplsOperationService.clear();
281 }
282
283 /**
284 * Trigger host event listener by HOST_ADDED event.
285 */
286 @Test
287 public void hostAddEventTest() {
288 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
289 vplsManager.addInterface(vplsData, V100H1);
290 HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_ADDED, V100HOST1);
291 hostService.postHostEvent(hostEvent);
292
293 vplsData = vplsStore.getVpls(VPLS1);
294 assertEquals(UPDATING, vplsData.state());
295 }
296
297 /**
298 * Trigger host event listener by HOST_REMOVED event.
299 */
300 @Test
301 public void hostRemoveEventTest() {
302 VplsData vplsData = vplsManager.createVpls(VPLS1, NONE);
303 vplsManager.addInterface(vplsData, V100H1);
304 HostEvent hostEvent = new HostEvent(HostEvent.Type.HOST_REMOVED, V100HOST1);
305 hostService.postHostEvent(hostEvent);
306
307 vplsData = vplsStore.getVpls(VPLS1);
308 assertEquals(UPDATING, vplsData.state());
309 }
310
311 /**
312 * Test VPLS operation service.
313 * Stores last operation submitted by VPLS manager.
314 */
315 class TestVplsOperationService implements VplsOperationService {
316 VplsOperation operation;
317
318 @Override
319 public void submit(VplsOperation vplsOperation) {
320 this.operation = vplsOperation;
321 }
322
323 /**
324 * Clears the VPLS operation.
325 */
326 public void clear() {
327 operation = null;
328 }
329
330 /**
331 * Gets the latest VPLS operation.
332 * @return the latest VPLS operation.
333 */
334 public VplsOperation operation() {
335 return operation;
336 }
337 }
338
339}