alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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()) |
Charles Chan | 3e04129 | 2016-02-27 15:58:43 -0800 | [diff] [blame] | 163 | .toString(); |
| 164 | } |
| 165 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Returns a new builder. |
| 168 | * |
| 169 | * @return new builder |
| 170 | */ |
| 171 | public static Builder builder() { |
| 172 | return new Builder(); |
| 173 | } |
| 174 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 175 | @Override |
| 176 | public Builder copy() { |
| 177 | return new Builder(this); |
| 178 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 179 | |
| 180 | public static final class Builder implements FilteringObjective.Builder { |
| 181 | private final ImmutableList.Builder<Criterion> listBuilder |
| 182 | = ImmutableList.builder(); |
| 183 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 184 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 185 | private boolean permanent = DEFAULT_PERMANENT; |
| 186 | private int timeout = DEFAULT_TIMEOUT; |
| 187 | private ApplicationId appId; |
| 188 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 189 | private Criterion key = Criteria.dummy(); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 190 | private List<Criterion> conditions; |
| 191 | private Operation op; |
| 192 | private ObjectiveContext context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 193 | private TrafficTreatment meta; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 194 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 195 | // Creates an empty builder |
| 196 | private Builder() { |
| 197 | } |
| 198 | |
| 199 | // Creates a builder set to create a copy of the specified objective. |
| 200 | private Builder(FilteringObjective objective) { |
| 201 | this.type = objective.type(); |
| 202 | this.key = objective.key(); |
Konstantinos Kanonakis | d7690cf | 2016-05-26 11:38:33 -0500 | [diff] [blame] | 203 | objective.conditions().forEach(this::addCondition); |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame] | 204 | this.permanent = objective.permanent(); |
| 205 | this.timeout = objective.timeout(); |
| 206 | this.priority = objective.priority(); |
| 207 | this.appId = objective.appId(); |
| 208 | this.meta = objective.meta(); |
| 209 | this.op = objective.op(); |
| 210 | } |
| 211 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 212 | @Override |
| 213 | public Builder withKey(Criterion key) { |
| 214 | this.key = key; |
| 215 | return this; |
| 216 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 217 | |
| 218 | @Override |
| 219 | public Builder addCondition(Criterion criterion) { |
| 220 | listBuilder.add(criterion); |
| 221 | return this; |
| 222 | } |
| 223 | |
| 224 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 225 | public Builder permit() { |
| 226 | this.type = Type.PERMIT; |
| 227 | return this; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Builder deny() { |
| 232 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 233 | return this; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Builder makeTemporary(int timeout) { |
| 238 | this.timeout = timeout; |
| 239 | permanent = false; |
| 240 | return this; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public Builder makePermanent() { |
| 245 | permanent = true; |
| 246 | return this; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public Builder fromApp(ApplicationId appId) { |
| 251 | this.appId = appId; |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public Builder withPriority(int priority) { |
| 257 | this.priority = priority; |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 262 | public Builder withMeta(TrafficTreatment treatment) { |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 263 | this.meta = treatment; |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 268 | public FilteringObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 269 | conditions = listBuilder.build(); |
| 270 | op = Operation.ADD; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 271 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 272 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 273 | checkNotNull(appId, "Must supply an application id"); |
Jayasree Ghosh | 3684dc7 | 2016-06-09 18:07:19 +0530 | [diff] [blame] | 274 | checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " + |
| 275 | "out of range"); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 276 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 277 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public FilteringObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 282 | conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 283 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 284 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 285 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 286 | op = Operation.REMOVE; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 287 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 288 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 289 | } |
| 290 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 291 | @Override |
| 292 | public FilteringObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 293 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 294 | checkNotNull(type, "Must have a type."); |
| 295 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 296 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 297 | op = Operation.ADD; |
| 298 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 299 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 300 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public FilteringObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 305 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 306 | checkNotNull(type, "Must have a type."); |
| 307 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 308 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 309 | op = Operation.REMOVE; |
| 310 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 311 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 312 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 313 | } |
| 314 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } |