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 | |
| 35 | static P4RuntimeOuterClass.MeterConfig getP4Config(PiMeterCellConfig piConfig) |
| 36 | throws CodecException { |
| 37 | if (piConfig.meterBands().size() != 2) { |
| 38 | throw new CodecException("Number of meter bands should be 2"); |
| 39 | } |
| 40 | final PiMeterBand[] bands = piConfig.meterBands().toArray(new PiMeterBand[0]); |
| 41 | long cir, cburst, pir, pburst; |
| 42 | // The band with bigger burst is peak if rate of them is equal. |
| 43 | if (bands[0].rate() > bands[1].rate() || |
| 44 | (bands[0].rate() == bands[1].rate() && |
| 45 | bands[0].burst() >= bands[1].burst())) { |
| 46 | cir = bands[1].rate(); |
| 47 | cburst = bands[1].burst(); |
| 48 | pir = bands[0].rate(); |
| 49 | pburst = bands[0].burst(); |
| 50 | } else { |
| 51 | cir = bands[0].rate(); |
| 52 | cburst = bands[0].burst(); |
| 53 | pir = bands[1].rate(); |
| 54 | pburst = bands[1].burst(); |
| 55 | } |
| 56 | return P4RuntimeOuterClass.MeterConfig.newBuilder() |
| 57 | .setCir(cir) |
| 58 | .setCburst(cburst) |
| 59 | .setPir(pir) |
| 60 | .setPburst(pburst) |
| 61 | .build(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected P4RuntimeOuterClass.MeterEntry encode( |
| 66 | PiMeterCellConfig piEntity, Object ignored, PiPipeconf pipeconf, |
| 67 | P4InfoBrowser browser) |
| 68 | throws P4InfoBrowser.NotFoundException, CodecException { |
| 69 | final int meterId = browser.meters().getByName( |
| 70 | piEntity.cellId().meterId().id()).getPreamble().getId(); |
| 71 | return P4RuntimeOuterClass.MeterEntry.newBuilder() |
| 72 | .setMeterId(meterId) |
| 73 | .setIndex(P4RuntimeOuterClass.Index.newBuilder() |
| 74 | .setIndex(piEntity.cellId().index()).build()) |
| 75 | .setConfig(getP4Config(piEntity)) |
| 76 | .build(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected P4RuntimeOuterClass.MeterEntry encodeKey( |
| 81 | PiMeterCellHandle handle, Object metadata, PiPipeconf pipeconf, |
| 82 | P4InfoBrowser browser) |
| 83 | throws P4InfoBrowser.NotFoundException { |
| 84 | return keyMsgBuilder(handle.cellId(), browser).build(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected P4RuntimeOuterClass.MeterEntry encodeKey( |
| 89 | PiMeterCellConfig piEntity, Object metadata, PiPipeconf pipeconf, |
| 90 | P4InfoBrowser browser) |
| 91 | throws P4InfoBrowser.NotFoundException { |
| 92 | return keyMsgBuilder(piEntity.cellId(), browser).build(); |
| 93 | } |
| 94 | |
| 95 | private P4RuntimeOuterClass.MeterEntry.Builder keyMsgBuilder( |
| 96 | PiMeterCellId cellId, P4InfoBrowser browser) |
| 97 | throws P4InfoBrowser.NotFoundException { |
| 98 | final int meterId = browser.meters().getByName( |
| 99 | cellId.meterId().id()).getPreamble().getId(); |
| 100 | return P4RuntimeOuterClass.MeterEntry.newBuilder() |
| 101 | .setMeterId(meterId) |
| 102 | .setIndex(P4RuntimeOuterClass.Index.newBuilder() |
| 103 | .setIndex(cellId.index()).build()); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected PiMeterCellConfig decode( |
| 108 | P4RuntimeOuterClass.MeterEntry message, Object ignored, |
| 109 | PiPipeconf pipeconf, P4InfoBrowser browser) |
| 110 | throws P4InfoBrowser.NotFoundException { |
| 111 | final String meterName = browser.meters() |
| 112 | .getById(message.getMeterId()) |
| 113 | .getPreamble() |
| 114 | .getName(); |
| 115 | return PiMeterCellConfig.builder() |
| 116 | .withMeterCellId(PiMeterCellId.ofIndirect( |
| 117 | PiMeterId.of(meterName), message.getIndex().getIndex())) |
| 118 | .withMeterBand(new PiMeterBand(message.getConfig().getCir(), |
| 119 | message.getConfig().getCburst())) |
| 120 | .withMeterBand(new PiMeterBand(message.getConfig().getPir(), |
| 121 | message.getConfig().getPburst())) |
| 122 | .build(); |
| 123 | } |
| 124 | } |