blob: 20f39dfe842b1049ac6b2f4cbca0d0b2722e263a [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 Cascone6af4e172018-06-15 16:01:30 +020024import p4.v1.P4RuntimeOuterClass.ActionProfileGroup;
25import p4.v1.P4RuntimeOuterClass.ActionProfileGroup.Member;
26import p4.v1.P4RuntimeOuterClass.ActionProfileMember;
27import p4.config.v1.P4InfoOuterClass;
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 /**
44 * Encode a PI action group to a action profile group.
45 *
46 * @param piActionGroup the action profile group
47 * @param pipeconf the pipeconf
48 * @return a action profile group encoded from PI action group
49 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
50 * @throws EncodeException if can't find P4Info from pipeconf
51 */
52 static ActionProfileGroup encode(PiActionGroup piActionGroup, PiPipeconf pipeconf)
53 throws P4InfoBrowser.NotFoundException, EncodeException {
54 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
55
56 if (browser == null) {
57 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
58 }
59
60 PiActionProfileId piActionProfileId = piActionGroup.actionProfileId();
Carmelo Cascone87b9b392017-10-02 18:33:20 +020061 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
Carmelo Casconecb0a49c2017-10-03 14:32:23 +020062 .getByName(piActionProfileId.id());
Carmelo Cascone87b9b392017-10-02 18:33:20 +020063 int actionProfileId = actionProfile.getPreamble().getId();
64 ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder()
Yi Tseng82512da2017-08-16 19:46:36 -070065 .setGroupId(piActionGroup.id().id())
66 .setActionProfileId(actionProfileId);
67
Yi Tseng82512da2017-08-16 19:46:36 -070068 piActionGroup.members().forEach(m -> {
69 // TODO: currently we don't set "watch" field of member
70 Member member = Member.newBuilder()
71 .setMemberId(m.id().id())
72 .setWeight(m.weight())
73 .build();
74 actionProfileGroupBuilder.addMembers(member);
75 });
76
Carmelo Cascone87b9b392017-10-02 18:33:20 +020077 actionProfileGroupBuilder.setMaxSize(piActionGroup.members().size());
78
Yi Tseng82512da2017-08-16 19:46:36 -070079 return actionProfileGroupBuilder.build();
80 }
81
82 /**
83 * Decode an action profile group with members information to a PI action group.
84 *
85 * @param actionProfileGroup the action profile group
86 * @param members members of the action profile group
87 * @param pipeconf the pipeconf
88 * @return decoded PI action group
89 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
90 * @throws EncodeException if can't find P4Info from pipeconf
91 */
92 static PiActionGroup decode(ActionProfileGroup actionProfileGroup,
Carmelo Cascone87b9b392017-10-02 18:33:20 +020093 Collection<ActionProfileMember> members,
94 PiPipeconf pipeconf)
Yi Tseng82512da2017-08-16 19:46:36 -070095 throws P4InfoBrowser.NotFoundException, EncodeException {
96 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
97 if (browser == null) {
98 throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf));
99 }
100 PiActionGroup.Builder piActionGroupBuilder = PiActionGroup.builder();
101
102 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
103 .getById(actionProfileGroup.getActionProfileId());
104 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200105
106 piActionGroupBuilder
107 .withActionProfileId(piActionProfileId)
Yi Tseng82512da2017-08-16 19:46:36 -0700108 .withId(PiActionGroupId.of(actionProfileGroup.getGroupId()));
109
Yi Tseng82512da2017-08-16 19:46:36 -0700110 Map<Integer, Integer> memberWeights = Maps.newHashMap();
111 actionProfileGroup.getMembersList().forEach(member -> {
112 int weight = member.getWeight();
113 if (weight < 1) {
114 // FIXME: currently PI has a bug which will always return weight 0
115 // ONOS won't accept group member with weight 0
116 weight = 1;
117 }
118 memberWeights.put(member.getMemberId(), weight);
119 });
120
121 for (ActionProfileMember member : members) {
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200122 if (!memberWeights.containsKey(member.getMemberId())) {
123 // Not a member of this group, ignore.
124 continue;
125 }
Yi Tseng82512da2017-08-16 19:46:36 -0700126 int weight = memberWeights.get(member.getMemberId());
Carmelo Cascone87b9b392017-10-02 18:33:20 +0200127 piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
Yi Tseng82512da2017-08-16 19:46:36 -0700128 }
129
130 return piActionGroupBuilder.build();
131 }
132}