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