use of p4.config.v1.P4InfoOuterClass.Table in project onos by opennetworkinglab.
the class TableEntryEncoderTest method testEncodeWithNoAction.
@Test
public void testEncodeWithNoAction() throws Exception {
TableEntry tableEntryMsg = Codecs.CODECS.tableEntry().encode(piTableEntryWithoutAction, null, defaultPipeconf);
PiTableEntry decodedPiTableEntry = Codecs.CODECS.tableEntry().decode(tableEntryMsg, null, defaultPipeconf);
// Test equality for decoded entry.
new EqualsTester().addEqualityGroup(piTableEntryWithoutAction, decodedPiTableEntry).testEquals();
// Table ID.
int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId();
int encodedTableId = tableEntryMsg.getTableId();
assertThat(encodedTableId, is(p4InfoTableId));
// Ternary match.
byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
// no action
assertThat(tableEntryMsg.hasAction(), is(false));
// Counter
CounterData counterData = tableEntryMsg.getCounterData();
PiCounterCellData encodedCounterData = new PiCounterCellData(counterData.getPacketCount(), counterData.getByteCount());
assertThat(encodedCounterData, is(counterCellData));
// TODO: improve, assert other field match types (ternary, LPM)
}
use of p4.config.v1.P4InfoOuterClass.Table in project onos by opennetworkinglab.
the class TableEntryEncoderTest method testTableEntryEncoderWithoutOptionalField.
@Test
public void testTableEntryEncoderWithoutOptionalField() throws Exception {
TableEntry tableEntryMsg = Codecs.CODECS.tableEntry().encode(piTableEntryWithoutOptionalField, null, defaultPipeconf2);
PiTableEntry decodedPiTableEntry = Codecs.CODECS.tableEntry().decode(tableEntryMsg, null, defaultPipeconf2);
// Table ID.
int p4InfoTableId = browser2.tables().getByName(tableId.id()).getPreamble().getId();
int encodedTableId = tableEntryMsg.getTableId();
assertThat(encodedTableId, is(p4InfoTableId));
// Test equality for decoded entry.
new EqualsTester().addEqualityGroup(piTableEntryWithoutOptionalField, decodedPiTableEntry).testEquals();
// no optional field
assertThat(tableEntryMsg.getMatchCount(), is(3));
assertThat(tableEntryMsg.getMatchList().stream().map(P4RuntimeOuterClass.FieldMatch::getFieldMatchTypeCase).collect(Collectors.toList()), not(hasItem(P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase.OPTIONAL)));
}
use of p4.config.v1.P4InfoOuterClass.Table in project onos by opennetworkinglab.
the class TableEntryEncoderTest method testActopProfileGroup.
@Test
public void testActopProfileGroup() throws Exception {
TableEntry tableEntryMsg = Codecs.CODECS.tableEntry().encode(piTableEntryWithGroupAction, null, defaultPipeconf);
PiTableEntry decodedPiTableEntry = Codecs.CODECS.tableEntry().decode(tableEntryMsg, null, defaultPipeconf);
// Test equality for decoded entry.
new EqualsTester().addEqualityGroup(piTableEntryWithGroupAction, decodedPiTableEntry).testEquals();
// Table ID.
int p4InfoTableId = browser.tables().getByName(ecmpTableId.id()).getPreamble().getId();
int encodedTableId = tableEntryMsg.getTableId();
assertThat(encodedTableId, is(p4InfoTableId));
// Exact match.
byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getExact().getValue().toByteArray();
assertThat(encodedTernaryMatchValue, is(new byte[] { (byte) 0xff }));
// Action profile group id
int actionProfileGroupId = tableEntryMsg.getAction().getActionProfileGroupId();
assertThat(actionProfileGroupId, is(1));
}
use of p4.config.v1.P4InfoOuterClass.Table in project onos by opennetworkinglab.
the class P4InfoParserTest method testParseP4RuntimeTranslationAndOptional.
/**
* Tests parse method with P4Runtime translation fields and optional fields.
* @throws Exception if equality group objects dose not match as expected
*/
@Test
public void testParseP4RuntimeTranslationAndOptional() throws Exception {
PiPipelineModel model = P4InfoParser.parse(p4InfoUrl2);
// Generate a P4Info object from the file
final P4Info p4info;
try {
p4info = getP4InfoMessage(p4InfoUrl2);
} 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());
// parse tables
PiTableModel table0Model = model.table(table0Id).orElse(null);
// 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;
case OPTIONAL:
piMatchType = PiMatchType.OPTIONAL;
break;
default:
Assert.fail();
return;
}
if (matchFieldIter.getTypeName().getName().equals("mac_addr_t")) {
piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()), P4MatchFieldModel.BIT_WIDTH_UNDEFINED, piMatchType));
} else {
piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()), matchFieldIter.getBitwidth(), piMatchType));
}
}
assertThat("Incorrect order for matchFields", table0Model.matchFields(), IsIterableContainingInOrder.contains(piMatchFieldList.get(0), piMatchFieldList.get(1), piMatchFieldList.get(2), piMatchFieldList.get(3)));
// check table0 actionsRefs
List<ActionRef> actionRefList = tableMsgs.get(0).getActionRefsList();
assertThat("Incorrect size for actionRefs", actionRefList.size(), is(equalTo(4)));
// create action instances
// Set egress with string as parameter
PiActionId actionId = PiActionId.of("set_egress_port");
PiActionParamId piActionParamId = PiActionParamId.of("port");
PiActionParamModel actionParamModel = new P4ActionParamModel(piActionParamId, P4ActionParamModel.BIT_WIDTH_UNDEFINED);
ImmutableMap<PiActionParamId, PiActionParamModel> params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
PiActionModel setEgressPortAction = new P4ActionModel(actionId, params);
// Set egress with 32 bit as parameter
actionId = PiActionId.of("set_egress_port2");
piActionParamId = PiActionParamId.of("port");
int bitWitdth = 32;
actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
PiActionModel setEgressPortAction2 = 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());
// check table0 actions
assertThat("action dose not match", table0Model.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(setEgressPortAction, setEgressPortAction2, sendToCpuAction, dropAction));
}
use of p4.config.v1.P4InfoOuterClass.Table in project up4 by omec-project.
the class Up4NorthComponent method readEntriesAndTranslate.
/**
* Find all table entries or meter entries that match the requested entry,
* and translate them to p4runtime entities for responding to a read request.
*
* @param requestedEntry the entry from a p4runtime read request
* @return all entries that match the request, translated to p4runtime entities
* @throws StatusException if the requested entry fails translation
*/
private List<P4RuntimeOuterClass.Entity> readEntriesAndTranslate(PiEntity requestedEntry) throws StatusException {
List<P4RuntimeOuterClass.Entity> translatedEntries = new ArrayList<>();
// TODO: return more specific responses matching the requested entry
try {
UpfEntityType entityType = up4Translator.getEntityType(requestedEntry);
boolean isMeter = entityType.equals(UpfEntityType.SESSION_METER) || entityType.equals(UpfEntityType.APPLICATION_METER);
Collection<? extends UpfEntity> entities = up4Service.readAll(entityType);
for (UpfEntity entity : entities) {
log.debug("Translating a {} entity for a read request: {}", entity.type(), entity);
P4RuntimeOuterClass.Entity responseEntity;
if (isMeter) {
responseEntity = Codecs.CODECS.entity().encode(up4Translator.upfEntityToUp4MeterEntry(entity), null, pipeconf);
} else {
responseEntity = Codecs.CODECS.entity().encode(up4Translator.upfEntityToUp4TableEntry(entity), null, pipeconf);
}
translatedEntries.add(responseEntity);
}
} catch (Up4Translator.Up4TranslationException | UpfProgrammableException | CodecException e) {
log.warn("Unable to encode/translate a read entry to a UP4 read response: {}", e.getMessage());
throw INVALID_ARGUMENT.withDescription("Unable to translate a read table entry to a p4runtime entity.").asException();
}
return translatedEntries;
}
Aggregations