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