blob: 68ded3645cca7cbd4160871d7fd189d6d4eeb28a [file] [log] [blame]
Daniele Morod900fe42021-02-11 16:12:57 +01001/*
2 * Copyright 2020-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 org.onosproject.net.pi.model.PiPipeconf;
20import org.onosproject.net.pi.runtime.PiActionSet;
21import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser;
22import p4.v1.P4RuntimeOuterClass;
23
24/**
25 * Codec for ActionSet.
26 */
27public class ActionSetCodec extends
28 AbstractCodec<PiActionSet, P4RuntimeOuterClass.ActionProfileActionSet, Object> {
29
30 @Override
31 protected P4RuntimeOuterClass.ActionProfileActionSet encode(
32 PiActionSet piActionSet, Object ignored,
33 PiPipeconf pipeconf, P4InfoBrowser browser)
34 throws CodecException, P4InfoBrowser.NotFoundException {
35 final var actProActSetBuilder =
36 P4RuntimeOuterClass.ActionProfileActionSet.newBuilder();
37 for (PiActionSet.WeightedAction act : piActionSet.actions()) {
38 // TODO: currently we don't set "watch_port" field
39 final var actProfAct =
40 P4RuntimeOuterClass.ActionProfileAction.newBuilder();
41 actProfAct.setAction(Codecs.CODECS.action().encode(
42 act.action(), null, pipeconf));
43 actProfAct.setWeight(act.weight());
44 actProActSetBuilder.addActionProfileActions(actProfAct.build());
45 }
46 return actProActSetBuilder.build();
47 }
48
49 @Override
50 protected PiActionSet decode(
51 P4RuntimeOuterClass.ActionProfileActionSet message, Object ignored,
52 PiPipeconf pipeconf, P4InfoBrowser browser)
53 throws CodecException, P4InfoBrowser.NotFoundException {
54 final var builder = PiActionSet.builder();
55 for (P4RuntimeOuterClass.ActionProfileAction act : message.getActionProfileActionsList()) {
56 final var piAction = Codecs.CODECS.action().decode(
57 act.getAction(), null, pipeconf);
58 builder.addWeightedAction(piAction, act.getWeight());
59 }
60 return builder.build();
61 }
62}