blob: 9d46413344ec5c60d982cabea75dc4b4296f77a4 [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;
Carmelo Cascone87892e22017-11-13 16:01:29 -080023import org.onosproject.net.pi.model.PiActionId;
24import org.onosproject.net.pi.model.PiActionParamId;
25import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040026import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Cascone87892e22017-11-13 16:01:29 -080027import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040028import org.onosproject.net.pi.runtime.PiAction;
Yi Tseng82512da2017-08-16 19:46:36 -070029import org.onosproject.net.pi.runtime.PiActionGroupId;
30import org.onosproject.net.pi.runtime.PiActionGroupMemberId;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040031import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040032import org.onosproject.net.pi.runtime.PiExactFieldMatch;
33import org.onosproject.net.pi.runtime.PiFieldMatch;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040034import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
Carmelo Cascone0e896a02017-07-31 07:22:27 +020035import org.onosproject.net.pi.runtime.PiMatchKey;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040036import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
37import org.onosproject.net.pi.runtime.PiTableAction;
38import org.onosproject.net.pi.runtime.PiTableEntry;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040039import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
40import org.onosproject.net.pi.runtime.PiValidFieldMatch;
41import org.slf4j.Logger;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020042import p4.P4RuntimeOuterClass.Action;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040043import p4.P4RuntimeOuterClass.FieldMatch;
44import p4.P4RuntimeOuterClass.TableAction;
45import p4.P4RuntimeOuterClass.TableEntry;
46import 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
Carmelo Cascone8d99b172017-07-18 17:26:31 -040064 private static final String VALUE_OF_PREFIX = "value of ";
65 private static final String MASK_OF_PREFIX = "mask of ";
66 private static final String HIGH_RANGE_VALUE_OF_PREFIX = "high range value of ";
67 private static final String LOW_RANGE_VALUE_OF_PREFIX = "low range value of ";
68
69 // TODO: implement cache of encoded entities.
70
71 private TableEntryEncoder() {
72 // hide.
73 }
74
75 /**
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080076 * Returns a collection of P4Runtime table entry protobuf messages, encoded
77 * from the given collection of PI table entries for the given pipeconf. If
78 * a PI table entry cannot be encoded, an EncodeException is thrown.
Carmelo Cascone8d99b172017-07-18 17:26:31 -040079 *
80 * @param piTableEntries PI table entries
81 * @param pipeconf PI pipeconf
82 * @return collection of P4Runtime table entry protobuf messages
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080083 * @throws EncodeException if a PI table entry cannot be encoded
Carmelo Cascone8d99b172017-07-18 17:26:31 -040084 */
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080085 static Collection<TableEntry> encode(Collection<PiTableEntry> piTableEntries,
86 PiPipeconf pipeconf)
87 throws EncodeException {
Carmelo Cascone8d99b172017-07-18 17:26:31 -040088
89 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
90
91 if (browser == null) {
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080092 throw new EncodeException(format(
93 "Unable to get a P4Info browser for pipeconf %s", pipeconf.id()));
Carmelo Cascone8d99b172017-07-18 17:26:31 -040094 }
95
96 ImmutableList.Builder<TableEntry> tableEntryMsgListBuilder = ImmutableList.builder();
97
98 for (PiTableEntry piTableEntry : piTableEntries) {
99 try {
100 tableEntryMsgListBuilder.add(encodePiTableEntry(piTableEntry, browser));
Carmelo Cascone5bc7e102018-02-18 18:27:55 -0800101 } catch (P4InfoBrowser.NotFoundException e) {
102 throw new EncodeException(e.getMessage());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400103 }
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
Carmelo Casconecb0a49c2017-10-03 14:32:23 +0200199 P4InfoOuterClass.Table tableInfo = browser.tables().getByName(tableId.id());
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200200
201 // Table id.
202 tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId());
203
204 // Field matches.
Carmelo Cascone4256bde2018-03-23 18:02:15 -0700205 if (matchKey.equals(PiMatchKey.EMPTY)) {
206 tableEntryMsgBuilder.setIsDefaultAction(true);
207 } else {
208 for (PiFieldMatch piFieldMatch : matchKey.fieldMatches()) {
209 tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser));
210 }
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200211 }
212
213 return tableEntryMsgBuilder.build();
214 }
215
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400216 private static TableEntry encodePiTableEntry(PiTableEntry piTableEntry, P4InfoBrowser browser)
217 throws P4InfoBrowser.NotFoundException, EncodeException {
218
219 TableEntry.Builder tableEntryMsgBuilder = TableEntry.newBuilder();
220
Carmelo Casconecb0a49c2017-10-03 14:32:23 +0200221 P4InfoOuterClass.Table tableInfo = browser.tables().getByName(piTableEntry.table().id());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400222
223 // Table id.
224 tableEntryMsgBuilder.setTableId(tableInfo.getPreamble().getId());
225
226 // Priority.
Yi Tseng088f4ce2017-08-15 10:54:14 -0700227 // FIXME: check on P4Runtime if/what is the default priority.
Yi Tseng02c4c572018-01-22 17:52:10 -0800228 piTableEntry.priority().ifPresent(tableEntryMsgBuilder::setPriority);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400229
230 // Controller metadata (cookie)
231 tableEntryMsgBuilder.setControllerMetadata(piTableEntry.cookie());
232
233 // Timeout.
234 if (piTableEntry.timeout().isPresent()) {
235 log.warn("Found PI table entry with timeout set, not supported in P4Runtime: {}", piTableEntry);
236 }
237
238 // Table action.
239 tableEntryMsgBuilder.setAction(encodePiTableAction(piTableEntry.action(), browser));
240
241 // Field matches.
Carmelo Cascone4256bde2018-03-23 18:02:15 -0700242 if (piTableEntry.matchKey().equals(PiMatchKey.EMPTY)) {
243 tableEntryMsgBuilder.setIsDefaultAction(true);
244 } else {
245 for (PiFieldMatch piFieldMatch : piTableEntry.matchKey().fieldMatches()) {
246 tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser));
247 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400248 }
249
250 return tableEntryMsgBuilder.build();
251 }
252
253 private static PiTableEntry decodeTableEntryMsg(TableEntry tableEntryMsg, P4InfoBrowser browser)
254 throws P4InfoBrowser.NotFoundException, EncodeException {
255
256 PiTableEntry.Builder piTableEntryBuilder = PiTableEntry.builder();
257
258 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
259
260 // Table id.
261 piTableEntryBuilder.forTable(PiTableId.of(tableInfo.getPreamble().getName()));
262
263 // Priority.
264 piTableEntryBuilder.withPriority(tableEntryMsg.getPriority());
265
266 // Controller metadata (cookie)
267 piTableEntryBuilder.withCookie(tableEntryMsg.getControllerMetadata());
268
269 // Table action.
270 piTableEntryBuilder.withAction(decodeTableActionMsg(tableEntryMsg.getAction(), browser));
271
272 // Timeout.
273 // FIXME: how to decode table entry messages with timeout, given that the timeout value is lost after encoding?
274
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200275 // Match key for field matches.
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200276 piTableEntryBuilder.withMatchKey(decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser));
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400277
278 return piTableEntryBuilder.build();
279 }
280
281 private static FieldMatch encodePiFieldMatch(PiFieldMatch piFieldMatch, P4InfoOuterClass.Table tableInfo,
282 P4InfoBrowser browser)
283 throws P4InfoBrowser.NotFoundException, EncodeException {
284
285 FieldMatch.Builder fieldMatchMsgBuilder = FieldMatch.newBuilder();
286
287 // FIXME: check how field names for stacked headers are constructed in P4Runtime.
288 String fieldName = piFieldMatch.fieldId().id();
289 int tableId = tableInfo.getPreamble().getId();
Carmelo Casconecb0a49c2017-10-03 14:32:23 +0200290 P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tableId).getByName(fieldName);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400291 String entityName = format("field match '%s' of table '%s'",
292 matchFieldInfo.getName(), tableInfo.getPreamble().getName());
293 int fieldId = matchFieldInfo.getId();
294 int fieldBitwidth = matchFieldInfo.getBitwidth();
295
296 fieldMatchMsgBuilder.setFieldId(fieldId);
297
298 switch (piFieldMatch.type()) {
299 case EXACT:
300 PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
301 ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
302 assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
303 return fieldMatchMsgBuilder.setExact(
304 FieldMatch.Exact
305 .newBuilder()
306 .setValue(exactValue)
307 .build())
308 .build();
309 case TERNARY:
310 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
311 ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer());
312 ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer());
313 assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
314 assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
315 return fieldMatchMsgBuilder.setTernary(
316 FieldMatch.Ternary
317 .newBuilder()
318 .setValue(ternaryValue)
319 .setMask(ternaryMask)
320 .build())
321 .build();
322 case LPM:
323 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
324 ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer());
325 int lpmPrefixLen = lpmMatch.prefixLength();
326 assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
327 assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
328 return fieldMatchMsgBuilder.setLpm(
329 FieldMatch.LPM.newBuilder()
330 .setValue(lpmValue)
331 .setPrefixLen(lpmPrefixLen)
332 .build())
333 .build();
334 case RANGE:
335 PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
336 ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer());
337 ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer());
338 assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
339 assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
340 return fieldMatchMsgBuilder.setRange(
341 FieldMatch.Range.newBuilder()
342 .setHigh(rangeHighValue)
343 .setLow(rangeLowValue)
344 .build())
345 .build();
346 case VALID:
347 PiValidFieldMatch validMatch = (PiValidFieldMatch) piFieldMatch;
348 return fieldMatchMsgBuilder.setValid(
349 FieldMatch.Valid.newBuilder()
350 .setValue(validMatch.isValid())
351 .build())
352 .build();
353 default:
354 throw new EncodeException(format(
355 "Building of match type %s not implemented", piFieldMatch.type()));
356 }
357 }
358
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200359 /**
360 * Returns a PI match key, decoded from the given table entry protobuf message, for the given pipeconf.
361 *
362 * @param tableEntryMsg table entry message
363 * @param pipeconf pipeconf
364 * @return PI match key
365 * @throws EncodeException if message cannot be decoded
366 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
367 */
368 static PiMatchKey decodeMatchKey(TableEntry tableEntryMsg, PiPipeconf pipeconf)
369 throws P4InfoBrowser.NotFoundException, EncodeException {
370 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
371 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
Carmelo Cascone4256bde2018-03-23 18:02:15 -0700372 if (tableEntryMsg.getMatchCount() == 0) {
373 return PiMatchKey.EMPTY;
374 } else {
375 return decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser);
376 }
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200377 }
378
379 private static PiMatchKey decodeFieldMatchMsgs(Collection<FieldMatch> fieldMatchs, P4InfoOuterClass.Table tableInfo,
380 P4InfoBrowser browser)
381 throws P4InfoBrowser.NotFoundException, EncodeException {
382 // Match key for field matches.
383 PiMatchKey.Builder piMatchKeyBuilder = PiMatchKey.builder();
384 for (FieldMatch fieldMatchMsg : fieldMatchs) {
385 piMatchKeyBuilder.addFieldMatch(decodeFieldMatchMsg(fieldMatchMsg, tableInfo, browser));
386 }
387 return piMatchKeyBuilder.build();
388 }
389
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400390 private static PiFieldMatch decodeFieldMatchMsg(FieldMatch fieldMatchMsg, P4InfoOuterClass.Table tableInfo,
391 P4InfoBrowser browser)
392 throws P4InfoBrowser.NotFoundException, EncodeException {
393
394 int tableId = tableInfo.getPreamble().getId();
395 String fieldMatchName = browser.matchFields(tableId).getById(fieldMatchMsg.getFieldId()).getName();
Carmelo Cascone87892e22017-11-13 16:01:29 -0800396 PiMatchFieldId headerFieldId = PiMatchFieldId.of(fieldMatchName);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400397
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);
Yi Tseng95390822018-01-22 17:57:22 -0800463 case ACTION_PROFILE_GROUP_ID:
464 return PiActionGroupId.of(tableActionMsg.getActionProfileGroupId());
465 case ACTION_PROFILE_MEMBER_ID:
466 return PiActionGroupMemberId.of(tableActionMsg.getActionProfileMemberId());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400467 default:
468 throw new EncodeException(
469 format("Decoding of table action type %s not implemented", typeCase.name()));
470 }
471 }
472
Yi Tseng82512da2017-08-16 19:46:36 -0700473 static Action encodePiAction(PiAction piAction, P4InfoBrowser browser)
474 throws P4InfoBrowser.NotFoundException, EncodeException {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400475
Carmelo Cascone87892e22017-11-13 16:01:29 -0800476 int actionId = browser.actions().getByName(piAction.id().toString()).getPreamble().getId();
Yi Tseng82512da2017-08-16 19:46:36 -0700477
478 Action.Builder actionMsgBuilder =
479 Action.newBuilder().setActionId(actionId);
480
481 for (PiActionParam p : piAction.parameters()) {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800482 P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId).getByName(p.id().toString());
Yi Tseng82512da2017-08-16 19:46:36 -0700483 ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
484 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
485 paramValue, paramInfo.getBitwidth());
486 actionMsgBuilder.addParams(Action.Param.newBuilder()
487 .setParamId(paramInfo.getId())
488 .setValue(paramValue)
489 .build());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400490 }
Yi Tseng82512da2017-08-16 19:46:36 -0700491
492 return actionMsgBuilder.build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400493 }
494
Yi Tseng82512da2017-08-16 19:46:36 -0700495 static PiAction decodeActionMsg(Action action, P4InfoBrowser browser)
496 throws P4InfoBrowser.NotFoundException {
497 P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
498 browser.actionParams(action.getActionId());
499 String actionName = browser.actions()
500 .getById(action.getActionId())
501 .getPreamble().getName();
502 PiActionId id = PiActionId.of(actionName);
503 List<PiActionParam> params = Lists.newArrayList();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400504
Yi Tseng82512da2017-08-16 19:46:36 -0700505 for (Action.Param p : action.getParamsList()) {
506 String paramName = paramInfo.getById(p.getParamId()).getName();
507 ImmutableByteSequence value = ImmutableByteSequence.copyFrom(p.getValue().toByteArray());
508 params.add(new PiActionParam(PiActionParamId.of(paramName), value));
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400509 }
Yi Tseng82512da2017-08-16 19:46:36 -0700510 return PiAction.builder().withId(id).withParameters(params).build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400511 }
512}