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; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 21 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 22 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
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 | */ |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 34 | final class ActionProfileMemberEncoder { |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 35 | private ActionProfileMemberEncoder() { |
| 36 | // Hide default constructor |
| 37 | } |
| 38 | |
| 39 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 40 | * Encode a PiActionProfileMember to a ActionProfileMember. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 41 | * |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 42 | * @param member the member to encode |
| 43 | * @param pipeconf the pipeconf, as encode spec |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 44 | * @return encoded member |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 45 | * @throws P4InfoBrowser.NotFoundException can't find action profile from |
| 46 | * P4Info browser |
| 47 | * @throws EncodeException can't find P4Info from pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 48 | */ |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 49 | static ActionProfileMember encode(PiActionProfileMember member, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 50 | PiPipeconf pipeconf) |
| 51 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 52 | |
| 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 | ActionProfileMember.Builder actionProfileMemberBuilder = |
| 60 | ActionProfileMember.newBuilder(); |
| 61 | |
| 62 | // member id |
| 63 | actionProfileMemberBuilder.setMemberId(member.id().id()); |
| 64 | |
| 65 | // action profile id |
| 66 | P4InfoOuterClass.ActionProfile actionProfile = |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 67 | browser.actionProfiles().getByName(member.actionProfile().id()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 68 | |
| 69 | int actionProfileId = actionProfile.getPreamble().getId(); |
| 70 | actionProfileMemberBuilder.setActionProfileId(actionProfileId); |
| 71 | |
| 72 | // Action |
| 73 | P4RuntimeOuterClass.Action action = encodePiAction(member.action(), browser); |
| 74 | actionProfileMemberBuilder.setAction(action); |
| 75 | |
| 76 | return actionProfileMemberBuilder.build(); |
| 77 | } |
| 78 | |
| 79 | /** |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 80 | * Decode an action profile member to PI action profile member. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 81 | * |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 82 | * @param member the action profile member |
| 83 | * @param weight the weight of the member |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 84 | * @param pipeconf the pipeconf, as decode spec |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 85 | * @return decoded PI action profile member |
Carmelo Cascone | 6af4e17 | 2018-06-15 16:01:30 +0200 | [diff] [blame] | 86 | * @throws P4InfoBrowser.NotFoundException can't find definition of action |
| 87 | * from P4 info |
| 88 | * @throws EncodeException can't get P4 info browser from |
| 89 | * pipeconf |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 90 | */ |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 91 | static PiActionProfileMember decode(ActionProfileMember member, |
| 92 | int weight, |
| 93 | PiPipeconf pipeconf) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 94 | throws P4InfoBrowser.NotFoundException, EncodeException { |
| 95 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 96 | if (browser == null) { |
| 97 | throw new EncodeException(format("Can't get P4 info browser from pipeconf %s", pipeconf)); |
| 98 | } |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 99 | |
| 100 | final PiActionProfileId actionProfileId = PiActionProfileId.of( |
| 101 | browser.actionProfiles() |
| 102 | .getById(member.getActionProfileId()) |
| 103 | .getPreamble() |
| 104 | .getName()); |
| 105 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 106 | return PiActionProfileMember.builder() |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 107 | .forActionProfile(actionProfileId) |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame^] | 108 | .withId(PiActionProfileMemberId.of(member.getMemberId())) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 109 | .withWeight(weight) |
| 110 | .withAction(decodeActionMsg(member.getAction(), browser)) |
| 111 | .build(); |
| 112 | } |
| 113 | } |