blob: 9a01a82ba76d248a2a4a0995a9daeca6b9c2e800 [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;
24import 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
Yi Tseng82512da2017-08-16 19:46:36 -070067 piActionGroup.members().forEach(m -> {
68 // TODO: currently we don't set "watch" field of member
69 Member member = Member.newBuilder()
70 .setMemberId(m.id().id())
71 .setWeight(m.weight())
72 .build();
73 actionProfileGroupBuilder.addMembers(member);
74 });
75
Carmelo Cascone87b9b392017-10-02 18:33:20 +020076 actionProfileGroupBuilder.setMaxSize(piActionGroup.members().size());
77
Yi Tseng82512da2017-08-16 19:46:36 -070078 return actionProfileGroupBuilder.build();
79 }
80
81 /**
82 * Decode an action profile group with members information to a PI action group.
83 *
84 * @param actionProfileGroup the action profile group
85 * @param members members of the action profile group
86 * @param pipeconf the pipeconf
87 * @return decoded PI action group
88 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
89 * @throws EncodeException if can't find P4Info from pipeconf
90 */
91 static PiActionGroup decode(ActionProfileGroup actionProfileGroup,
Carmelo Cascone87b9b392017-10-02 18:33:20 +020092 Collection<ActionProfileMember> members,
93 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -070094 throws P4InfoBrowser.NotFoundException, EncodeException {
95 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
96 if (browser == null) {
97 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
98 }
99 PiActionGroup.Builder piActionGroupBuilder = PiActionGroup.builder();
100
101 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
102 .getById(actionProfileGroup.getActionProfileId());
103 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200104
105 piActionGroupBuilder
106 .withActionProfileId(piActionProfileId)
Yi Tseng82512da2017-08-16 19:46:36 -0700107 .withId(PiActionGroupId.of(actionProfileGroup.getGroupId()));
108
Yi Tseng82512da2017-08-16 19:46:36 -0700109 Map<Integer, Integer> memberWeights = Maps.newHashMap();
110 actionProfileGroup.getMembersList().forEach(member -> {
111 int weight = member.getWeight();
112 if (weight < 1) {
113 // FIXME: currently PI has a bug which will always return weight 0
114 // ONOS won't accept group member with weight 0
115 weight = 1;
116 }
117 memberWeights.put(member.getMemberId(), weight);
118 });
119
120 for (ActionProfileMember member : members) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200121 if (!memberWeights.containsKey(member.getMemberId())) {
122 // Not a member of this group, ignore.
123 continue;
124 }
Yi Tseng82512da2017-08-16 19:46:36 -0700125 int weight = memberWeights.get(member.getMemberId());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200126 piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
Yi Tseng82512da2017-08-16 19:46:36 -0700127 }
128
129 return piActionGroupBuilder.build();
130 }
131}