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