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 | |
| 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, |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [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 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Returns a new builder. |
| 126 | * |
| 127 | * @return new builder |
| 128 | */ |
| 129 | public static Builder builder() { |
| 130 | return new Builder(); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | public static final class Builder implements FilteringObjective.Builder { |
| 135 | private final ImmutableList.Builder<Criterion> listBuilder |
| 136 | = ImmutableList.builder(); |
| 137 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 138 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 139 | private boolean permanent = DEFAULT_PERMANENT; |
| 140 | private int timeout = DEFAULT_TIMEOUT; |
| 141 | private ApplicationId appId; |
| 142 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 143 | private Criterion key = Criteria.dummy(); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 144 | private List<Criterion> conditions; |
| 145 | private Operation op; |
| 146 | private ObjectiveContext context; |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 147 | private TrafficTreatment meta; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public Builder withKey(Criterion key) { |
| 151 | this.key = key; |
| 152 | return this; |
| 153 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public Builder addCondition(Criterion criterion) { |
| 157 | listBuilder.add(criterion); |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 162 | public Builder permit() { |
| 163 | this.type = Type.PERMIT; |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Builder deny() { |
| 169 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 170 | return this; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Builder makeTemporary(int timeout) { |
| 175 | this.timeout = timeout; |
| 176 | permanent = false; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder makePermanent() { |
| 182 | permanent = true; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Builder fromApp(ApplicationId appId) { |
| 188 | this.appId = appId; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Builder withPriority(int priority) { |
| 194 | this.priority = priority; |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 199 | public Builder withMeta(TrafficTreatment treatment) { |
Saurav Das | 88f4c84 | 2015-10-27 13:13:19 -0700 | [diff] [blame] | 200 | this.meta = treatment; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | @Override |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 205 | public FilteringObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 206 | conditions = listBuilder.build(); |
| 207 | op = Operation.ADD; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 208 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 209 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 210 | checkNotNull(appId, "Must supply an application id"); |
| 211 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 212 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 213 | |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public FilteringObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 218 | conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 219 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 220 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 221 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 222 | op = Operation.REMOVE; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 223 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 224 | return new DefaultFilteringObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 225 | |
| 226 | } |
| 227 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public FilteringObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 230 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 231 | checkNotNull(type, "Must have a type."); |
| 232 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 233 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 234 | op = Operation.ADD; |
| 235 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 236 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 237 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public FilteringObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 242 | conditions = listBuilder.build(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 243 | checkNotNull(type, "Must have a type."); |
| 244 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 245 | checkNotNull(appId, "Must supply an application id"); |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 246 | op = Operation.REMOVE; |
| 247 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 248 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 249 | return new DefaultFilteringObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 250 | } |
| 251 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 252 | |
| 253 | } |
| 254 | |
| 255 | } |