Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -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.codec; |
| 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.PiActionProfileMemberHandle; |
| 23 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
| 24 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 25 | import p4.v1.P4RuntimeOuterClass; |
| 26 | import p4.v1.P4RuntimeOuterClass.ActionProfileMember; |
| 27 | |
| 28 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 29 | |
| 30 | /** |
| 31 | * Codec for ActionProfileMember. |
| 32 | */ |
| 33 | public final class ActionProfileMemberCodec |
| 34 | extends AbstractEntityCodec<PiActionProfileMember, PiActionProfileMemberHandle, ActionProfileMember, Object> { |
| 35 | |
| 36 | @Override |
| 37 | public ActionProfileMember encode( |
| 38 | PiActionProfileMember piEntity, Object ignored, |
| 39 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 40 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 41 | return keyMsgBuilder( |
| 42 | piEntity.actionProfile(), piEntity.id(), browser) |
| 43 | .setAction(CODECS.action().encode( |
| 44 | piEntity.action(), null, pipeconf)) |
| 45 | .build(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | protected ActionProfileMember encodeKey( |
| 50 | PiActionProfileMemberHandle handle, Object ignored, |
| 51 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 52 | throws P4InfoBrowser.NotFoundException { |
| 53 | return keyMsgBuilder(handle.actionProfileId(), handle.memberId(), browser) |
| 54 | .build(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected ActionProfileMember encodeKey( |
| 59 | PiActionProfileMember piEntity, Object metadata, |
| 60 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 61 | throws P4InfoBrowser.NotFoundException { |
| 62 | return keyMsgBuilder( |
| 63 | piEntity.actionProfile(), piEntity.id(), browser) |
| 64 | .build(); |
| 65 | } |
| 66 | |
| 67 | private P4RuntimeOuterClass.ActionProfileMember.Builder keyMsgBuilder( |
| 68 | PiActionProfileId actProfId, PiActionProfileMemberId memberId, |
| 69 | P4InfoBrowser browser) |
| 70 | throws P4InfoBrowser.NotFoundException { |
| 71 | return P4RuntimeOuterClass.ActionProfileMember.newBuilder() |
| 72 | .setActionProfileId(browser.actionProfiles() |
| 73 | .getByName(actProfId.id()) |
| 74 | .getPreamble().getId()) |
| 75 | .setMemberId(memberId.id()); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public PiActionProfileMember decode( |
| 80 | ActionProfileMember message, Object ignored, |
| 81 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 82 | throws P4InfoBrowser.NotFoundException, CodecException { |
| 83 | final PiActionProfileId actionProfileId = PiActionProfileId.of( |
| 84 | browser.actionProfiles() |
| 85 | .getById(message.getActionProfileId()) |
| 86 | .getPreamble() |
| 87 | .getName()); |
| 88 | return PiActionProfileMember.builder() |
| 89 | .forActionProfile(actionProfileId) |
| 90 | .withId(PiActionProfileMemberId.of(message.getMemberId())) |
| 91 | .withAction(CODECS.action().decode( |
| 92 | message.getAction(), null, pipeconf)) |
| 93 | .build(); |
| 94 | } |
| 95 | } |