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.PiPipeconf; |
| 20 | import org.onosproject.net.pi.runtime.PiActionProfileGroupHandle; |
| 21 | import org.onosproject.net.pi.runtime.PiActionProfileMemberHandle; |
| 22 | import org.onosproject.net.pi.runtime.PiCounterCellHandle; |
| 23 | import org.onosproject.net.pi.runtime.PiHandle; |
| 24 | import org.onosproject.net.pi.runtime.PiMeterCellHandle; |
| 25 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntryHandle; |
| 26 | import org.onosproject.net.pi.runtime.PiTableEntryHandle; |
| 27 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 28 | import p4.v1.P4RuntimeOuterClass; |
| 29 | |
| 30 | import static java.lang.String.format; |
| 31 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 32 | |
| 33 | public final class HandleCodec extends AbstractCodec<PiHandle, P4RuntimeOuterClass.Entity, Object> { |
| 34 | |
| 35 | @Override |
| 36 | protected P4RuntimeOuterClass.Entity encode( |
| 37 | PiHandle piHandle, Object ignored, PiPipeconf pipeconf, |
| 38 | P4InfoBrowser browser) |
| 39 | throws CodecException { |
| 40 | final P4RuntimeOuterClass.Entity.Builder p4Entity = P4RuntimeOuterClass.Entity.newBuilder(); |
| 41 | |
| 42 | switch (piHandle.entityType()) { |
| 43 | case TABLE_ENTRY: |
| 44 | return p4Entity.setTableEntry( |
| 45 | CODECS.tableEntry().encodeKey( |
| 46 | (PiTableEntryHandle) piHandle, null, pipeconf)) |
| 47 | .build(); |
| 48 | case ACTION_PROFILE_GROUP: |
| 49 | return p4Entity.setActionProfileGroup( |
| 50 | CODECS.actionProfileGroup().encodeKey( |
| 51 | (PiActionProfileGroupHandle) piHandle, null, pipeconf)) |
| 52 | .build(); |
| 53 | case ACTION_PROFILE_MEMBER: |
| 54 | return p4Entity.setActionProfileMember( |
| 55 | CODECS.actionProfileMember().encodeKey( |
| 56 | (PiActionProfileMemberHandle) piHandle, null, pipeconf)) |
| 57 | .build(); |
| 58 | case PRE_MULTICAST_GROUP_ENTRY: |
| 59 | return p4Entity.setPacketReplicationEngineEntry( |
| 60 | P4RuntimeOuterClass.PacketReplicationEngineEntry.newBuilder() |
| 61 | .setMulticastGroupEntry(CODECS.multicastGroupEntry().encodeKey( |
| 62 | (PiMulticastGroupEntryHandle) piHandle, null, pipeconf)) |
| 63 | .build()) |
| 64 | .build(); |
| 65 | case METER_CELL_CONFIG: |
| 66 | final PiMeterCellHandle meterCellHandle = (PiMeterCellHandle) piHandle; |
| 67 | switch (meterCellHandle.cellId().meterType()) { |
| 68 | case DIRECT: |
| 69 | return p4Entity.setDirectMeterEntry( |
| 70 | CODECS.directMeterEntry().encodeKey( |
| 71 | meterCellHandle, null, pipeconf)) |
| 72 | .build(); |
| 73 | case INDIRECT: |
| 74 | return p4Entity.setMeterEntry( |
| 75 | CODECS.meterEntry().encodeKey( |
| 76 | meterCellHandle, null, pipeconf)) |
| 77 | .build(); |
| 78 | default: |
| 79 | throw new CodecException(format( |
| 80 | "Encoding of handle for %s of type %s is not supported", |
| 81 | piHandle.entityType(), |
| 82 | meterCellHandle.cellId().meterType())); |
| 83 | } |
| 84 | case COUNTER_CELL: |
| 85 | final PiCounterCellHandle counterCellHandle = (PiCounterCellHandle) piHandle; |
| 86 | switch (counterCellHandle.cellId().counterType()) { |
| 87 | case DIRECT: |
| 88 | return p4Entity.setDirectCounterEntry( |
| 89 | CODECS.directCounterEntry().encodeKey( |
| 90 | counterCellHandle, null, pipeconf)) |
| 91 | .build(); |
| 92 | case INDIRECT: |
| 93 | return p4Entity.setCounterEntry( |
| 94 | CODECS.counterEntry().encodeKey( |
| 95 | counterCellHandle, null, pipeconf)) |
| 96 | .build(); |
| 97 | default: |
| 98 | throw new CodecException(format( |
| 99 | "Encoding of handle for %s of type %s is not supported", |
| 100 | piHandle.entityType(), |
| 101 | counterCellHandle.cellId().counterType())); |
| 102 | } |
| 103 | case REGISTER_CELL: |
| 104 | case PRE_CLONE_SESSION_ENTRY: |
| 105 | default: |
| 106 | throw new CodecException(format( |
| 107 | "Encoding of handle for %s not supported", |
| 108 | piHandle.entityType())); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected PiHandle decode( |
| 114 | P4RuntimeOuterClass.Entity message, Object ignored, |
| 115 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 116 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 117 | throw new CodecException("Decoding of Entity to PiHandle is not supported"); |
| 118 | } |
| 119 | } |