Carmelo Cascone | 99c59db | 2019-01-17 15:39:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 org.onosproject.net.pi.model.PiActionProfileId; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 22 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
| 23 | import p4.config.v1.P4InfoOuterClass; |
| 24 | import p4.v1.P4RuntimeOuterClass; |
| 25 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 26 | |
| 27 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.decodeActionMsg; |
| 28 | import static org.onosproject.p4runtime.ctl.TableEntryEncoder.encodePiAction; |
| 29 | /** |
| 30 | * Codec for P4Runtime ActionProfileMember. |
| 31 | */ |
| 32 | final class ActionProfileMemberCodec |
| 33 | extends AbstractP4RuntimeCodec<PiActionProfileMember, ActionProfileMember> { |
| 34 | |
| 35 | @Override |
| 36 | public ActionProfileMember encode(PiActionProfileMember piEntity, |
| 37 | PiPipeconf pipeconf, |
| 38 | P4InfoBrowser browser) |
| 39 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 40 | final ActionProfileMember.Builder actionProfileMemberBuilder = |
| 41 | ActionProfileMember.newBuilder(); |
| 42 | // Member ID |
| 43 | actionProfileMemberBuilder.setMemberId(piEntity.id().id()); |
| 44 | // Action profile ID |
| 45 | P4InfoOuterClass.ActionProfile actionProfile = |
| 46 | browser.actionProfiles().getByName(piEntity.actionProfile().id()); |
| 47 | final int actionProfileId = actionProfile.getPreamble().getId(); |
| 48 | actionProfileMemberBuilder.setActionProfileId(actionProfileId); |
| 49 | // Action |
| 50 | final P4RuntimeOuterClass.Action action = encodePiAction(piEntity.action(), browser); |
| 51 | actionProfileMemberBuilder.setAction(action); |
| 52 | return actionProfileMemberBuilder.build(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public PiActionProfileMember decode(ActionProfileMember message, |
| 57 | PiPipeconf pipeconf, |
| 58 | P4InfoBrowser browser) |
| 59 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 60 | final PiActionProfileId actionProfileId = PiActionProfileId.of( |
| 61 | browser.actionProfiles() |
| 62 | .getById(message.getActionProfileId()) |
| 63 | .getPreamble() |
| 64 | .getName()); |
| 65 | return PiActionProfileMember.builder() |
| 66 | .forActionProfile(actionProfileId) |
| 67 | .withId(PiActionProfileMemberId.of(message.getMemberId())) |
| 68 | .withAction(decodeActionMsg(message.getAction(), browser)) |
| 69 | .build(); |
| 70 | } |
| 71 | } |