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