blob: 414a0e67a9fbf1c9e1c4cb28debd388137430f64 [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 com.google.common.collect.Maps;
Carmelo Cascone87892e22017-11-13 16:01:29 -080020import org.onosproject.net.pi.model.PiActionProfileId;
Yi Tseng82512da2017-08-16 19:46:36 -070021import org.onosproject.net.pi.model.PiPipeconf;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070022import org.onosproject.net.pi.runtime.PiActionProfileGroup;
23import org.onosproject.net.pi.runtime.PiActionProfileGroupId;
Carmelo Casconee44592f2018-09-12 02:24:47 -070024import p4.config.v1.P4InfoOuterClass;
Carmelo Cascone6af4e172018-06-15 16:01:30 +020025import p4.v1.P4RuntimeOuterClass.ActionProfileGroup;
26import p4.v1.P4RuntimeOuterClass.ActionProfileGroup.Member;
27import p4.v1.P4RuntimeOuterClass.ActionProfileMember;
Yi Tseng82512da2017-08-16 19:46:36 -070028
29import java.util.Collection;
30import java.util.Map;
31
32import static java.lang.String.format;
33
34/**
35 * Encoder/Decoder for action profile group.
36 */
Carmelo Cascone6af4e172018-06-15 16:01:30 +020037final class ActionProfileGroupEncoder {
38
Yi Tseng82512da2017-08-16 19:46:36 -070039 private ActionProfileGroupEncoder() {
40 // hide default constructor
41 }
42
43 /**
Carmelo Casconecb4327a2018-09-11 15:17:23 -070044 * Encode a PI action profile group to a action profile group.
Yi Tseng82512da2017-08-16 19:46:36 -070045 *
46 * @param piActionGroup the action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070047 * @param pipeconf the pipeconf
ghj0504520ed7340c2018-10-26 13:06:35 -070048 * @param maxMemberSize the max member size of action group
Carmelo Casconecb4327a2018-09-11 15:17:23 -070049 * @return a action profile group encoded from PI action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070050 * @throws P4InfoBrowser.NotFoundException if can't find action profile from
51 * P4Info browser
52 * @throws EncodeException if can't find P4Info from
53 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070054 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -070055 static ActionProfileGroup encode(PiActionProfileGroup piActionGroup, PiPipeconf pipeconf, int maxMemberSize)
Yi Tseng82512da2017-08-16 19:46:36 -070056 throws P4InfoBrowser.NotFoundException, EncodeException {
57 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
58
59 if (browser == null) {
60 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
61 }
62
63 PiActionProfileId piActionProfileId = piActionGroup.actionProfileId();
Carmelo Cascone87b9b392017-10-02 18:33:20 +020064 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020065 .getByName(piActionProfileId.id());
Carmelo Cascone87b9b392017-10-02 18:33:20 +020066 int actionProfileId = actionProfile.getPreamble().getId();
67 ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder()
Carmelo Casconee44592f2018-09-12 02:24:47 -070068 .setGroupId(piActionGroup.id().id())
69 .setActionProfileId(actionProfileId);
Yi Tseng82512da2017-08-16 19:46:36 -070070
Yi Tseng82512da2017-08-16 19:46:36 -070071 piActionGroup.members().forEach(m -> {
72 // TODO: currently we don't set "watch" field of member
73 Member member = Member.newBuilder()
74 .setMemberId(m.id().id())
75 .setWeight(m.weight())
76 .build();
77 actionProfileGroupBuilder.addMembers(member);
78 });
79
ghj0504520ed7340c2018-10-26 13:06:35 -070080 if (maxMemberSize > 0) {
81 actionProfileGroupBuilder.setMaxSize(maxMemberSize);
82 }
Carmelo Cascone87b9b392017-10-02 18:33:20 +020083
Yi Tseng82512da2017-08-16 19:46:36 -070084 return actionProfileGroupBuilder.build();
85 }
86
87 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -070088 * Decode an action profile group with members information to a PI action
Carmelo Casconecb4327a2018-09-11 15:17:23 -070089 * profile group.
Yi Tseng82512da2017-08-16 19:46:36 -070090 *
91 * @param actionProfileGroup the action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070092 * @param members members of the action profile group
93 * @param pipeconf the pipeconf
Carmelo Casconecb4327a2018-09-11 15:17:23 -070094 * @return decoded PI action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070095 * @throws P4InfoBrowser.NotFoundException if can't find action profile from
96 * P4Info browser
97 * @throws EncodeException if can't find P4Info from
98 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070099 */
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700100 static PiActionProfileGroup decode(ActionProfileGroup actionProfileGroup,
101 Collection<ActionProfileMember> members,
102 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -0700103 throws P4InfoBrowser.NotFoundException, EncodeException {
104 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
105 if (browser == null) {
106 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
107 }
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700108 PiActionProfileGroup.Builder piActionGroupBuilder = PiActionProfileGroup.builder();
Yi Tseng82512da2017-08-16 19:46:36 -0700109
110 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
111 .getById(actionProfileGroup.getActionProfileId());
112 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200113
114 piActionGroupBuilder
115 .withActionProfileId(piActionProfileId)
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700116 .withId(PiActionProfileGroupId.of(actionProfileGroup.getGroupId()));
Yi Tseng82512da2017-08-16 19:46:36 -0700117
Yi Tseng82512da2017-08-16 19:46:36 -0700118 Map<Integer, Integer> memberWeights = Maps.newHashMap();
119 actionProfileGroup.getMembersList().forEach(member -> {
120 int weight = member.getWeight();
121 if (weight < 1) {
122 // FIXME: currently PI has a bug which will always return weight 0
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700123 // ONOS won't accept group buckets with weight 0
Yi Tseng82512da2017-08-16 19:46:36 -0700124 weight = 1;
125 }
126 memberWeights.put(member.getMemberId(), weight);
127 });
128
129 for (ActionProfileMember member : members) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200130 if (!memberWeights.containsKey(member.getMemberId())) {
131 // Not a member of this group, ignore.
132 continue;
133 }
Yi Tseng82512da2017-08-16 19:46:36 -0700134 int weight = memberWeights.get(member.getMemberId());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200135 piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
Yi Tseng82512da2017-08-16 19:46:36 -0700136 }
137
138 return piActionGroupBuilder.build();
139 }
140}