blob: eb43f4cb78adbc1042ca2427858c1d503a44d714 [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;
20import org.onosproject.net.pi.model.PiPipeconf;
Yi Tseng82512da2017-08-16 19:46:36 -070021import org.onosproject.net.pi.runtime.PiActionGroup;
22import org.onosproject.net.pi.runtime.PiActionGroupId;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020023import org.onosproject.net.pi.runtime.PiActionProfileId;
Yi Tseng82512da2017-08-16 19:46:36 -070024import p4.P4RuntimeOuterClass.ActionProfileGroup;
25import p4.P4RuntimeOuterClass.ActionProfileGroup.Member;
26import p4.P4RuntimeOuterClass.ActionProfileMember;
27import p4.config.P4InfoOuterClass;
28
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 */
37public final class ActionProfileGroupEncoder {
38 private ActionProfileGroupEncoder() {
39 // hide default constructor
40 }
41
42 /**
43 * Encode a PI action group to a action profile group.
44 *
45 * @param piActionGroup the action profile group
46 * @param pipeconf the pipeconf
47 * @return a action profile group encoded from PI action group
48 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
49 * @throws EncodeException if can't find P4Info from pipeconf
50 */
51 static ActionProfileGroup encode(PiActionGroup piActionGroup, PiPipeconf pipeconf)
52 throws P4InfoBrowser.NotFoundException, EncodeException {
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 PiActionProfileId piActionProfileId = piActionGroup.actionProfileId();
Carmelo Cascone87b9b392017-10-02 18:33:20 +020060 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020061 .getByName(piActionProfileId.id());
Carmelo Cascone87b9b392017-10-02 18:33:20 +020062 int actionProfileId = actionProfile.getPreamble().getId();
63 ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder()
Yi Tseng82512da2017-08-16 19:46:36 -070064 .setGroupId(piActionGroup.id().id())
65 .setActionProfileId(actionProfileId);
66
67 switch (piActionGroup.type()) {
68 case SELECT:
69 actionProfileGroupBuilder.setType(ActionProfileGroup.Type.SELECT);
70 break;
71 default:
Carmelo Cascone87b9b392017-10-02 18:33:20 +020072 throw new EncodeException(format("PI action group type %s not supported", piActionGroup.type()));
Yi Tseng82512da2017-08-16 19:46:36 -070073 }
74
75 piActionGroup.members().forEach(m -> {
76 // TODO: currently we don't set "watch" field of member
77 Member member = Member.newBuilder()
78 .setMemberId(m.id().id())
79 .setWeight(m.weight())
80 .build();
81 actionProfileGroupBuilder.addMembers(member);
82 });
83
Carmelo Cascone87b9b392017-10-02 18:33:20 +020084 actionProfileGroupBuilder.setMaxSize(piActionGroup.members().size());
85
Yi Tseng82512da2017-08-16 19:46:36 -070086 return actionProfileGroupBuilder.build();
87 }
88
89 /**
90 * Decode an action profile group with members information to a PI action group.
91 *
92 * @param actionProfileGroup the action profile group
93 * @param members members of the action profile group
94 * @param pipeconf the pipeconf
95 * @return decoded PI action group
96 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
97 * @throws EncodeException if can't find P4Info from pipeconf
98 */
99 static PiActionGroup decode(ActionProfileGroup actionProfileGroup,
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200100 Collection<ActionProfileMember> members,
101 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -0700102 throws P4InfoBrowser.NotFoundException, EncodeException {
103 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
104 if (browser == null) {
105 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
106 }
107 PiActionGroup.Builder piActionGroupBuilder = PiActionGroup.builder();
108
109 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
110 .getById(actionProfileGroup.getActionProfileId());
111 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200112
113 piActionGroupBuilder
114 .withActionProfileId(piActionProfileId)
Yi Tseng82512da2017-08-16 19:46:36 -0700115 .withId(PiActionGroupId.of(actionProfileGroup.getGroupId()));
116
117 switch (actionProfileGroup.getType()) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200118 case UNSPECIFIED:
119 // FIXME: PI returns unspecified for select groups. Remove this case when PI bug will be fixed.
Yi Tseng82512da2017-08-16 19:46:36 -0700120 case SELECT:
121 piActionGroupBuilder.withType(PiActionGroup.Type.SELECT);
122 break;
123 default:
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200124 throw new EncodeException(format("Action profile type %s is not supported",
Yi Tseng82512da2017-08-16 19:46:36 -0700125 actionProfileGroup.getType()));
126 }
127
128 Map<Integer, Integer> memberWeights = Maps.newHashMap();
129 actionProfileGroup.getMembersList().forEach(member -> {
130 int weight = member.getWeight();
131 if (weight < 1) {
132 // FIXME: currently PI has a bug which will always return weight 0
133 // ONOS won't accept group member with weight 0
134 weight = 1;
135 }
136 memberWeights.put(member.getMemberId(), weight);
137 });
138
139 for (ActionProfileMember member : members) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200140 if (!memberWeights.containsKey(member.getMemberId())) {
141 // Not a member of this group, ignore.
142 continue;
143 }
Yi Tseng82512da2017-08-16 19:46:36 -0700144 int weight = memberWeights.get(member.getMemberId());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200145 piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
Yi Tseng82512da2017-08-16 19:46:36 -0700146 }
147
148 return piActionGroupBuilder.build();
149 }
150}