blob: 709b92ea53459ccd84f172a83bd18c6b7a03f398 [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;
Yi Tseng82512da2017-08-16 19:46:36 -070022import org.onosproject.net.pi.runtime.PiActionGroup;
23import org.onosproject.net.pi.runtime.PiActionGroupId;
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
Carmelo Casconee44592f2018-09-12 02:24:47 -070039 private static final int GROUP_SIZE_ADDITIONAL_MEMBERS = 10;
40
Yi Tseng82512da2017-08-16 19:46:36 -070041 private ActionProfileGroupEncoder() {
42 // hide default constructor
43 }
44
45 /**
46 * Encode a PI action group to a action profile group.
47 *
48 * @param piActionGroup the action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070049 * @param pipeconf the pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070050 * @return a action profile group encoded from PI action group
Carmelo Casconee44592f2018-09-12 02:24:47 -070051 * @throws P4InfoBrowser.NotFoundException if can't find action profile from
52 * P4Info browser
53 * @throws EncodeException if can't find P4Info from
54 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -070055 */
56 static ActionProfileGroup encode(PiActionGroup piActionGroup, PiPipeconf pipeconf)
57 throws P4InfoBrowser.NotFoundException, EncodeException {
58 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
59
60 if (browser == null) {
61 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
62 }
63
64 PiActionProfileId piActionProfileId = piActionGroup.actionProfileId();
Carmelo Cascone87b9b392017-10-02 18:33:20 +020065 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020066 .getByName(piActionProfileId.id());
Carmelo Cascone87b9b392017-10-02 18:33:20 +020067 int actionProfileId = actionProfile.getPreamble().getId();
68 ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder()
Carmelo Casconee44592f2018-09-12 02:24:47 -070069 .setGroupId(piActionGroup.id().id())
70 .setActionProfileId(actionProfileId);
Yi Tseng82512da2017-08-16 19:46:36 -070071
Yi Tseng82512da2017-08-16 19:46:36 -070072 piActionGroup.members().forEach(m -> {
73 // TODO: currently we don't set "watch" field of member
74 Member member = Member.newBuilder()
75 .setMemberId(m.id().id())
76 .setWeight(m.weight())
77 .build();
78 actionProfileGroupBuilder.addMembers(member);
79 });
80
Carmelo Casconee44592f2018-09-12 02:24:47 -070081 // FIXME: ONOS-7797 Make this configurable, or find a different way of
82 // supporting group modify. In P4Runtime, group size cannot be modified
83 // once the group is created. To allow adding members to an existing
84 // group we set max_size to support an additional number of members
85 // other than the one already defined in the PI group. Clearly, this
86 // will break if we try to add more than GROUP_SIZE_ADDITIONAL_MEMBERS
87 // to the same group.
88 actionProfileGroupBuilder.setMaxSize(
89 piActionGroup.members().size() + GROUP_SIZE_ADDITIONAL_MEMBERS);
Carmelo Cascone87b9b392017-10-02 18:33:20 +020090
Yi Tseng82512da2017-08-16 19:46:36 -070091 return actionProfileGroupBuilder.build();
92 }
93
94 /**
Carmelo Casconee44592f2018-09-12 02:24:47 -070095 * Decode an action profile group with members information to a PI action
96 * group.
Yi Tseng82512da2017-08-16 19:46:36 -070097 *
98 * @param actionProfileGroup the action profile group
Carmelo Casconee44592f2018-09-12 02:24:47 -070099 * @param members members of the action profile group
100 * @param pipeconf the pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -0700101 * @return decoded PI action group
Carmelo Casconee44592f2018-09-12 02:24:47 -0700102 * @throws P4InfoBrowser.NotFoundException if can't find action profile from
103 * P4Info browser
104 * @throws EncodeException if can't find P4Info from
105 * pipeconf
Yi Tseng82512da2017-08-16 19:46:36 -0700106 */
107 static PiActionGroup decode(ActionProfileGroup actionProfileGroup,
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200108 Collection<ActionProfileMember> members,
109 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -0700110 throws P4InfoBrowser.NotFoundException, EncodeException {
111 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
112 if (browser == null) {
113 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
114 }
115 PiActionGroup.Builder piActionGroupBuilder = PiActionGroup.builder();
116
117 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
118 .getById(actionProfileGroup.getActionProfileId());
119 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200120
121 piActionGroupBuilder
122 .withActionProfileId(piActionProfileId)
Yi Tseng82512da2017-08-16 19:46:36 -0700123 .withId(PiActionGroupId.of(actionProfileGroup.getGroupId()));
124
Yi Tseng82512da2017-08-16 19:46:36 -0700125 Map<Integer, Integer> memberWeights = Maps.newHashMap();
126 actionProfileGroup.getMembersList().forEach(member -> {
127 int weight = member.getWeight();
128 if (weight < 1) {
129 // FIXME: currently PI has a bug which will always return weight 0
130 // ONOS won't accept group member with weight 0
131 weight = 1;
132 }
133 memberWeights.put(member.getMemberId(), weight);
134 });
135
136 for (ActionProfileMember member : members) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200137 if (!memberWeights.containsKey(member.getMemberId())) {
138 // Not a member of this group, ignore.
139 continue;
140 }
Yi Tseng82512da2017-08-16 19:46:36 -0700141 int weight = memberWeights.get(member.getMemberId());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200142 piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
Yi Tseng82512da2017-08-16 19:46:36 -0700143 }
144
145 return piActionGroupBuilder.build();
146 }
147}