blob: 8278b3ec1ec6e274ad15b2948780326a13732e21 [file] [log] [blame]
Carmelo Cascone8d99b172017-07-18 17:26:31 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Cascone8d99b172017-07-18 17:26:31 -04003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.p4runtime.ctl;
18
19import com.google.common.collect.ImmutableList;
Yi Tseng82512da2017-08-16 19:46:36 -070020import com.google.common.collect.Lists;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040021import com.google.protobuf.ByteString;
22import org.onlab.util.ImmutableByteSequence;
23import org.onosproject.net.pi.model.PiPipeconf;
24import org.onosproject.net.pi.runtime.PiAction;
Yi Tseng82512da2017-08-16 19:46:36 -070025import org.onosproject.net.pi.runtime.PiActionGroupId;
26import org.onosproject.net.pi.runtime.PiActionGroupMemberId;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040027import org.onosproject.net.pi.runtime.PiActionId;
28import org.onosproject.net.pi.runtime.PiActionParam;
29import org.onosproject.net.pi.runtime.PiActionParamId;
30import org.onosproject.net.pi.runtime.PiExactFieldMatch;
31import org.onosproject.net.pi.runtime.PiFieldMatch;
32import org.onosproject.net.pi.runtime.PiHeaderFieldId;
33import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020034import org.onosproject.net.pi.runtime.PiMatchKey;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040035import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
36import org.onosproject.net.pi.runtime.PiTableAction;
37import org.onosproject.net.pi.runtime.PiTableEntry;
38import org.onosproject.net.pi.runtime.PiTableId;
39import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
40import org.onosproject.net.pi.runtime.PiValidFieldMatch;
41import org.slf4j.Logger;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040042import p4.P4RuntimeOuterClass.FieldMatch;
43import p4.P4RuntimeOuterClass.TableAction;
44import p4.P4RuntimeOuterClass.TableEntry;
Yi Tseng82512da2017-08-16 19:46:36 -070045import p4.P4RuntimeOuterClass.Action;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040046import p4.config.P4InfoOuterClass;
47
48import java.util.Collection;
49import java.util.Collections;
Yi Tseng82512da2017-08-16 19:46:36 -070050import java.util.List;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040051
52import static java.lang.String.format;
53import static org.onlab.util.ImmutableByteSequence.copyFrom;
Yi Tseng82512da2017-08-16 19:46:36 -070054import static org.onosproject.p4runtime.ctl.P4RuntimeUtils.assertPrefixLen;
55import static org.onosproject.p4runtime.ctl.P4RuntimeUtils.assertSize;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040056import static org.slf4j.LoggerFactory.getLogger;
57
58/**
Yi Tseng82512da2017-08-16 19:46:36 -070059 * Encoder/Decoder of table entries, from ONOS Pi* format, to P4Runtime protobuf messages, and vice versa.
Carmelo Cascone8d99b172017-07-18 17:26:31 -040060 */
61final class TableEntryEncoder {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040062 private static final Logger log = getLogger(TableEntryEncoder.class);
63
64 private static final String HEADER_PREFIX = "hdr.";
65 private static final String VALUE_OF_PREFIX = "value of ";
66 private static final String MASK_OF_PREFIX = "mask of ";
67 private static final String HIGH_RANGE_VALUE_OF_PREFIX = "high range value of ";
68 private static final String LOW_RANGE_VALUE_OF_PREFIX = "low range value of ";
69
70 // TODO: implement cache of encoded entities.
71
72 private TableEntryEncoder() {
73 // hide.
74 }
75
76 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +020077 * Returns a collection of P4Runtime table entry protobuf messages, encoded from the given collection of PI table
78 * entries for the given pipeconf. If a PI table entry cannot be encoded, it is skipped, hence the returned
Carmelo Cascone8d99b172017-07-18 17:26:31 -040079 * collection might have different size than the input one.
80 * <p>
81 * Please check the log for an explanation of any error that might have occurred.
82 *
83 * @param piTableEntries PI table entries
84 * @param pipeconf PI pipeconf
85 * @return collection of P4Runtime table entry protobuf messages
86 */
87 static Collection<TableEntry> encode(Collection<PiTableEntry> piTableEntries, PiPipeconf pipeconf) {
88
89 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
90
91 if (browser == null) {
92 log.error("Unable to get a P4Info browser for pipeconf {}, skipping encoding of all table entries");
93 return Collections.emptyList();
94 }
95
96 ImmutableList.Builder<TableEntry> tableEntryMsgListBuilder = ImmutableList.builder();
97
98 for (PiTableEntry piTableEntry : piTableEntries) {
99 try {
100 tableEntryMsgListBuilder.add(encodePiTableEntry(piTableEntry, browser));
101 } catch (P4InfoBrowser.NotFoundException | EncodeException e) {
102 log.error("Unable to encode PI table entry: {}", e.getMessage());
103 }
104 }
105
106 return tableEntryMsgListBuilder.build();
107 }
108
109 /**
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200110 * Same as {@link #encode(Collection, PiPipeconf)} but encodes only one entry.
111 *
112 * @param piTableEntry table entry
113 * @param pipeconf pipeconf
114 * @return encoded table entry message
115 * @throws EncodeException if entry cannot be encoded
116 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
117 */
118 static TableEntry encode(PiTableEntry piTableEntry, PiPipeconf pipeconf)
119 throws EncodeException, P4InfoBrowser.NotFoundException {
120
121 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
122 if (browser == null) {
123 throw new EncodeException(format("Unable to get a P4Info browser for pipeconf %s", pipeconf.id()));
124 }
125
126 return encodePiTableEntry(piTableEntry, browser);
127 }
128
129 /**
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400130 * Returns a collection of PI table entry objects, decoded from the given collection of P4Runtime table entry
131 * messages for the given pipeconf. If a table entry message cannot be decoded, it is skipped, hence the returned
132 * collection might have different size than the input one.
133 * <p>
134 * Please check the log for an explanation of any error that might have occurred.
135 *
136 * @param tableEntryMsgs P4Runtime table entry messages
137 * @param pipeconf PI pipeconf
138 * @return collection of PI table entry objects
139 */
140 static Collection<PiTableEntry> decode(Collection<TableEntry> tableEntryMsgs, PiPipeconf pipeconf) {
141
142 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
143
144 if (browser == null) {
145 log.error("Unable to get a P4Info browser for pipeconf {}, skipping decoding of all table entries");
146 return Collections.emptyList();
147 }
148
149 ImmutableList.Builder<PiTableEntry> piTableEntryListBuilder = ImmutableList.builder();
150
151 for (TableEntry tableEntryMsg : tableEntryMsgs) {
152 try {
153 piTableEntryListBuilder.add(decodeTableEntryMsg(tableEntryMsg, browser));
154 } catch (P4InfoBrowser.NotFoundException | EncodeException e) {
155 log.error("Unable to decode table entry message: {}", e.getMessage());
156 }
157 }
158
159 return piTableEntryListBuilder.build();
160 }
161
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200162 /**
163 * Same as {@link #decode(Collection, PiPipeconf)} but decodes only one entry.
164 *
165 * @param tableEntryMsg table entry message
166 * @param pipeconf pipeconf
167 * @return decoded PI table entry
168 * @throws EncodeException if message cannot be decoded
169 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
170 */
171 static PiTableEntry decode(TableEntry tableEntryMsg, PiPipeconf pipeconf)
172 throws EncodeException, P4InfoBrowser.NotFoundException {
173
174 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
175 if (browser == null) {
176 throw new EncodeException(format("Unable to get a P4Info browser for pipeconf %s", pipeconf.id()));
177 }
178 return decodeTableEntryMsg(tableEntryMsg, browser);
179 }
180
181 /**
182 * Returns a table entry protobuf message, encoded from the given table id and match key, for the given pipeconf.
183 * The returned table entry message can be only used to reference an existing entry, i.e. a read operation, and not
184 * a write one wince it misses other fields (action, priority, etc.).
185 *
186 * @param tableId table identifier
187 * @param matchKey match key
188 * @param pipeconf pipeconf
189 * @return table entry message
190 * @throws EncodeException if message cannot be encoded
191 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
192 */
193 static TableEntry encode(PiTableId tableId, PiMatchKey matchKey, PiPipeconf pipeconf)
194 throws EncodeException, P4InfoBrowser.NotFoundException {
195
196 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
197 TableEntry.Builder tableEntryMsgBuilder = TableEntry.newBuilder();
198
199 //FIXME this throws some kind of NPE
200 P4InfoOuterClass.Table tableInfo = browser.tables().getByName(tableId.id());
201
202 // Table id.
203 tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId());
204
205 // Field matches.
206 for (PiFieldMatch piFieldMatch : matchKey.fieldMatches()) {
207 tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser));
208 }
209
210 return tableEntryMsgBuilder.build();
211 }
212
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400213 private static TableEntry encodePiTableEntry(PiTableEntry piTableEntry, P4InfoBrowser browser)
214 throws P4InfoBrowser.NotFoundException, EncodeException {
215
216 TableEntry.Builder tableEntryMsgBuilder = TableEntry.newBuilder();
217
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200218 //FIXME this throws some kind of NPE
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400219 P4InfoOuterClass.Table tableInfo = browser.tables().getByName(piTableEntry.table().id());
220
221 // Table id.
222 tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId());
223
224 // Priority.
Yi Tseng088f4ce2017-08-15 10:54:14 -0700225 // FIXME: check on P4Runtime if/what is the default priority.
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400226 int priority = piTableEntry.priority().orElse(0);
227 tableEntryMsgBuilder.setPriority(priority);
228
229 // Controller metadata (cookie)
230 tableEntryMsgBuilder.setControllerMetadata(piTableEntry.cookie());
231
232 // Timeout.
233 if (piTableEntry.timeout().isPresent()) {
234 log.warn("Found PI table entry with timeout set, not supported in P4Runtime: {}", piTableEntry);
235 }
236
237 // Table action.
238 tableEntryMsgBuilder.setAction(encodePiTableAction(piTableEntry.action(), browser));
239
240 // Field matches.
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200241 for (PiFieldMatch piFieldMatch : piTableEntry.matchKey().fieldMatches()) {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400242 tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser));
243 }
244
245 return tableEntryMsgBuilder.build();
246 }
247
248 private static PiTableEntry decodeTableEntryMsg(TableEntry tableEntryMsg, P4InfoBrowser browser)
249 throws P4InfoBrowser.NotFoundException, EncodeException {
250
251 PiTableEntry.Builder piTableEntryBuilder = PiTableEntry.builder();
252
253 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
254
255 // Table id.
256 piTableEntryBuilder.forTable(PiTableId.of(tableInfo.getPreamble().getName()));
257
258 // Priority.
259 piTableEntryBuilder.withPriority(tableEntryMsg.getPriority());
260
261 // Controller metadata (cookie)
262 piTableEntryBuilder.withCookie(tableEntryMsg.getControllerMetadata());
263
264 // Table action.
265 piTableEntryBuilder.withAction(decodeTableActionMsg(tableEntryMsg.getAction(), browser));
266
267 // Timeout.
268 // FIXME: how to decode table entry messages with timeout, given that the timeout value is lost after encoding?
269
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200270 // Match key for field matches.
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200271 piTableEntryBuilder.withMatchKey(decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser));
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400272
273 return piTableEntryBuilder.build();
274 }
275
276 private static FieldMatch encodePiFieldMatch(PiFieldMatch piFieldMatch, P4InfoOuterClass.Table tableInfo,
277 P4InfoBrowser browser)
278 throws P4InfoBrowser.NotFoundException, EncodeException {
279
280 FieldMatch.Builder fieldMatchMsgBuilder = FieldMatch.newBuilder();
281
282 // FIXME: check how field names for stacked headers are constructed in P4Runtime.
283 String fieldName = piFieldMatch.fieldId().id();
284 int tableId = tableInfo.getPreamble().getId();
285 P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tableId).getByNameOrAlias(fieldName);
286 String entityName = format("field match '%s' of table '%s'",
287 matchFieldInfo.getName(), tableInfo.getPreamble().getName());
288 int fieldId = matchFieldInfo.getId();
289 int fieldBitwidth = matchFieldInfo.getBitwidth();
290
291 fieldMatchMsgBuilder.setFieldId(fieldId);
292
293 switch (piFieldMatch.type()) {
294 case EXACT:
295 PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
296 ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
297 assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
298 return fieldMatchMsgBuilder.setExact(
299 FieldMatch.Exact
300 .newBuilder()
301 .setValue(exactValue)
302 .build())
303 .build();
304 case TERNARY:
305 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
306 ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer());
307 ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer());
308 assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
309 assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
310 return fieldMatchMsgBuilder.setTernary(
311 FieldMatch.Ternary
312 .newBuilder()
313 .setValue(ternaryValue)
314 .setMask(ternaryMask)
315 .build())
316 .build();
317 case LPM:
318 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
319 ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer());
320 int lpmPrefixLen = lpmMatch.prefixLength();
321 assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
322 assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
323 return fieldMatchMsgBuilder.setLpm(
324 FieldMatch.LPM.newBuilder()
325 .setValue(lpmValue)
326 .setPrefixLen(lpmPrefixLen)
327 .build())
328 .build();
329 case RANGE:
330 PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
331 ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer());
332 ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer());
333 assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
334 assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
335 return fieldMatchMsgBuilder.setRange(
336 FieldMatch.Range.newBuilder()
337 .setHigh(rangeHighValue)
338 .setLow(rangeLowValue)
339 .build())
340 .build();
341 case VALID:
342 PiValidFieldMatch validMatch = (PiValidFieldMatch) piFieldMatch;
343 return fieldMatchMsgBuilder.setValid(
344 FieldMatch.Valid.newBuilder()
345 .setValue(validMatch.isValid())
346 .build())
347 .build();
348 default:
349 throw new EncodeException(format(
350 "Building of match type %s not implemented", piFieldMatch.type()));
351 }
352 }
353
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200354 /**
355 * Returns a PI match key, decoded from the given table entry protobuf message, for the given pipeconf.
356 *
357 * @param tableEntryMsg table entry message
358 * @param pipeconf pipeconf
359 * @return PI match key
360 * @throws EncodeException if message cannot be decoded
361 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
362 */
363 static PiMatchKey decodeMatchKey(TableEntry tableEntryMsg, PiPipeconf pipeconf)
364 throws P4InfoBrowser.NotFoundException, EncodeException {
365 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
366 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
367 return decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser);
368 }
369
370 private static PiMatchKey decodeFieldMatchMsgs(Collection<FieldMatch> fieldMatchs, P4InfoOuterClass.Table tableInfo,
371 P4InfoBrowser browser)
372 throws P4InfoBrowser.NotFoundException, EncodeException {
373 // Match key for field matches.
374 PiMatchKey.Builder piMatchKeyBuilder = PiMatchKey.builder();
375 for (FieldMatch fieldMatchMsg : fieldMatchs) {
376 piMatchKeyBuilder.addFieldMatch(decodeFieldMatchMsg(fieldMatchMsg, tableInfo, browser));
377 }
378 return piMatchKeyBuilder.build();
379 }
380
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400381 private static PiFieldMatch decodeFieldMatchMsg(FieldMatch fieldMatchMsg, P4InfoOuterClass.Table tableInfo,
382 P4InfoBrowser browser)
383 throws P4InfoBrowser.NotFoundException, EncodeException {
384
385 int tableId = tableInfo.getPreamble().getId();
386 String fieldMatchName = browser.matchFields(tableId).getById(fieldMatchMsg.getFieldId()).getName();
387 if (fieldMatchName.startsWith(HEADER_PREFIX)) {
388 fieldMatchName = fieldMatchName.substring(HEADER_PREFIX.length());
389 }
390
391 // FIXME: Add support for decoding of stacked header names.
392 String[] pieces = fieldMatchName.split("\\.");
393 if (pieces.length != 2) {
394 throw new EncodeException(format("unrecognized field match name '%s'", fieldMatchName));
395 }
396 PiHeaderFieldId headerFieldId = PiHeaderFieldId.of(pieces[0], pieces[1]);
397
398 FieldMatch.FieldMatchTypeCase typeCase = fieldMatchMsg.getFieldMatchTypeCase();
399
400 switch (typeCase) {
401 case EXACT:
402 FieldMatch.Exact exactFieldMatch = fieldMatchMsg.getExact();
403 ImmutableByteSequence exactValue = copyFrom(exactFieldMatch.getValue().asReadOnlyByteBuffer());
404 return new PiExactFieldMatch(headerFieldId, exactValue);
405 case TERNARY:
406 FieldMatch.Ternary ternaryFieldMatch = fieldMatchMsg.getTernary();
407 ImmutableByteSequence ternaryValue = copyFrom(ternaryFieldMatch.getValue().asReadOnlyByteBuffer());
408 ImmutableByteSequence ternaryMask = copyFrom(ternaryFieldMatch.getMask().asReadOnlyByteBuffer());
409 return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
410 case LPM:
411 FieldMatch.LPM lpmFieldMatch = fieldMatchMsg.getLpm();
412 ImmutableByteSequence lpmValue = copyFrom(lpmFieldMatch.getValue().asReadOnlyByteBuffer());
413 int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
414 return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
415 case RANGE:
416 FieldMatch.Range rangeFieldMatch = fieldMatchMsg.getRange();
417 ImmutableByteSequence rangeHighValue = copyFrom(rangeFieldMatch.getHigh().asReadOnlyByteBuffer());
418 ImmutableByteSequence rangeLowValue = copyFrom(rangeFieldMatch.getLow().asReadOnlyByteBuffer());
419 return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
420 case VALID:
421 FieldMatch.Valid validFieldMatch = fieldMatchMsg.getValid();
422 return new PiValidFieldMatch(headerFieldId, validFieldMatch.getValue());
423 default:
424 throw new EncodeException(format(
425 "Decoding of field match type '%s' not implemented", typeCase.name()));
426 }
427 }
428
Yi Tseng82512da2017-08-16 19:46:36 -0700429 static TableAction encodePiTableAction(PiTableAction piTableAction, P4InfoBrowser browser)
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400430 throws P4InfoBrowser.NotFoundException, EncodeException {
431
432 TableAction.Builder tableActionMsgBuilder = TableAction.newBuilder();
433
434 switch (piTableAction.type()) {
435 case ACTION:
436 PiAction piAction = (PiAction) piTableAction;
Yi Tseng82512da2017-08-16 19:46:36 -0700437 Action theAction = encodePiAction(piAction, browser);
438 tableActionMsgBuilder.setAction(theAction);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400439 break;
Yi Tseng82512da2017-08-16 19:46:36 -0700440 case ACTION_GROUP_ID:
441 PiActionGroupId actionGroupId = (PiActionGroupId) piTableAction;
442 tableActionMsgBuilder.setActionProfileGroupId(actionGroupId.id());
443 break;
444 case GROUP_MEMBER_ID:
445 PiActionGroupMemberId actionGroupMemberId = (PiActionGroupMemberId) piTableAction;
446 tableActionMsgBuilder.setActionProfileMemberId(actionGroupMemberId.id());
447 break;
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400448 default:
449 throw new EncodeException(
450 format("Building of table action type %s not implemented", piTableAction.type()));
451 }
452
453 return tableActionMsgBuilder.build();
454 }
455
Yi Tseng82512da2017-08-16 19:46:36 -0700456 static PiTableAction decodeTableActionMsg(TableAction tableActionMsg, P4InfoBrowser browser)
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400457 throws P4InfoBrowser.NotFoundException, EncodeException {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400458 TableAction.TypeCase typeCase = tableActionMsg.getTypeCase();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400459 switch (typeCase) {
460 case ACTION:
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400461 Action actionMsg = tableActionMsg.getAction();
Yi Tseng82512da2017-08-16 19:46:36 -0700462 return decodeActionMsg(actionMsg, browser);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400463 default:
464 throw new EncodeException(
465 format("Decoding of table action type %s not implemented", typeCase.name()));
466 }
467 }
468
Yi Tseng82512da2017-08-16 19:46:36 -0700469 static Action encodePiAction(PiAction piAction, P4InfoBrowser browser)
470 throws P4InfoBrowser.NotFoundException, EncodeException {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400471
Yi Tseng82512da2017-08-16 19:46:36 -0700472 int actionId = browser.actions().getByName(piAction.id().name()).getPreamble().getId();
473
474 Action.Builder actionMsgBuilder =
475 Action.newBuilder().setActionId(actionId);
476
477 for (PiActionParam p : piAction.parameters()) {
478 P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId).getByName(p.id().name());
479 ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
480 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
481 paramValue, paramInfo.getBitwidth());
482 actionMsgBuilder.addParams(Action.Param.newBuilder()
483 .setParamId(paramInfo.getId())
484 .setValue(paramValue)
485 .build());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400486 }
Yi Tseng82512da2017-08-16 19:46:36 -0700487
488 return actionMsgBuilder.build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400489 }
490
Yi Tseng82512da2017-08-16 19:46:36 -0700491 static PiAction decodeActionMsg(Action action, P4InfoBrowser browser)
492 throws P4InfoBrowser.NotFoundException {
493 P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
494 browser.actionParams(action.getActionId());
495 String actionName = browser.actions()
496 .getById(action.getActionId())
497 .getPreamble().getName();
498 PiActionId id = PiActionId.of(actionName);
499 List<PiActionParam> params = Lists.newArrayList();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400500
Yi Tseng82512da2017-08-16 19:46:36 -0700501 for (Action.Param p : action.getParamsList()) {
502 String paramName = paramInfo.getById(p.getParamId()).getName();
503 ImmutableByteSequence value = ImmutableByteSequence.copyFrom(p.getValue().toByteArray());
504 params.add(new PiActionParam(PiActionParamId.of(paramName), value));
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400505 }
Yi Tseng82512da2017-08-16 19:46:36 -0700506 return PiAction.builder().withId(id).withParameters(params).build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400507 }
508}