blob: d5d909f1c961357e7a1f05633e642334fd6a2b59 [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;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040040import org.slf4j.Logger;
Carmelo Casconee44592f2018-09-12 02:24:47 -070041import p4.config.v1.P4InfoOuterClass;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020042import p4.v1.P4RuntimeOuterClass.Action;
43import p4.v1.P4RuntimeOuterClass.FieldMatch;
44import p4.v1.P4RuntimeOuterClass.TableAction;
45import p4.v1.P4RuntimeOuterClass.TableEntry;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040046
47import java.util.Collection;
48import java.util.Collections;
Yi Tseng82512da2017-08-16 19:46:36 -070049import java.util.List;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040050
Yi Tsengd28936e2018-02-23 22:11:11 +010051import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040052import 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 Casconee44592f2018-09-12 02:24:47 -070085 static List<TableEntry> encode(List<PiTableEntry> piTableEntries,
Carmelo Cascone5bc7e102018-02-18 18:27:55 -080086 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 */
Carmelo Casconee44592f2018-09-12 02:24:47 -0700140 static List<PiTableEntry> decode(List<TableEntry> tableEntryMsgs, PiPipeconf pipeconf) {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400141
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.
Yi Tsengd28936e2018-02-23 22:11:11 +0100239 if (piTableEntry.action() != null) {
240 tableEntryMsgBuilder.setAction(encodePiTableAction(piTableEntry.action(), browser));
241 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400242
243 // Field matches.
Carmelo Cascone4256bde2018-03-23 18:02:15 -0700244 if (piTableEntry.matchKey().equals(PiMatchKey.EMPTY)) {
245 tableEntryMsgBuilder.setIsDefaultAction(true);
246 } else {
247 for (PiFieldMatch piFieldMatch : piTableEntry.matchKey().fieldMatches()) {
248 tableEntryMsgBuilder.addMatch(encodePiFieldMatch(piFieldMatch, tableInfo, browser));
249 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400250 }
251
252 return tableEntryMsgBuilder.build();
253 }
254
255 private static PiTableEntry decodeTableEntryMsg(TableEntry tableEntryMsg, P4InfoBrowser browser)
256 throws P4InfoBrowser.NotFoundException, EncodeException {
257
258 PiTableEntry.Builder piTableEntryBuilder = PiTableEntry.builder();
259
260 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
261
262 // Table id.
263 piTableEntryBuilder.forTable(PiTableId.of(tableInfo.getPreamble().getName()));
264
265 // Priority.
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700266 if (tableEntryMsg.getPriority() > 0) {
267 piTableEntryBuilder.withPriority(tableEntryMsg.getPriority());
268 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400269
270 // Controller metadata (cookie)
271 piTableEntryBuilder.withCookie(tableEntryMsg.getControllerMetadata());
272
273 // Table action.
Yi Tsengd28936e2018-02-23 22:11:11 +0100274 if (tableEntryMsg.hasAction()) {
275 piTableEntryBuilder.withAction(decodeTableActionMsg(tableEntryMsg.getAction(), browser));
276 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400277
278 // Timeout.
279 // FIXME: how to decode table entry messages with timeout, given that the timeout value is lost after encoding?
280
Carmelo Cascone0e896a02017-07-31 07:22:27 +0200281 // Match key for field matches.
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200282 piTableEntryBuilder.withMatchKey(decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser));
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400283
284 return piTableEntryBuilder.build();
285 }
286
287 private static FieldMatch encodePiFieldMatch(PiFieldMatch piFieldMatch, P4InfoOuterClass.Table tableInfo,
288 P4InfoBrowser browser)
289 throws P4InfoBrowser.NotFoundException, EncodeException {
290
291 FieldMatch.Builder fieldMatchMsgBuilder = FieldMatch.newBuilder();
292
293 // FIXME: check how field names for stacked headers are constructed in P4Runtime.
294 String fieldName = piFieldMatch.fieldId().id();
295 int tableId = tableInfo.getPreamble().getId();
Carmelo Casconecb0a49c2017-10-03 14:32:23 +0200296 P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tableId).getByName(fieldName);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400297 String entityName = format("field match '%s' of table '%s'",
298 matchFieldInfo.getName(), tableInfo.getPreamble().getName());
299 int fieldId = matchFieldInfo.getId();
300 int fieldBitwidth = matchFieldInfo.getBitwidth();
301
302 fieldMatchMsgBuilder.setFieldId(fieldId);
303
304 switch (piFieldMatch.type()) {
305 case EXACT:
306 PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
307 ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
308 assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
309 return fieldMatchMsgBuilder.setExact(
310 FieldMatch.Exact
311 .newBuilder()
312 .setValue(exactValue)
313 .build())
314 .build();
315 case TERNARY:
316 PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
317 ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer());
318 ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer());
319 assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
320 assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
321 return fieldMatchMsgBuilder.setTernary(
322 FieldMatch.Ternary
323 .newBuilder()
324 .setValue(ternaryValue)
325 .setMask(ternaryMask)
326 .build())
327 .build();
328 case LPM:
329 PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
330 ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer());
331 int lpmPrefixLen = lpmMatch.prefixLength();
332 assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
333 assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
334 return fieldMatchMsgBuilder.setLpm(
335 FieldMatch.LPM.newBuilder()
336 .setValue(lpmValue)
337 .setPrefixLen(lpmPrefixLen)
338 .build())
339 .build();
340 case RANGE:
341 PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
342 ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer());
343 ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer());
344 assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
345 assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
346 return fieldMatchMsgBuilder.setRange(
347 FieldMatch.Range.newBuilder()
348 .setHigh(rangeHighValue)
349 .setLow(rangeLowValue)
350 .build())
351 .build();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400352 default:
353 throw new EncodeException(format(
354 "Building of match type %s not implemented", piFieldMatch.type()));
355 }
356 }
357
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200358 /**
359 * Returns a PI match key, decoded from the given table entry protobuf message, for the given pipeconf.
360 *
361 * @param tableEntryMsg table entry message
362 * @param pipeconf pipeconf
363 * @return PI match key
364 * @throws EncodeException if message cannot be decoded
365 * @throws P4InfoBrowser.NotFoundException if the required information cannot be find in the pipeconf's P4info
366 */
367 static PiMatchKey decodeMatchKey(TableEntry tableEntryMsg, PiPipeconf pipeconf)
368 throws P4InfoBrowser.NotFoundException, EncodeException {
369 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
370 P4InfoOuterClass.Table tableInfo = browser.tables().getById(tableEntryMsg.getTableId());
Carmelo Cascone4256bde2018-03-23 18:02:15 -0700371 if (tableEntryMsg.getMatchCount() == 0) {
372 return PiMatchKey.EMPTY;
373 } else {
374 return decodeFieldMatchMsgs(tableEntryMsg.getMatchList(), tableInfo, browser);
375 }
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200376 }
377
Carmelo Casconee44592f2018-09-12 02:24:47 -0700378 private static PiMatchKey decodeFieldMatchMsgs(List<FieldMatch> fieldMatchs, P4InfoOuterClass.Table tableInfo,
Carmelo Cascone7f75be42017-09-07 14:37:02 +0200379 P4InfoBrowser browser)
380 throws P4InfoBrowser.NotFoundException, EncodeException {
381 // Match key for field matches.
382 PiMatchKey.Builder piMatchKeyBuilder = PiMatchKey.builder();
383 for (FieldMatch fieldMatchMsg : fieldMatchs) {
384 piMatchKeyBuilder.addFieldMatch(decodeFieldMatchMsg(fieldMatchMsg, tableInfo, browser));
385 }
386 return piMatchKeyBuilder.build();
387 }
388
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400389 private static PiFieldMatch decodeFieldMatchMsg(FieldMatch fieldMatchMsg, P4InfoOuterClass.Table tableInfo,
390 P4InfoBrowser browser)
391 throws P4InfoBrowser.NotFoundException, EncodeException {
392
393 int tableId = tableInfo.getPreamble().getId();
394 String fieldMatchName = browser.matchFields(tableId).getById(fieldMatchMsg.getFieldId()).getName();
Carmelo Cascone87892e22017-11-13 16:01:29 -0800395 PiMatchFieldId headerFieldId = PiMatchFieldId.of(fieldMatchName);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400396
397 FieldMatch.FieldMatchTypeCase typeCase = fieldMatchMsg.getFieldMatchTypeCase();
398
399 switch (typeCase) {
400 case EXACT:
401 FieldMatch.Exact exactFieldMatch = fieldMatchMsg.getExact();
402 ImmutableByteSequence exactValue = copyFrom(exactFieldMatch.getValue().asReadOnlyByteBuffer());
403 return new PiExactFieldMatch(headerFieldId, exactValue);
404 case TERNARY:
405 FieldMatch.Ternary ternaryFieldMatch = fieldMatchMsg.getTernary();
406 ImmutableByteSequence ternaryValue = copyFrom(ternaryFieldMatch.getValue().asReadOnlyByteBuffer());
407 ImmutableByteSequence ternaryMask = copyFrom(ternaryFieldMatch.getMask().asReadOnlyByteBuffer());
408 return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
409 case LPM:
410 FieldMatch.LPM lpmFieldMatch = fieldMatchMsg.getLpm();
411 ImmutableByteSequence lpmValue = copyFrom(lpmFieldMatch.getValue().asReadOnlyByteBuffer());
412 int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
413 return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
414 case RANGE:
415 FieldMatch.Range rangeFieldMatch = fieldMatchMsg.getRange();
416 ImmutableByteSequence rangeHighValue = copyFrom(rangeFieldMatch.getHigh().asReadOnlyByteBuffer());
417 ImmutableByteSequence rangeLowValue = copyFrom(rangeFieldMatch.getLow().asReadOnlyByteBuffer());
418 return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400419 default:
420 throw new EncodeException(format(
421 "Decoding of field match type '%s' not implemented", typeCase.name()));
422 }
423 }
424
Yi Tseng82512da2017-08-16 19:46:36 -0700425 static TableAction encodePiTableAction(PiTableAction piTableAction, P4InfoBrowser browser)
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400426 throws P4InfoBrowser.NotFoundException, EncodeException {
Yi Tsengd28936e2018-02-23 22:11:11 +0100427 checkNotNull(piTableAction, "Cannot encode null PiTableAction");
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400428 TableAction.Builder tableActionMsgBuilder = TableAction.newBuilder();
429
430 switch (piTableAction.type()) {
431 case ACTION:
432 PiAction piAction = (PiAction) piTableAction;
Yi Tseng82512da2017-08-16 19:46:36 -0700433 Action theAction = encodePiAction(piAction, browser);
434 tableActionMsgBuilder.setAction(theAction);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400435 break;
Yi Tseng82512da2017-08-16 19:46:36 -0700436 case ACTION_GROUP_ID:
437 PiActionGroupId actionGroupId = (PiActionGroupId) piTableAction;
438 tableActionMsgBuilder.setActionProfileGroupId(actionGroupId.id());
439 break;
440 case GROUP_MEMBER_ID:
441 PiActionGroupMemberId actionGroupMemberId = (PiActionGroupMemberId) piTableAction;
442 tableActionMsgBuilder.setActionProfileMemberId(actionGroupMemberId.id());
443 break;
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400444 default:
445 throw new EncodeException(
446 format("Building of table action type %s not implemented", piTableAction.type()));
447 }
448
449 return tableActionMsgBuilder.build();
450 }
451
Yi Tseng82512da2017-08-16 19:46:36 -0700452 static PiTableAction decodeTableActionMsg(TableAction tableActionMsg, P4InfoBrowser browser)
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400453 throws P4InfoBrowser.NotFoundException, EncodeException {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400454 TableAction.TypeCase typeCase = tableActionMsg.getTypeCase();
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400455 switch (typeCase) {
456 case ACTION:
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400457 Action actionMsg = tableActionMsg.getAction();
Yi Tseng82512da2017-08-16 19:46:36 -0700458 return decodeActionMsg(actionMsg, browser);
Yi Tseng95390822018-01-22 17:57:22 -0800459 case ACTION_PROFILE_GROUP_ID:
460 return PiActionGroupId.of(tableActionMsg.getActionProfileGroupId());
461 case ACTION_PROFILE_MEMBER_ID:
462 return PiActionGroupMemberId.of(tableActionMsg.getActionProfileMemberId());
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
Carmelo Cascone87892e22017-11-13 16:01:29 -0800472 int actionId = browser.actions().getByName(piAction.id().toString()).getPreamble().getId();
Yi Tseng82512da2017-08-16 19:46:36 -0700473
474 Action.Builder actionMsgBuilder =
475 Action.newBuilder().setActionId(actionId);
476
477 for (PiActionParam p : piAction.parameters()) {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800478 P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId).getByName(p.id().toString());
Yi Tseng82512da2017-08-16 19:46:36 -0700479 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}