tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.annotations.Beta; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * A structure representing the UE Termination in the uplink direction on the |
| 28 | * UPF-programmable device. |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 29 | * Provide means to configure the traffic behavior (e.g. set Traffic Class). |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 30 | */ |
| 31 | @Beta |
| 32 | public final class UpfTerminationUplink implements UpfEntity { |
| 33 | // Match Keys |
| 34 | private final Ip4Address ueSessionId; // UE Session ID, use UE IP address to uniquely identify a session. |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 35 | private final byte applicationId; // Application ID defaults to DEFAULT_APP_ID |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 36 | // Action parameters |
| 37 | private final Integer ctrId; // Counter ID unique to this UPF Termination Rule |
| 38 | private final Byte trafficClass; |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 39 | private final int appMeterIdx; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 40 | private final boolean dropping; |
| 41 | |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 42 | private UpfTerminationUplink(Ip4Address ueSessionId, byte applicationId, |
| 43 | Integer ctrId, Byte trafficClass, |
| 44 | int appMeterIdx, boolean dropping) { |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 45 | this.ueSessionId = ueSessionId; |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 46 | this.applicationId = applicationId; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 47 | this.ctrId = ctrId; |
| 48 | this.trafficClass = trafficClass; |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 49 | this.appMeterIdx = appMeterIdx; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 50 | this.dropping = dropping; |
| 51 | } |
| 52 | |
| 53 | public static Builder builder() { |
| 54 | return new Builder(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object obj) { |
| 59 | if (obj == this) { |
| 60 | return true; |
| 61 | } |
| 62 | if (obj == null) { |
| 63 | return false; |
| 64 | } |
| 65 | if (getClass() != obj.getClass()) { |
| 66 | return false; |
| 67 | } |
| 68 | UpfTerminationUplink that = (UpfTerminationUplink) obj; |
| 69 | |
| 70 | // Safe comparisons between potentially null objects |
| 71 | return this.dropping == that.dropping && |
| 72 | Objects.equals(this.ueSessionId, that.ueSessionId) && |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 73 | Objects.equals(this.applicationId, that.applicationId) && |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 74 | Objects.equals(this.ctrId, that.ctrId) && |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 75 | this.appMeterIdx == that.appMeterIdx && |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 76 | Objects.equals(this.trafficClass, that.trafficClass); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int hashCode() { |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 81 | return Objects.hash(ueSessionId, applicationId, ctrId, trafficClass, appMeterIdx, dropping); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get UE Session ID associated with UPF Termination rule. |
| 86 | * |
| 87 | * @return UE Session ID |
| 88 | */ |
| 89 | public Ip4Address ueSessionId() { |
| 90 | return ueSessionId; |
| 91 | } |
| 92 | |
| 93 | /** |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 94 | * Get the application ID associated with UPF Termination rule. |
| 95 | * |
| 96 | * @return Application ID |
| 97 | */ |
| 98 | public byte applicationId() { |
| 99 | return applicationId; |
| 100 | } |
| 101 | |
| 102 | /** |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 103 | * Get PDR Counter ID associated with UPF Termination rule. |
| 104 | * |
| 105 | * @return PDR counter cell ID |
| 106 | */ |
| 107 | public Integer counterId() { |
| 108 | return ctrId; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get Traffic Class set by this UPF Termination rule. |
| 113 | * |
| 114 | * @return Traffic Class |
| 115 | */ |
| 116 | public Byte trafficClass() { |
| 117 | return trafficClass; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * True if this UPF Termination needs to drop traffic. |
| 122 | * |
| 123 | * @return true if the UPF Termination needs dropping. |
| 124 | */ |
| 125 | public boolean needsDropping() { |
| 126 | return dropping; |
| 127 | } |
| 128 | |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 129 | /** |
| 130 | * Get the app meter index set by this UPF Termination rule. |
| 131 | * |
| 132 | * @return App meter index |
| 133 | */ |
| 134 | public int appMeterIdx() { |
| 135 | return appMeterIdx; |
| 136 | } |
| 137 | |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 138 | @Override |
| 139 | public UpfEntityType type() { |
| 140 | return UpfEntityType.TERMINATION_UPLINK; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 145 | return "UpfTerminationUL(" + matchString() + " -> " + actionString() + ")"; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private String matchString() { |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 149 | return "Match(ue_addr=" + this.ueSessionId() + ", app_id=" + this.applicationId() + ")"; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | private String actionString() { |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 153 | String fwd = "FWD"; |
| 154 | if (this.needsDropping()) { |
| 155 | fwd = "DROP"; |
| 156 | } |
Daniele Moro | de1f1f7 | 2022-01-26 16:47:08 +0100 | [diff] [blame] | 157 | return "Action(" + fwd + |
| 158 | ", ctr_id=" + this.counterId() + |
| 159 | ", tc=" + this.trafficClass() + |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 160 | ", app_meter_idx=" + this.appMeterIdx() + |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 161 | ")"; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public static class Builder { |
| 165 | private Ip4Address ueSessionId = null; |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 166 | private Byte applicationId = null; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 167 | private Integer ctrId = null; |
| 168 | private Byte trafficClass = null; |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 169 | private int appMeterIdx = DEFAULT_APP_INDEX; |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 170 | private boolean dropping = false; |
| 171 | |
| 172 | public Builder() { |
| 173 | |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Set the ID of the UE session. |
| 178 | * |
| 179 | * @param ueSessionId UE session ID |
| 180 | * @return This builder object |
| 181 | */ |
| 182 | public Builder withUeSessionId(Ip4Address ueSessionId) { |
| 183 | this.ueSessionId = ueSessionId; |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | /** |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 188 | * Set the ID of the application. |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 189 | * If not set, default to {@link UpfEntity#DEFAULT_APP_ID}. |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 190 | * |
| 191 | * @param applicationId Application ID |
| 192 | * @return This builder object |
| 193 | */ |
| 194 | public Builder withApplicationId(byte applicationId) { |
| 195 | this.applicationId = applicationId; |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | /** |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 200 | * Set the dataplane counter cell ID. |
| 201 | * |
| 202 | * @param ctrId PDR counter cell ID |
| 203 | * @return This builder object |
| 204 | */ |
| 205 | public Builder withCounterId(int ctrId) { |
| 206 | this.ctrId = ctrId; |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Set the Traffic Class. |
| 212 | * |
| 213 | * @param trafficClass Traffic Class |
| 214 | * @return This builder object |
| 215 | */ |
| 216 | public Builder withTrafficClass(byte trafficClass) { |
| 217 | this.trafficClass = trafficClass; |
| 218 | return this; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets whether to drop uplink UPF termination traffic or not. |
| 223 | * |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 224 | * @param dropping True if request to drop, false otherwise |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 225 | * @return This builder object |
| 226 | */ |
| 227 | public Builder needsDropping(boolean dropping) { |
| 228 | this.dropping = dropping; |
| 229 | return this; |
| 230 | } |
| 231 | |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 232 | /** |
| 233 | * Sets the app meter index. |
| 234 | * If not set, default to {@link UpfEntity#DEFAULT_APP_INDEX}. |
| 235 | * |
| 236 | * @param appMeterIdx App meter index |
| 237 | * @return This builder object |
| 238 | */ |
| 239 | public Builder withAppMeterIdx(int appMeterIdx) { |
| 240 | this.appMeterIdx = appMeterIdx; |
| 241 | return this; |
| 242 | } |
| 243 | |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 244 | public UpfTerminationUplink build() { |
| 245 | // Match fields must be provided |
| 246 | checkNotNull(ueSessionId, "UE session ID must be provided"); |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 247 | if (applicationId == null) { |
| 248 | applicationId = DEFAULT_APP_ID; |
| 249 | } |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 250 | checkNotNull(ctrId, "Counter ID must be provided"); |
Daniele Moro | b471bd1 | 2022-03-09 10:52:32 +0100 | [diff] [blame] | 251 | if (!dropping) { |
| 252 | checkNotNull(trafficClass, "Traffic class must be provided"); |
| 253 | } |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 254 | // TODO: should we verify that when dropping no other fields are provided |
| 255 | return new UpfTerminationUplink( |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 256 | this.ueSessionId, this.applicationId, this.ctrId, |
Daniele Moro | b25299a | 2022-01-26 15:47:29 +0100 | [diff] [blame] | 257 | this.trafficClass, this.appMeterIdx, this.dropping |
Daniele Moro | 97fd80c | 2022-01-13 19:16:34 +0100 | [diff] [blame] | 258 | ); |
tosinski | a57652d | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | } |