blob: 017de89d650ee64aae44cef0f3d6359a0683df37 [file] [log] [blame]
Daniele Moro97fd80c2022-01-13 19:16:34 +01001/*
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
17package org.onosproject.net.behaviour.upf;
18
19import com.google.common.annotations.Beta;
20import com.google.common.collect.Range;
21import org.onlab.packet.Ip4Prefix;
22
23import java.util.Objects;
24import java.util.Optional;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * A structure representing the application filtering for the UPF-programmable device.
31 */
32@Beta
33public final class UpfApplication implements UpfEntity {
34 // Match Keys
35 private final Ip4Prefix ipPrefix;
36 private final Range<Short> l4PortRange;
37 private final Byte ipProto;
Daniele Morode1f1f72022-01-26 16:47:08 +010038 // TODO: move to SliceId object when slice APIs will be promoted to ONOS core.
39 private final int sliceId;
Daniele Moro97fd80c2022-01-13 19:16:34 +010040 // Action parameter
41 private final byte appId;
42
43 private final int priority;
44
45 private UpfApplication(Ip4Prefix ipPrefix, Range<Short> l4PortRange,
Daniele Morode1f1f72022-01-26 16:47:08 +010046 Byte ipProto, int sliceId, byte appId, int priority) {
Daniele Moro97fd80c2022-01-13 19:16:34 +010047 this.ipPrefix = ipPrefix;
48 this.l4PortRange = l4PortRange;
49 this.ipProto = ipProto;
Daniele Morode1f1f72022-01-26 16:47:08 +010050 this.sliceId = sliceId;
Daniele Moro97fd80c2022-01-13 19:16:34 +010051 this.appId = appId;
52 this.priority = priority;
53 }
54
55 public static Builder builder() {
56 return new Builder();
57 }
58
59 @Override
60 public boolean equals(Object object) {
61 if (object == this) {
62 return true;
63 }
64 if (object == null) {
65 return false;
66 }
67 if (getClass() != object.getClass()) {
68 return false;
69 }
70
71 UpfApplication that = (UpfApplication) object;
72
73 return Objects.equals(this.ipPrefix, that.ipPrefix) &&
74 Objects.equals(this.l4PortRange, that.l4PortRange) &&
75 Objects.equals(this.ipProto, that.ipProto) &&
Daniele Morode1f1f72022-01-26 16:47:08 +010076 this.sliceId == that.sliceId &&
Daniele Moro97fd80c2022-01-13 19:16:34 +010077 this.appId == that.appId &&
78 this.priority == that.priority;
79 }
80
81 @Override
82 public int hashCode() {
Daniele Morode1f1f72022-01-26 16:47:08 +010083 return Objects.hash(ipPrefix, l4PortRange, ipProto, sliceId, appId, priority);
Daniele Moro97fd80c2022-01-13 19:16:34 +010084 }
85
86 @Override
87 public String toString() {
Daniele Morode1f1f72022-01-26 16:47:08 +010088 return "UpfApplication(priority=" + this.priority + ", " + matchString() + " -> " + actionString() + ")";
Daniele Moro97fd80c2022-01-13 19:16:34 +010089 }
90
91 private String matchString() {
92 StringBuilder matchStrBuilder = new StringBuilder("Match(");
93 if (this.ipPrefix != null) {
94 matchStrBuilder.append("ip_prefix=")
95 .append(this.ipPrefix)
96 .append(", ");
97 }
98 if (this.l4PortRange != null) {
99 matchStrBuilder.append("l4_port_range=")
100 .append(l4PortRange)
101 .append(", ");
102 }
103 if (this.ipProto != null) {
104 matchStrBuilder.append("ip_proto=")
105 .append(this.ipProto)
106 .append(", ");
107 }
Daniele Morode1f1f72022-01-26 16:47:08 +0100108 matchStrBuilder.append("slice_id=")
109 .append(this.sliceId)
110 .append(")");
111 return matchStrBuilder.toString();
Daniele Moro97fd80c2022-01-13 19:16:34 +0100112 }
113
114 private String actionString() {
Daniele Morode1f1f72022-01-26 16:47:08 +0100115 return "Action(app_id=" + this.appId + ")";
Daniele Moro97fd80c2022-01-13 19:16:34 +0100116 }
117
118 /**
119 * Gets the IPv4 prefix of this UPF application rule.
120 *
121 * @return The IPv4 prefix, Empty if none.
122 */
123 public Optional<Ip4Prefix> ip4Prefix() {
124 return Optional.ofNullable(ipPrefix);
125 }
126
127 /**
128 * Gets the L4 port range of this application filtering rule.
129 *
130 * @return A bounded range of L4 port
131 */
132 public Optional<Range<Short>> l4PortRange() {
133 return Optional.ofNullable(l4PortRange);
134 }
135
136 /**
137 * Gets the IP protocol field value of this UPF application rule.
138 *
139 * @return IP protocol field, Empty if none
140 */
141 public Optional<Byte> ipProto() {
142 return Optional.ofNullable(ipProto);
143 }
144
145 /**
Daniele Morode1f1f72022-01-26 16:47:08 +0100146 * Gets the slice ID of this UPF application rule.
147 *
148 * @return Slice ID
149 */
150 public int sliceId() {
151 return this.sliceId;
152 }
153
154 /**
Daniele Moro97fd80c2022-01-13 19:16:34 +0100155 * Get the application ID of this UPF application rule.
156 *
157 * @return Application ID
158 */
159 public byte appId() {
160 return appId;
161 }
162
163 /**
164 * Get the priority of this UPF application rule.
165 *
166 * @return Priority
167 */
168 public int priority() {
169 return priority;
170 }
171
172 @Override
173 public UpfEntityType type() {
174 return UpfEntityType.APPLICATION;
175 }
176
177 /**
178 * Builder of UpfApplication object.
179 */
180 public static class Builder {
181 // Match Keys
182 private Ip4Prefix ipPrefix = null;
183 private Range<Short> l4PortRange = null;
184 private Byte ipProto = null;
Daniele Morode1f1f72022-01-26 16:47:08 +0100185 private Integer sliceId = null;
Daniele Moro97fd80c2022-01-13 19:16:34 +0100186 // Action parameters
187 private Byte appId = null;
188
189 private Integer priority = null;
190
191 public Builder() {
192
193 }
194
195 /**
196 * Set the IP prefix of the UPF application rule.
197 *
198 * @param ipPrefix IPv4 prefix
199 * @return This builder object
200 */
201 public Builder withIp4Prefix(Ip4Prefix ipPrefix) {
202 this.ipPrefix = ipPrefix;
203 return this;
204 }
205
206 /**
207 * Set the L4 port range of the UPF application rule.
208 *
209 * @param l4PortRange bounded range of L4 port
210 * @return This builder object
211 */
212 public Builder withL4PortRange(Range<Short> l4PortRange) {
213 checkArgument(l4PortRange.hasLowerBound() && l4PortRange.hasUpperBound(),
214 "Range must be provided with bounds");
215 this.l4PortRange = l4PortRange;
216 return this;
217 }
218
219 /**
220 * Set the IP protocol field value of the UPF application rule.
221 *
222 * @param ipProto IP protocol field
223 * @return This builder object
224 */
225 public Builder withIpProto(byte ipProto) {
226 this.ipProto = ipProto;
227 return this;
228 }
229
230 /**
Daniele Morode1f1f72022-01-26 16:47:08 +0100231 * Set the slice ID of the UPF application rule.
232 *
233 * @param sliceId the slice ID
234 * @return This builder object
235 */
236 public Builder withSliceId(int sliceId) {
237 this.sliceId = sliceId;
238 return this;
239 }
240
241 /**
Daniele Moro97fd80c2022-01-13 19:16:34 +0100242 * Set the application ID of the UPF application rule.
243 *
244 * @param appId Application ID
245 * @return This builder object
246 */
247 public Builder withAppId(byte appId) {
248 this.appId = appId;
249 return this;
250 }
251
252 /**
253 * Set the priority of the UPF application rule.
254 *
255 * @param priority Priority
256 * @return This builder object
257 */
258 public Builder withPriority(int priority) {
259 this.priority = priority;
260 return this;
261 }
262
263 public UpfApplication build() {
264 checkArgument(ipPrefix != null || l4PortRange != null ||
265 ipProto != null,
266 "At least one match field is required");
Daniele Morode1f1f72022-01-26 16:47:08 +0100267 checkNotNull(sliceId, "Slice ID must be provided");
Daniele Moro97fd80c2022-01-13 19:16:34 +0100268 checkNotNull(appId, "Application ID must be provided");
269 checkNotNull(priority, "Priority must be provided");
Daniele Morode1f1f72022-01-26 16:47:08 +0100270 return new UpfApplication(ipPrefix, l4PortRange, ipProto, sliceId, appId, priority);
Daniele Moro97fd80c2022-01-13 19:16:34 +0100271 }
272 }
273}