blob: 30dd43c07af33107314ff975c36b532b4ad59d51 [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;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070021import org.onosproject.net.pi.runtime.PiActionProfileMember;
22import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
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 */
Carmelo Casconee5b28722018-06-22 17:28:28 +020034final class ActionProfileMemberEncoder {
Yi Tseng82512da2017-08-16 19:46:36 -070035 private ActionProfileMemberEncoder() {
36 // Hide default constructor
37 }
38
39 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -070040 * Encode a PiActionProfileMember to a ActionProfileMember.
Yi Tseng82512da2017-08-16 19:46:36 -070041 *
Carmelo Casconecb4327a2018-09-11 15:17:23 -070042 * @param member the member to encode
43 * @param pipeconf the pipeconf, as encode spec
Yi Tseng82512da2017-08-16 19:46:36 -070044 * @return encoded member
Carmelo Cascone6af4e172018-06-15 16:01:30 +020045 * @throws P4InfoBrowser.NotFoundException can't find action profile from
46 * P4Info browser
47 * @throws EncodeException can't find P4Info from pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070048 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -070049 static ActionProfileMember encode(PiActionProfileMember member,
Yi Tseng82512da2017-08-16 19:46:36 -070050 PiPipeconf pipeconf)
51 throws P4InfoBrowser.NotFoundException, EncodeException {
52
53 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
54
55 if (browser == null) {
56 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
57 }
58
59 ActionProfileMember.Builder actionProfileMemberBuilder =
60 ActionProfileMember.newBuilder();
61
62 // member id
63 actionProfileMemberBuilder.setMemberId(member.id().id());
64
65 // action profile id
66 P4InfoOuterClass.ActionProfile actionProfile =
Carmelo Casconee44592f2018-09-12 02:24:47 -070067 browser.actionProfiles().getByName(member.actionProfile().id());
Yi Tseng82512da2017-08-16 19:46:36 -070068
69 int actionProfileId = actionProfile.getPreamble().getId();
70 actionProfileMemberBuilder.setActionProfileId(actionProfileId);
71
72 // Action
73 P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser);
74 actionProfileMemberBuilder.setAction(action);
75
76 return actionProfileMemberBuilder.build();
77 }
78
79 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -070080 * Decode an action profile member to PI action profile member.
Yi Tseng82512da2017-08-16 19:46:36 -070081 *
Carmelo Cascone6af4e172018-06-15 16:01:30 +020082 * @param member the action profile member
83 * @param weight the weight of the member
Yi Tseng82512da2017-08-16 19:46:36 -070084 * @param pipeconf the pipeconf, as decode spec
Carmelo Casconecb4327a2018-09-11 15:17:23 -070085 * @return decoded PI action profile member
Carmelo Cascone6af4e172018-06-15 16:01:30 +020086 * @throws P4InfoBrowser.NotFoundException can't find definition of action
87 * from P4 info
88 * @throws EncodeException can't get P4 info browser from
89 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070090 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -070091 static PiActionProfileMember decode(ActionProfileMember member,
92 int weight,
93 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -070094 throws P4InfoBrowser.NotFoundException, EncodeException {
95 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
Yi Tseng82512da2017-08-16 19:46:36 -070096 if (browser == null) {
97 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
98 }
Carmelo Casconee44592f2018-09-12 02:24:47 -070099
100 final PiActionProfileId actionProfileId = PiActionProfileId.of(
101 browser.actionProfiles()
102 .getById(member.getActionProfileId())
103 .getPreamble()
104 .getName());
105
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700106 return PiActionProfileMember.builder()
Carmelo Casconee44592f2018-09-12 02:24:47 -0700107 .forActionProfile(actionProfileId)
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700108 .withId(PiActionProfileMemberId.of(member.getMemberId()))
Yi Tseng82512da2017-08-16 19:46:36 -0700109 .withWeight(weight)
110 .withAction(decodeActionMsg(member.getAction(), browser))
111 .build();
112 }
113}