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 | |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 19 | import org.onosproject.net.pi.model.PiActionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 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.PiActionGroupMember; |
| 22 | import org.onosproject.net.pi.runtime.PiActionGroupMemberId; |
| 23 | import p4.P4RuntimeOuterClass; |
| 24 | import p4.P4RuntimeOuterClass.ActionProfileMember; |
| 25 | import p4.config.P4InfoOuterClass; |
| 26 | |
| 27 | import static java.lang.String.format; |
| 28 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decodeActionMsg; |
| 29 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encodePiAction; |
| 30 | |
| 31 | /** |
| 32 | * Encoder/Decoder of action profile member. |
| 33 | */ |
| 34 | public final class ActionProfileMemberEncoder { |
| 35 | private ActionProfileMemberEncoder() { |
| 36 | // Hide default constructor |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Encode a PiActionGroupMember to a ActionProfileMember. |
| 41 | * |
| 42 | * @param group the PI action group of members |
| 43 | * @param member the member to encode |
| 44 | * @param pipeconf the pipeconf |
| 45 | * @return encoded member |
| 46 | */ |
| 47 | /** |
| 48 | * Encode a PiActionGroupMember to a ActionProfileMember. |
| 49 | * |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 50 | * @param profileId the PI action group profile ID of members |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 51 | * @param member the member to encode |
| 52 | * @param pipeconf the pipeconf, as encode spec |
| 53 | * @return encoded member |
| 54 | * @throws P4InfoBrowser.NotFoundException can't find action profile from P4Info browser |
| 55 | * @throws EncodeException can't find P4Info from pipeconf |
| 56 | */ |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 57 | static ActionProfileMember encode(PiActionProfileId profileId, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 58 | PiActionGroupMember member, |
| 59 | PiPipeconf pipeconf) |
| 60 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 61 | |
| 62 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 63 | |
| 64 | if (browser == null) { |
| 65 | throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf)); |
| 66 | } |
| 67 | |
| 68 | ActionProfileMember.Builder actionProfileMemberBuilder = |
| 69 | ActionProfileMember.newBuilder(); |
| 70 | |
| 71 | // member id |
| 72 | actionProfileMemberBuilder.setMemberId(member.id().id()); |
| 73 | |
| 74 | // action profile id |
| 75 | P4InfoOuterClass.ActionProfile actionProfile = |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 76 | browser.actionProfiles().getByName(profileId.id()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 77 | |
| 78 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 79 | actionProfileMemberBuilder.setActionProfileId(actionProfileId); |
| 80 | |
| 81 | // Action |
| 82 | P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser); |
| 83 | actionProfileMemberBuilder.setAction(action); |
| 84 | |
| 85 | return actionProfileMemberBuilder.build(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Decode an action profile member to PI action group member. |
| 90 | * |
| 91 | * @param member the action profile member |
| 92 | * @param weight the weight of the member |
| 93 | * @param pipeconf the pipeconf, as decode spec |
| 94 | * @return decoded PI action group member |
| 95 | * @throws P4InfoBrowser.NotFoundException can't find definition of action from P4 info |
| 96 | * @throws EncodeException can't get P4 info browser from pipeconf |
| 97 | */ |
| 98 | static PiActionGroupMember decode(ActionProfileMember member, |
| 99 | int weight, |
| 100 | PiPipeconf pipeconf) |
| 101 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 102 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 103 | |
| 104 | if (browser == null) { |
| 105 | throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf)); |
| 106 | } |
| 107 | return PiActionGroupMember.builder().withId(PiActionGroupMemberId.of(member.getMemberId())) |
| 108 | .withWeight(weight) |
| 109 | .withAction(decodeActionMsg(member.getAction(), browser)) |
| 110 | .build(); |
| 111 | } |
| 112 | } |