Open Kilda Java Documentation
SwitchManagerOF12Test.java
Go to the documentation of this file.
1 package org.openkilda.floodlight.switchmanager;
2 
3 import static java.util.Collections.singletonList;
4 import static org.easymock.EasyMock.capture;
5 import static org.easymock.EasyMock.createMock;
6 import static org.easymock.EasyMock.expect;
7 import static org.junit.Assert.assertEquals;
9 
10 import net.floodlightcontroller.core.IOFSwitch;
11 import net.floodlightcontroller.core.internal.IOFSwitchService;
12 import net.floodlightcontroller.core.module.FloodlightModuleContext;
13 import net.floodlightcontroller.core.module.FloodlightModuleException;
14 import org.easymock.Capture;
15 import org.easymock.EasyMock;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
20 import org.projectfloodlight.openflow.protocol.OFFactory;
21 import org.projectfloodlight.openflow.protocol.OFFlowMod;
22 import org.projectfloodlight.openflow.protocol.match.MatchField;
23 import org.projectfloodlight.openflow.types.DatapathId;
24 import org.projectfloodlight.openflow.types.OFPort;
25 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
26 import org.projectfloodlight.openflow.types.U64;
27 
28 public class SwitchManagerOF12Test {
29  private static final FloodlightModuleContext context = new FloodlightModuleContext();
30  private static IOFSwitchService switchService = createMock(IOFSwitchService.class);
31 
32  private final OFFactory ofFactory = new OFFactoryVer12Mock();
33  private final DatapathId switchDpId = DatapathId.of(0xdeadbeaf00000001L);
34  private final long commonFlowCookie = 0x77AA55AA0001L;
35  private IOFSwitch ofSwitch = createMock(IOFSwitch.class);
36 
37  private SwitchManager switchManager;
38 
39  @BeforeClass
40  public static void setUpClass() {
41  context.addService(IOFSwitchService.class, switchService);
42  }
43 
44  @Before
45  public void setUp() throws FloodlightModuleException {
46  EasyMock.reset(switchService);
47 
48  switchManager = new SwitchManager();
49  switchManager.init(context);
50  }
51 
52  @Test
53  public void installTransitFlow() throws Exception {
54  Capture<OFFlowMod> capture = prepareForInstallFlowOperation();
55 
56  String flowId = "test-transit-flow-rule";
57  int inputPort = 2;
58  int outputPort = 4;
59  int transitVlanId = 512;
60  switchManager.installTransitFlow(switchDpId, flowId, commonFlowCookie, inputPort, outputPort, transitVlanId);
61 
62  OFFactory referenceOfFactory = new OFFactoryVer12Mock();
63  OFFlowMod expected = referenceOfFactory.buildFlowAdd()
64  .setCookie(U64.of(commonFlowCookie).applyMask(U64.of(SwitchManager.FLOW_COOKIE_MASK)))
65  .setPriority(DEFAULT_RULE_PRIORITY)
66  .setMatch(referenceOfFactory.buildMatch()
67  .setExact(MatchField.IN_PORT, OFPort.of(inputPort))
68  .setMasked(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlanId), OFVlanVidMatch.ofRawVid(
69  (short) 0x0FFF))
70  .build())
71  .setInstructions(singletonList(
72  referenceOfFactory.instructions().applyActions(singletonList(
73  referenceOfFactory.actions().buildOutput()
74  .setMaxLen(0xFFFFFFFF)
75  .setPort(OFPort.of(outputPort))
76  .build()))
77  .createBuilder()
78  .build()))
79  .build();
80 
81  assertEquals(expected, capture.getValue());
82  }
83 
84  private Capture<OFFlowMod> prepareForInstallFlowOperation() {
85  Capture<OFFlowMod> capture = EasyMock.newCapture();
86 
87  expect(switchService.getSwitch(switchDpId)).andStubReturn(ofSwitch);
88  expect(ofSwitch.getOFFactory()).andStubReturn(ofFactory);
89  expect(ofSwitch.write(capture(capture))).andReturn(true);
90  EasyMock.expectLastCall();
91 
92  EasyMock.replay(switchService);
93  EasyMock.replay(ofSwitch);
94 
95  return capture;
96  }
97 }
void init(FloodlightModuleContext context)
long installTransitFlow(final DatapathId dpid, final String flowId, final Long cookie, final int inputPort, final int outputPort, final int transitVlanId)
def build()
Definition: plan-e.py:73
net
Definition: plan-b.py:46