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