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