use of p4.v1.P4RuntimeOuterClass.Update 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);
}
use of p4.v1.P4RuntimeOuterClass.Update in project onos by opennetworkinglab.
the class P4RuntimeGroupTest method testInsertPiActionProfileGroup.
@Test
public void testInsertPiActionProfileGroup() throws Exception {
CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync();
assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync().isSuccess());
complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
assertEquals(1, result.getDeviceId());
assertEquals(1, result.getUpdatesCount());
assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
Update update = result.getUpdatesList().get(0);
assertEquals(Update.Type.INSERT, update.getType());
Entity entity = update.getEntity();
ActionProfileGroup actionProfileGroup = entity.getActionProfileGroup();
assertNotNull(actionProfileGroup);
assertEquals(P4_INFO_ACT_PROF_ID, actionProfileGroup.getActionProfileId());
assertEquals(3, actionProfileGroup.getMembersCount());
List<ActionProfileGroup.Member> members = actionProfileGroup.getMembersList();
for (ActionProfileGroup.Member member : members) {
// XXX: We can't guarantee the order of member, just make sure we
// have these member ids
assertTrue(MEMBER_IDS.contains(member.getMemberId()));
assertEquals(DEFAULT_MEMBER_WEIGHT, member.getWeight());
}
}
use of p4.v1.P4RuntimeOuterClass.Update in project onos by opennetworkinglab.
the class P4RuntimeGroupTest method testInsertPiActionMembers.
@Test
public void testInsertPiActionMembers() throws Exception {
CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP_MEMBER_INSTANCES).submitSync().isSuccess());
complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
assertEquals(1, result.getDeviceId());
assertEquals(3, result.getUpdatesCount());
assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
List<Update> updates = result.getUpdatesList();
for (Update update : updates) {
assertEquals(Update.Type.INSERT, update.getType());
Entity entity = update.getEntity();
ActionProfileMember member = entity.getActionProfileMember();
assertNotNull(member);
assertEquals(P4_INFO_ACT_PROF_ID, member.getActionProfileId());
assertTrue(MEMBER_IDS.contains(member.getMemberId()));
Action action = member.getAction();
assertEquals(SET_EGRESS_PORT_ID, action.getActionId());
assertEquals(1, action.getParamsCount());
Action.Param param = action.getParamsList().get(0);
assertEquals(1, param.getParamId());
byte outPort = (byte) (member.getMemberId() - BASE_MEM_ID);
ByteString bs = ByteString.copyFrom(new byte[] { 0, outPort });
assertEquals(bs, param.getValue());
}
}
Aggregations