blob: 3ae46c9c8fa36544e759b5152310c039f097d6e1 [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());
51 assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
52 paramValue, paramInfo.getBitwidth());
53 actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder()
54 .setParamId(paramInfo.getId())
55 .setValue(paramValue)
56 .build());
57 }
58 return actionMsgBuilder.build();
59 }
60
61 @Override
62 protected PiAction decode(
63 P4RuntimeOuterClass.Action message, Object ignored,
64 PiPipeconf pipeconf, P4InfoBrowser browser)
65 throws P4InfoBrowser.NotFoundException {
66 final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
67 browser.actionParams(message.getActionId());
68 final String actionName = browser.actions()
69 .getById(message.getActionId())
70 .getPreamble().getName();
71 final PiAction.Builder builder = PiAction.builder()
72 .withId(PiActionId.of(actionName));
73 for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
74 final String paramName = paramInfo.getById(p.getParamId()).getName();
75 final ImmutableByteSequence value = ImmutableByteSequence.copyFrom(
76 p.getValue().toByteArray());
77 builder.withParameter(new PiActionParam(PiActionParamId.of(paramName), value));
78 }
79 return builder.build();
80 }
81}