blob: ba31e69a58b03e9612cfaa11d037621f6bfa998f [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
Yi Tseng8d355132018-04-13 01:40:48 +080019import org.onosproject.net.pi.model.PiActionProfileId;
Yi Tseng82512da2017-08-16 19:46:36 -070020import org.onosproject.net.pi.model.PiPipeconf;
Yi Tseng82512da2017-08-16 19:46:36 -070021import org.onosproject.net.pi.runtime.PiActionGroupMember;
22import org.onosproject.net.pi.runtime.PiActionGroupMemberId;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020023import p4.config.v1.P4InfoOuterClass;
24import p4.v1.P4RuntimeOuterClass;
25import p4.v1.P4RuntimeOuterClass.ActionProfileMember;
Yi Tseng82512da2017-08-16 19:46:36 -070026
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 *
Yi Tseng8d355132018-04-13 01:40:48 +080042 * @param profileId the PI action group profile ID of members
Carmelo Cascone6af4e172018-06-15 16:01:30 +020043 * @param member the member to encode
44 * @param pipeconf the pipeconf, as encode spec
Yi Tseng82512da2017-08-16 19:46:36 -070045 * @return encoded member
Carmelo Cascone6af4e172018-06-15 16:01:30 +020046 * @throws P4InfoBrowser.NotFoundException can't find action profile from
47 * P4Info browser
48 * @throws EncodeException can't find P4Info from pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070049 */
Yi Tseng8d355132018-04-13 01:40:48 +080050 static ActionProfileMember encode(PiActionProfileId profileId,
Yi Tseng82512da2017-08-16 19:46:36 -070051 PiActionGroupMember member,
52 PiPipeconf pipeconf)
53 throws P4InfoBrowser.NotFoundException, EncodeException {
54
55 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
56
57 if (browser == null) {
58 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
59 }
60
61 ActionProfileMember.Builder actionProfileMemberBuilder =
62 ActionProfileMember.newBuilder();
63
64 // member id
65 actionProfileMemberBuilder.setMemberId(member.id().id());
66
67 // action profile id
68 P4InfoOuterClass.ActionProfile actionProfile =
Yi Tseng8d355132018-04-13 01:40:48 +080069 browser.actionProfiles().getByName(profileId.id());
Yi Tseng82512da2017-08-16 19:46:36 -070070
71 int actionProfileId = actionProfile.getPreamble().getId();
72 actionProfileMemberBuilder.setActionProfileId(actionProfileId);
73
74 // Action
75 P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser);
76 actionProfileMemberBuilder.setAction(action);
77
78 return actionProfileMemberBuilder.build();
79 }
80
81 /**
82 * Decode an action profile member to PI action group member.
83 *
Carmelo Cascone6af4e172018-06-15 16:01:30 +020084 * @param member the action profile member
85 * @param weight the weight of the member
Yi Tseng82512da2017-08-16 19:46:36 -070086 * @param pipeconf the pipeconf, as decode spec
87 * @return decoded PI action group member
Carmelo Cascone6af4e172018-06-15 16:01:30 +020088 * @throws P4InfoBrowser.NotFoundException can't find definition of action
89 * from P4 info
90 * @throws EncodeException can't get P4 info browser from
91 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070092 */
93 static PiActionGroupMember decode(ActionProfileMember member,
94 int weight,
95 PiPipeconf pipeconf)
96 throws P4InfoBrowser.NotFoundException, EncodeException {
97 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
98
99 if (browser == null) {
100 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
101 }
102 return PiActionGroupMember.builder().withId(PiActionGroupMemberId.of(member.getMemberId()))
103 .withWeight(weight)
104 .withAction(decodeActionMsg(member.getAction(), browser))
105 .build();
106 }
107}