Open Kilda Java Documentation
SwitchManagerTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.switchmanager;
17 
18 import static java.util.Collections.emptySet;
19 import static java.util.Collections.singletonList;
20 import static org.easymock.EasyMock.anyLong;
21 import static org.easymock.EasyMock.anyObject;
22 import static org.easymock.EasyMock.capture;
23 import static org.easymock.EasyMock.createMock;
24 import static org.easymock.EasyMock.expect;
25 import static org.easymock.EasyMock.expectLastCall;
26 import static org.easymock.EasyMock.mock;
27 import static org.easymock.EasyMock.replay;
28 import static org.hamcrest.CoreMatchers.equalTo;
29 import static org.hamcrest.CoreMatchers.hasItem;
30 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.containsInAnyOrder;
32 import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
33 import static org.hamcrest.core.Every.everyItem;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNull;
48 
49 import com.google.common.util.concurrent.Futures;
50 import com.google.common.util.concurrent.ListenableFuture;
51 import net.floodlightcontroller.core.IOFSwitch;
52 import net.floodlightcontroller.core.SwitchDescription;
53 import net.floodlightcontroller.core.internal.IOFSwitchService;
54 import net.floodlightcontroller.core.module.FloodlightModuleContext;
55 import net.floodlightcontroller.core.module.FloodlightModuleException;
56 import net.floodlightcontroller.restserver.IRestApiService;
57 import org.easymock.Capture;
58 import org.easymock.CaptureType;
59 import org.easymock.EasyMock;
60 import org.junit.Before;
61 import org.junit.Test;
66 import org.projectfloodlight.openflow.protocol.OFBarrierReply;
67 import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
68 import org.projectfloodlight.openflow.protocol.OFFlowMod;
69 import org.projectfloodlight.openflow.protocol.OFFlowModCommand;
70 import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
71 import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
72 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
73 import org.projectfloodlight.openflow.protocol.OFMeterMod;
74 import org.projectfloodlight.openflow.protocol.OFMeterModCommand;
75 import org.projectfloodlight.openflow.protocol.match.MatchField;
76 import org.projectfloodlight.openflow.types.DatapathId;
77 import org.projectfloodlight.openflow.types.U64;
78 
79 import java.util.Arrays;
80 import java.util.List;
81 import java.util.concurrent.ExecutionException;
82 import java.util.concurrent.TimeoutException;
83 import java.util.stream.Collectors;
84 
85 public class SwitchManagerTest {
86  private static final OutputCommands scheme = new ReplaceSchemeOutputCommands();
87  private static final FloodlightModuleContext context = new FloodlightModuleContext();
88  private static final long cookie = 123L;
89  private static final String cookieHex = "7B";
90  private SwitchManager switchManager;
91  private IOFSwitchService ofSwitchService;
92  private IRestApiService restApiService;
93  private IOFSwitch iofSwitch;
94  private SwitchDescription switchDescription;
95  private DatapathId dpid;
96 
97  @Before
98  public void setUp() throws FloodlightModuleException {
99  ofSwitchService = createMock(IOFSwitchService.class);
100  restApiService = createMock(IRestApiService.class);
101  iofSwitch = createMock(IOFSwitch.class);
102  switchDescription = createMock(SwitchDescription.class);
103  dpid = createMock(DatapathId.class);
104 
105  context.addService(IRestApiService.class, restApiService);
106  context.addService(IOFSwitchService.class, ofSwitchService);
107 
108  switchManager = new SwitchManager();
109  switchManager.init(context);
110  }
111 
112  @Test
113  public void installDefaultRules() throws Exception {
114  // TODO
115  }
116 
117  @Test
118  public void installIngressFlowReplaceAction() throws Exception {
119  Capture<OFFlowMod> capture = prepareForInstallTest();
120 
121  switchManager.installIngressFlow(dpid, cookieHex, cookie,
122  inputPort, outputPort, inputVlanId, transitVlanId, OutputVlanType.REPLACE, meterId);
123 
124  assertEquals(
125  scheme.ingressReplaceFlowMod(inputPort, outputPort, inputVlanId, transitVlanId, meterId, cookie),
126  capture.getValue());
127  }
128 
129  @Test
130  public void installIngressFlowPopAction() throws Exception {
131  Capture<OFFlowMod> capture = prepareForInstallTest();
132 
133  switchManager.installIngressFlow(dpid, cookieHex, cookie,
134  inputPort, outputPort, inputVlanId, transitVlanId, OutputVlanType.POP, meterId);
135 
136  assertEquals(
137  scheme.ingressPopFlowMod(inputPort, outputPort, inputVlanId, transitVlanId, meterId, cookie),
138  capture.getValue());
139  }
140 
141  @Test
142  public void installIngressFlowPushAction() throws Exception {
143  Capture<OFFlowMod> capture = prepareForInstallTest();
144 
145  switchManager.installIngressFlow(dpid, cookieHex, cookie,
146  inputPort, outputPort, 0, transitVlanId, OutputVlanType.PUSH, meterId);
147 
148  assertEquals(
149  scheme.ingressPushFlowMod(inputPort, outputPort, transitVlanId, meterId, cookie),
150  capture.getValue());
151  }
152 
153  @Test
154  public void installIngressFlowNoneAction() throws Exception {
155  Capture<OFFlowMod> capture = prepareForInstallTest();
156 
157  switchManager.installIngressFlow(dpid, cookieHex, cookie,
158  inputPort, outputPort, 0, transitVlanId, OutputVlanType.NONE, meterId);
159 
160  assertEquals(
161  scheme.ingressNoneFlowMod(inputPort, outputPort, transitVlanId, meterId, cookie),
162  capture.getValue());
163  }
164 
165  @Test
166  public void installEgressFlowNoneAction() throws Exception {
167  Capture<OFFlowMod> capture = prepareForInstallTest();
168 
169  switchManager.installEgressFlow(dpid, cookieHex, cookie,
170  inputPort, outputPort, transitVlanId, 0, OutputVlanType.NONE);
171 
172  assertEquals(
173  scheme.egressNoneFlowMod(inputPort, outputPort, transitVlanId, cookie),
174  capture.getValue());
175  }
176 
177  @Test
178  public void installEgressFlowPushAction() throws Exception {
179  Capture<OFFlowMod> capture = prepareForInstallTest();
180 
181  switchManager.installEgressFlow(dpid, cookieHex, cookie,
182  inputPort, outputPort, transitVlanId, outputVlanId, OutputVlanType.PUSH);
183 
184  assertEquals(
185  scheme.egressPushFlowMod(inputPort, outputPort, transitVlanId, outputVlanId, cookie),
186  capture.getValue());
187  }
188 
189  @Test
190  public void installEgressFlowPopAction() throws Exception {
191  Capture<OFFlowMod> capture = prepareForInstallTest();
192 
193  switchManager.installEgressFlow(dpid, cookieHex, cookie,
194  inputPort, outputPort, transitVlanId, 0, OutputVlanType.POP);
195 
196  assertEquals(
197  scheme.egressPopFlowMod(inputPort, outputPort, transitVlanId, cookie),
198  capture.getValue());
199  }
200 
201  @Test
202  public void installEgressFlowReplaceAction() throws Exception {
203  Capture<OFFlowMod> capture = prepareForInstallTest();
204 
205  switchManager.installEgressFlow(dpid, cookieHex, cookie,
206  inputPort, outputPort, transitVlanId, outputVlanId, OutputVlanType.REPLACE);
207 
208  assertEquals(
209  scheme.egressReplaceFlowMod(inputPort, outputPort, transitVlanId, outputVlanId, cookie),
210  capture.getValue());
211  }
212 
213  @Test
214  public void installTransitFlow() throws Exception {
215  Capture<OFFlowMod> capture = prepareForInstallTest();
216 
217  switchManager.installTransitFlow(dpid, cookieHex, cookie, inputPort, outputPort, transitVlanId);
218 
219  assertEquals(
220  scheme.transitFlowMod(inputPort, outputPort, transitVlanId, cookie),
221  capture.getValue());
222  }
223 
224  @Test
225  public void installOneSwitchFlowReplaceAction() throws Exception {
226  Capture<OFFlowMod> capture = prepareForInstallTest();
227 
228  switchManager.installOneSwitchFlow(dpid, cookieHex, cookie,
229  inputPort, outputPort, inputVlanId, outputVlanId, OutputVlanType.REPLACE, meterId);
230 
231  assertEquals(
232  scheme.oneSwitchReplaceFlowMod(inputPort, outputPort, inputVlanId, outputVlanId, meterId, cookie),
233  capture.getValue());
234  }
235 
236  @Test
237  public void installOneSwitchFlowPushAction() throws Exception {
238  Capture<OFFlowMod> capture = prepareForInstallTest();
239 
240  switchManager.installOneSwitchFlow(dpid, cookieHex, cookie,
241  inputPort, outputPort, 0, outputVlanId, OutputVlanType.PUSH, meterId);
242 
243  assertEquals(
244  scheme.oneSwitchPushFlowMod(inputPort, outputPort, outputVlanId, meterId, cookie),
245  capture.getValue());
246  }
247 
248  @Test
249  public void installOneSwitchFlowPopAction() throws Exception {
250  Capture<OFFlowMod> capture = prepareForInstallTest();
251 
252  switchManager.installOneSwitchFlow(dpid, cookieHex, cookie,
253  inputPort, outputPort, inputVlanId, 0, OutputVlanType.POP, meterId);
254 
255  assertEquals(
256  scheme.oneSwitchPopFlowMod(inputPort, outputPort, inputVlanId, meterId, cookie),
257  capture.getValue());
258  }
259 
260  @Test
261  public void installOneSwitchFlowNoneAction() throws Exception {
262  Capture<OFFlowMod> capture = prepareForInstallTest();
263 
264  switchManager.installOneSwitchFlow(dpid, cookieHex, cookie,
265  inputPort, outputPort, 0, 0, OutputVlanType.NONE, meterId);
266 
267  assertEquals(
268  scheme.oneSwitchNoneFlowMod(inputPort, outputPort, meterId, cookie),
269  capture.getValue());
270  }
271 
272  @Test
273  public void dumpFlowTable() throws Exception {
274  // TODO
275  }
276 
277  @Test
278  public void dumpMeters() throws Exception {
279  // TODO
280  }
281 
282  @Test
283  public void installBandwidthMeter() throws Exception {
284  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
285  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
286  expect(iofSwitch.getSwitchDescription()).andStubReturn(switchDescription);
287  expect(switchDescription.getManufacturerDescription()).andStubReturn("");
288 
289  expect(iofSwitch.write(scheme.installMeter(bandwidth, burstSize, meterId))).andReturn(true);
290  expect(iofSwitch.writeRequest(anyObject(OFBarrierRequest.class)))
291  .andReturn(Futures.immediateFuture(createMock(OFBarrierReply.class)));
292 
293  replay(ofSwitchService);
294  replay(iofSwitch);
295  replay(switchDescription);
296 
297  switchManager.installMeter(dpid, bandwidth, burstSize, meterId);
298  }
299 
300  @Test
301  public void deleteMeter() throws SwitchOperationException {
302  final Capture<OFMeterMod> capture = prepareForMeterTest();
303  switchManager.deleteMeter(dpid, meterId);
304  final OFMeterMod meterMod = capture.getValue();
305  assertEquals(meterMod.getCommand(), OFMeterModCommand.DELETE);
306  assertEquals(meterMod.getMeterId(), meterId);
307  }
308 
309  @Test
310  public void shouldDeleteAllNonDefaultRules() throws Exception {
311  // given
312  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
313  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
314 
315  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
316  VERIFICATION_UNICAST_RULE_COOKIE);
317 
318  Capture<OFFlowMod> capture = EasyMock.newCapture();
319  expect(iofSwitch.write(capture(capture))).andReturn(true);
320 
321  mockBarrierRequest();
322  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
323  expectLastCall();
324 
325  replay(ofSwitchService, iofSwitch);
326 
327  // when
328  List<Long> deletedRules = switchManager.deleteAllNonDefaultRules(dpid);
329 
330  // then
331  final OFFlowMod actual = capture.getValue();
332  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
333  assertNull(actual.getMatch().get(MatchField.IN_PORT));
334  assertNull(actual.getMatch().get(MatchField.VLAN_VID));
335  assertEquals("any", actual.getOutPort().toString());
336  assertEquals(0, actual.getInstructions().size());
337  assertEquals(cookie, actual.getCookie().getValue());
338  assertEquals(U64.NO_MASK, actual.getCookieMask());
339  assertThat(deletedRules, containsInAnyOrder(cookie));
340  }
341 
342  @Test
343  public void shouldDeleteDefaultRules() throws Exception {
344  // given
345  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
346  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
347 
348  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
349  VERIFICATION_UNICAST_RULE_COOKIE);
350 
351  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
352  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
353 
354  mockBarrierRequest();
355  mockFlowStatsRequest(cookie);
356  expectLastCall();
357 
358  replay(ofSwitchService, iofSwitch);
359 
360  // when
361  List<Long> deletedRules = switchManager.deleteDefaultRules(dpid);
362 
363  // then
364  final List<OFFlowMod> actual = capture.getValues();
365  assertEquals(3, actual.size());
366  assertThat(actual, everyItem(hasProperty("command", equalTo(OFFlowModCommand.DELETE))));
367  assertThat(actual, hasItem(hasProperty("cookie", equalTo(U64.of(DROP_RULE_COOKIE)))));
368  assertThat(actual, hasItem(hasProperty("cookie", equalTo(U64.of(VERIFICATION_BROADCAST_RULE_COOKIE)))));
369  assertThat(actual, hasItem(hasProperty("cookie", equalTo(U64.of(VERIFICATION_UNICAST_RULE_COOKIE)))));
370  assertThat(deletedRules, containsInAnyOrder(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
371  VERIFICATION_UNICAST_RULE_COOKIE));
372  }
373 
374  @Test
375  public void shouldDeleteRuleByCookie() throws Exception {
376  // given
377  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
378  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
379 
380  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
381  VERIFICATION_UNICAST_RULE_COOKIE);
382 
383  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
384  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
385 
386  mockBarrierRequest();
387  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
388  expectLastCall();
389 
390  replay(ofSwitchService, iofSwitch);
391 
392  // when
393  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder().cookie(cookie).build();
394  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
395 
396  // then
397  final OFFlowMod actual = capture.getValue();
398  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
399  assertNull(actual.getMatch().get(MatchField.IN_PORT));
400  assertNull(actual.getMatch().get(MatchField.VLAN_VID));
401  assertEquals("any", actual.getOutPort().toString());
402  assertEquals(0, actual.getInstructions().size());
403  assertEquals(cookie, actual.getCookie().getValue());
404  assertEquals(U64.NO_MASK, actual.getCookieMask());
405  assertThat(deletedRules, containsInAnyOrder(cookie));
406  }
407 
408  @Test
409  public void shouldDeleteRuleByInPort() throws Exception {
410  // given
411  final int testInPort = 11;
412 
413  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
414  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
415 
416  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
417  VERIFICATION_UNICAST_RULE_COOKIE);
418 
419  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
420  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
421 
422  mockBarrierRequest();
423  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
424  expectLastCall();
425 
426  replay(ofSwitchService, iofSwitch);
427 
428  // when
429  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder().inPort(testInPort).build();
430  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
431 
432  // then
433  final OFFlowMod actual = capture.getValue();
434  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
435  assertEquals(testInPort, actual.getMatch().get(MatchField.IN_PORT).getPortNumber());
436  assertNull(actual.getMatch().get(MatchField.VLAN_VID));
437  assertEquals("any", actual.getOutPort().toString());
438  assertEquals(0, actual.getInstructions().size());
439  assertEquals(0L, actual.getCookie().getValue());
440  assertEquals(0L, actual.getCookieMask().getValue());
441  assertThat(deletedRules, containsInAnyOrder(cookie));
442  }
443 
444  @Test
445  public void shouldDeleteRuleByInVlan() throws Exception {
446  // given
447  final short testInVlan = 101;
448 
449  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
450  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
451 
452  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
453  VERIFICATION_UNICAST_RULE_COOKIE);
454 
455  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
456  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
457 
458  mockBarrierRequest();
459  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
460  expectLastCall();
461 
462  replay(ofSwitchService, iofSwitch);
463 
464  // when
465  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder().inVlan((int) testInVlan).build();
466  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
467 
468  // then
469  final OFFlowMod actual = capture.getValue();
470  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
471  assertEquals(testInVlan, actual.getMatch().get(MatchField.VLAN_VID).getVlan());
472  assertNull(actual.getMatch().get(MatchField.IN_PORT));
473  assertEquals("any", actual.getOutPort().toString());
474  assertEquals(0, actual.getInstructions().size());
475  assertEquals(0L, actual.getCookie().getValue());
476  assertEquals(0L, actual.getCookieMask().getValue());
477  assertThat(deletedRules, containsInAnyOrder(cookie));
478  }
479 
480  @Test
481  public void shouldDeleteRuleByInPortAndVlan() throws Exception {
482  // given
483  final int testInPort = 11;
484  final short testInVlan = 101;
485 
486  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
487  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
488 
489  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
490  VERIFICATION_UNICAST_RULE_COOKIE);
491 
492  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
493  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
494 
495  mockBarrierRequest();
496  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
497  expectLastCall();
498 
499  replay(ofSwitchService, iofSwitch);
500 
501  // when
502  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder()
503  .inPort(testInPort)
504  .inVlan((int) testInVlan).build();
505  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
506 
507  // then
508  final OFFlowMod actual = capture.getValue();
509  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
510  assertEquals(testInPort, actual.getMatch().get(MatchField.IN_PORT).getPortNumber());
511  assertEquals(testInVlan, actual.getMatch().get(MatchField.VLAN_VID).getVlan());
512  assertEquals("any", actual.getOutPort().toString());
513  assertEquals(0, actual.getInstructions().size());
514  assertEquals(0L, actual.getCookie().getValue());
515  assertEquals(0L, actual.getCookieMask().getValue());
516  assertThat(deletedRules, containsInAnyOrder(cookie));
517  }
518 
519  @Test
520  public void shouldDeleteRuleByPriority() throws Exception {
521  // given
522  final int testPriority = 999;
523 
524  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
525  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
526 
527  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
528  VERIFICATION_UNICAST_RULE_COOKIE);
529 
530  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
531  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
532 
533  mockBarrierRequest();
534  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
535  expectLastCall();
536 
537  replay(ofSwitchService, iofSwitch);
538 
539  // when
540  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder().priority(testPriority).build();
541  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
542 
543  // then
544  final OFFlowMod actual = capture.getValue();
545  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
546  assertNull(actual.getMatch().get(MatchField.IN_PORT));
547  assertNull(actual.getMatch().get(MatchField.VLAN_VID));
548  assertEquals("any", actual.getOutPort().toString());
549  assertEquals(0, actual.getInstructions().size());
550  assertEquals(0L, actual.getCookie().getValue());
551  assertEquals(0L, actual.getCookieMask().getValue());
552  assertEquals(testPriority, actual.getPriority());
553  assertThat(deletedRules, containsInAnyOrder(cookie));
554  }
555 
556  @Test
557  public void shouldDeleteRuleByInPortVlanAndPriority() throws Exception {
558  // given
559  final int testInPort = 11;
560  final short testInVlan = 101;
561  final int testPriority = 999;
562 
563  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
564  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
565 
566  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
567  VERIFICATION_UNICAST_RULE_COOKIE);
568 
569  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
570  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
571 
572  mockBarrierRequest();
573  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
574  expectLastCall();
575 
576  replay(ofSwitchService, iofSwitch);
577 
578  // when
579  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder()
580  .inPort(testInPort)
581  .inVlan((int) testInVlan)
582  .priority(testPriority).build();
583  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
584 
585  // then
586  final OFFlowMod actual = capture.getValue();
587  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
588  assertEquals(testInPort, actual.getMatch().get(MatchField.IN_PORT).getPortNumber());
589  assertEquals(testInVlan, actual.getMatch().get(MatchField.VLAN_VID).getVlan());
590  assertEquals("any", actual.getOutPort().toString());
591  assertEquals(0, actual.getInstructions().size());
592  assertEquals(0L, actual.getCookie().getValue());
593  assertEquals(0L, actual.getCookieMask().getValue());
594  assertEquals(testPriority, actual.getPriority());
595  assertThat(deletedRules, containsInAnyOrder(cookie));
596  }
597 
598  @Test
599  public void shouldDeleteRuleByOutPort() throws Exception {
600  // given
601  final int testOutPort = 21;
602 
603  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
604  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
605 
606  mockFlowStatsRequest(cookie, DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE,
607  VERIFICATION_UNICAST_RULE_COOKIE);
608 
609  Capture<OFFlowMod> capture = EasyMock.newCapture(CaptureType.ALL);
610  expect(iofSwitch.write(capture(capture))).andReturn(true).times(3);
611 
612  mockBarrierRequest();
613  mockFlowStatsRequest(DROP_RULE_COOKIE, VERIFICATION_BROADCAST_RULE_COOKIE, VERIFICATION_UNICAST_RULE_COOKIE);
614  expectLastCall();
615 
616  replay(ofSwitchService, iofSwitch);
617 
618  // when
619  DeleteRulesCriteria criteria = DeleteRulesCriteria.builder().outPort(testOutPort).build();
620  List<Long> deletedRules = switchManager.deleteRulesByCriteria(dpid, criteria);
621 
622  // then
623  final OFFlowMod actual = capture.getValue();
624  assertEquals(OFFlowModCommand.DELETE, actual.getCommand());
625  assertNull(actual.getMatch().get(MatchField.IN_PORT));
626  assertNull(actual.getMatch().get(MatchField.VLAN_VID));
627  assertEquals(testOutPort, actual.getOutPort().getPortNumber());
628  assertEquals(0L, actual.getCookie().getValue());
629  assertEquals(0L, actual.getCookieMask().getValue());
630  assertThat(deletedRules, containsInAnyOrder(cookie));
631  }
632 
633  private void mockBarrierRequest() throws InterruptedException, ExecutionException, TimeoutException {
634  OFBarrierReply ofBarrierReply = mock(OFBarrierReply.class);
635 
636  ListenableFuture<OFBarrierReply> ofBarrierFuture = mock(ListenableFuture.class);
637  expect(ofBarrierFuture.get(anyLong(), anyObject())).andStubReturn(ofBarrierReply);
638  replay(ofBarrierFuture);
639 
640  expect(iofSwitch.writeRequest(anyObject(OFBarrierRequest.class))).andReturn(ofBarrierFuture);
641  }
642 
643  private void mockFlowStatsRequest(Long... cookies)
644  throws InterruptedException, ExecutionException, TimeoutException {
645  List<OFFlowStatsEntry> ofFlowStatsEntries = Arrays.stream(cookies)
646  .map(cookie -> {
647  OFFlowStatsEntry ofFlowStatsEntry = mock(OFFlowStatsEntry.class);
648  expect(ofFlowStatsEntry.getCookie()).andStubReturn(U64.of(cookie));
649  replay(ofFlowStatsEntry);
650  return ofFlowStatsEntry;
651  })
652  .collect(Collectors.toList());
653 
654  OFFlowStatsEntry ofFlowStatsEntry = mock(OFFlowStatsEntry.class);
655  expect(ofFlowStatsEntry.getCookie()).andStubReturn(U64.of(cookie));
656  replay(ofFlowStatsEntry);
657 
658  OFFlowStatsReply ofFlowStatsReply = mock(OFFlowStatsReply.class);
659  expect(ofFlowStatsReply.getXid()).andReturn(0L);
660  expect(ofFlowStatsReply.getFlags()).andReturn(emptySet());
661  expect(ofFlowStatsReply.getEntries()).andStubReturn(ofFlowStatsEntries);
662  replay(ofFlowStatsReply);
663 
664  ListenableFuture<List<OFFlowStatsReply>> ofStatsFuture = mock(ListenableFuture.class);
665  expect(ofStatsFuture.get(anyLong(), anyObject())).andStubReturn(singletonList(ofFlowStatsReply));
666  replay(ofStatsFuture);
667 
668  expect(iofSwitch.writeStatsRequest(anyObject(OFFlowStatsRequest.class))).andReturn(ofStatsFuture);
669  }
670 
671  private Capture<OFFlowMod> prepareForInstallTest() {
672  Capture<OFFlowMod> capture = EasyMock.newCapture();
673 
674  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
675  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
676  expect(iofSwitch.getSwitchDescription()).andStubReturn(switchDescription);
677  expect(switchDescription.getManufacturerDescription()).andStubReturn("");
678  expect(iofSwitch.write(capture(capture))).andReturn(true);
679  expectLastCall();
680 
681  replay(ofSwitchService);
682  replay(iofSwitch);
683  replay(switchDescription);
684 
685  return capture;
686  }
687 
688  private Capture<OFMeterMod> prepareForMeterTest() {
689  Capture<OFMeterMod> capture = EasyMock.newCapture();
690 
691  expect(ofSwitchService.getSwitch(dpid)).andStubReturn(iofSwitch);
692  expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory);
693  expect(iofSwitch.getSwitchDescription()).andStubReturn(switchDescription);
694  expect(switchDescription.getManufacturerDescription()).andStubReturn("");
695  expect(iofSwitch.write(capture(capture))).andReturn(true);
696  expectLastCall();
697 
698  replay(ofSwitchService);
699  replay(iofSwitch);
700  replay(switchDescription);
701 
702  return capture;
703  }
704 }
long installMeter(final DatapathId dpid, final long bandwidth, final long burstSize, final long meterId)
default OFFlowAdd ingressPopFlowMod(int inputPort, int outputPort, int inputVlan, int transitVlan, long meterId, long cookie)
OFFlowAdd egressPushFlowMod(int inputPort, int outputPort, int transitVlanId, int outputVlan, long cookie)
long installEgressFlow(final DatapathId dpid, String flowId, final Long cookie, final int inputPort, final int outputPort, final int transitVlanId, final int outputVlanId, final OutputVlanType outputVlanType)
default OFFlowAdd oneSwitchReplaceFlowMod(int inputPort, int outputPort, int inputVlan, int outputVlan, long meterId, long cookie)
default OFMeterMod installMeter(long bandwidth, long burstSize, long meterId)
void init(FloodlightModuleContext context)
long installIngressFlow(final DatapathId dpid, final String flowId, final Long cookie, final int inputPort, final int outputPort, final int inputVlanId, final int transitVlanId, final OutputVlanType outputVlanType, final long meterId)
OFFlowAdd egressPopFlowMod(int inputPort, int outputPort, int transitVlanId, long cookie)
List< Long > deleteRulesByCriteria(final DatapathId dpid, DeleteRulesCriteria... criteria)
default OFFlowAdd ingressNoneFlowMod(int inputPort, int outputPort, int transitVlan, long meterId, long cookie)
List< Long > deleteDefaultRules(final DatapathId dpid)
List< Long > deleteAllNonDefaultRules(final DatapathId dpid)
default OFFlowAdd oneSwitchPushFlowMod(int inputPort, int outputPort, int outputVlan, long meterId, long cookie)
long installTransitFlow(final DatapathId dpid, final String flowId, final Long cookie, final int inputPort, final int outputPort, final int transitVlanId)
default OFFlowAdd oneSwitchNoneFlowMod(int inputPort, int outputPort, long meterId, long cookie)
default OFFlowAdd transitFlowMod(int inputPort, int outputPort, int transitVlan, long cookie)
default OFFlowAdd oneSwitchPopFlowMod(int inputPort, int outputPort, int inputVlan, long meterId, long cookie)
default OFFlowAdd ingressReplaceFlowMod(int inputPort, int outputPort, int inputVlan, int transitVlan, long meterId, long cookie)
default OFFlowAdd ingressPushFlowMod(int inputPort, int outputPort, int transitVlan, long meterId, long cookie)
long installOneSwitchFlow(final DatapathId dpid, final String flowId, final Long cookie, final int inputPort, final int outputPort, final int inputVlanId, final int outputVlanId, final OutputVlanType outputVlanType, final long meterId)
OFFlowAdd egressNoneFlowMod(int inputPort, int outputPort, int transitVlanId, long cookie)
long deleteMeter(final DatapathId dpid, final long meterId)
net
Definition: plan-b.py:46
OFFlowAdd egressReplaceFlowMod(int inputPort, int outputPort, int inputVlan, int outputVlan, long cookie)