default OFFlowAdd transitFlowMod(int inputPort, int outputPort, int transitVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(singletonList( ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
default OFFlowAdd oneSwitchNoneFlowMod(int inputPort, int outputPort, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(singletonList( ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
default OFFlowAdd oneSwitchPopFlowMod(int inputPort, int outputPort, int inputVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(inputVlan)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().popVlan(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd egressPopFlowMod(int inputPort, int outputPort, int transitVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().popVlan(), ofFactory.actions().popVlan(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd egressNoneFlowMod(int inputPort, int outputPort, int transitVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().popVlan(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd buildFlowAdd() { Match match = buildMatch(); List<OFAction> actions = buildActions(); long cookie = flowRule().id().value(); OFFlowAdd fm = factory().buildFlowAdd() .setXid(xid) .setCookie(U64.of(cookie)) .setBufferId(OFBufferId.NO_BUFFER) .setActions(actions) .setMatch(match) .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) .setPriority(flowRule().priority()) .build(); return fm; }
@Override public OFFlowAdd buildFlowAdd() { Match match = buildMatch(); List<OFAction> actions = buildActions(); long cookie = flowRule().id().value(); OFFlowAdd.Builder fm = factory().buildFlowAdd() .setXid(xid) .setCookie(U64.of(cookie)) .setBufferId(OFBufferId.NO_BUFFER) .setActions(actions) .setMatch(match) .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) .setPriority(flowRule().priority()); if (flowRule().timeout() != 0) { fm.setIdleTimeout(flowRule().timeout()); } else { fm.setHardTimeout(flowRule().hardTimeout()); } return fm.build(); }
@Override public OFFlowAdd buildFlowAdd() { Match match = buildMatch(); List<OFAction> actions = buildActions(); long cookie = flowRule().id().value(); OFFlowAdd fm = factory().buildFlowAdd() .setXid(xid) .setCookie(U64.of(cookie)) .setBufferId(OFBufferId.NO_BUFFER) .setActions(actions) .setMatch(match) .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) .setPriority(flowRule().priority()) .setHardTimeout(flowRule().hardTimeout()) .build(); return fm; }
@Test public void testFlowAdd() { OFFlowAdd flowAdd = OFFactories.getFactory(OFVersion.OF_13).buildFlowAdd() .setXid(0x12345678) .setCookie(U64.parseHex("FEDCBA9876543210")) .setCookieMask(U64.parseHex("FF00FF00FF00FF00")) .setTableId(TableId.of(3)) .setIdleTimeout(5) .setHardTimeout(10) .setPriority(6000) .build(); ByteBuf bb = Unpooled.buffer(); flowAdd.writeTo(bb); byte[] flowAddBytes = new byte[bb.readableBytes()]; bb.readBytes(flowAddBytes); OFErrorCauseData emptyCause = OFErrorCauseData.of(flowAddBytes, OFVersion.OF_13); assertThat(emptyCause.getData(), equalTo(flowAddBytes)); assertThat(emptyCause.getParsedMessage().isPresent(), equalTo(true)); assertThat(emptyCause.toString(), Matchers.containsString("OFFlowAdd")); assertThat(emptyCause.toString(), Matchers.containsString("idleTimeout=5")); }
default OFFlowAdd oneSwitchReplaceFlowMod(int inputPort, int outputPort, int inputVlan, int outputVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(inputVlan)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(outputVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
default OFFlowAdd oneSwitchPushFlowMod(int inputPort, int outputPort, int outputVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildPushVlan() .setEthertype(EthType.of(ETH_TYPE)) .build(), ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(outputVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd ingressMatchVlanIdFlowMod(int inputPort, int outputPort, int inputVlan, int transitVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(inputVlan)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(transitVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd egressPushFlowMod(int inputPort, int outputPort, int transitVlan, int outputVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(outputVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd ingressMatchVlanIdFlowMod(int inputPort, int outputPort, int inputVlan, int transitVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(inputVlan)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildPushVlan() .setEthertype(EthType.of(ETH_TYPE)) .build(), ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(transitVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd ingressNoMatchVlanIdFlowMod(int inputPort, int outputPort, int transitVlan, long meterId, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .build()) .setInstructions(Arrays.asList( ofFactory.instructions().buildMeter().setMeterId(meterId).build(), ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().buildPushVlan() .setEthertype(EthType.of(ETH_TYPE)) .build(), ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(transitVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd egressPushFlowMod(int inputPort, int outputPort, int transitVlan, int outputVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(transitVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().popVlan(), ofFactory.actions().buildPushVlan() .setEthertype(EthType.of(ETH_TYPE)) .build(), ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(outputVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Override public OFFlowAdd egressReplaceFlowMod(int inputPort, int outputPort, int inputVlan, int outputVlan, long cookie) { return ofFactory.buildFlowAdd() .setCookie(U64.of(cookie & FLOW_COOKIE_MASK)) .setHardTimeout(FlowModUtils.INFINITE_TIMEOUT) .setIdleTimeout(FlowModUtils.INFINITE_TIMEOUT) .setBufferId(OFBufferId.NO_BUFFER) .setPriority(FlowModUtils.PRIORITY_VERY_HIGH) .setMatch(ofFactory.buildMatch() .setExact(MatchField.IN_PORT, OFPort.of(inputPort)) .setExact(MatchField.VLAN_VID, OFVlanVidMatch.ofVlan(inputVlan)) .build()) .setInstructions(singletonList( ofFactory.instructions().applyActions(Arrays.asList( ofFactory.actions().popVlan(), ofFactory.actions().buildSetField() .setField(ofFactory.oxms().buildVlanVid() .setValue(OFVlanVidMatch.ofVlan(outputVlan)) .build()) .build(), ofFactory.actions().buildOutput() .setMaxLen(0xFFFFFFFF) .setPort(OFPort.of(outputPort)) .build())) .createBuilder() .build())) .setXid(0L) .build(); }
@Test public void installOneSwitchNoneFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_one_switch_none_flow.json"), Charsets.UTF_8); InstallOneSwitchFlow data = (InstallOneSwitchFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.oneSwitchNoneFlowMod(data.getInputPort(), data.getOutputPort(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installOneSwitchReplaceFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_one_switch_replace_flow.json"), Charsets.UTF_8); InstallOneSwitchFlow data = (InstallOneSwitchFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.oneSwitchReplaceFlowMod(data.getInputPort(), data.getOutputPort(), data.getInputVlanId(), data.getOutputVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installOneSwitchPushFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_one_switch_push_flow.json"), Charsets.UTF_8); InstallOneSwitchFlow data = (InstallOneSwitchFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.oneSwitchPushFlowMod(data.getInputPort(), data.getOutputPort(), data.getOutputVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installOneSwitchPopFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_one_switch_pop_flow.json"), Charsets.UTF_8); InstallOneSwitchFlow data = (InstallOneSwitchFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.oneSwitchPopFlowMod(data.getInputPort(), data.getOutputPort(), data.getInputVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installIngressNoneFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_ingress_none_flow.json"), Charsets.UTF_8); InstallIngressFlow data = (InstallIngressFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.ingressNoneFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installIngressReplaceFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_ingress_replace_flow.json"), Charsets.UTF_8); InstallIngressFlow data = (InstallIngressFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.ingressReplaceFlowMod(data.getInputPort(), data.getOutputPort(), data.getInputVlanId(), data.getTransitVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installIngressPushFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_ingress_push_flow.json"), Charsets.UTF_8); InstallIngressFlow data = (InstallIngressFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.ingressPushFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installIngressPopFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_ingress_pop_flow.json"), Charsets.UTF_8); InstallIngressFlow data = (InstallIngressFlow) prepareData(value); OFMeterMod meterCommand = scheme.installMeter(data.getBandwidth(), 1024, data.getMeterId()); OFFlowAdd flowCommand = scheme.ingressPopFlowMod(data.getInputPort(), data.getOutputPort(), data.getInputVlanId(), data.getTransitVlanId(), data.getMeterId(), 123L); runTest(value, flowCommand, meterCommand, null, null); }
@Test public void installEgressNoneFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_egress_none_flow.json"), Charsets.UTF_8); InstallEgressFlow data = (InstallEgressFlow) prepareData(value); OFFlowAdd flowCommand = scheme.egressNoneFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), 123L); runTest(value, flowCommand, null, null, null); }
@Test public void installEgressReplaceFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_egress_replace_flow.json"), Charsets.UTF_8); InstallEgressFlow data = (InstallEgressFlow) prepareData(value); OFFlowAdd flowCommand = scheme.egressReplaceFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), data.getOutputVlanId(), 123L); runTest(value, flowCommand, null, null, null); }
@Test public void installEgressPushFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_egress_push_flow.json"), Charsets.UTF_8); InstallEgressFlow data = (InstallEgressFlow) prepareData(value); OFFlowAdd flowCommand = scheme.egressPushFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), data.getOutputVlanId(), 123L); runTest(value, flowCommand, null, null, null); }
@Test public void installEgressPopFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_egress_pop_flow.json"), Charsets.UTF_8); InstallEgressFlow data = (InstallEgressFlow) prepareData(value); OFFlowAdd flowCommand = scheme.egressPopFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), 123L); runTest(value, flowCommand, null, null, null); }
@Test public void installTransitFlow() throws IOException, InterruptedException { String value = Resources.toString(getClass().getResource("/install_transit_flow.json"), Charsets.UTF_8); InstallTransitFlow data = (InstallTransitFlow) prepareData(value); OFFlowAdd flowCommand = scheme.transitFlowMod(data.getInputPort(), data.getOutputPort(), data.getTransitVlanId(), 123L); runTest(value, flowCommand, null, null, null); }
/** * Runs test case. * * @param value data string from json resource file * @param flowCommand OFFlowAdd instance to compare result with * @throws InterruptedException if test was interrupted during run */ private void runTest(final String value, final OFFlowAdd flowCommand, final OFMeterMod meterCommand, final OFFlowAdd reverseFlowCommand, final OFMeterMod reverseMeterCommand) throws InterruptedException { // construct kafka message ConsumerRecord<String, String> record = new ConsumerRecord<>("", 0, 0, "", value); // create parser instance KafkaMessageCollector.ParseRecord parseRecord = collector.new ParseRecord(record); // init test mocks Capture<OFFlowAdd> flowAddCapture = flowCommand == null ? null : newCapture(CaptureType.ALL); Capture<OFMeterMod> meterAddCapture = meterCommand == null ? null : newCapture(CaptureType.ALL); prepareMocks(flowAddCapture, meterAddCapture, reverseFlowCommand != null, reverseMeterCommand != null); // run parser and wait for termination or timeout parseRecordExecutor.execute(parseRecord); parseRecordExecutor.shutdown(); parseRecordExecutor.awaitTermination(10, TimeUnit.SECONDS); // verify results if (meterCommand != null) { assertEquals(meterCommand, meterAddCapture.getValues().get(0)); if (reverseMeterCommand != null) { assertEquals(reverseMeterCommand, meterAddCapture.getValues().get(1)); } } if (flowCommand != null) { assertEquals(flowCommand, flowAddCapture.getValues().get(0)); if (reverseFlowCommand != null) { assertEquals(reverseFlowCommand, flowAddCapture.getValues().get(1)); } } }
/** * Prepares test mocks for run. * * @param flowAddCapture Capture for FlowAdd command * @param meterAddCapture Capture for MeterMod<Add> command */ private void prepareMocks(Capture<OFFlowAdd> flowAddCapture, Capture<OFMeterMod> meterAddCapture, boolean needCheckReverseFlow, boolean needCheckReverseMeter) { IOFSwitch iofSwitch = createMock(IOFSwitch.class); expect(ofSwitchService.getSwitch(anyObject(DatapathId.class))).andStubReturn(iofSwitch); expect(iofSwitch.getOFFactory()).andStubReturn(ofFactory); expect(iofSwitch.getSwitchDescription()).andStubReturn(switchDescription); if (meterAddCapture != null) { expect(iofSwitch.write(capture(meterAddCapture))).andReturn(true); if (flowAddCapture != null) { expect(iofSwitch.write(capture(flowAddCapture))).andReturn(true); } if (needCheckReverseMeter) { expect(iofSwitch.write(capture(meterAddCapture))).andReturn(true); } if (needCheckReverseFlow) { expect(iofSwitch.write(capture(flowAddCapture))).andReturn(true); } } else if (flowAddCapture != null) { expect(iofSwitch.write(capture(flowAddCapture))).andReturn(true).times(needCheckReverseFlow ? 2 : 1); } replay(ofSwitchService); replay(iofSwitch); }
@Override public void startDriverHandshake() { if (factory().getVersion() == OFVersion.OF_10) { OFFlowAdd.Builder fmBuilder = factory().buildFlowAdd(); fmBuilder.setPriority(LOWEST_PRIORITY); sendHandshakeMessage(fmBuilder.build()); } }
private OFMessage createHubFlowMod(IOFSwitch sw, OFMessage msg) { OFPacketIn pi = (OFPacketIn) msg; OFFlowAdd.Builder fmb = sw.getOFFactory().buildFlowAdd(); fmb.setBufferId(pi.getBufferId()) .setXid(pi.getXid()); // set actions OFActionOutput.Builder actionBuilder = sw.getOFFactory().actions().buildOutput(); actionBuilder.setPort(OFPort.FLOOD); fmb.setActions(Collections.singletonList((OFAction) actionBuilder.build())); return fmb.build(); }
public static OFFlowAdd toFlowAdd(OFFlowMod fm) { OFVersion version = fm.getVersion(); OFFlowAdd.Builder b = OFFactories.getFactory(version).buildFlowAdd(); if (b.getVersion().compareTo(OFVersion.OF_10) == 0) { return b.setActions(fm.getActions()) .setBufferId(fm.getBufferId()) .setCookie(fm.getCookie()) // cookie-mask not supported .setFlags(fm.getFlags()) .setHardTimeout(fm.getHardTimeout()) .setIdleTimeout(fm.getIdleTimeout()) // instructions not supported .setMatch(fm.getMatch()) // out-group not supported .setOutPort(fm.getOutPort()) .setPriority(fm.getPriority()) // table-id not supported .setXid(fm.getXid()) .build(); } else { return b.setActions(fm.getActions()) .setBufferId(fm.getBufferId()) .setCookie(fm.getCookie()) .setCookieMask(fm.getCookieMask()) // added in OF1.1 .setFlags(fm.getFlags()) .setHardTimeout(fm.getHardTimeout()) .setIdleTimeout(fm.getIdleTimeout()) .setInstructions(fm.getInstructions()) // added in OF1.1 .setMatch(fm.getMatch()) .setOutGroup(fm.getOutGroup()) // added in OF1.1 .setOutPort(fm.getOutPort()) .setPriority(fm.getPriority()) .setTableId(fm.getTableId()) .setXid(fm.getXid()) .build(); } }
@Override public void switchAdded(DatapathId dpid) { /* Insert static flows on all ports of the switch to redirect * DHCP client --> DHCP DHCPServer traffic to the controller. * DHCP client's operate on UDP port 67 */ IOFSwitch sw = switchService.getSwitch(dpid); //fix concurrency flaw if (sw == null){ return; } OFFlowAdd.Builder flow = sw.getOFFactory().buildFlowAdd(); Match.Builder match = sw.getOFFactory().buildMatch(); ArrayList<OFAction> actionList = new ArrayList<OFAction>(); OFActionOutput.Builder action = sw.getOFFactory().actions().buildOutput(); for (OFPortDesc port : sw.getPorts()) { match.setExact(MatchField.IN_PORT, port.getPortNo()); match.setExact(MatchField.ETH_TYPE, EthType.IPv4); match.setExact(MatchField.IP_PROTO, IpProtocol.UDP); match.setExact(MatchField.UDP_SRC, UDP.DHCP_CLIENT_PORT); action.setMaxLen(0xffFFffFF); action.setPort(OFPort.CONTROLLER); actionList.add(action.build()); flow.setBufferId(OFBufferId.NO_BUFFER); flow.setHardTimeout(0); flow.setIdleTimeout(0); flow.setOutPort(OFPort.CONTROLLER); flow.setActions(actionList); flow.setMatch(match.build()); flow.setPriority(32767); sfp.addFlow("dhcp-port---" + port.getPortNo().getPortNumber() + "---(" + port.getName() + ")", flow.build(), sw.getId()); } }
@Test public void testMasterSlaveWrites() { OFFactory factory = OFFactories.getFactory(OFVersion.OF_13); OFFlowAdd fa = factory.buildFlowAdd().build(); OFFlowStatsRequest fsr = factory.buildFlowStatsRequest().build(); List<OFMessage> msgList = new ArrayList<OFMessage>(); msgList.add(fa); msgList.add(fsr); reset(switchManager); expect(switchManager.isCategoryRegistered(LogicalOFMessageCategory.MAIN)).andReturn(true).times(6); switchManager.handleOutgoingMessage(sw, fa); expectLastCall().times(2); switchManager.handleOutgoingMessage(sw, fsr); expectLastCall().times(4); replay(switchManager); /* test master -- both messages should be written */ sw.setControllerRole(OFControllerRole.ROLE_MASTER); assertTrue(sw.write(fa)); assertTrue(sw.write(fsr)); assertEquals(Collections.<OFMessage>emptyList(), sw.write(msgList)); /* test slave -- flow-add (mod op) should fail each time; flow stats (read op) should pass */ sw.setControllerRole(OFControllerRole.ROLE_SLAVE); assertFalse(sw.write(fa)); /* flow-add should be stopped (mod op) */ assertTrue(sw.write(fsr)); /* stats request makes it (read op) */ assertEquals(Collections.<OFMessage>singletonList(fa), sw.write(msgList)); /* return bad flow-add */ }