Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.Maps; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 25 | |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 26 | import java.util.Collections; |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 27 | import java.util.Map; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Configuration of a meter cell of a protocol-independent pipeline. |
| 33 | */ |
| 34 | @Beta |
| 35 | public final class PiMeterCellConfig implements PiEntity { |
| 36 | |
| 37 | private final PiMeterCellId cellId; |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 38 | private final ImmutableMap<PiMeterBandType, PiMeterBand> piMeterBands; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Creates a new meter cell configuration for the given cell identifier and meter bands. |
| 42 | * |
| 43 | * @param cellId meter cell identifier |
| 44 | * @param piMeterBands meter bands |
| 45 | */ |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 46 | private PiMeterCellConfig(PiMeterCellId cellId, Map<PiMeterBandType, PiMeterBand> piMeterBands) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 47 | this.cellId = cellId; |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 48 | this.piMeterBands = ImmutableMap.copyOf(piMeterBands); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the cell identifier. |
| 53 | * |
| 54 | * @return cell identifier |
| 55 | */ |
| 56 | public PiMeterCellId cellId() { |
| 57 | return cellId; |
| 58 | } |
| 59 | |
| 60 | /** |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 61 | * Returns the map of bands of this cell. |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 62 | * |
| 63 | * @return meter bands |
| 64 | */ |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 65 | public Map<PiMeterBandType, PiMeterBand> meterBands() { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 66 | return piMeterBands; |
| 67 | } |
| 68 | |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 69 | /** |
| 70 | * Check if the config represents a modify operation. |
Wailok Shum | 8ebb793 | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 71 | * Or it is a non-default config read from south bound. |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 72 | * |
| 73 | * @return true if there are exactly 2 bands |
| 74 | */ |
Wailok Shum | 8ebb793 | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 75 | public boolean isModifyConfig() { |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 76 | return piMeterBands.size() == 2; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Check if the config represents a reset operation. |
Wailok Shum | 8ebb793 | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 81 | * Or it is a default config read from south bound. |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 82 | * |
Wailok Shum | 8ebb793 | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 83 | * @return true if there is no band |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 84 | */ |
Wailok Shum | 8ebb793 | 2021-08-22 19:40:13 +0800 | [diff] [blame] | 85 | public boolean isDefaultConfig() { |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 86 | return piMeterBands.isEmpty(); |
| 87 | } |
| 88 | |
| 89 | /** |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 90 | * Returns the committed configuration if present. |
| 91 | * |
| 92 | * @return the committed band. Null otherwise |
| 93 | */ |
| 94 | public PiMeterBand committedBand() { |
| 95 | return piMeterBands.get(PiMeterBandType.COMMITTED); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the peak configuration if present. |
| 100 | * |
| 101 | * @return the peak band. Null otherwise |
| 102 | */ |
| 103 | public PiMeterBand peakBand() { |
| 104 | return piMeterBands.get(PiMeterBandType.PEAK); |
| 105 | } |
| 106 | |
| 107 | /** |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 108 | * Returns a PiMeterCellConfig with no bands. |
| 109 | * Used to reset a PI meter cell. |
| 110 | * |
| 111 | * @param piMeterCellId the PiMeterCellId need to be reset |
| 112 | * @return a PiMeterCellConfig with no bands |
| 113 | */ |
| 114 | public static PiMeterCellConfig reset(PiMeterCellId piMeterCellId) { |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 115 | return new PiMeterCellConfig(piMeterCellId, Collections.emptyMap()); |
Wailok Shum | 8aa2145 | 2021-08-06 16:23:36 +0800 | [diff] [blame] | 116 | } |
| 117 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 118 | @Override |
| 119 | public PiEntityType piEntityType() { |
| 120 | return PiEntityType.METER_CELL_CONFIG; |
| 121 | } |
| 122 | |
| 123 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 124 | public PiMeterCellHandle handle(DeviceId deviceId) { |
| 125 | return PiMeterCellHandle.of(deviceId, this); |
| 126 | } |
| 127 | |
| 128 | @Override |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 129 | public boolean equals(Object o) { |
| 130 | if (this == o) { |
| 131 | return true; |
| 132 | } |
| 133 | if (!(o instanceof PiMeterCellConfig)) { |
| 134 | return false; |
| 135 | } |
| 136 | PiMeterCellConfig that = (PiMeterCellConfig) o; |
| 137 | |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 138 | return Objects.equal(piMeterBands, that.piMeterBands) && |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 139 | Objects.equal(cellId, that.cellId); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | return Objects.hashCode(cellId, piMeterBands); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public String toString() { |
| 149 | return MoreObjects.toStringHelper(this) |
| 150 | .add("cellId", cellId) |
| 151 | .add("meterBands", piMeterBands) |
| 152 | .toString(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns a meter cell configuration builder. |
| 157 | * |
| 158 | * @return a new builder |
| 159 | */ |
| 160 | public static PiMeterCellConfig.Builder builder() { |
| 161 | return new PiMeterCellConfig.Builder(); |
| 162 | } |
| 163 | |
| 164 | public static final class Builder { |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 165 | private PiMeterCellId cellId; |
| 166 | private Map<PiMeterBandType, PiMeterBand> bands = Maps.newHashMap(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 167 | |
| 168 | |
| 169 | private Builder() { |
| 170 | // Hides constructor. |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Sets the meter cell identifier for this meter. |
| 175 | * |
| 176 | * @param meterCellId meter cell identifier |
| 177 | * @return this |
| 178 | */ |
| 179 | public PiMeterCellConfig.Builder withMeterCellId(PiMeterCellId meterCellId) { |
| 180 | this.cellId = meterCellId; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Sets a meter band of this meter. |
| 187 | * |
| 188 | * @param band meter band |
| 189 | * @return this |
| 190 | */ |
| 191 | public PiMeterCellConfig.Builder withMeterBand(PiMeterBand band) { |
pierventre | 36b9a89 | 2021-08-27 15:25:02 +0200 | [diff] [blame] | 192 | this.bands.put(band.type(), band); |
| 193 | return this; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets the committed band of this meter. |
| 198 | * |
| 199 | * @param rate committed rate |
| 200 | * @param burst committed burst |
| 201 | * @return this |
| 202 | */ |
| 203 | public PiMeterCellConfig.Builder withCommittedBand(long rate, long burst) { |
| 204 | this.bands.put(PiMeterBandType.COMMITTED, new PiMeterBand(PiMeterBandType.COMMITTED, rate, burst)); |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Sets the peak band of this meter. |
| 210 | * |
| 211 | * @param rate peak rate |
| 212 | * @param burst peak burst |
| 213 | * @return this |
| 214 | */ |
| 215 | public PiMeterCellConfig.Builder withPeakBand(long rate, long burst) { |
| 216 | this.bands.put(PiMeterBandType.PEAK, new PiMeterBand(PiMeterBandType.PEAK, rate, burst)); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 217 | return this; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Builds the meter cell configuration. |
| 222 | * |
| 223 | * @return a new meter cell configuration |
| 224 | */ |
| 225 | public PiMeterCellConfig build() { |
| 226 | checkNotNull(cellId); |
| 227 | return new PiMeterCellConfig(cellId, bands); |
| 228 | } |
| 229 | } |
| 230 | } |