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.PiActionProfileGroup; |
| 21 | import org.onosproject.net.pi.runtime.PiActionProfileMember; |
| 22 | import org.onosproject.net.pi.runtime.PiCounterCell; |
| 23 | import org.onosproject.net.pi.runtime.PiEntity; |
| 24 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 25 | import org.onosproject.net.pi.runtime.PiMulticastGroupEntry; |
| 26 | import org.onosproject.net.pi.runtime.PiTableEntry; |
| 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 | /** |
| 34 | * Codec for P4Runtime Entity. |
| 35 | */ |
| 36 | public final class EntityCodec extends AbstractCodec<PiEntity, P4RuntimeOuterClass.Entity, Object> { |
| 37 | |
| 38 | @Override |
| 39 | protected P4RuntimeOuterClass.Entity encode( |
| 40 | PiEntity piEntity, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser) |
| 41 | throws CodecException { |
| 42 | final P4RuntimeOuterClass.Entity.Builder p4Entity = P4RuntimeOuterClass.Entity.newBuilder(); |
| 43 | switch (piEntity.piEntityType()) { |
| 44 | case TABLE_ENTRY: |
| 45 | return p4Entity.setTableEntry( |
| 46 | CODECS.tableEntry().encode( |
| 47 | (PiTableEntry) piEntity, null, pipeconf)) |
| 48 | .build(); |
| 49 | case ACTION_PROFILE_GROUP: |
| 50 | return p4Entity.setActionProfileGroup( |
| 51 | CODECS.actionProfileGroup().encode( |
| 52 | (PiActionProfileGroup) piEntity, null, pipeconf)) |
| 53 | .build(); |
| 54 | case ACTION_PROFILE_MEMBER: |
| 55 | return p4Entity.setActionProfileMember( |
| 56 | CODECS.actionProfileMember().encode( |
| 57 | (PiActionProfileMember) piEntity, null, pipeconf)) |
| 58 | .build(); |
| 59 | case PRE_MULTICAST_GROUP_ENTRY: |
| 60 | return p4Entity.setPacketReplicationEngineEntry( |
| 61 | P4RuntimeOuterClass.PacketReplicationEngineEntry.newBuilder() |
| 62 | .setMulticastGroupEntry(CODECS.multicastGroupEntry().encode( |
| 63 | (PiMulticastGroupEntry) piEntity, null, pipeconf)) |
| 64 | .build()) |
| 65 | .build(); |
| 66 | case METER_CELL_CONFIG: |
| 67 | final PiMeterCellConfig meterCellConfig = (PiMeterCellConfig) piEntity; |
| 68 | switch (meterCellConfig.cellId().meterType()) { |
| 69 | case DIRECT: |
| 70 | return p4Entity.setDirectMeterEntry( |
| 71 | CODECS.directMeterEntry().encode( |
| 72 | meterCellConfig, null, pipeconf)) |
| 73 | .build(); |
| 74 | case INDIRECT: |
| 75 | return p4Entity.setMeterEntry( |
| 76 | CODECS.meterEntry().encode( |
| 77 | meterCellConfig, null, pipeconf)) |
| 78 | .build(); |
| 79 | default: |
| 80 | throw new CodecException(format( |
| 81 | "Encoding of %s of type %s is not supported", |
| 82 | piEntity.piEntityType(), |
| 83 | meterCellConfig.cellId().meterType())); |
| 84 | } |
| 85 | case COUNTER_CELL: |
| 86 | final PiCounterCell counterCell = (PiCounterCell) piEntity; |
| 87 | switch (counterCell.cellId().counterType()) { |
| 88 | case DIRECT: |
| 89 | return p4Entity.setDirectCounterEntry( |
| 90 | CODECS.directCounterEntry().encode( |
| 91 | counterCell, null, pipeconf)) |
| 92 | .build(); |
| 93 | case INDIRECT: |
| 94 | return p4Entity.setCounterEntry( |
| 95 | CODECS.counterEntry().encode( |
| 96 | counterCell, null, pipeconf)) |
| 97 | .build(); |
| 98 | default: |
| 99 | throw new CodecException(format( |
| 100 | "Encoding of %s of type %s is not supported", |
| 101 | piEntity.piEntityType(), |
| 102 | counterCell.cellId().counterType())); |
| 103 | } |
| 104 | case REGISTER_CELL: |
| 105 | case PRE_CLONE_SESSION_ENTRY: |
| 106 | default: |
| 107 | throw new CodecException(format( |
| 108 | "Encoding of %s not supported", |
| 109 | piEntity.piEntityType())); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | protected PiEntity decode( |
| 115 | P4RuntimeOuterClass.Entity message, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser) |
| 116 | throws CodecException, P4InfoBrowser.NotFoundException { |
| 117 | switch (message.getEntityCase()) { |
| 118 | case TABLE_ENTRY: |
| 119 | return CODECS.tableEntry().decode( |
| 120 | message.getTableEntry(), null, pipeconf); |
| 121 | case ACTION_PROFILE_MEMBER: |
| 122 | return CODECS.actionProfileMember().decode( |
| 123 | message.getActionProfileMember(), null, pipeconf); |
| 124 | case ACTION_PROFILE_GROUP: |
| 125 | return CODECS.actionProfileGroup().decode( |
| 126 | message.getActionProfileGroup(), null, pipeconf); |
| 127 | case METER_ENTRY: |
| 128 | return CODECS.meterEntry().decode( |
| 129 | message.getMeterEntry(), null, pipeconf); |
| 130 | case DIRECT_METER_ENTRY: |
| 131 | return CODECS.directMeterEntry().decode( |
| 132 | message.getDirectMeterEntry(), null, pipeconf); |
| 133 | case COUNTER_ENTRY: |
| 134 | return CODECS.counterEntry().decode( |
| 135 | message.getCounterEntry(), null, pipeconf); |
| 136 | case DIRECT_COUNTER_ENTRY: |
| 137 | return CODECS.directCounterEntry().decode( |
| 138 | message.getDirectCounterEntry(), null, pipeconf); |
| 139 | case PACKET_REPLICATION_ENGINE_ENTRY: |
| 140 | switch (message.getPacketReplicationEngineEntry().getTypeCase()) { |
| 141 | case MULTICAST_GROUP_ENTRY: |
| 142 | return CODECS.multicastGroupEntry().decode( |
| 143 | message.getPacketReplicationEngineEntry() |
| 144 | .getMulticastGroupEntry(), null, pipeconf); |
| 145 | case CLONE_SESSION_ENTRY: |
| 146 | case TYPE_NOT_SET: |
| 147 | default: |
| 148 | throw new CodecException(format( |
| 149 | "Decoding of %s of type %s not supported", |
| 150 | message.getEntityCase(), |
| 151 | message.getPacketReplicationEngineEntry().getTypeCase())); |
| 152 | } |
| 153 | case VALUE_SET_ENTRY: |
| 154 | case REGISTER_ENTRY: |
| 155 | case DIGEST_ENTRY: |
| 156 | case EXTERN_ENTRY: |
| 157 | case ENTITY_NOT_SET: |
| 158 | default: |
| 159 | throw new CodecException(format( |
| 160 | "Decoding of %s not supported", |
| 161 | message.getEntityCase())); |
| 162 | |
| 163 | } |
| 164 | } |
| 165 | } |