Search in sources :

Example 1 with PiActionProfileModel

use of org.onosproject.net.pi.model.PiActionProfileModel in project onos by opennetworkinglab.

the class PiGroupTranslatorImpl method translate.

/**
 * Returns a PI action profile group equivalent to the given group, for the
 * given pipeconf and device.
 *
 * @param group    group
 * @param pipeconf pipeconf
 * @param device   device
 * @return PI action profile group
 * @throws PiTranslationException if the group cannot be translated
 */
static PiActionProfileGroup translate(Group group, PiPipeconf pipeconf, Device device) throws PiTranslationException {
    if (!SUPPORTED_GROUP_TYPES.contains(group.type())) {
        throw new PiTranslationException(format("group type %s not supported", group.type()));
    }
    // TODO: define proper field in group class.
    if (!(group.appCookie() instanceof PiGroupKey)) {
        throw new PiTranslationException("group app cookie is not PI (class should be PiGroupKey)");
    }
    final PiGroupKey groupKey = (PiGroupKey) group.appCookie();
    final PiActionProfileId actionProfileId = groupKey.actionProfileId();
    // Check validity of action profile against pipeconf.
    final PiActionProfileModel actionProfileModel = pipeconf.pipelineModel().actionProfiles(actionProfileId).orElseThrow(() -> new PiTranslationException(format("no such action profile '%s'", actionProfileId)));
    if (!actionProfileModel.hasSelector()) {
        throw new PiTranslationException(format("action profile '%s' does not support dynamic selection", actionProfileId));
    }
    // Check if the table associated with the action profile supports only
    // one-shot action profile programming.
    boolean isTableOneShot = actionProfileModel.tables().stream().map(tableId -> pipeconf.pipelineModel().table(tableId)).allMatch(piTableModel -> piTableModel.isPresent() && piTableModel.get().oneShotOnly());
    if (isTableOneShot) {
        throw new PiTranslationException(format("Table associated to action profile '%s' supports only one-shot action profile programming", actionProfileId));
    }
    // Check group validity.
    if (actionProfileModel.maxGroupSize() > 0 && group.buckets().buckets().size() > actionProfileModel.maxGroupSize()) {
        throw new PiTranslationException(format("too many buckets, max group size for action profile '%s' is %d", actionProfileId, actionProfileModel.maxGroupSize()));
    }
    // If not INDIRECT, we set the maximum group size as specified in the
    // model, however this might be highly inefficient for some HW targets
    // which pre-allocate resources for the whole group.
    final int maxGroupSize = group.type() == GroupDescription.Type.INDIRECT ? 1 : actionProfileModel.maxGroupSize();
    final PiActionProfileGroup.Builder piActionGroupBuilder = PiActionProfileGroup.builder().withId(PiActionProfileGroupId.of(group.id().id())).withActionProfileId(groupKey.actionProfileId()).withMaxSize(maxGroupSize);
    // Translate group buckets to PI group members
    final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf);
    short bucketIdx = 0;
    for (GroupBucket bucket : group.buckets().buckets()) {
        /*
            FIXME: the way member IDs are computed can cause collisions!
            Problem: In P4Runtime action profile members, i.e. action buckets,
            are associated to a numeric ID chosen at member insertion time. This
            ID must be unique for the whole action profile (i.e. the group table
            in OpenFlow). In ONOS, GroupBucket doesn't specify any ID.

            Solutions:
            - Change GroupBucket API to force application wanting to perform
            group operations to specify a member id.
            - Maintain state to dynamically allocate/deallocate member IDs, e.g.
            in a dedicated service, or in a P4Runtime Group Provider.

            Hack: Statically derive member ID by combining groupId and position
            of the bucket in the list.
             */
        final int memberId = Objects.hash(group.id(), bucketIdx);
        if (memberId == 0) {
            throw new PiTranslationException("GroupBucket produces PiActionProfileMember " + "with invalid ID 0");
        }
        bucketIdx++;
        final PiTableAction tableAction = translateTreatment(bucket.treatment(), interpreter, groupKey.tableId(), pipeconf.pipelineModel());
        if (tableAction == null) {
            throw new PiTranslationException("bucket treatment translator returned null");
        }
        if (tableAction.type() != ACTION) {
            throw new PiTranslationException(format("action of type '%s' cannot be used in action profile members", tableAction.type()));
        }
        final PiActionProfileMember member = PiActionProfileMember.builder().forActionProfile(groupKey.actionProfileId()).withId(PiActionProfileMemberId.of(memberId)).withAction((PiAction) tableAction).build();
        // NOTE Indirect groups have weight set to -1 which is not supported
        // by P4RT - setting to 1 to avoid problems with the p4rt server.
        final int weight = group.type() == GroupDescription.Type.INDIRECT ? 1 : bucket.weight();
        piActionGroupBuilder.addMember(member, weight);
    }
    return piActionGroupBuilder.build();
}
Also used : PiTableAction(org.onosproject.net.pi.runtime.PiTableAction) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) PiUtils.getInterpreterOrNull(org.onosproject.net.pi.impl.PiUtils.getInterpreterOrNull) Device(org.onosproject.net.Device) PiPipeconf(org.onosproject.net.pi.model.PiPipeconf) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) PiActionProfileMemberId(org.onosproject.net.pi.runtime.PiActionProfileMemberId) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) Set(java.util.Set) GroupBucket(org.onosproject.net.group.GroupBucket) PiGroupKey(org.onosproject.net.pi.runtime.PiGroupKey) Sets(com.google.common.collect.Sets) String.format(java.lang.String.format) Group(org.onosproject.net.group.Group) Objects(java.util.Objects) PiAction(org.onosproject.net.pi.runtime.PiAction) PiFlowRuleTranslatorImpl.translateTreatment(org.onosproject.net.pi.impl.PiFlowRuleTranslatorImpl.translateTreatment) PiTranslationException(org.onosproject.net.pi.service.PiTranslationException) ACTION(org.onosproject.net.pi.runtime.PiTableAction.Type.ACTION) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) GroupDescription(org.onosproject.net.group.GroupDescription) PiActionProfileGroupId(org.onosproject.net.pi.runtime.PiActionProfileGroupId) PiActionProfileId(org.onosproject.net.pi.model.PiActionProfileId) PiTableAction(org.onosproject.net.pi.runtime.PiTableAction) PiActionProfileId(org.onosproject.net.pi.model.PiActionProfileId) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) PiTranslationException(org.onosproject.net.pi.service.PiTranslationException) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) PiAction(org.onosproject.net.pi.runtime.PiAction) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) PiGroupKey(org.onosproject.net.pi.runtime.PiGroupKey) GroupBucket(org.onosproject.net.group.GroupBucket) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter)

Example 2 with PiActionProfileModel

use of org.onosproject.net.pi.model.PiActionProfileModel in project onos by opennetworkinglab.

the class P4RuntimeActionGroupProgrammable method getGroups.

@Override
public Collection<Group> getGroups() {
    if (!setupBehaviour("getGroups()")) {
        return Collections.emptyList();
    }
    if (driverBoolProperty(READ_ACTION_GROUPS_FROM_MIRROR, DEFAULT_READ_ACTION_GROUPS_FROM_MIRROR)) {
        return getGroupsFromMirror();
    }
    // Dump groups and members from device for all action profiles.
    final P4RuntimeReadClient.ReadRequest request = client.read(p4DeviceId, pipeconf);
    pipeconf.pipelineModel().actionProfiles().stream().filter(piActionProfileModel -> piActionProfileModel.tables().stream().map(tableId -> pipeconf.pipelineModel().table(tableId)).allMatch(piTableModel -> piTableModel.isPresent() && !piTableModel.get().oneShotOnly())).map(PiActionProfileModel::id).forEach(id -> request.actionProfileGroups(id).actionProfileMembers(id));
    final P4RuntimeReadClient.ReadResponse response = request.submitSync();
    if (!response.isSuccess()) {
        // Error at client level.
        return Collections.emptyList();
    }
    final Collection<PiActionProfileGroup> groupsOnDevice = response.all(PiActionProfileGroup.class);
    final Map<PiActionProfileMemberHandle, PiActionProfileMember> membersOnDevice = response.all(PiActionProfileMember.class).stream().collect(toMap(m -> m.handle(deviceId), m -> m));
    // Sync mirrors.
    groupMirror.sync(deviceId, groupsOnDevice);
    memberMirror.sync(deviceId, membersOnDevice.values());
    // Retrieve the original PD group before translation.
    final List<Group> result = Lists.newArrayList();
    final List<PiActionProfileGroup> groupsToRemove = Lists.newArrayList();
    final Set<PiActionProfileMemberHandle> memberHandlesToKeep = Sets.newHashSet();
    for (PiActionProfileGroup piGroup : groupsOnDevice) {
        final Group pdGroup = checkAndForgeGroupEntry(piGroup, membersOnDevice);
        if (pdGroup == null) {
            // Entry is on device but is inconsistent with controller state.
            // Mark for removal.
            groupsToRemove.add(piGroup);
        } else {
            result.add(pdGroup);
            // Keep track of member handles used in groups.
            piGroup.members().stream().map(m -> PiActionProfileMemberHandle.of(deviceId, piGroup.actionProfile(), m.id())).forEach(memberHandlesToKeep::add);
        }
    }
    // Trigger clean up of inconsistent groups and members (if any). Also
    // take care of removing any orphan member, e.g. from a
    // partial/unsuccessful group insertion.
    final Set<PiActionProfileMemberHandle> memberHandlesToRemove = Sets.difference(membersOnDevice.keySet(), memberHandlesToKeep);
    final Set<PiActionProfileGroupHandle> groupHandlesToRemove = groupsToRemove.stream().map(g -> g.handle(deviceId)).collect(toSet());
    if (groupHandlesToRemove.size() + memberHandlesToRemove.size() > 0) {
        log.warn("Cleaning up {} action profile groups and " + "{} members on {}...", groupHandlesToRemove.size(), memberHandlesToRemove.size(), deviceId);
        client.write(p4DeviceId, pipeconf).delete(groupHandlesToRemove).delete(memberHandlesToRemove).submit().whenComplete((r, ex) -> {
            if (ex != null) {
                log.error("Exception removing inconsistent group/members", ex);
            } else {
                log.debug("Completed removal of inconsistent " + "groups/members ({} of {} updates succeeded)", r.success().size(), r.all().size());
                groupMirror.applyWriteResponse(r);
                memberMirror.applyWriteResponse(r);
            }
        });
    }
    // Done.
    return result;
}
Also used : P4RuntimeReadClient(org.onosproject.p4runtime.api.P4RuntimeReadClient) PiActionProfileGroupHandle(org.onosproject.net.pi.runtime.PiActionProfileGroupHandle) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) DefaultGroup(org.onosproject.net.group.DefaultGroup) GroupOperation(org.onosproject.net.group.GroupOperation) P4RuntimeActionProfileGroupMirror(org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileGroupMirror) Group(org.onosproject.net.group.Group) PiEntity(org.onosproject.net.pi.runtime.PiEntity) GroupStore(org.onosproject.net.group.GroupStore) GroupProgrammable(org.onosproject.net.group.GroupProgrammable) Lists(com.google.common.collect.Lists) Collectors.toMap(java.util.stream.Collectors.toMap) PiGroupTranslator(org.onosproject.net.pi.service.PiGroupTranslator) Map(java.util.Map) PiTranslationException(org.onosproject.net.pi.service.PiTranslationException) GroupOperations(org.onosproject.net.group.GroupOperations) Collectors.toSet(java.util.stream.Collectors.toSet) PiActionProfileMemberHandle(org.onosproject.net.pi.runtime.PiActionProfileMemberHandle) Striped(com.google.common.util.concurrent.Striped) PiTranslatedEntity(org.onosproject.net.pi.service.PiTranslatedEntity) P4RuntimeReadClient(org.onosproject.p4runtime.api.P4RuntimeReadClient) Collection(java.util.Collection) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) Set(java.util.Set) WriteRequest(org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) PiHandle(org.onosproject.net.pi.runtime.PiHandle) Objects(java.util.Objects) TimedEntry(org.onosproject.drivers.p4runtime.mirror.TimedEntry) List(java.util.List) Lock(java.util.concurrent.locks.Lock) P4RuntimeActionProfileMemberMirror(org.onosproject.drivers.p4runtime.mirror.P4RuntimeActionProfileMemberMirror) P4RuntimeMirror(org.onosproject.drivers.p4runtime.mirror.P4RuntimeMirror) Optional(java.util.Optional) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) DeviceId(org.onosproject.net.DeviceId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) GroupDescription(org.onosproject.net.group.GroupDescription) Collections(java.util.Collections) DefaultGroup(org.onosproject.net.group.DefaultGroup) Group(org.onosproject.net.group.Group) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) PiActionProfileGroupHandle(org.onosproject.net.pi.runtime.PiActionProfileGroupHandle) PiActionProfileMemberHandle(org.onosproject.net.pi.runtime.PiActionProfileMemberHandle)

Example 3 with PiActionProfileModel

use of org.onosproject.net.pi.model.PiActionProfileModel in project onos by opennetworkinglab.

the class P4InfoParser method parseActionProfiles.

private static Map<Integer, PiActionProfileModel> parseActionProfiles(P4Info p4info) throws P4InfoParserException {
    final Map<Integer, PiActionProfileModel> actProfileMap = Maps.newHashMap();
    for (ActionProfile actProfileMsg : p4info.getActionProfilesList()) {
        final ImmutableSet.Builder<PiTableId> tableIdSetBuilder = ImmutableSet.builder();
        for (int tableId : actProfileMsg.getTableIdsList()) {
            tableIdSetBuilder.add(PiTableId.of(getTableName(tableId, p4info)));
        }
        // TODO: we should copy all annotations to model classes for later
        // use in the PI framework.
        // This is a temporary workaround to the inability of p4c to
        // correctly interpret P4Runtime-defined max_group_size annotation:
        // https://s3-us-west-2.amazonaws.com/p4runtime/docs/master/
        // P4Runtime-Spec.html#sec-p4info-action-profile
        final String maxSizeAnnString = getAnnotationValue(MAX_GROUP_SIZE_ANNOTATION, actProfileMsg.getPreamble());
        final int maxSizeAnn = maxSizeAnnString != null ? Integer.valueOf(maxSizeAnnString) : 0;
        final int maxGroupSize;
        if (actProfileMsg.getMaxGroupSize() == 0 && maxSizeAnn != 0) {
            log.warn("Found valid 'max_group_size' annotation for " + "ActionProfile {}, using that...", actProfileMsg.getPreamble().getName());
            maxGroupSize = maxSizeAnn;
        } else {
            maxGroupSize = actProfileMsg.getMaxGroupSize();
        }
        actProfileMap.put(actProfileMsg.getPreamble().getId(), new P4ActionProfileModel(PiActionProfileId.of(actProfileMsg.getPreamble().getName()), tableIdSetBuilder.build(), actProfileMsg.getWithSelector(), actProfileMsg.getSize(), maxGroupSize));
    }
    return actProfileMap;
}
Also used : PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) ImmutableSet(com.google.common.collect.ImmutableSet) PiTableId(org.onosproject.net.pi.model.PiTableId) ActionProfile(p4.config.v1.P4InfoOuterClass.ActionProfile)

Example 4 with PiActionProfileModel

use of org.onosproject.net.pi.model.PiActionProfileModel in project onos by opennetworkinglab.

the class P4InfoParser method parse.

/**
 * Parse the given URL pointing to a P4Info file (in text format) to a PI pipeline model.
 *
 * @param p4InfoUrl URL to P4Info in text form
 * @return PI pipeline model
 * @throws P4InfoParserException if the P4Info file cannot be parsed (see message)
 */
public static PiPipelineModel parse(URL p4InfoUrl) throws P4InfoParserException {
    final P4Info p4info;
    try {
        p4info = getP4InfoMessage(p4InfoUrl);
    } catch (IOException e) {
        throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e);
    }
    // Generate fingerprint of the pipeline by hashing p4info file
    final int fingerprint;
    try {
        HashingInputStream hin = new HashingInputStream(Hashing.crc32(), p4InfoUrl.openStream());
        // noinspection StatementWithEmptyBody
        while (hin.read() != -1) {
        // Do nothing. Reading all input stream to update hash.
        }
        fingerprint = hin.hash().asInt();
    } catch (IOException e) {
        throw new P4InfoParserException("Unable to generate fingerprint " + p4InfoUrl.toString(), e);
    }
    // Start by parsing and mapping instances to to their integer P4Info IDs.
    // Convenient to build the table model at the end.
    final String architecture = parseArchitecture(p4info);
    // Counters.
    final Map<Integer, PiCounterModel> counterMap = Maps.newHashMap();
    counterMap.putAll(parseCounters(p4info));
    counterMap.putAll(parseDirectCounters(p4info));
    // Meters.
    final Map<Integer, PiMeterModel> meterMap = Maps.newHashMap();
    meterMap.putAll(parseMeters(p4info));
    meterMap.putAll(parseDirectMeters(p4info));
    // Registers.
    final Map<Integer, PiRegisterModel> registerMap = Maps.newHashMap();
    registerMap.putAll(parseRegisters(p4info));
    // Action profiles.
    final Map<Integer, PiActionProfileModel> actProfileMap = parseActionProfiles(p4info);
    // Actions.
    final Map<Integer, PiActionModel> actionMap = parseActions(p4info);
    // Controller packet metadatas.
    final Map<PiPacketOperationType, PiPacketOperationModel> pktOpMap = parseCtrlPktMetadatas(p4info);
    // Finally, parse tables.
    final ImmutableMap.Builder<PiTableId, PiTableModel> tableImmMapBuilder = ImmutableMap.builder();
    for (Table tableMsg : p4info.getTablesList()) {
        final PiTableId tableId = PiTableId.of(tableMsg.getPreamble().getName());
        // Parse match fields.
        final ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel> tableFieldMapBuilder = ImmutableMap.builder();
        for (MatchField fieldMsg : tableMsg.getMatchFieldsList()) {
            final PiMatchFieldId fieldId = PiMatchFieldId.of(fieldMsg.getName());
            tableFieldMapBuilder.put(fieldId, new P4MatchFieldModel(fieldId, isFieldString(p4info, fieldMsg.getTypeName().getName()) ? P4MatchFieldModel.BIT_WIDTH_UNDEFINED : fieldMsg.getBitwidth(), mapMatchFieldType(fieldMsg.getMatchType())));
        }
        // Retrieve action models by inter IDs.
        final ImmutableMap.Builder<PiActionId, PiActionModel> tableActionMapBuilder = ImmutableMap.builder();
        tableMsg.getActionRefsList().stream().map(ActionRef::getId).map(actionMap::get).forEach(actionModel -> tableActionMapBuilder.put(actionModel.id(), actionModel));
        // Retrieve direct meters by integer IDs.
        final ImmutableMap.Builder<PiMeterId, PiMeterModel> tableMeterMapBuilder = ImmutableMap.builder();
        tableMsg.getDirectResourceIdsList().stream().map(meterMap::get).filter(Objects::nonNull).forEach(meterModel -> tableMeterMapBuilder.put(meterModel.id(), meterModel));
        // Retrieve direct counters by integer IDs.
        final ImmutableMap.Builder<PiCounterId, PiCounterModel> tableCounterMapBuilder = ImmutableMap.builder();
        tableMsg.getDirectResourceIdsList().stream().map(counterMap::get).filter(Objects::nonNull).forEach(counterModel -> tableCounterMapBuilder.put(counterModel.id(), counterModel));
        // Check if table supports one-shot only
        boolean oneShotOnly = isAnnotationPresent(ONE_SHOT_ONLY_ANNOTATION, tableMsg.getPreamble());
        tableImmMapBuilder.put(tableId, new P4TableModel(PiTableId.of(tableMsg.getPreamble().getName()), tableMsg.getImplementationId() == 0 ? PiTableType.DIRECT : PiTableType.INDIRECT, actProfileMap.get(tableMsg.getImplementationId()), tableMsg.getSize(), tableCounterMapBuilder.build(), tableMeterMapBuilder.build(), !tableMsg.getIdleTimeoutBehavior().equals(Table.IdleTimeoutBehavior.NO_TIMEOUT), tableFieldMapBuilder.build(), tableActionMapBuilder.build(), actionMap.get(tableMsg.getConstDefaultActionId()), tableMsg.getIsConstTable(), oneShotOnly));
    }
    // Get a map with proper PI IDs for some of those maps we created at the beginning.
    ImmutableMap<PiCounterId, PiCounterModel> counterImmMap = ImmutableMap.copyOf(counterMap.values().stream().collect(Collectors.toMap(PiCounterModel::id, c -> c)));
    ImmutableMap<PiMeterId, PiMeterModel> meterImmMap = ImmutableMap.copyOf(meterMap.values().stream().collect(Collectors.toMap(PiMeterModel::id, m -> m)));
    ImmutableMap<PiRegisterId, PiRegisterModel> registerImmMap = ImmutableMap.copyOf(registerMap.values().stream().collect(Collectors.toMap(PiRegisterModel::id, r -> r)));
    ImmutableMap<PiActionProfileId, PiActionProfileModel> actProfileImmMap = ImmutableMap.copyOf(actProfileMap.values().stream().collect(Collectors.toMap(PiActionProfileModel::id, a -> a)));
    return new P4PipelineModel(tableImmMapBuilder.build(), counterImmMap, meterImmMap, registerImmMap, actProfileImmMap, ImmutableMap.copyOf(pktOpMap), architecture, fingerprint);
}
Also used : PiCounterModel(org.onosproject.net.pi.model.PiCounterModel) PiTableModel(org.onosproject.net.pi.model.PiTableModel) PiActionId(org.onosproject.net.pi.model.PiActionId) PiMeterId(org.onosproject.net.pi.model.PiMeterId) PiPacketOperationType(org.onosproject.net.pi.model.PiPacketOperationType) PiPacketOperationModel(org.onosproject.net.pi.model.PiPacketOperationModel) MatchField(p4.config.v1.P4InfoOuterClass.MatchField) PiTableId(org.onosproject.net.pi.model.PiTableId) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) ActionRef(p4.config.v1.P4InfoOuterClass.ActionRef) PiRegisterModel(org.onosproject.net.pi.model.PiRegisterModel) Table(p4.config.v1.P4InfoOuterClass.Table) PiActionModel(org.onosproject.net.pi.model.PiActionModel) PiMeterModel(org.onosproject.net.pi.model.PiMeterModel) PiActionProfileId(org.onosproject.net.pi.model.PiActionProfileId) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) HashingInputStream(com.google.common.hash.HashingInputStream) P4Info(p4.config.v1.P4InfoOuterClass.P4Info) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) PiRegisterId(org.onosproject.net.pi.model.PiRegisterId) PiMatchFieldModel(org.onosproject.net.pi.model.PiMatchFieldModel) PiCounterId(org.onosproject.net.pi.model.PiCounterId)

Example 5 with PiActionProfileModel

use of org.onosproject.net.pi.model.PiActionProfileModel in project onos by opennetworkinglab.

the class P4InfoParserTest method testParse.

/**
 * Tests parse method.
 * @throws Exception if equality group objects dose not match as expected
 */
@Test
public void testParse() throws Exception {
    // Generate two PiPipelineModels from the same p4Info file
    PiPipelineModel model = P4InfoParser.parse(p4InfoUrl);
    PiPipelineModel sameAsModel = P4InfoParser.parse(p4InfoUrl);
    // Check equality
    new EqualsTester().addEqualityGroup(model, sameAsModel).testEquals();
    // Generate a P4Info object from the file
    final P4Info p4info;
    try {
        p4info = getP4InfoMessage(p4InfoUrl);
    } catch (IOException e) {
        throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e);
    }
    List<Table> tableMsgs = p4info.getTablesList();
    PiTableId table0Id = PiTableId.of(tableMsgs.get(0).getPreamble().getName());
    PiTableId wcmpTableId = PiTableId.of(tableMsgs.get(1).getPreamble().getName());
    PiTableId wcmpTableOneShotId = PiTableId.of(tableMsgs.get(2).getPreamble().getName());
    // parse tables
    PiTableModel table0Model = model.table(table0Id).orElse(null);
    PiTableModel wcmpTableModel = model.table(wcmpTableId).orElse(null);
    PiTableModel wcmpTableOneShotModel = model.table(wcmpTableOneShotId).orElse(null);
    PiTableModel table0Model2 = sameAsModel.table(table0Id).orElse(null);
    PiTableModel wcmpTableModel2 = sameAsModel.table(wcmpTableId).orElse(null);
    new EqualsTester().addEqualityGroup(table0Model, table0Model2).addEqualityGroup(wcmpTableModel, wcmpTableModel2).testEquals();
    // Check existence
    assertThat("model parsed value is null", table0Model, notNullValue());
    assertThat("model parsed value is null", wcmpTableModel, notNullValue());
    assertThat("model parsed value is null", wcmpTableOneShotModel, notNullValue());
    assertThat("Incorrect size for table0 size", table0Model.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE)));
    assertThat("Incorrect size for wcmp_table size", wcmpTableModel.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE)));
    assertThat("Incorrect size for wcmp_table_one_shot size", wcmpTableOneShotModel.maxSize(), is(equalTo(DEFAULT_MAX_TABLE_SIZE)));
    // Check one-shot annotation
    assertThat("error parsing one-shot annotation", wcmpTableModel.oneShotOnly(), is(false));
    assertThat("error parsing one-shot annotation", wcmpTableOneShotModel.oneShotOnly(), is(true));
    // Check matchFields
    List<MatchField> matchFieldList = tableMsgs.get(0).getMatchFieldsList();
    List<PiMatchFieldModel> piMatchFieldList = new ArrayList<>();
    for (MatchField matchFieldIter : matchFieldList) {
        MatchField.MatchType matchType = matchFieldIter.getMatchType();
        PiMatchType piMatchType;
        switch(matchType) {
            case EXACT:
                piMatchType = PiMatchType.EXACT;
                break;
            case LPM:
                piMatchType = PiMatchType.LPM;
                break;
            case TERNARY:
                piMatchType = PiMatchType.TERNARY;
                break;
            case RANGE:
                piMatchType = PiMatchType.RANGE;
                break;
            default:
                Assert.fail();
                return;
        }
        piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()), matchFieldIter.getBitwidth(), piMatchType));
    }
    // Check MatchFields size
    assertThat("Incorrect size for matchFields", table0Model.matchFields().size(), is(equalTo(9)));
    // Check if matchFields are in order
    assertThat("Incorrect order for matchFields", table0Model.matchFields(), IsIterableContainingInOrder.contains(piMatchFieldList.get(0), piMatchFieldList.get(1), piMatchFieldList.get(2), piMatchFieldList.get(3), piMatchFieldList.get(4), piMatchFieldList.get(5), piMatchFieldList.get(6), piMatchFieldList.get(7), piMatchFieldList.get(8)));
    assertThat("Incorrect size for matchFields", wcmpTableModel.matchFields().size(), is(equalTo(1)));
    // check if matchFields are in order
    matchFieldList = tableMsgs.get(1).getMatchFieldsList();
    assertThat("Incorrect order for matchFields", wcmpTableModel.matchFields(), IsIterableContainingInOrder.contains(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldList.get(0).getName()), matchFieldList.get(0).getBitwidth(), PiMatchType.EXACT)));
    // check table0 actionsRefs
    List<ActionRef> actionRefList = tableMsgs.get(0).getActionRefsList();
    assertThat("Incorrect size for actionRefs", actionRefList.size(), is(equalTo(4)));
    // create action instances
    PiActionId actionId = PiActionId.of("set_egress_port");
    PiActionParamId piActionParamId = PiActionParamId.of("port");
    int bitWitdth = 9;
    PiActionParamModel actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
    ImmutableMap<PiActionParamId, PiActionParamModel> params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
    PiActionModel setEgressPortAction = new P4ActionModel(actionId, params);
    actionId = PiActionId.of("send_to_cpu");
    PiActionModel sendToCpuAction = new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
    actionId = PiActionId.of("_drop");
    PiActionModel dropAction = new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
    actionId = PiActionId.of("NoAction");
    PiActionModel noAction = new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
    actionId = PiActionId.of("table0_control.set_next_hop_id");
    piActionParamId = PiActionParamId.of("next_hop_id");
    bitWitdth = 16;
    actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
    params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
    PiActionModel setNextHopIdAction = new P4ActionModel(actionId, params);
    // check table0 actions
    assertThat("action dose not match", table0Model.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(setEgressPortAction, sendToCpuAction, setNextHopIdAction, dropAction));
    // check wcmp_table actions
    assertThat("actions dose not match", wcmpTableModel.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(setEgressPortAction, noAction));
    PiActionModel table0DefaultAction = table0Model.constDefaultAction().orElse(null);
    new EqualsTester().addEqualityGroup(table0DefaultAction, dropAction).testEquals();
    // Check existence
    assertThat("model parsed value is null", table0DefaultAction, notNullValue());
    // parse action profiles
    PiTableId tableId = PiTableId.of("wcmp_control.wcmp_table");
    ImmutableSet<PiTableId> tableIds = new ImmutableSet.Builder<PiTableId>().add(tableId).build();
    PiActionProfileId actionProfileId = PiActionProfileId.of("wcmp_control.wcmp_selector");
    PiActionProfileModel wcmpSelector3 = new P4ActionProfileModel(actionProfileId, tableIds, true, DEFAULT_MAX_ACTION_PROFILE_SIZE, DEFAULT_MAX_GROUP_SIZE);
    PiActionProfileModel wcmpSelector = model.actionProfiles(actionProfileId).orElse(null);
    PiActionProfileModel wcmpSelector2 = sameAsModel.actionProfiles(actionProfileId).orElse(null);
    new EqualsTester().addEqualityGroup(wcmpSelector, wcmpSelector2, wcmpSelector3).testEquals();
    // Check existence
    assertThat("model parsed value is null", wcmpSelector, notNullValue());
    assertThat("Incorrect value for actions profiles", model.actionProfiles(), containsInAnyOrder(wcmpSelector));
    // ActionProfiles size
    assertThat("Incorrect size for action profiles", model.actionProfiles().size(), is(equalTo(1)));
    // parse counters
    PiCounterModel ingressPortCounterModel = model.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null);
    PiCounterModel egressPortCounterModel = model.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null);
    PiCounterModel table0CounterModel = model.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null);
    PiCounterModel wcmpTableCounterModel = model.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null);
    PiCounterModel ingressPortCounterModel2 = sameAsModel.counter(PiCounterId.of("port_counters_ingress.ingress_port_counter")).orElse(null);
    PiCounterModel egressPortCounterModel2 = sameAsModel.counter(PiCounterId.of("port_counters_egress.egress_port_counter")).orElse(null);
    PiCounterModel table0CounterModel2 = sameAsModel.counter(PiCounterId.of("table0_control.table0_counter")).orElse(null);
    PiCounterModel wcmpTableCounterModel2 = sameAsModel.counter(PiCounterId.of("wcmp_control.wcmp_table_counter")).orElse(null);
    new EqualsTester().addEqualityGroup(ingressPortCounterModel, ingressPortCounterModel2).addEqualityGroup(egressPortCounterModel, egressPortCounterModel2).addEqualityGroup(table0CounterModel, table0CounterModel2).addEqualityGroup(wcmpTableCounterModel, wcmpTableCounterModel2).testEquals();
    assertThat("model parsed value is null", ingressPortCounterModel, notNullValue());
    assertThat("model parsed value is null", egressPortCounterModel, notNullValue());
    assertThat("model parsed value is null", table0CounterModel, notNullValue());
    assertThat("model parsed value is null", wcmpTableCounterModel, notNullValue());
    // Parse meters
    Collection<PiMeterModel> meterModel = model.meters();
    Collection<PiMeterModel> meterModel2 = sameAsModel.meters();
    assertThat("model parsed meter collection should be empty", meterModel.isEmpty(), is(true));
    assertThat("model parsed meter collection should be empty", meterModel2.isEmpty(), is(true));
    // parse packet operations
    PiPacketOperationModel packetInOperationalModel = model.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null);
    PiPacketOperationModel packetOutOperationalModel = model.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null);
    PiPacketOperationModel packetInOperationalModel2 = sameAsModel.packetOperationModel(PiPacketOperationType.PACKET_IN).orElse(null);
    PiPacketOperationModel packetOutOperationalModel2 = sameAsModel.packetOperationModel(PiPacketOperationType.PACKET_OUT).orElse(null);
    new EqualsTester().addEqualityGroup(packetInOperationalModel, packetInOperationalModel2).addEqualityGroup(packetOutOperationalModel, packetOutOperationalModel2).testEquals();
    // Check existence
    assertThat("model parsed value is null", packetInOperationalModel, notNullValue());
    assertThat("model parsed value is null", packetOutOperationalModel, notNullValue());
}
Also used : PiTableModel(org.onosproject.net.pi.model.PiTableModel) PiActionId(org.onosproject.net.pi.model.PiActionId) PiActionParamId(org.onosproject.net.pi.model.PiActionParamId) PiCounterModel(org.onosproject.net.pi.model.PiCounterModel) ArrayList(java.util.ArrayList) PiPacketOperationModel(org.onosproject.net.pi.model.PiPacketOperationModel) MatchField(p4.config.v1.P4InfoOuterClass.MatchField) PiTableId(org.onosproject.net.pi.model.PiTableId) ActionRef(p4.config.v1.P4InfoOuterClass.ActionRef) PiPipelineModel(org.onosproject.net.pi.model.PiPipelineModel) Table(p4.config.v1.P4InfoOuterClass.Table) PiMatchType(org.onosproject.net.pi.model.PiMatchType) PiActionModel(org.onosproject.net.pi.model.PiActionModel) EqualsTester(com.google.common.testing.EqualsTester) PiMeterModel(org.onosproject.net.pi.model.PiMeterModel) PiActionParamModel(org.onosproject.net.pi.model.PiActionParamModel) PiActionProfileId(org.onosproject.net.pi.model.PiActionProfileId) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) P4Info(p4.config.v1.P4InfoOuterClass.P4Info) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) PiMatchFieldModel(org.onosproject.net.pi.model.PiMatchFieldModel) Test(org.junit.Test)

Aggregations

PiActionProfileModel (org.onosproject.net.pi.model.PiActionProfileModel)5 PiActionProfileId (org.onosproject.net.pi.model.PiActionProfileId)3 PiTableId (org.onosproject.net.pi.model.PiTableId)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 Objects (java.util.Objects)2 Set (java.util.Set)2 PiActionId (org.onosproject.net.pi.model.PiActionId)2 PiActionModel (org.onosproject.net.pi.model.PiActionModel)2 PiCounterModel (org.onosproject.net.pi.model.PiCounterModel)2 PiMatchFieldModel (org.onosproject.net.pi.model.PiMatchFieldModel)2 PiMeterModel (org.onosproject.net.pi.model.PiMeterModel)2 PiPacketOperationModel (org.onosproject.net.pi.model.PiPacketOperationModel)2 PiTableModel (org.onosproject.net.pi.model.PiTableModel)2 ActionRef (p4.config.v1.P4InfoOuterClass.ActionRef)2 MatchField (p4.config.v1.P4InfoOuterClass.MatchField)2 P4Info (p4.config.v1.P4InfoOuterClass.P4Info)2 Table (p4.config.v1.P4InfoOuterClass.Table)2 ImmutableSet (com.google.common.collect.ImmutableSet)1