alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 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 | package org.onosproject.net.flowobjective; |
| 17 | |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.core.ApplicationId; |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 21 | import org.onosproject.net.AbstractAnnotated; |
| 22 | import org.onosproject.net.Annotations; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.criteria.Criteria; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 25 | import org.onosproject.net.flow.criteria.Criterion; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.List; |
| 29 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 30 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 31 | |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 32 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * Default implementation of a filtering objective. |
| 38 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 39 | @Beta |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 40 | public final class DefaultFilteringObjective extends AbstractAnnotated |
| 41 | implements FilteringObjective { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 42 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 43 | private final Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 44 | private final boolean permanent; |
| 45 | private final int timeout; |
| 46 | private final ApplicationId appId; |
| 47 | private final int priority; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 48 | private final Criterion key; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 49 | private final List<Criterion> conditions; |
| 50 | private final int id; |
| 51 | private final Operation op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 52 | private final Optional<ObjectiveContext> context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 53 | private final TrafficTreatment meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 54 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 55 | private DefaultFilteringObjective(Builder builder) { |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 56 | super(builder.annotations); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 57 | this.key = builder.key; |
| 58 | this.type = builder.type; |
| 59 | this.permanent = builder.permanent; |
| 60 | this.timeout = builder.timeout; |
| 61 | this.appId = builder.appId; |
| 62 | this.priority = builder.priority; |
| 63 | this.conditions = builder.conditions; |
| 64 | this.op = builder.op; |
| 65 | this.context = Optional.ofNullable(builder.context); |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 66 | this.meta = builder.meta; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 67 | |
| 68 | this.id = Objects.hash(type, key, conditions, permanent, |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 69 | timeout, appId, priority); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 70 | } |
| 71 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 72 | @Override |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 73 | public Criterion key() { |
| 74 | return key; |
| 75 | } |
| 76 | |
| 77 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 78 | public Type type() { |
| 79 | return this.type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Collection<Criterion> conditions() { |
| 84 | return conditions; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int id() { |
| 89 | return id; |
| 90 | } |
| 91 | |
| 92 | @Override |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 93 | public TrafficTreatment meta() { |
| 94 | return meta; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 99 | public int priority() { |
| 100 | return priority; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public ApplicationId appId() { |
| 105 | return appId; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int timeout() { |
| 110 | return timeout; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean permanent() { |
| 115 | return permanent; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Operation op() { |
| 120 | return op; |
| 121 | } |
| 122 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public Optional<ObjectiveContext> context() { |
| 125 | return context; |
| 126 | } |
| 127 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 128 | @Override |
| 129 | public int hashCode() { |
| 130 | return Objects.hash(type, permanent, timeout, appId, priority, key, |
| 131 | conditions, op, meta); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean equals(Object obj) { |
| 136 | if (this == obj) { |
| 137 | return true; |
| 138 | } |
| 139 | if (obj instanceof DefaultFilteringObjective) { |
| 140 | final DefaultFilteringObjective other = (DefaultFilteringObjective) obj; |
| 141 | return Objects.equals(this.type, other.type) |
| 142 | && Objects.equals(this.permanent, other.permanent) |
| 143 | && Objects.equals(this.timeout, other.timeout) |
| 144 | && Objects.equals(this.appId, other.appId) |
| 145 | && Objects.equals(this.priority, other.priority) |
| 146 | && Objects.equals(this.key, other.key) |
| 147 | && Objects.equals(this.conditions, other.conditions) |
| 148 | && Objects.equals(this.op, other.op) |
| 149 | && Objects.equals(this.meta, other.meta); |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 154 | @Override |
| 155 | public String toString() { |
| 156 | return toStringHelper(this) |
| 157 | .add("id", id()) |
| 158 | .add("type", type()) |
| 159 | .add("op", op()) |
| 160 | .add("priority", priority()) |
| 161 | .add("key", key()) |
| 162 | .add("conditions", conditions()) |
| 163 | .add("meta", meta()) |
| 164 | .add("appId", appId()) |
| 165 | .add("permanent", permanent()) |
| 166 | .add("timeout", timeout()) |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 167 | .add("annotations", annotations()) |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 168 | .toString(); |
| 169 | } |
| 170 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Returns a new builder. |
| 173 | * |
| 174 | * @return new builder |
| 175 | */ |
| 176 | public static Builder builder() { |
| 177 | return new Builder(); |
| 178 | } |
| 179 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 180 | @Override |
| 181 | public Builder copy() { |
| 182 | return new Builder(this); |
| 183 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 184 | |
| 185 | public static final class Builder implements FilteringObjective.Builder { |
| 186 | private final ImmutableList.Builder<Criterion> listBuilder |
| 187 | = ImmutableList.builder(); |
| 188 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 189 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 190 | private boolean permanent = DEFAULT_PERMANENT; |
| 191 | private int timeout = DEFAULT_TIMEOUT; |
| 192 | private ApplicationId appId; |
| 193 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 194 | private Criterion key = Criteria.dummy(); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 195 | private List<Criterion> conditions; |
| 196 | private Operation op; |
| 197 | private ObjectiveContext context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 198 | private TrafficTreatment meta; |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 199 | private Annotations annotations; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 200 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 201 | // Creates an empty builder |
| 202 | private Builder() { |
| 203 | } |
| 204 | |
| 205 | // Creates a builder set to create a copy of the specified objective. |
| 206 | private Builder(FilteringObjective objective) { |
| 207 | this.type = objective.type(); |
| 208 | this.key = objective.key(); |
Konstantinos Kanonakis | d7690cf | 2016-05-26 11:38:33 -0500 | [diff] [blame] | 209 | objective.conditions().forEach(this::addCondition); |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 210 | this.permanent = objective.permanent(); |
| 211 | this.timeout = objective.timeout(); |
| 212 | this.priority = objective.priority(); |
| 213 | this.appId = objective.appId(); |
| 214 | this.meta = objective.meta(); |
| 215 | this.op = objective.op(); |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 216 | this.annotations = objective.annotations(); |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 217 | } |
| 218 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public Builder withKey(Criterion key) { |
| 221 | this.key = key; |
| 222 | return this; |
| 223 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 224 | |
| 225 | @Override |
| 226 | public Builder addCondition(Criterion criterion) { |
| 227 | listBuilder.add(criterion); |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 232 | public Builder permit() { |
| 233 | this.type = Type.PERMIT; |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public Builder deny() { |
| 239 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 240 | return this; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public Builder makeTemporary(int timeout) { |
| 245 | this.timeout = timeout; |
| 246 | permanent = false; |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Builder makePermanent() { |
| 252 | permanent = true; |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public Builder fromApp(ApplicationId appId) { |
| 258 | this.appId = appId; |
| 259 | return this; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Builder withPriority(int priority) { |
| 264 | this.priority = priority; |
| 265 | return this; |
| 266 | } |
| 267 | |
| 268 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 269 | public Builder withMeta(TrafficTreatment treatment) { |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 270 | this.meta = treatment; |
| 271 | return this; |
| 272 | } |
| 273 | |
| 274 | @Override |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame^] | 275 | public Builder withAnnotations(Annotations annotations) { |
| 276 | this.annotations = annotations; |
| 277 | return this; |
| 278 | } |
| 279 | |
| 280 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 281 | public FilteringObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 282 | conditions = listBuilder.build(); |
| 283 | op = Operation.ADD; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 284 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 285 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 286 | checkNotNull(appId, "Must supply an application id"); |
Jayasree Ghosh | 3684dc7 | 2016-06-09 18:07:19 +0530 | [diff] [blame] | 287 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " + |
| 288 | "out of range"); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 289 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 290 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public FilteringObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 295 | conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 296 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 297 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 298 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 299 | op = Operation.REMOVE; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 300 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 301 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 302 | } |
| 303 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 304 | @Override |
| 305 | public FilteringObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 306 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 307 | checkNotNull(type, "Must have a type."); |
| 308 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 309 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 310 | op = Operation.ADD; |
| 311 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 312 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 313 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public FilteringObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 318 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 319 | checkNotNull(type, "Must have a type."); |
| 320 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 321 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 322 | op = Operation.REMOVE; |
| 323 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 324 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 325 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 326 | } |
| 327 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | } |