Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022-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.behaviour.upf; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.net.meter.Band; |
| 22 | import org.onosproject.net.meter.DefaultBand; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.onosproject.net.behaviour.upf.UpfEntityType.APPLICATION_METER; |
| 31 | import static org.onosproject.net.behaviour.upf.UpfEntityType.SESSION_METER; |
| 32 | import static org.onosproject.net.meter.Band.Type.MARK_RED; |
| 33 | import static org.onosproject.net.meter.Band.Type.MARK_YELLOW; |
| 34 | |
| 35 | /** |
| 36 | * A structure representing a UPF meter, either for metering session (UE) or |
| 37 | * application traffic. |
| 38 | * UPF meters represent PFCP QER MBR and GBR information. |
| 39 | * UPF meters of type session support only the peak band. |
| 40 | * UPF meters of type application support both peak and committed bands. |
| 41 | */ |
| 42 | public final class UpfMeter implements UpfEntity { |
| 43 | private final int cellId; |
| 44 | private final ImmutableMap<Band.Type, Band> meterBands; |
| 45 | private final UpfEntityType type; |
| 46 | |
| 47 | private UpfMeter(int cellId, Map<Band.Type, Band> meterBands, UpfEntityType type) { |
| 48 | this.cellId = cellId; |
| 49 | this.meterBands = ImmutableMap.copyOf(meterBands); |
| 50 | this.type = type; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public String toString() { |
| 55 | return String.format("UpfMeter(type=%s, index=%d, committed=%s, peak=%s)", |
| 56 | type, cellId, committedBand().orElse(null), |
| 57 | peakBand().orElse(null)); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean equals(Object object) { |
| 62 | if (this == object) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | if (object == null) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (getClass() != object.getClass()) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | UpfMeter that = (UpfMeter) object; |
| 75 | return this.type.equals(that.type) && |
| 76 | this.cellId == that.cellId && |
| 77 | this.meterBands.equals(that.meterBands); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| 82 | return Objects.hash(type, cellId, meterBands); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public UpfEntityType type() { |
| 87 | return this.type; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the meter cell index of this meter. |
| 92 | * |
| 93 | * @return the cell index |
| 94 | */ |
| 95 | public int cellId() { |
| 96 | return this.cellId; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the committed band of this meter. |
| 101 | * |
| 102 | * @return the committed band, Empty if none |
| 103 | */ |
| 104 | public Optional<Band> committedBand() { |
| 105 | return Optional.ofNullable(meterBands.getOrDefault(MARK_YELLOW, null)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the peak band of this meter. |
| 110 | * |
| 111 | * @return the peak band, Empty if none |
| 112 | */ |
| 113 | public Optional<Band> peakBand() { |
| 114 | return Optional.ofNullable(meterBands.getOrDefault(MARK_RED, null)); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if this UPF meter is for sessions (UE) traffic. |
| 119 | * |
| 120 | * @return true if the meter is for session traffic |
| 121 | */ |
| 122 | public boolean isSession() { |
| 123 | return type.equals(SESSION_METER); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check if this UPF meter is for application traffic. |
| 128 | * |
| 129 | * @return true if the meter is for application traffic |
| 130 | */ |
| 131 | public boolean isApplication() { |
| 132 | return type.equals(APPLICATION_METER); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Check if this UPF meter is a reset. |
| 137 | * |
| 138 | * @return true if this represents a meter reset. |
| 139 | */ |
| 140 | public boolean isReset() { |
| 141 | return meterBands.isEmpty(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Return a session UPF meter with no bands. Used to reset the meter. |
| 146 | * |
| 147 | * @param cellId the meter cell index of this meter |
| 148 | * @return a UpfMeter of type session with no bands |
| 149 | */ |
| 150 | public static UpfMeter resetSession(int cellId) { |
| 151 | return new UpfMeter(cellId, Maps.newHashMap(), SESSION_METER); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Return an application UPF meter with no bands. Used to reset the meter. |
| 156 | * |
| 157 | * @param cellId the meter cell index of this meter |
| 158 | * @return a UpfMeter of type application with no bands |
| 159 | */ |
| 160 | public static UpfMeter resetApplication(int cellId) { |
| 161 | return new UpfMeter(cellId, Maps.newHashMap(), APPLICATION_METER); |
| 162 | } |
| 163 | |
| 164 | public static Builder builder() { |
| 165 | return new Builder(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Builder of UpfMeter object. Use {@link #resetApplication(int)} and |
| 170 | * {@link #resetSession(int)} to reset the meter config. |
| 171 | */ |
| 172 | public static class Builder { |
| 173 | private Integer cellId = null; |
| 174 | private Map<Band.Type, Band> bands = Maps.newHashMap(); |
| 175 | private UpfEntityType type; |
| 176 | |
| 177 | public Builder() { |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Set the meter cell index of this meter. |
| 183 | * |
| 184 | * @param cellId the meter cell index |
| 185 | * @return this builder object |
| 186 | */ |
| 187 | public Builder setCellId(int cellId) { |
| 188 | this.cellId = cellId; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Set the committed band of this meter. |
| 194 | * Valid only for meter of type application. |
| 195 | * |
| 196 | * @param cir the Committed Information Rate in bytes/s |
| 197 | * @param cburst the Committed Burst in bytes |
| 198 | * @return this builder object |
| 199 | */ |
| 200 | public Builder setCommittedBand(long cir, long cburst) { |
| 201 | this.bands.put(MARK_YELLOW, |
| 202 | DefaultBand.builder() |
| 203 | .ofType(MARK_YELLOW) |
| 204 | .withRate(cir) |
| 205 | .burstSize(cburst) |
| 206 | .build() |
| 207 | ); |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Set the peak band of this meter. |
| 213 | * |
| 214 | * @param pir the Peak Information Rate in bytes/s |
| 215 | * @param pburst the Peak Burst in bytes |
| 216 | * @return this builder object |
| 217 | */ |
| 218 | public Builder setPeakBand(long pir, long pburst) { |
| 219 | this.bands.put(MARK_RED, |
| 220 | DefaultBand.builder() |
| 221 | .ofType(MARK_RED) |
| 222 | .withRate(pir) |
| 223 | .burstSize(pburst) |
| 224 | .build() |
| 225 | ); |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Make this meter a session meter. |
| 231 | * |
| 232 | * @return this builder object |
| 233 | */ |
| 234 | public Builder setSession() { |
| 235 | this.type = SESSION_METER; |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Make this meter an application meter. |
| 241 | * |
| 242 | * @return this builder object |
| 243 | */ |
| 244 | public Builder setApplication() { |
| 245 | this.type = APPLICATION_METER; |
| 246 | return this; |
| 247 | } |
| 248 | |
| 249 | public UpfMeter build() { |
| 250 | checkNotNull(type, "A meter type must be assigned"); |
| 251 | switch (type) { |
| 252 | case SESSION_METER: |
| 253 | checkArgument(!bands.containsKey(MARK_YELLOW), |
| 254 | "Committed band can not be provided for session meter!"); |
| 255 | break; |
| 256 | case APPLICATION_METER: |
| 257 | checkArgument((bands.containsKey(MARK_YELLOW) && bands.containsKey(MARK_RED)) || bands.isEmpty(), |
| 258 | "Bands (committed and peak) must be provided together or not at all!"); |
| 259 | break; |
| 260 | default: |
| 261 | // I should never reach this point |
| 262 | throw new IllegalArgumentException("Invalid meter type, I should never reach this point"); |
| 263 | } |
| 264 | checkNotNull(cellId, "Meter cell ID must be provided!"); |
| 265 | return new UpfMeter(cellId, bands, type); |
| 266 | } |
| 267 | } |
| 268 | } |