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