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