blob: d0a285e05f28aa7c293e8e492acda73ac0b02ed8 [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;
31import static org.onosproject.p4runtime.ctl.codec.Utils.assertSize;
32
33/**
34 * Codec for P4Runtime Action.
35 */
36public final class ActionCodec
37 extends AbstractCodec<PiAction, P4RuntimeOuterClass.Action, Object> {
38
39 @Override
40 protected P4RuntimeOuterClass.Action encode(
41 PiAction piAction, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser)
42 throws CodecException, P4InfoBrowser.NotFoundException {
43 final int actionId = browser.actions()
44 .getByName(piAction.id().toString()).getPreamble().getId();
45 final P4RuntimeOuterClass.Action.Builder actionMsgBuilder =
46 P4RuntimeOuterClass.Action.newBuilder().setActionId(actionId);
47 for (PiActionParam p : piAction.parameters()) {
48 final P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId)
49 .getByName(p.id().toString());
50 final ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
Daniele Moro5c82b0f2020-12-07 20:56:30 +010051 if (!browser.isTypeString(paramInfo.getTypeName())) {
52 // Check size only if the param type is not a sdn_string
53 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
54 paramValue, paramInfo.getBitwidth());
55 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080056 actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder()
57 .setParamId(paramInfo.getId())
58 .setValue(paramValue)
59 .build());
60 }
61 return actionMsgBuilder.build();
62 }
63
64 @Override
65 protected PiAction decode(
66 P4RuntimeOuterClass.Action message, Object ignored,
67 PiPipeconf pipeconf, P4InfoBrowser browser)
68 throws P4InfoBrowser.NotFoundException {
69 final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
70 browser.actionParams(message.getActionId());
71 final String actionName = browser.actions()
72 .getById(message.getActionId())
73 .getPreamble().getName();
74 final PiAction.Builder builder = PiAction.builder()
75 .withId(PiActionId.of(actionName));
76 for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
Daniele Moro7aa13e62021-02-23 15:28:07 +010077 final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
78 final ImmutableByteSequence value;
79 if (browser.isTypeString(actionParam.getTypeName())) {
80 value = ImmutableByteSequence.copyFrom(new String(p.getValue().toByteArray()));
81 } else {
82 value = ImmutableByteSequence.copyFrom(p.getValue().toByteArray());
83 }
84 builder.withParameter(new PiActionParam(PiActionParamId.of(actionParam.getName()), value));
Carmelo Cascone4c289b72019-01-22 15:30:45 -080085 }
86 return builder.build();
87 }
88}