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 | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 24 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup; |
| 25 | import p4.v1.P4RuntimeOuterClass.ActionProfileGroup.Member; |
| 26 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 27 | import p4.config.v1.P4InfoOuterClass; |
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 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 39 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 61 | P4InfoOuterClass.ActionProfile actionProfile = browser.actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 62 | .getByName(piActionProfileId.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 63 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 64 | ActionProfileGroup.Builder actionProfileGroupBuilder = ActionProfileGroup.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 65 | .setGroupId(piActionGroup.id().id()) |
| 66 | .setActionProfileId(actionProfileId); |
| 67 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 68 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 77 | actionProfileGroupBuilder.setMaxSize(piActionGroup.members().size()); |
| 78 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 79 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 93 | Collection<ActionProfileMember> members, |
| 94 | PiPipeconf pipeconf) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 95 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 105 | |
| 106 | piActionGroupBuilder |
| 107 | .withActionProfileId(piActionProfileId) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 108 | .withId(PiActionGroupId.of(actionProfileGroup.getGroupId())); |
| 109 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 110 | 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 Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 122 | if (!memberWeights.containsKey(member.getMemberId())) { |
| 123 | // Not a member of this group, ignore. |
| 124 | continue; |
| 125 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 126 | int weight = memberWeights.get(member.getMemberId()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 127 | piActionGroupBuilder.addMember(ActionProfileMemberEncoder.decode(member, weight, pipeconf)); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return piActionGroupBuilder.build(); |
| 131 | } |
| 132 | } |