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 | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.TrafficSelector; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.List; |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 28 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 29 | import java.util.Optional; |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
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 next 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 DefaultNextObjective extends AbstractAnnotated |
| 41 | implements NextObjective { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 42 | |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 43 | private final List<NextTreatment> treatments; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 44 | private final ApplicationId appId; |
| 45 | private final Type type; |
| 46 | private final Integer id; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 47 | private final Operation op; |
| 48 | private final Optional<ObjectiveContext> context; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 49 | private final TrafficSelector meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 50 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 51 | private DefaultNextObjective(Builder builder) { |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame] | 52 | super(builder.annotations); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 53 | this.treatments = builder.treatments; |
| 54 | this.appId = builder.appId; |
| 55 | this.type = builder.type; |
| 56 | this.id = builder.id; |
| 57 | this.op = builder.op; |
| 58 | this.context = Optional.ofNullable(builder.context); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 59 | this.meta = builder.meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<TrafficTreatment> next() { |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 64 | return treatments.stream() |
| 65 | .filter(t -> t.type() == NextTreatment.Type.TREATMENT) |
| 66 | .map(t -> ((DefaultNextTreatment) t).treatment()) |
| 67 | .collect(Collectors.toList()); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Collection<NextTreatment> nextTreatments() { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 72 | return treatments; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Type type() { |
| 77 | return type; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int id() { |
| 82 | return id; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int priority() { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public ApplicationId appId() { |
| 92 | return appId; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int timeout() { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean permanent() { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Operation op() { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 107 | return op; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Optional<ObjectiveContext> context() { |
| 112 | return context; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 115 | @Override |
| 116 | public TrafficSelector meta() { |
| 117 | return meta; |
| 118 | } |
| 119 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 120 | @Override |
| 121 | public int hashCode() { |
| 122 | return Objects.hash(treatments, appId, type, id, op, meta); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) { |
| 128 | return true; |
| 129 | } |
| 130 | if (obj instanceof DefaultNextObjective) { |
| 131 | final DefaultNextObjective other = (DefaultNextObjective) obj; |
| 132 | return Objects.equals(this.treatments, other.treatments) |
| 133 | && Objects.equals(this.appId, other.appId) |
| 134 | && Objects.equals(this.type, other.type) |
| 135 | && Objects.equals(this.id, other.id) |
| 136 | && Objects.equals(this.op, other.op) |
| 137 | && Objects.equals(this.meta, other.meta); |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public String toString() { |
| 144 | return toStringHelper(this) |
| 145 | .add("id", id()) |
| 146 | .add("type", type()) |
| 147 | .add("op", op()) |
| 148 | .add("priority", priority()) |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 149 | .add("nextTreatments", nextTreatments()) |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 150 | .add("meta", meta()) |
| 151 | .add("appId", appId()) |
| 152 | .add("permanent", permanent()) |
| 153 | .add("timeout", timeout()) |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame] | 154 | .add("annotations", annotations()) |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 155 | .toString(); |
| 156 | } |
| 157 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Returns a new builder. |
| 160 | * |
| 161 | * @return new builder |
| 162 | */ |
| 163 | public static Builder builder() { |
| 164 | return new Builder(); |
| 165 | } |
| 166 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 167 | @Override |
| 168 | public Builder copy() { |
| 169 | return new Builder(this); |
| 170 | } |
| 171 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 172 | public static final class Builder implements NextObjective.Builder { |
| 173 | |
| 174 | private ApplicationId appId; |
| 175 | private Type type; |
| 176 | private Integer id; |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 177 | private List<NextTreatment> treatments; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 178 | private Operation op; |
| 179 | private ObjectiveContext context; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 180 | private TrafficSelector meta; |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame] | 181 | private Annotations annotations; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 182 | |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 183 | private final ImmutableList.Builder<NextTreatment> listBuilder |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 184 | = ImmutableList.builder(); |
| 185 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 186 | // Creates an empty builder |
| 187 | private Builder() { |
| 188 | } |
| 189 | |
| 190 | // Creates a builder set to create a copy of the specified objective. |
| 191 | private Builder(NextObjective objective) { |
| 192 | this.type = objective.type(); |
| 193 | this.id = objective.id(); |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 194 | this.treatments = ImmutableList.copyOf(objective.nextTreatments()); |
| 195 | this.listBuilder.addAll(objective.nextTreatments()); |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 196 | this.meta = objective.meta(); |
| 197 | this.appId = objective.appId(); |
| 198 | this.op = objective.op(); |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame] | 199 | this.annotations = objective.annotations(); |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 200 | } |
| 201 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 202 | @Override |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 203 | public Builder withId(int nextId) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 204 | this.id = nextId; |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Builder withType(Type type) { |
| 210 | this.type = type; |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Builder addTreatment(TrafficTreatment treatment) { |
Charles Chan | b87d9f1 | 2018-10-31 21:00:06 -0700 | [diff] [blame] | 216 | listBuilder.add(DefaultNextTreatment.of(treatment)); |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Builder addTreatment(NextTreatment nextTreatment) { |
| 222 | listBuilder.add(nextTreatment); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 223 | return this; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Noop. This method has no effect. |
| 228 | * |
| 229 | * @param timeout a timeout |
| 230 | * @return a next objective builder |
| 231 | */ |
| 232 | @Override |
| 233 | public Builder makeTemporary(int timeout) { |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Noop. This method has no effect. |
| 239 | * |
| 240 | * @return a next objective builder |
| 241 | */ |
| 242 | @Override |
| 243 | public Builder makePermanent() { |
| 244 | return this; |
| 245 | } |
| 246 | |
| 247 | @Override |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 248 | public Builder fromApp(ApplicationId appId) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 249 | this.appId = appId; |
| 250 | return this; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Noop. This method has no effect. |
| 255 | * |
| 256 | * @param priority an integer |
| 257 | * @return a next objective builder |
| 258 | */ |
| 259 | @Override |
| 260 | public Builder withPriority(int priority) { |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 265 | public Builder withMeta(TrafficSelector meta) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 266 | this.meta = meta; |
| 267 | return this; |
| 268 | } |
| 269 | |
| 270 | @Override |
Andrea Campanella | cbd1694 | 2021-06-01 16:33:14 +0200 | [diff] [blame] | 271 | public Builder withAnnotations(Annotations annotations) { |
| 272 | this.annotations = annotations; |
| 273 | return this; |
| 274 | } |
| 275 | |
| 276 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 277 | public NextObjective add() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 278 | return add(null); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public NextObjective remove() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 283 | return remove(null); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public NextObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 288 | treatments = listBuilder.build(); |
| 289 | op = Operation.ADD; |
| 290 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 291 | checkNotNull(appId, "Must supply an application id"); |
| 292 | checkNotNull(id, "id cannot be null"); |
| 293 | checkNotNull(type, "The type cannot be null"); |
| 294 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 295 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 296 | return new DefaultNextObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public NextObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 301 | treatments = listBuilder.build(); |
| 302 | op = Operation.REMOVE; |
| 303 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 304 | checkNotNull(appId, "Must supply an application id"); |
| 305 | checkNotNull(id, "id cannot be null"); |
| 306 | checkNotNull(type, "The type cannot be null"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 307 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 308 | return new DefaultNextObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 309 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 310 | |
| 311 | @Override |
| 312 | public NextObjective addToExisting() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 313 | return addToExisting(null); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public NextObjective removeFromExisting() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 318 | return removeFromExisting(null); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public NextObjective addToExisting(ObjectiveContext context) { |
| 323 | treatments = listBuilder.build(); |
| 324 | op = Operation.ADD_TO_EXISTING; |
| 325 | this.context = context; |
| 326 | checkNotNull(appId, "Must supply an application id"); |
| 327 | checkNotNull(id, "id cannot be null"); |
| 328 | checkNotNull(type, "The type cannot be null"); |
| 329 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 330 | |
| 331 | return new DefaultNextObjective(this); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public NextObjective removeFromExisting(ObjectiveContext context) { |
| 336 | treatments = listBuilder.build(); |
| 337 | op = Operation.REMOVE_FROM_EXISTING; |
| 338 | this.context = context; |
| 339 | checkNotNull(appId, "Must supply an application id"); |
| 340 | checkNotNull(id, "id cannot be null"); |
| 341 | checkNotNull(type, "The type cannot be null"); |
| 342 | |
| 343 | return new DefaultNextObjective(this); |
| 344 | } |
| 345 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 346 | @Override |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 347 | public NextObjective modify() { |
| 348 | return modify(null); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public NextObjective modify(ObjectiveContext context) { |
| 353 | treatments = listBuilder.build(); |
| 354 | op = Operation.MODIFY; |
| 355 | this.context = context; |
| 356 | checkNotNull(appId, "Must supply an application id"); |
| 357 | checkNotNull(id, "id cannot be null"); |
| 358 | checkNotNull(type, "The type cannot be null"); |
| 359 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 360 | |
| 361 | return new DefaultNextObjective(this); |
| 362 | } |
| 363 | |
| 364 | @Override |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 365 | public NextObjective verify() { |
| 366 | return verify(null); |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public NextObjective verify(ObjectiveContext context) { |
| 371 | treatments = listBuilder.build(); |
| 372 | op = Operation.VERIFY; |
| 373 | this.context = context; |
| 374 | checkNotNull(appId, "Must supply an application id"); |
| 375 | checkNotNull(id, "id cannot be null"); |
| 376 | checkNotNull(type, "The type cannot be null"); |
| 377 | return new DefaultNextObjective(this); |
| 378 | } |
| 379 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 380 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 381 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 382 | } |