blob: 472bf8e1c2c74acf91892a25590cf2bf20da5843 [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;
21import org.onosproject.net.pi.runtime.PiActionProfileId;
22import 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();
60 int actionProfileId;
61 P4InfoOuterClass.ActionProfile actionProfile =
62 browser.actionProfiles().getByName(piActionProfileId.id());
63 actionProfileId = actionProfile.getPreamble().getId();
64 ActionProfileGroup.Builder actionProfileGroupBuilder =
65 ActionProfileGroup.newBuilder()
66 .setGroupId(piActionGroup.id().id())
67 .setActionProfileId(actionProfileId);
68
69 switch (piActionGroup.type()) {
70 case SELECT:
71 actionProfileGroupBuilder.setType(ActionProfileGroup.Type.SELECT);
72 break;
73 default:
74 throw new EncodeException(format("Unsupported pi action group type %s",
75 piActionGroup.type()));
76 }
77
78 piActionGroup.members().forEach(m -> {
79 // TODO: currently we don't set "watch" field of member
80 Member member = Member.newBuilder()
81 .setMemberId(m.id().id())
82 .setWeight(m.weight())
83 .build();
84 actionProfileGroupBuilder.addMembers(member);
85 });
86
87 return actionProfileGroupBuilder.build();
88 }
89
90 /**
91 * Decode an action profile group with members information to a PI action group.
92 *
93 * @param actionProfileGroup the action profile group
94 * @param members members of the action profile group
95 * @param pipeconf the pipeconf
96 * @return decoded PI action group
97 * @throws P4InfoBrowser.NotFoundException if can't find action profile from P4Info browser
98 * @throws EncodeException if can't find P4Info from pipeconf
99 */
100 static PiActionGroup decode(ActionProfileGroup actionProfileGroup,
101 Collection<ActionProfileMember> members,
102 PiPipeconf pipeconf)
103 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 }
108 PiActionGroup.Builder piActionGroupBuilder = PiActionGroup.builder();
109
110 P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles()
111 .getById(actionProfileGroup.getActionProfileId());
112 PiActionProfileId piActionProfileId = PiActionProfileId.of(actionProfile.getPreamble().getName());
113 piActionGroupBuilder.withActionProfileId(piActionProfileId)
114 .withId(PiActionGroupId.of(actionProfileGroup.getGroupId()));
115
116 switch (actionProfileGroup.getType()) {
117 case SELECT:
118 piActionGroupBuilder.withType(PiActionGroup.Type.SELECT);
119 break;
120 default:
121 throw new EncodeException(format("Unsupported action profile type %s",
122 actionProfileGroup.getType()));
123 }
124
125 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) {
137 int weight = memberWeights.get(member.getMemberId());
138 piActionGroupBuilder
139 .addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf));
140 }
141
142 return piActionGroupBuilder.build();
143 }
144}