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; |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 23 | import p4.config.v1.P4InfoOuterClass; |
| 24 | import p4.v1.P4RuntimeOuterClass; |
| 25 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 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 | * |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 42 | * @param profileId the PI action group profile ID of members |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 43 | * @param member the member to encode |
| 44 | * @param pipeconf the pipeconf, as encode spec |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 45 | * @return encoded member |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 46 | * @throws P4InfoBrowser.NotFoundException can't find action profile from |
| 47 | * P4Info browser |
| 48 | * @throws EncodeException can't find P4Info from pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 49 | */ |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 50 | static ActionProfileMember encode(PiActionProfileId profileId, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 51 | PiActionGroupMember member, |
| 52 | PiPipeconf pipeconf) |
| 53 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 54 | |
| 55 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 56 | |
| 57 | if (browser == null) { |
| 58 | throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf)); |
| 59 | } |
| 60 | |
| 61 | ActionProfileMember.Builder actionProfileMemberBuilder = |
| 62 | ActionProfileMember.newBuilder(); |
| 63 | |
| 64 | // member id |
| 65 | actionProfileMemberBuilder.setMemberId(member.id().id()); |
| 66 | |
| 67 | // action profile id |
| 68 | P4InfoOuterClass.ActionProfile actionProfile = |
Yi Tseng | 8d35513 | 2018-04-13 01:40:48 +0800 | [diff] [blame] | 69 | browser.actionProfiles().getByName(profileId.id()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 70 | |
| 71 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 72 | actionProfileMemberBuilder.setActionProfileId(actionProfileId); |
| 73 | |
| 74 | // Action |
| 75 | P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser); |
| 76 | actionProfileMemberBuilder.setAction(action); |
| 77 | |
| 78 | return actionProfileMemberBuilder.build(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Decode an action profile member to PI action group member. |
| 83 | * |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 84 | * @param member the action profile member |
| 85 | * @param weight the weight of the member |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 86 | * @param pipeconf the pipeconf, as decode spec |
| 87 | * @return decoded PI action group member |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame^] | 88 | * @throws P4InfoBrowser.NotFoundException can't find definition of action |
| 89 | * from P4 info |
| 90 | * @throws EncodeException can't get P4 info browser from |
| 91 | * pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | static PiActionGroupMember decode(ActionProfileMember member, |
| 94 | int weight, |
| 95 | PiPipeconf pipeconf) |
| 96 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 97 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 98 | |
| 99 | if (browser == null) { |
| 100 | throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf)); |
| 101 | } |
| 102 | return PiActionGroupMember.builder().withId(PiActionGroupMemberId.of(member.getMemberId())) |
| 103 | .withWeight(weight) |
| 104 | .withAction(decodeActionMsg(member.getAction(), browser)) |
| 105 | .build(); |
| 106 | } |
| 107 | } |