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; |
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 | |
| 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Default implementation of a filtering objective. |
| 35 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 36 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 37 | public final class DefaultFilteringObjective implements FilteringObjective { |
| 38 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 39 | private final Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 40 | private final boolean permanent; |
| 41 | private final int timeout; |
| 42 | private final ApplicationId appId; |
| 43 | private final int priority; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 44 | private final Criterion key; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 45 | private final List<Criterion> conditions; |
| 46 | private final int id; |
| 47 | private final Operation op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 48 | private final Optional<ObjectiveContext> context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 49 | private final TrafficTreatment meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 50 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 51 | private DefaultFilteringObjective(Builder builder) { |
| 52 | this.key = builder.key; |
| 53 | this.type = builder.type; |
| 54 | this.permanent = builder.permanent; |
| 55 | this.timeout = builder.timeout; |
| 56 | this.appId = builder.appId; |
| 57 | this.priority = builder.priority; |
| 58 | this.conditions = builder.conditions; |
| 59 | this.op = builder.op; |
| 60 | this.context = Optional.ofNullable(builder.context); |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 61 | this.meta = builder.meta; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 62 | |
| 63 | this.id = Objects.hash(type, key, conditions, permanent, |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame^] | 64 | timeout, appId, priority); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 65 | } |
| 66 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 67 | @Override |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 68 | public Criterion key() { |
| 69 | return key; |
| 70 | } |
| 71 | |
| 72 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 73 | public Type type() { |
| 74 | return this.type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Collection<Criterion> conditions() { |
| 79 | return conditions; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int id() { |
| 84 | return id; |
| 85 | } |
| 86 | |
| 87 | @Override |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 88 | public TrafficTreatment meta() { |
| 89 | return meta; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame^] | 123 | |
| 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 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns a new builder. |
| 152 | * |
| 153 | * @return new builder |
| 154 | */ |
| 155 | public static Builder builder() { |
| 156 | return new Builder(); |
| 157 | } |
| 158 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame^] | 159 | @Override |
| 160 | public Builder copy() { |
| 161 | return new Builder(this); |
| 162 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 163 | |
| 164 | public static final class Builder implements FilteringObjective.Builder { |
| 165 | private final ImmutableList.Builder<Criterion> listBuilder |
| 166 | = ImmutableList.builder(); |
| 167 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 168 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 169 | private boolean permanent = DEFAULT_PERMANENT; |
| 170 | private int timeout = DEFAULT_TIMEOUT; |
| 171 | private ApplicationId appId; |
| 172 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 173 | private Criterion key = Criteria.dummy(); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 174 | private List<Criterion> conditions; |
| 175 | private Operation op; |
| 176 | private ObjectiveContext context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 177 | private TrafficTreatment meta; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 178 | |
Thomas Vachuska | 00f4816 | 2016-02-29 17:07:23 -0800 | [diff] [blame^] | 179 | // Creates an empty builder |
| 180 | private Builder() { |
| 181 | } |
| 182 | |
| 183 | // Creates a builder set to create a copy of the specified objective. |
| 184 | private Builder(FilteringObjective objective) { |
| 185 | this.type = objective.type(); |
| 186 | this.key = objective.key(); |
| 187 | this.conditions = ImmutableList.copyOf(objective.conditions()); |
| 188 | this.permanent = objective.permanent(); |
| 189 | this.timeout = objective.timeout(); |
| 190 | this.priority = objective.priority(); |
| 191 | this.appId = objective.appId(); |
| 192 | this.meta = objective.meta(); |
| 193 | this.op = objective.op(); |
| 194 | } |
| 195 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 196 | @Override |
| 197 | public Builder withKey(Criterion key) { |
| 198 | this.key = key; |
| 199 | return this; |
| 200 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public Builder addCondition(Criterion criterion) { |
| 204 | listBuilder.add(criterion); |
| 205 | return this; |
| 206 | } |
| 207 | |
| 208 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 209 | public Builder permit() { |
| 210 | this.type = Type.PERMIT; |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public Builder deny() { |
| 216 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 217 | return this; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Builder makeTemporary(int timeout) { |
| 222 | this.timeout = timeout; |
| 223 | permanent = false; |
| 224 | return this; |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public Builder makePermanent() { |
| 229 | permanent = true; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Builder fromApp(ApplicationId appId) { |
| 235 | this.appId = appId; |
| 236 | return this; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public Builder withPriority(int priority) { |
| 241 | this.priority = priority; |
| 242 | return this; |
| 243 | } |
| 244 | |
| 245 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 246 | public Builder withMeta(TrafficTreatment treatment) { |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 247 | this.meta = treatment; |
| 248 | return this; |
| 249 | } |
| 250 | |
| 251 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 252 | public FilteringObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 253 | conditions = listBuilder.build(); |
| 254 | op = Operation.ADD; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 255 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 256 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 257 | checkNotNull(appId, "Must supply an application id"); |
| 258 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 259 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public FilteringObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 264 | conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 265 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 266 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 267 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 268 | op = Operation.REMOVE; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 269 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 270 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 271 | } |
| 272 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 273 | @Override |
| 274 | public FilteringObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 275 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 276 | checkNotNull(type, "Must have a type."); |
| 277 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 278 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 279 | op = Operation.ADD; |
| 280 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 281 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 282 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public FilteringObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 287 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 288 | checkNotNull(type, "Must have a type."); |
| 289 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 290 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 291 | op = Operation.REMOVE; |
| 292 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 293 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 294 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 295 | } |
| 296 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | } |