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.PiMeterId; |
| 20 | import org.onosproject.net.pi.model.PiPipeconf; |
| 21 | import org.onosproject.net.pi.runtime.PiMeterBand; |
| 22 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 23 | import org.onosproject.net.pi.runtime.PiMeterCellHandle; |
| 24 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
| 25 | import org.onosproject.p4runtime.ctl.utils.P4InfoBrowser; |
| 26 | import p4.v1.P4RuntimeOuterClass; |
| 27 | |
| 28 | /** |
| 29 | * Codec for P4Runtime MeterEntry. |
| 30 | */ |
| 31 | public final class MeterEntryCodec |
| 32 | extends AbstractEntityCodec<PiMeterCellConfig, PiMeterCellHandle, |
| 33 | P4RuntimeOuterClass.MeterEntry, Object> { |
| 34 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 35 | static P4RuntimeOuterClass.MeterConfig getP4Config(PiMeterCellConfig piConfig) { |
Wailok Shum | 6d42cff | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 36 | // The config has no band, we don't have to create a P4RT meter config |
| 37 | if (piConfig.isDefaultConfig()) { |
Wailok Shum | 9664209 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 38 | return null; |
| 39 | } |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 40 | |
| 41 | final PiMeterBand committedBand = piConfig.committedBand(); |
| 42 | final PiMeterBand peakBand = piConfig.peakBand(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 43 | return P4RuntimeOuterClass.MeterConfig.newBuilder() |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 44 | .setCir(committedBand.rate()) |
| 45 | .setCburst(committedBand.burst()) |
| 46 | .setPir(peakBand.rate()) |
| 47 | .setPburst(peakBand.burst()) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 48 | .build(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | protected P4RuntimeOuterClass.MeterEntry encode( |
| 53 | PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf, |
| 54 | P4InfoBrowser browser) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 55 | throws P4InfoBrowser.NotFoundException { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 56 | final int meterId = browser.meters().getByName( |
| 57 | piEntity.cellId().meterId().id()).getPreamble().getId(); |
Wailok Shum | 9664209 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 58 | P4RuntimeOuterClass.MeterEntry.Builder builder = |
| 59 | P4RuntimeOuterClass.MeterEntry.newBuilder() |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 60 | .setMeterId(meterId) |
| 61 | .setIndex(P4RuntimeOuterClass.Index.newBuilder() |
Wailok Shum | 9664209 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 62 | .setIndex(piEntity.cellId().index()).build()); |
| 63 | // We keep the config field unset if it is reset scenario |
| 64 | P4RuntimeOuterClass.MeterConfig meterConfig = getP4Config(piEntity); |
| 65 | if (meterConfig != null) { |
| 66 | builder = builder.setConfig(meterConfig); |
| 67 | } |
| 68 | return builder.build(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected P4RuntimeOuterClass.MeterEntry encodeKey( |
| 73 | PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf, |
| 74 | P4InfoBrowser browser) |
| 75 | throws P4InfoBrowser.NotFoundException { |
| 76 | return keyMsgBuilder(handle.cellId(), browser).build(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected P4RuntimeOuterClass.MeterEntry encodeKey( |
| 81 | PiMeterCellConfig piEntity, Object metadata, PiPipeconf pipeconf, |
| 82 | P4InfoBrowser browser) |
| 83 | throws P4InfoBrowser.NotFoundException { |
| 84 | return keyMsgBuilder(piEntity.cellId(), browser).build(); |
| 85 | } |
| 86 | |
| 87 | private P4RuntimeOuterClass.MeterEntry.Builder keyMsgBuilder( |
| 88 | PiMeterCellId cellId, P4InfoBrowser browser) |
| 89 | throws P4InfoBrowser.NotFoundException { |
| 90 | final int meterId = browser.meters().getByName( |
| 91 | cellId.meterId().id()).getPreamble().getId(); |
| 92 | return P4RuntimeOuterClass.MeterEntry.newBuilder() |
| 93 | .setMeterId(meterId) |
| 94 | .setIndex(P4RuntimeOuterClass.Index.newBuilder() |
| 95 | .setIndex(cellId.index()).build()); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected PiMeterCellConfig decode( |
| 100 | P4RuntimeOuterClass.MeterEntry message, Object ignored, |
| 101 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 102 | throws P4InfoBrowser.NotFoundException { |
| 103 | final String meterName = browser.meters() |
| 104 | .getById(message.getMeterId()) |
| 105 | .getPreamble() |
| 106 | .getName(); |
Wailok Shum | 221d70d | 2021-08-22 19:44:56 +0800 | [diff] [blame] | 107 | PiMeterCellId cellId = |
| 108 | PiMeterCellId.ofIndirect(PiMeterId.of(meterName), message.getIndex().getIndex()); |
| 109 | // When a field is unset, gRPC (P4RT) will return a default value |
| 110 | // So, if the meter config is unset, the value of rate and burst will be 0, |
| 111 | // while 0 is a meaningful value and not equals to UNSET in P4RT. |
| 112 | // We cannot extract the values directly. |
| 113 | P4RuntimeOuterClass.MeterConfig p4Config = |
| 114 | message.hasConfig() ? message.getConfig() : null; |
| 115 | |
| 116 | return getPiMeterCellConfig(cellId, p4Config); |
| 117 | } |
| 118 | |
| 119 | public static PiMeterCellConfig getPiMeterCellConfig( |
| 120 | PiMeterCellId cellId, P4RuntimeOuterClass.MeterConfig p4Config) { |
| 121 | PiMeterCellConfig.Builder builder = |
| 122 | PiMeterCellConfig.builder().withMeterCellId(cellId); |
| 123 | if (p4Config != null) { |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 124 | builder.withCommittedBand(p4Config.getCir(), p4Config.getCburst()) |
| 125 | .withPeakBand(p4Config.getPir(), p4Config.getPburst()); |
Wailok Shum | 221d70d | 2021-08-22 19:44:56 +0800 | [diff] [blame] | 126 | } |
| 127 | return builder.build(); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 128 | } |
| 129 | } |