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 | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criteria; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.criteria.Criterion; |
| 24 | |
| 25 | import java.util.Collection; |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 28 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 29 | |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Default implementation of a filtering objective. |
| 36 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 37 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 38 | public final class DefaultFilteringObjective implements FilteringObjective { |
| 39 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 40 | private final Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 41 | private final boolean permanent; |
| 42 | private final int timeout; |
| 43 | private final ApplicationId appId; |
| 44 | private final int priority; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 45 | private final Criterion key; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 46 | private final List<Criterion> conditions; |
| 47 | private final int id; |
| 48 | private final Operation op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 49 | private final Optional<ObjectiveContext> context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 50 | private final TrafficTreatment meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 51 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 52 | private DefaultFilteringObjective(Builder builder) { |
| 53 | this.key = builder.key; |
| 54 | this.type = builder.type; |
| 55 | this.permanent = builder.permanent; |
| 56 | this.timeout = builder.timeout; |
| 57 | this.appId = builder.appId; |
| 58 | this.priority = builder.priority; |
| 59 | this.conditions = builder.conditions; |
| 60 | this.op = builder.op; |
| 61 | this.context = Optional.ofNullable(builder.context); |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -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(type, key, conditions, permanent, |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 65 | timeout, appId, priority); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 66 | } |
| 67 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 68 | @Override |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 69 | public Criterion key() { |
| 70 | return key; |
| 71 | } |
| 72 | |
| 73 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 74 | public Type type() { |
| 75 | return this.type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Collection<Criterion> conditions() { |
| 80 | return conditions; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int id() { |
| 85 | return id; |
| 86 | } |
| 87 | |
| 88 | @Override |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 89 | public TrafficTreatment meta() { |
| 90 | return meta; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 124 | @Override |
| 125 | public int hashCode() { |
| 126 | return Objects.hash(type, permanent, timeout, appId, priority, key, |
| 127 | conditions, op, meta); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean equals(Object obj) { |
| 132 | if (this == obj) { |
| 133 | return true; |
| 134 | } |
| 135 | if (obj instanceof DefaultFilteringObjective) { |
| 136 | final DefaultFilteringObjective other = (DefaultFilteringObjective) obj; |
| 137 | return Objects.equals(this.type, other.type) |
| 138 | && Objects.equals(this.permanent, other.permanent) |
| 139 | && Objects.equals(this.timeout, other.timeout) |
| 140 | && Objects.equals(this.appId, other.appId) |
| 141 | && Objects.equals(this.priority, other.priority) |
| 142 | && Objects.equals(this.key, other.key) |
| 143 | && Objects.equals(this.conditions, other.conditions) |
| 144 | && Objects.equals(this.op, other.op) |
| 145 | && Objects.equals(this.meta, other.meta); |
| 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("type", type()) |
| 155 | .add("op", op()) |
| 156 | .add("priority", priority()) |
| 157 | .add("key", key()) |
| 158 | .add("conditions", conditions()) |
| 159 | .add("meta", meta()) |
| 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 | @Override |
| 177 | public Builder copy() { |
| 178 | return new Builder(this); |
| 179 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 180 | |
| 181 | public static final class Builder implements FilteringObjective.Builder { |
| 182 | private final ImmutableList.Builder<Criterion> listBuilder |
| 183 | = ImmutableList.builder(); |
| 184 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 185 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 186 | private boolean permanent = DEFAULT_PERMANENT; |
| 187 | private int timeout = DEFAULT_TIMEOUT; |
| 188 | private ApplicationId appId; |
| 189 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 190 | private Criterion key = Criteria.dummy(); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 191 | private List<Criterion> conditions; |
| 192 | private Operation op; |
| 193 | private ObjectiveContext context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 194 | private TrafficTreatment meta; |
alshabib | a3a476d | 2015-04-10 14:35:38 -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(FilteringObjective objective) { |
| 202 | this.type = objective.type(); |
| 203 | this.key = objective.key(); |
| 204 | this.conditions = ImmutableList.copyOf(objective.conditions()); |
| 205 | this.permanent = objective.permanent(); |
| 206 | this.timeout = objective.timeout(); |
| 207 | this.priority = objective.priority(); |
| 208 | this.appId = objective.appId(); |
| 209 | this.meta = objective.meta(); |
| 210 | this.op = objective.op(); |
| 211 | } |
| 212 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public Builder withKey(Criterion key) { |
| 215 | this.key = key; |
| 216 | return this; |
| 217 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public Builder addCondition(Criterion criterion) { |
| 221 | listBuilder.add(criterion); |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 226 | public Builder permit() { |
| 227 | this.type = Type.PERMIT; |
| 228 | return this; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Builder deny() { |
| 233 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 234 | return this; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public Builder makeTemporary(int timeout) { |
| 239 | this.timeout = timeout; |
| 240 | permanent = false; |
| 241 | return this; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Builder makePermanent() { |
| 246 | 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 |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 263 | public Builder withMeta(TrafficTreatment treatment) { |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 264 | this.meta = treatment; |
| 265 | return this; |
| 266 | } |
| 267 | |
| 268 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 269 | public FilteringObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 270 | conditions = listBuilder.build(); |
| 271 | op = Operation.ADD; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 272 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 273 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 274 | checkNotNull(appId, "Must supply an application id"); |
Jayasree Ghosh | cd9df3e | 2016-06-14 13:25:06 +0530 | [diff] [blame] | 275 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " + |
| 276 | "out of range"); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 277 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 278 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public FilteringObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 283 | conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 284 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 285 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 286 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 287 | op = Operation.REMOVE; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 288 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 289 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 290 | } |
| 291 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 292 | @Override |
| 293 | public FilteringObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 294 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 295 | checkNotNull(type, "Must have a type."); |
| 296 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 297 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 298 | op = Operation.ADD; |
| 299 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 300 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 301 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public FilteringObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 306 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 307 | checkNotNull(type, "Must have a type."); |
| 308 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 309 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 310 | op = Operation.REMOVE; |
| 311 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 312 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 313 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 314 | } |
| 315 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | } |