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