blob: daf91f5bf27cc7167c10f0d2a8aa8fc69b844ac8 [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());
52 final ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
Daniele Moroc9f115a2020-12-07 20:56:30 +010053 if (!browser.isTypeString(paramInfo.getTypeName())) {
54 // Check size only if the param type is not a sdn_string
55 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
56 paramValue, paramInfo.getBitwidth());
57 }
Carmelo Cascone4c289b72019-01-22 15:30:45 -080058 actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder()
59 .setParamId(paramInfo.getId())
60 .setValue(paramValue)
61 .build());
62 }
63 return actionMsgBuilder.build();
64 }
65
66 @Override
67 protected PiAction decode(
68 P4RuntimeOuterClass.Action message, Object ignored,
69 PiPipeconf pipeconf, P4InfoBrowser browser)
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020070 throws P4InfoBrowser.NotFoundException, CodecException {
Carmelo Cascone4c289b72019-01-22 15:30:45 -080071 final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
72 browser.actionParams(message.getActionId());
73 final String actionName = browser.actions()
74 .getById(message.getActionId())
75 .getPreamble().getName();
76 final PiAction.Builder builder = PiAction.builder()
77 .withId(PiActionId.of(actionName));
78 for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
Daniele Moro787a0642021-02-23 15:28:07 +010079 final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
80 final ImmutableByteSequence value;
81 if (browser.isTypeString(actionParam.getTypeName())) {
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020082 value = copyFrom(new String(p.getValue().toByteArray()));
Daniele Moro787a0642021-02-23 15:28:07 +010083 } else {
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020084 try {
85 value = copyAndFit(p.getValue().asReadOnlyByteBuffer(),
86 actionParam.getBitwidth());
87 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
88 throw new CodecException(e.getMessage());
89 }
Daniele Moro787a0642021-02-23 15:28:07 +010090 }
Daniele Moro8eb7c0a2021-05-17 14:49:31 +020091 builder.withParameter(new PiActionParam(
92 PiActionParamId.of(actionParam.getName()), value));
Carmelo Cascone4c289b72019-01-22 15:30:45 -080093 }
94 return builder.build();
95 }
96}