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; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 20 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 21 | import org.onosproject.net.pi.runtime.PiMeterCellHandle; |
| 22 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
| 23 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 24 | import p4.v1.P4RuntimeOuterClass; |
| 25 | |
| 26 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 27 | |
| 28 | /** |
| 29 | * Codec for P4Runtime DirectMeterEntryCodec. |
| 30 | */ |
| 31 | public final class DirectMeterEntryCodec |
| 32 | extends AbstractEntityCodec<PiMeterCellConfig, PiMeterCellHandle, |
| 33 | P4RuntimeOuterClass.DirectMeterEntry, Object> { |
| 34 | |
| 35 | @Override |
| 36 | protected P4RuntimeOuterClass.DirectMeterEntry encode( |
| 37 | PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf, |
| 38 | P4InfoBrowser browser) |
| 39 | throws CodecException { |
Wailok Shum | 9664209 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 40 | P4RuntimeOuterClass.DirectMeterEntry.Builder builder = |
| 41 | P4RuntimeOuterClass.DirectMeterEntry.newBuilder() |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 42 | .setTableEntry(CODECS.tableEntry().encode( |
Wailok Shum | 9664209 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 43 | piEntity.cellId().tableEntry(), null, pipeconf)); |
| 44 | // We keep the config field unset if it is reset scenario |
| 45 | P4RuntimeOuterClass.MeterConfig meterConfig = MeterEntryCodec.getP4Config(piEntity); |
| 46 | if (meterConfig != null) { |
| 47 | builder = builder.setConfig(meterConfig); |
| 48 | } |
| 49 | return builder.build(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected P4RuntimeOuterClass.DirectMeterEntry encodeKey( |
| 54 | PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf, |
| 55 | P4InfoBrowser browser) |
| 56 | throws CodecException { |
| 57 | return keyMsgBuilder(handle.cellId(), pipeconf).build(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected P4RuntimeOuterClass.DirectMeterEntry encodeKey( |
| 62 | PiMeterCellConfig piEntity, Object metadata, |
| 63 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 64 | throws CodecException { |
| 65 | return keyMsgBuilder(piEntity.cellId(), pipeconf).build(); |
| 66 | } |
| 67 | |
| 68 | private P4RuntimeOuterClass.DirectMeterEntry.Builder keyMsgBuilder( |
| 69 | PiMeterCellId cellId, PiPipeconf pipeconf) |
| 70 | throws CodecException { |
| 71 | return P4RuntimeOuterClass.DirectMeterEntry.newBuilder() |
| 72 | .setTableEntry(CODECS.tableEntry().encodeKey( |
| 73 | cellId.tableEntry(), null, pipeconf)); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected PiMeterCellConfig decode( |
| 78 | P4RuntimeOuterClass.DirectMeterEntry message, Object ignored, |
| 79 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 80 | throws CodecException { |
Wailok Shum | 221d70d | 2021-08-22 19:44:56 +0800 | [diff] [blame^] | 81 | PiMeterCellId cellId = |
| 82 | PiMeterCellId.ofDirect( |
| 83 | CODECS.tableEntry().decode( |
| 84 | message.getTableEntry(), null, pipeconf)); |
| 85 | // When a field is unset, gRPC (P4RT) will return a default value |
| 86 | // So, if the meter config is unset, the value of rate and burst will be 0, |
| 87 | // while 0 is a meaningful value and not equals to UNSET in P4RT. |
| 88 | // We cannot extract the values directly. |
| 89 | P4RuntimeOuterClass.MeterConfig p4Config = |
| 90 | message.hasConfig() ? message.getConfig() : null; |
| 91 | |
| 92 | return MeterEntryCodec.getPiMeterCellConfig(cellId, p4Config); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 93 | } |
| 94 | } |