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