blob: 2f08a59ce9bffe1ddaa8df25d6c749b55e704bce [file] [log] [blame]
Yi Tseng82512da2017-08-16 19:46:36 -07001/*
2 * Copyright 2017-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;
18
19import org.onosproject.net.pi.model.PiPipeconf;
20import org.onosproject.net.pi.runtime.PiActionGroup;
21import org.onosproject.net.pi.runtime.PiActionGroupMember;
22import org.onosproject.net.pi.runtime.PiActionGroupMemberId;
23import p4.P4RuntimeOuterClass;
24import p4.P4RuntimeOuterClass.ActionProfileMember;
25import p4.config.P4InfoOuterClass;
26
27import static java.lang.String.format;
28import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decodeActionMsg;
29import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encodePiAction;
30
31/**
32 * Encoder/Decoder of action profile member.
33 */
34public final class ActionProfileMemberEncoder {
35 private ActionProfileMemberEncoder() {
36 // Hide default constructor
37 }
38
39 /**
40 * Encode a PiActionGroupMember to a ActionProfileMember.
41 *
42 * @param group the PI action group of members
43 * @param member the member to encode
44 * @param pipeconf the pipeconf
45 * @return encoded member
46 */
47 /**
48 * Encode a PiActionGroupMember to a ActionProfileMember.
49 *
50 * @param group the PI action group of members
51 * @param member the member to encode
52 * @param pipeconf the pipeconf, as encode spec
53 * @return encoded member
54 * @throws P4InfoBrowser.NotFoundException can't find action profile from P4Info browser
55 * @throws EncodeException can't find P4Info from pipeconf
56 */
57 static ActionProfileMember encode(PiActionGroup group,
58 PiActionGroupMember member,
59 PiPipeconf pipeconf)
60 throws P4InfoBrowser.NotFoundException, EncodeException {
61
62 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
63
64 if (browser == null) {
65 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
66 }
67
68 ActionProfileMember.Builder actionProfileMemberBuilder =
69 ActionProfileMember.newBuilder();
70
71 // member id
72 actionProfileMemberBuilder.setMemberId(member.id().id());
73
74 // action profile id
75 P4InfoOuterClass.ActionProfile actionProfile =
76 browser.actionProfiles().getByName(group.actionProfileId().id());
77
78 int actionProfileId = actionProfile.getPreamble().getId();
79 actionProfileMemberBuilder.setActionProfileId(actionProfileId);
80
81 // Action
82 P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser);
83 actionProfileMemberBuilder.setAction(action);
84
85 return actionProfileMemberBuilder.build();
86 }
87
88 /**
89 * Decode an action profile member to PI action group member.
90 *
91 * @param member the action profile member
92 * @param weight the weight of the member
93 * @param pipeconf the pipeconf, as decode spec
94 * @return decoded PI action group member
95 * @throws P4InfoBrowser.NotFoundException can't find definition of action from P4 info
96 * @throws EncodeException can't get P4 info browser from pipeconf
97 */
98 static PiActionGroupMember decode(ActionProfileMember member,
99 int weight,
100 PiPipeconf pipeconf)
101 throws P4InfoBrowser.NotFoundException, EncodeException {
102 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
103
104 if (browser == null) {
105 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
106 }
107 return PiActionGroupMember.builder().withId(PiActionGroupMemberId.of(member.getMemberId()))
108 .withWeight(weight)
109 .withAction(decodeActionMsg(member.getAction(), browser))
110 .build();
111 }
112}