Java Code Examples for org.onlab.util.ImmutableByteSequence#copyFrom()

The following examples show how to use org.onlab.util.ImmutableByteSequence#copyFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected PiAction decode(
        P4RuntimeOuterClass.Action message, Object ignored,
        PiPipeconf pipeconf, P4InfoBrowser browser)
        throws P4InfoBrowser.NotFoundException {
    final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
            browser.actionParams(message.getActionId());
    final String actionName = browser.actions()
            .getById(message.getActionId())
            .getPreamble().getName();
    final PiAction.Builder builder = PiAction.builder()
            .withId(PiActionId.of(actionName));
    for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
        final String paramName = paramInfo.getById(p.getParamId()).getName();
        final ImmutableByteSequence value = ImmutableByteSequence.copyFrom(
                p.getValue().toByteArray());
        builder.withParameter(new PiActionParam(PiActionParamId.of(paramName), value));
    }
    return builder.build();
}
 
Example 2
Source File: FabricInterpreterTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Map treatment to push_internal_vlan action.
 */
@Test
public void testFilteringTreatmentPermitWithInternalVlan() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .pushVlan()
            .setVlanId(VLAN_100)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(treatment,
                                                     FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
    PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID,
                                            ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
            .withParameter(param)
            .build();

    assertEquals(expectedAction, mappedAction);
}
 
Example 3
Source File: InstructionCodecTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the encoding of protocol-independent instructions.
 */
@Test
public void piInstructionEncodingTest() {
    PiActionId actionId = PiActionId.of("set_egress_port");
    PiActionParamId actionParamId = PiActionParamId.of("port");
    PiActionParam actionParam = new PiActionParam(actionParamId, ImmutableByteSequence.copyFrom(10));
    PiTableAction action = PiAction.builder().withId(actionId).withParameter(actionParam).build();
    final PiInstruction actionInstruction = Instructions.piTableAction(action);
    final ObjectNode actionInstructionJson =
            instructionCodec.encode(actionInstruction, context);
    assertThat(actionInstructionJson, matchesInstruction(actionInstruction));

    PiTableAction actionGroupId = PiActionProfileGroupId.of(10);
    final PiInstruction actionGroupIdInstruction = Instructions.piTableAction(actionGroupId);
    final ObjectNode actionGroupIdInstructionJson =
            instructionCodec.encode(actionGroupIdInstruction, context);
    assertThat(actionGroupIdInstructionJson, matchesInstruction(actionGroupIdInstruction));

    PiTableAction actionProfileMemberId = PiActionProfileMemberId.of(10);
    final PiInstruction actionProfileMemberIdInstruction = Instructions.piTableAction(actionProfileMemberId);
    final ObjectNode actionProfileMemberIdInstructionJson =
            instructionCodec.encode(actionProfileMemberIdInstruction, context);
    assertThat(actionProfileMemberIdInstructionJson, matchesInstruction(actionProfileMemberIdInstruction));
}
 
Example 4
Source File: P4RuntimeGroupTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private static PiActionProfileMember outputMember(short portNum) {
    PiActionParam param = new PiActionParam(PORT_PARAM_ID,
                                            ImmutableByteSequence.copyFrom(portNum));
    PiAction piAction = PiAction.builder()
            .withId(EGRESS_PORT_ACTION_ID)
            .withParameter(param).build();

    return PiActionProfileMember.builder()
            .forActionProfile(ACT_PROF_ID)
            .withAction(piAction)
            .withId(PiActionProfileMemberId.of(BASE_MEM_ID + portNum))
            .build();
}
 
Example 5
Source File: FabricFilteringPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private Collection<FlowRule> buildExpectedFwdClassifierRule(PortNumber inPort,
                                                            MacAddress dstMac,
                                                            MacAddress dstMacMask,
                                                            short ethType,
                                                            byte fwdClass) {
    PiActionParam classParam = new PiActionParam(FabricConstants.FWD_TYPE,
                                                 ImmutableByteSequence.copyFrom(fwdClass));
    PiAction fwdClassifierAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE)
            .withParameter(classParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(fwdClassifierAction)
            .build();

    TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
            .matchInPort(inPort);
    if (dstMacMask != null) {
        sbuilder.matchEthDstMasked(dstMac, dstMacMask);
    } else {
        sbuilder.matchEthDstMasked(dstMac, MacAddress.EXACT_MASK);
    }
    // Special case for MPLS UNICAST forwarding, need to build 2 rules for MPLS+IPv4 and MPLS+IPv6
    if (ethType == Ethernet.MPLS_UNICAST) {
        return buildExpectedFwdClassifierRulesMpls(fwdClassifierAction, treatment, sbuilder);
    }
    sbuilder.matchPi(PiCriterion.builder()
                             .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ethType)
                             .build());
    TrafficSelector selector = sbuilder.build();
    return List.of(DefaultFlowRule.builder()
                           .withPriority(PRIORITY)
                           .withSelector(selector)
                           .withTreatment(treatment)
                           .fromApp(APP_ID)
                           .forDevice(DEVICE_ID)
                           .makePermanent()
                           .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
                           .build());
}
 
Example 6
Source File: IntProgrammableImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init() {
    if (!setupBehaviour()) {
        return false;
    }

    PiActionParam transitIdParam = new PiActionParam(
            IntConstants.SWITCH_ID,
            ImmutableByteSequence.copyFrom(
                    Integer.parseInt(deviceId.toString().substring(
                            deviceId.toString().length() - 2))));
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(PiCriterion.builder().matchExact(
                    IntConstants.HDR_INT_IS_VALID, (byte) 0x01)
                     .build())
            .build();
    PiAction transitAction = PiAction.builder()
            .withId(IntConstants.EGRESS_PROCESS_INT_TRANSIT_INIT_METADATA)
            .withParameter(transitIdParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(transitAction)
            .build();

    FlowRule transitFlowRule = DefaultFlowRule.builder()
            .withSelector(selector)
            .withTreatment(treatment)
            .fromApp(appId)
            .withPriority(DEFAULT_PRIORITY)
            .makePermanent()
            .forDevice(deviceId)
            .forTable(IntConstants.EGRESS_PROCESS_INT_TRANSIT_TB_INT_INSERT)
            .build();

    flowRuleService.applyFlowRules(transitFlowRule);

    return true;
}
 
Example 7
Source File: PiTableEntryTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
 */
@Test
public void testBuilder() {

    PiTableId piTableId = PiTableId.of("table10");
    long cookie = 0xfff0323;
    int priority = 100;
    double timeout = 1000;
    PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
    PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchFieldId, ImmutableByteSequence.copyFrom(0x0a010101));
    PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build();
    final Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
    fieldMatches.put(piMatchFieldId, piFieldMatch);
    final PiTableEntry piTableEntry = PiTableEntry.builder()
            .forTable(piTableId)
            .withMatchKey(PiMatchKey.builder()
                                  .addFieldMatches(fieldMatches.values())
                                  .build())
            .withAction(piAction)
            .withCookie(cookie)
            .withPriority(priority)
            .withTimeout(timeout)
            .build();

    assertThat(piTableEntry.table(), is(piTableId));
    assertThat(piTableEntry.cookie(), is(cookie));
    assertThat("Priority must be set", piTableEntry.priority().isPresent());
    assertThat("Timeout must be set", piTableEntry.timeout().isPresent());
    assertThat(piTableEntry.priority().getAsInt(), is(priority));
    assertThat(piTableEntry.timeout().get(), is(timeout));
    assertThat("Incorrect match param value",
               CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values()));
    assertThat(piTableEntry.action(), is(piAction));
}
 
Example 8
Source File: PiCriterionTranslatorsTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void testLpmToTernaryTranslation() throws Exception {
    IpPrefix ipPrefix = IpPrefix.valueOf("10.0.0.1/23");
    int bitWidth = ipPrefix.address().toOctets().length * Byte.SIZE;

    IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(ipPrefix);
    PiTernaryFieldMatch ternaryMatch =
            (PiTernaryFieldMatch) translateCriterion(criterion, fieldId, TERNARY, bitWidth);

    ImmutableByteSequence expectedMask = ImmutableByteSequence.prefixOnes(Integer.BYTES, 23);
    ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(ipPrefix.address().toOctets());

    assertThat(ternaryMatch.mask(), is(expectedMask));
    assertThat(ternaryMatch.value(), is(expectedValue));
}
 
Example 9
Source File: PiCriterionTranslatorsTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void testTernaryToLpmTranslation() throws Exception {
    EthCriterion criterion =
            (EthCriterion) Criteria.matchEthDstMasked(MacAddress.ONOS,
                                                      MacAddress.IPV4_MULTICAST_MASK);

    PiLpmFieldMatch lpmMatch =
            (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM,
                                                 MacAddress.MAC_ADDRESS_LENGTH * Byte.SIZE);
    ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(MacAddress.ONOS.toBytes());

    assertThat(lpmMatch.prefixLength(), is(25));
    assertThat(lpmMatch.value(), is(expectedValue));
}
 
Example 10
Source File: ImmutableByteSequenceSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableByteSequence read(Kryo kryo, Input input, Class<ImmutableByteSequence> type) {
    int length = input.readInt();
    byte[] data = new byte[length];
    int bytesRead = input.read(data);
    if (bytesRead != length) {
        throw new IllegalStateException("Byte sequence serializer read expected " + length +
                " but got " + bytesRead);
    }
    return ImmutableByteSequence.copyFrom(data);
}
 
Example 11
Source File: IntProgrammableImpl.java    From onos with Apache License 2.0 4 votes vote down vote up
private FlowRule buildWatchlistEntry(IntObjective obj) {
    int instructionBitmap = buildInstructionBitmap(obj.metadataTypes());
    PiActionParam hopMetaLenParam = new PiActionParam(
            IntConstants.HOP_METADATA_LEN,
            ImmutableByteSequence.copyFrom(Integer.bitCount(instructionBitmap)));
    PiActionParam hopCntParam = new PiActionParam(
            IntConstants.REMAINING_HOP_CNT,
            ImmutableByteSequence.copyFrom(MAXHOP));
    PiActionParam inst0003Param = new PiActionParam(
            IntConstants.INS_MASK0003,
            ImmutableByteSequence.copyFrom((instructionBitmap >> 12) & 0xF));
    PiActionParam inst0407Param = new PiActionParam(
            IntConstants.INS_MASK0407,
            ImmutableByteSequence.copyFrom((instructionBitmap >> 8) & 0xF));

    PiAction intSourceAction = PiAction.builder()
            .withId(IntConstants.INGRESS_PROCESS_INT_SOURCE_INT_SOURCE_DSCP)
            .withParameter(hopMetaLenParam)
            .withParameter(hopCntParam)
            .withParameter(inst0003Param)
            .withParameter(inst0407Param)
            .build();

    TrafficTreatment instTreatment = DefaultTrafficTreatment.builder()
            .piTableAction(intSourceAction)
            .build();

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
    for (Criterion criterion : obj.selector().criteria()) {
        switch (criterion.type()) {
            case IPV4_SRC:
                sBuilder.matchIPSrc(((IPCriterion) criterion).ip());
                break;
            case IPV4_DST:
                sBuilder.matchIPDst(((IPCriterion) criterion).ip());
                break;
            case TCP_SRC:
                sBuilder.matchPi(
                        PiCriterion.builder().matchTernary(
                                IntConstants.HDR_LOCAL_METADATA_L4_SRC_PORT,
                                ((TcpPortCriterion) criterion).tcpPort().toInt(), PORTMASK)
                                .build());
                break;
            case UDP_SRC:
                sBuilder.matchPi(
                        PiCriterion.builder().matchTernary(
                                IntConstants.HDR_LOCAL_METADATA_L4_SRC_PORT,
                                ((UdpPortCriterion) criterion).udpPort().toInt(), PORTMASK)
                                .build());
                break;
            case TCP_DST:
                sBuilder.matchPi(
                        PiCriterion.builder().matchTernary(
                                IntConstants.HDR_LOCAL_METADATA_L4_DST_PORT,
                                ((TcpPortCriterion) criterion).tcpPort().toInt(), PORTMASK)
                                .build());
                break;
            case UDP_DST:
                sBuilder.matchPi(
                        PiCriterion.builder().matchTernary(
                                IntConstants.HDR_LOCAL_METADATA_L4_DST_PORT,
                                ((UdpPortCriterion) criterion).udpPort().toInt(), PORTMASK)
                                .build());
                break;
            default:
                log.warn("Unsupported criterion type: {}", criterion.type());
        }
    }

    return DefaultFlowRule.builder()
            .forDevice(this.data().deviceId())
            .withSelector(sBuilder.build())
            .withTreatment(instTreatment)
            .withPriority(DEFAULT_PRIORITY)
            .forTable(IntConstants.INGRESS_PROCESS_INT_SOURCE_TB_INT_SOURCE)
            .fromApp(appId)
            .withIdleTimeout(IDLE_TIMEOUT)
            .build();
}
 
Example 12
Source File: IntProgrammableImpl.java    From onos with Apache License 2.0 4 votes vote down vote up
private FlowRule buildReportEntry(IntConfig cfg) {
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(PiCriterion.builder().matchExact(
                    IntConstants.HDR_INT_IS_VALID, (byte) 0x01)
                             .build())
            .build();
    PiActionParam srcMacParam = new PiActionParam(
            IntConstants.SRC_MAC,
            ImmutableByteSequence.copyFrom(cfg.sinkMac().toBytes()));
    PiActionParam nextHopMacParam = new PiActionParam(
            IntConstants.MON_MAC,
            ImmutableByteSequence.copyFrom(cfg.collectorNextHopMac().toBytes()));
    PiActionParam srcIpParam = new PiActionParam(
            IntConstants.SRC_IP,
            ImmutableByteSequence.copyFrom(cfg.sinkIp().toOctets()));
    PiActionParam monIpParam = new PiActionParam(
            IntConstants.MON_IP,
            ImmutableByteSequence.copyFrom(cfg.collectorIp().toOctets()));
    PiActionParam monPortParam = new PiActionParam(
            IntConstants.MON_PORT,
            ImmutableByteSequence.copyFrom(cfg.collectorPort().toInt()));
    PiAction reportAction = PiAction.builder()
            .withId(IntConstants.EGRESS_PROCESS_INT_REPORT_DO_REPORT_ENCAPSULATION)
            .withParameter(srcMacParam)
            .withParameter(nextHopMacParam)
            .withParameter(srcIpParam)
            .withParameter(monIpParam)
            .withParameter(monPortParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(reportAction)
            .build();

    return DefaultFlowRule.builder()
            .withSelector(selector)
            .withTreatment(treatment)
            .fromApp(appId)
            .withPriority(DEFAULT_PRIORITY)
            .makePermanent()
            .forDevice(this.data().deviceId())
            .forTable(IntConstants.EGRESS_PROCESS_INT_REPORT_TB_GENERATE_REPORT)
            .build();
}