blob: cbf112cf865facd58981849b875cfdc62de08022 [file] [log] [blame]
Carmelo Cascone4c289b72019-01-22 15:30:45 -08001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
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.codec;
18
19import com.google.protobuf.ByteString;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.net.pi.model.PiActionId;
22import org.onosproject.net.pi.model.PiActionParamId;
23import org.onosproject.net.pi.model.PiPipeconf;
24import org.onosproject.net.pi.runtime.PiAction;
25import org.onosproject.net.pi.runtime.PiActionParam;
26import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
27import p4.config.v1.P4InfoOuterClass;
28import p4.v1.P4RuntimeOuterClass;
29
30import static java.lang.String.format;
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020031import static org.onlab.util.ImmutableByteSequence.copyAndFit;
32import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080033import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize;
34
35/**
36 * Codec for P4Runtime Action.
37 */
38public final class ActionCodec
39 extends AbstractCodec<PiAction, P4RuntimeOuterClass.Action, Object> {
40
41 @Override
42 protected P4RuntimeOuterClass.Action encode(
43 PiAction piAction, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser)
44 throws CodecException, P4InfoBrowser.NotFoundException {
45 final int actionId = browser.actions()
46 .getByName(piAction.id().toString()).getPreamble().getId();
47 final P4RuntimeOuterClass.Action.Builder actionMsgBuilder =
48 P4RuntimeOuterClass.Action.newBuilder().setActionId(actionId);
49 for (PiActionParam p : piAction.parameters()) {
50 final P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId)
51 .getByName(p.id().toString());
Yi Tseng38213992022-03-17 16:04:06 -070052 final ByteString paramValue;
53 if (browser.isTypeString(paramInfo.getTypeName())) {
54 paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
55 } else {
56 paramValue = ByteString.copyFrom(p.value().canonical().asReadOnlyBuffer());
Daniele Moroc9f115a2020-12-07 20:56:30 +010057 // Check size only if the param type is not a sdn_string
58 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
Yi Tseng38213992022-03-17 16:04:06 -070059 paramValue, paramInfo.getBitwidth());
Daniele Moroc9f115a2020-12-07 20:56:30 +010060 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080061 actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder()
62 .setParamId(paramInfo.getId())
63 .setValue(paramValue)
64 .build());
65 }
66 return actionMsgBuilder.build();
67 }
68
69 @Override
70 protected PiAction decode(
71 P4RuntimeOuterClass.Action message, Object ignored,
72 PiPipeconf pipeconf, P4InfoBrowser browser)
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020073 throws P4InfoBrowser.NotFoundException, CodecException {
Carmelo Cascone4c289b72019-01-22 15:30:45 -080074 final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
75 browser.actionParams(message.getActionId());
76 final String actionName = browser.actions()
77 .getById(message.getActionId())
78 .getPreamble().getName();
79 final PiAction.Builder builder = PiAction.builder()
80 .withId(PiActionId.of(actionName));
81 for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
Daniele Moro787a0642021-02-23 15:28:07 +010082 final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
83 final ImmutableByteSequence value;
84 if (browser.isTypeString(actionParam.getTypeName())) {
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020085 value = copyFrom(new String(p.getValue().toByteArray()));
Daniele Moro787a0642021-02-23 15:28:07 +010086 } else {
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020087 try {
88 value = copyAndFit(p.getValue().asReadOnlyByteBuffer(),
89 actionParam.getBitwidth());
90 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
91 throw new CodecException(e.getMessage());
92 }
Daniele Moro787a0642021-02-23 15:28:07 +010093 }
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020094 builder.withParameter(new PiActionParam(
95 PiActionParamId.of(actionParam.getName()), value));
Carmelo Cascone4c289b72019-01-22 15:30:45 -080096 }
97 return builder.build();
98 }
99}