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; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 22 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 23 | import org.onosproject.net.pi.runtime.PiActionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 24 | import p4.P4RuntimeOuterClass.ActionProfileGroup; |
| 25 | import p4.P4RuntimeOuterClass.ActionProfileGroup.Member; |
| 26 | import p4.P4RuntimeOuterClass.ActionProfileMember; |
| 27 | import p4.config.P4InfoOuterClass; |
| 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 | */ |
| 37 | public 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 60 | P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 61 | .getByName(piActionProfileId.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 62 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 63 | ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 64 | .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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 72 | throw new EncodeException(format("PI action group type %s not supported", piActionGroup.type())); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 73 | } |
| 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 84 | actionProfileGroupBuilder.setMaxSize(piActionGroup.members().size()); |
| 85 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 86 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 100 | Collection<ActionProfileMember> members, |
| 101 | PiPipeconf pipeconf) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 102 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 112 | |
| 113 | piActionGroupBuilder |
| 114 | .withActionProfileId(piActionProfileId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 115 | .withId(PiActionGroupId.of(actionProfileGroup.getGroupId())); |
| 116 | |
| 117 | switch (actionProfileGroup.getType()) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 118 | case UNSPECIFIED: |
| 119 | // FIXME: PI returns unspecified for select groups. Remove this case when PI bug will be fixed. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 120 | case SELECT: |
| 121 | piActionGroupBuilder.withType(PiActionGroup.Type.SELECT); |
| 122 | break; |
| 123 | default: |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 124 | throw new EncodeException(format("Action profile type %s is not supported", |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 125 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 140 | if (!memberWeights.containsKey(member.getMemberId())) { |
| 141 | // Not a member of this group, ignore. |
| 142 | continue; |
| 143 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 144 | int weight = memberWeights.get(member.getMemberId()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 145 | piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return piActionGroupBuilder.build(); |
| 149 | } |
| 150 | } |