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