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; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.criteria.Criteria; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criterion; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 27 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Default implementation of a filtering objective. |
| 34 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame^] | 35 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 36 | public final class DefaultFilteringObjective implements FilteringObjective { |
| 37 | |
| 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; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 49 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 50 | private DefaultFilteringObjective(Type type, boolean permanent, int timeout, |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 51 | ApplicationId appId, int priority, Criterion key, |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 52 | List<Criterion> conditions, Operation op) { |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 53 | this.key = key; |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 54 | this.type = type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 55 | this.permanent = permanent; |
| 56 | this.timeout = timeout; |
| 57 | this.appId = appId; |
| 58 | this.priority = priority; |
| 59 | this.conditions = conditions; |
| 60 | this.op = op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 61 | this.context = Optional.empty(); |
| 62 | |
| 63 | this.id = Objects.hash(type, key, conditions, permanent, |
| 64 | timeout, appId, priority); |
| 65 | } |
| 66 | |
| 67 | public DefaultFilteringObjective(Type type, boolean permanent, int timeout, |
| 68 | ApplicationId appId, int priority, Criterion key, |
| 69 | List<Criterion> conditions, |
| 70 | ObjectiveContext context, Operation op) { |
| 71 | this.key = key; |
| 72 | this.type = type; |
| 73 | this.permanent = permanent; |
| 74 | this.timeout = timeout; |
| 75 | this.appId = appId; |
| 76 | this.priority = priority; |
| 77 | this.conditions = conditions; |
| 78 | this.op = op; |
| 79 | this.context = Optional.ofNullable(context); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 80 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 81 | this.id = Objects.hash(type, key, conditions, permanent, |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 82 | timeout, appId, priority); |
| 83 | } |
| 84 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 85 | @Override |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 86 | public Criterion key() { |
| 87 | return key; |
| 88 | } |
| 89 | |
| 90 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 91 | public Type type() { |
| 92 | return this.type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<Criterion> conditions() { |
| 97 | return conditions; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public int id() { |
| 102 | return id; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int priority() { |
| 107 | return priority; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public ApplicationId appId() { |
| 112 | return appId; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int timeout() { |
| 117 | return timeout; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean permanent() { |
| 122 | return permanent; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Operation op() { |
| 127 | return op; |
| 128 | } |
| 129 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public Optional<ObjectiveContext> context() { |
| 132 | return context; |
| 133 | } |
| 134 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns a new builder. |
| 137 | * |
| 138 | * @return new builder |
| 139 | */ |
| 140 | public static Builder builder() { |
| 141 | return new Builder(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | public static final class Builder implements FilteringObjective.Builder { |
| 146 | private final ImmutableList.Builder<Criterion> listBuilder |
| 147 | = ImmutableList.builder(); |
| 148 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 149 | private Type type; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 150 | private boolean permanent = DEFAULT_PERMANENT; |
| 151 | private int timeout = DEFAULT_TIMEOUT; |
| 152 | private ApplicationId appId; |
| 153 | private int priority = DEFAULT_PRIORITY; |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 154 | private Criterion key = Criteria.dummy(); |
| 155 | |
| 156 | @Override |
| 157 | public Builder withKey(Criterion key) { |
| 158 | this.key = key; |
| 159 | return this; |
| 160 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 161 | |
| 162 | @Override |
| 163 | public Builder addCondition(Criterion criterion) { |
| 164 | listBuilder.add(criterion); |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Override |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 169 | public Builder permit() { |
| 170 | this.type = Type.PERMIT; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Builder deny() { |
| 176 | this.type = Type.DENY; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder makeTemporary(int timeout) { |
| 182 | this.timeout = timeout; |
| 183 | permanent = false; |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Builder makePermanent() { |
| 189 | permanent = true; |
| 190 | return this; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Builder fromApp(ApplicationId appId) { |
| 195 | this.appId = appId; |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Builder withPriority(int priority) { |
| 201 | this.priority = priority; |
| 202 | return this; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public FilteringObjective add() { |
| 207 | List<Criterion> conditions = listBuilder.build(); |
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 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 212 | return new DefaultFilteringObjective(type, permanent, timeout, |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 213 | appId, priority, key, conditions, |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 214 | Operation.ADD); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public FilteringObjective remove() { |
| 220 | List<Criterion> conditions = listBuilder.build(); |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 221 | checkNotNull(type, "Must have a type."); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 222 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 223 | checkNotNull(appId, "Must supply an application id"); |
| 224 | |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 225 | |
alshabib | 910aff1 | 2015-04-09 16:55:57 -0700 | [diff] [blame] | 226 | return new DefaultFilteringObjective(type, permanent, timeout, |
alshabib | a3a476d | 2015-04-10 14:35:38 -0700 | [diff] [blame] | 227 | appId, priority, key, conditions, |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 228 | Operation.REMOVE); |
| 229 | |
| 230 | } |
| 231 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public FilteringObjective add(ObjectiveContext context) { |
| 234 | List<Criterion> conditions = listBuilder.build(); |
| 235 | checkNotNull(type, "Must have a type."); |
| 236 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 237 | checkNotNull(appId, "Must supply an application id"); |
| 238 | |
| 239 | return new DefaultFilteringObjective(type, permanent, timeout, |
| 240 | appId, priority, key, conditions, |
| 241 | context, Operation.ADD); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public FilteringObjective remove(ObjectiveContext context) { |
| 246 | List<Criterion> conditions = listBuilder.build(); |
| 247 | checkNotNull(type, "Must have a type."); |
| 248 | checkArgument(!conditions.isEmpty(), "Must have at least one condition."); |
| 249 | checkNotNull(appId, "Must supply an application id"); |
| 250 | |
| 251 | |
| 252 | return new DefaultFilteringObjective(type, permanent, timeout, |
| 253 | appId, priority, key, conditions, |
| 254 | context, Operation.REMOVE); |
| 255 | } |
| 256 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 257 | |
| 258 | } |
| 259 | |
| 260 | } |