Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 20 | import org.onosproject.net.pi.model.PiActionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import org.onosproject.net.pi.model.PiPipeconf; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 22 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 23 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 24 | import p4.config.v1.P4InfoOuterClass; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 25 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 26 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup.Member; |
| 27 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | import static java.lang.String.format; |
| 33 | |
| 34 | /** |
| 35 | * Encoder/Decoder for action profile group. |
| 36 | */ |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 37 | final class ActionProfileGroupEncoder { |
| 38 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 39 | private static final int GROUP_SIZE_ADDITIONAL_MEMBERS = 10; |
| 40 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 41 | 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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 49 | * @param pipeconf the pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 50 | * @return a action profile group encoded from PI action group |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 51 | * @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 Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 55 | */ |
| 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 65 | P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 66 | .getByName(piActionProfileId.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 67 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 68 | ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 69 | .setGroupId(piActionGroup.id().id()) |
| 70 | .setActionProfileId(actionProfileId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 71 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 72 | 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 Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 81 | // 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 90 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 91 | return actionProfileGroupBuilder.build(); |
| 92 | } |
| 93 | |
| 94 | /** |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 95 | * Decode an action profile group with members information to a PI action |
| 96 | * group. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 97 | * |
| 98 | * @param actionProfileGroup the action profile group |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 99 | * @param members members of the action profile group |
| 100 | * @param pipeconf the pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 101 | * @return decoded PI action group |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 102 | * @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 Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 106 | */ |
| 107 | static PiActionGroup decode(ActionProfileGroup actionProfileGroup, |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 108 | Collection<ActionProfileMember> members, |
| 109 | PiPipeconf pipeconf) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 110 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 120 | |
| 121 | piActionGroupBuilder |
| 122 | .withActionProfileId(piActionProfileId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 123 | .withId(PiActionGroupId.of(actionProfileGroup.getGroupId())); |
| 124 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 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) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 137 | if (!memberWeights.containsKey(member.getMemberId())) { |
| 138 | // Not a member of this group, ignore. |
| 139 | continue; |
| 140 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 141 | int weight = memberWeights.get(member.getMemberId()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 142 | piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return piActionGroupBuilder.build(); |
| 146 | } |
| 147 | } |