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