blob: 83bb82078f16e6d4abb180a5b9f6a2dae62fb698 [file] [log] [blame]
Daniele Moroa1c7ba42022-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;
38 // Action parameter
39 private final byte appId;
40
41 private final int priority;
42
43 private UpfApplication(Ip4Prefix ipPrefix, Range<Short> l4PortRange,
44 Byte ipProto, byte appId, int priority) {
45 this.ipPrefix = ipPrefix;
46 this.l4PortRange = l4PortRange;
47 this.ipProto = ipProto;
48 this.appId = appId;
49 this.priority = priority;
50 }
51
52 public static Builder builder() {
53 return new Builder();
54 }
55
56 @Override
57 public boolean equals(Object object) {
58 if (object == this) {
59 return true;
60 }
61 if (object == null) {
62 return false;
63 }
64 if (getClass() != object.getClass()) {
65 return false;
66 }
67
68 UpfApplication that = (UpfApplication) object;
69
70 return Objects.equals(this.ipPrefix, that.ipPrefix) &&
71 Objects.equals(this.l4PortRange, that.l4PortRange) &&
72 Objects.equals(this.ipProto, that.ipProto) &&
73 this.appId == that.appId &&
74 this.priority == that.priority;
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hash(ipPrefix, l4PortRange, ipProto, appId, priority);
80 }
81
82 @Override
83 public String toString() {
84 return "UpfApplication{priority=" + this.priority + ", " + matchString() + " -> " + actionString() + "}";
85 }
86
87 private String matchString() {
88 StringBuilder matchStrBuilder = new StringBuilder("Match(");
89 if (this.ipPrefix != null) {
90 matchStrBuilder.append("ip_prefix=")
91 .append(this.ipPrefix)
92 .append(", ");
93 }
94 if (this.l4PortRange != null) {
95 matchStrBuilder.append("l4_port_range=")
96 .append(l4PortRange)
97 .append(", ");
98 }
99 if (this.ipProto != null) {
100 matchStrBuilder.append("ip_proto=")
101 .append(this.ipProto)
102 .append(", ");
103 }
104 matchStrBuilder.delete(matchStrBuilder.length() - 2, matchStrBuilder.length());
105 return matchStrBuilder.append(")").toString();
106 }
107
108 private String actionString() {
109 return "(app_id=" + this.appId + ")";
110 }
111
112 /**
113 * Gets the IPv4 prefix of this UPF application rule.
114 *
115 * @return The IPv4 prefix, Empty if none.
116 */
117 public Optional<Ip4Prefix> ip4Prefix() {
118 return Optional.ofNullable(ipPrefix);
119 }
120
121 /**
122 * Gets the L4 port range of this application filtering rule.
123 *
124 * @return A bounded range of L4 port
125 */
126 public Optional<Range<Short>> l4PortRange() {
127 return Optional.ofNullable(l4PortRange);
128 }
129
130 /**
131 * Gets the IP protocol field value of this UPF application rule.
132 *
133 * @return IP protocol field, Empty if none
134 */
135 public Optional<Byte> ipProto() {
136 return Optional.ofNullable(ipProto);
137 }
138
139 /**
140 * Get the application ID of this UPF application rule.
141 *
142 * @return Application ID
143 */
144 public byte appId() {
145 return appId;
146 }
147
148 /**
149 * Get the priority of this UPF application rule.
150 *
151 * @return Priority
152 */
153 public int priority() {
154 return priority;
155 }
156
157 @Override
158 public UpfEntityType type() {
159 return UpfEntityType.APPLICATION;
160 }
161
162 /**
163 * Builder of UpfApplication object.
164 */
165 public static class Builder {
166 // Match Keys
167 private Ip4Prefix ipPrefix = null;
168 private Range<Short> l4PortRange = null;
169 private Byte ipProto = null;
170 // Action parameters
171 private Byte appId = null;
172
173 private Integer priority = null;
174
175 public Builder() {
176
177 }
178
179 /**
180 * Set the IP prefix of the UPF application rule.
181 *
182 * @param ipPrefix IPv4 prefix
183 * @return This builder object
184 */
185 public Builder withIp4Prefix(Ip4Prefix ipPrefix) {
186 this.ipPrefix = ipPrefix;
187 return this;
188 }
189
190 /**
191 * Set the L4 port range of the UPF application rule.
192 *
193 * @param l4PortRange bounded range of L4 port
194 * @return This builder object
195 */
196 public Builder withL4PortRange(Range<Short> l4PortRange) {
197 checkArgument(l4PortRange.hasLowerBound() && l4PortRange.hasUpperBound(),
198 "Range must be provided with bounds");
199 this.l4PortRange = l4PortRange;
200 return this;
201 }
202
203 /**
204 * Set the IP protocol field value of the UPF application rule.
205 *
206 * @param ipProto IP protocol field
207 * @return This builder object
208 */
209 public Builder withIpProto(byte ipProto) {
210 this.ipProto = ipProto;
211 return this;
212 }
213
214 /**
215 * Set the application ID of the UPF application rule.
216 *
217 * @param appId Application ID
218 * @return This builder object
219 */
220 public Builder withAppId(byte appId) {
221 this.appId = appId;
222 return this;
223 }
224
225 /**
226 * Set the priority of the UPF application rule.
227 *
228 * @param priority Priority
229 * @return This builder object
230 */
231 public Builder withPriority(int priority) {
232 this.priority = priority;
233 return this;
234 }
235
236 public UpfApplication build() {
237 checkArgument(ipPrefix != null || l4PortRange != null ||
238 ipProto != null,
239 "At least one match field is required");
240 checkNotNull(appId, "Application ID must be provided");
241 checkNotNull(priority, "Priority must be provided");
242 return new UpfApplication(ipPrefix, l4PortRange, ipProto, appId, priority);
243 }
244 }
245}