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 org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.flow.TrafficSelector; |
| 21 | import org.onosproject.net.flow.TrafficTreatment; |
| 22 | |
| 23 | import java.util.Objects; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 24 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 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 forwarding objective. |
| 31 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 32 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 33 | public final class DefaultForwardingObjective implements ForwardingObjective { |
| 34 | |
| 35 | private final TrafficSelector selector; |
| 36 | private final Flag flag; |
| 37 | private final boolean permanent; |
| 38 | private final int timeout; |
| 39 | private final ApplicationId appId; |
| 40 | private final int priority; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 41 | private final Integer nextId; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 42 | private final TrafficTreatment treatment; |
| 43 | private final Operation op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 44 | private final Optional<ObjectiveContext> context; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 45 | |
| 46 | private final int id; |
| 47 | |
| 48 | private DefaultForwardingObjective(TrafficSelector selector, |
| 49 | Flag flag, boolean permanent, |
| 50 | int timeout, ApplicationId appId, |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 51 | int priority, Integer nextId, |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 52 | TrafficTreatment treatment, Operation op) { |
| 53 | this.selector = selector; |
| 54 | this.flag = flag; |
| 55 | this.permanent = permanent; |
| 56 | this.timeout = timeout; |
| 57 | this.appId = appId; |
| 58 | this.priority = priority; |
| 59 | this.nextId = nextId; |
| 60 | this.treatment = treatment; |
| 61 | this.op = op; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 62 | this.context = Optional.empty(); |
| 63 | |
| 64 | this.id = Objects.hash(selector, flag, permanent, |
| 65 | timeout, appId, priority, nextId, |
| 66 | treatment, op); |
| 67 | } |
| 68 | |
| 69 | private DefaultForwardingObjective(TrafficSelector selector, |
| 70 | Flag flag, boolean permanent, |
| 71 | int timeout, ApplicationId appId, |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 72 | int priority, Integer nextId, |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 73 | TrafficTreatment treatment, |
| 74 | ObjectiveContext context, Operation op) { |
| 75 | this.selector = selector; |
| 76 | this.flag = flag; |
| 77 | this.permanent = permanent; |
| 78 | this.timeout = timeout; |
| 79 | this.appId = appId; |
| 80 | this.priority = priority; |
| 81 | this.nextId = nextId; |
| 82 | this.treatment = treatment; |
| 83 | this.op = op; |
| 84 | this.context = Optional.ofNullable(context); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 85 | |
| 86 | this.id = Objects.hash(selector, flag, permanent, |
| 87 | timeout, appId, priority, nextId, |
| 88 | treatment, op); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | @Override |
| 93 | public TrafficSelector selector() { |
| 94 | return selector; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Integer nextId() { |
| 99 | return nextId; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public TrafficTreatment treatment() { |
| 104 | return treatment; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | @Override |
| 109 | public Flag flag() { |
| 110 | return flag; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public int id() { |
| 115 | return id; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public int priority() { |
| 120 | return priority; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public ApplicationId appId() { |
| 125 | return appId; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public int timeout() { |
| 130 | return timeout; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean permanent() { |
| 135 | return permanent; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Operation op() { |
| 140 | return op; |
| 141 | } |
| 142 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public Optional<ObjectiveContext> context() { |
| 145 | return context; |
| 146 | } |
| 147 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Returns a new builder. |
| 150 | * |
| 151 | * @return new builder |
| 152 | */ |
| 153 | public static Builder builder() { |
| 154 | return new Builder(); |
| 155 | } |
| 156 | |
| 157 | public static final class Builder implements ForwardingObjective.Builder { |
| 158 | |
| 159 | private TrafficSelector selector; |
| 160 | private Flag flag; |
| 161 | private boolean permanent = DEFAULT_PERMANENT; |
| 162 | private int timeout = DEFAULT_TIMEOUT; |
| 163 | private int priority = DEFAULT_PRIORITY; |
| 164 | private ApplicationId appId; |
| 165 | private Integer nextId; |
| 166 | private TrafficTreatment treatment; |
| 167 | |
| 168 | @Override |
| 169 | public Builder withSelector(TrafficSelector selector) { |
| 170 | this.selector = selector; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Builder nextStep(int nextId) { |
| 176 | this.nextId = nextId; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder withTreatment(TrafficTreatment treatment) { |
| 182 | this.treatment = treatment; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Builder withFlag(Flag flag) { |
| 188 | this.flag = flag; |
| 189 | return this; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Builder makeTemporary(int timeout) { |
| 194 | this.timeout = timeout; |
| 195 | this.permanent = false; |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Builder makePermanent() { |
| 201 | this.permanent = true; |
| 202 | return this; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Builder fromApp(ApplicationId appId) { |
| 207 | this.appId = appId; |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Builder withPriority(int priority) { |
| 213 | this.priority = priority; |
| 214 | return this; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public ForwardingObjective add() { |
| 219 | checkNotNull(selector, "Must have a selector"); |
| 220 | checkNotNull(flag, "A flag must be set"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 221 | checkArgument(nextId != null || treatment != null, "Must supply at " + |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 222 | "least a treatment and/or a nextId"); |
| 223 | checkNotNull(appId, "Must supply an application id"); |
| 224 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 225 | timeout, appId, priority, |
| 226 | nextId, treatment, Operation.ADD); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public ForwardingObjective remove() { |
| 231 | checkNotNull(selector, "Must have a selector"); |
| 232 | checkNotNull(flag, "A flag must be set"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 233 | checkArgument(nextId != null || treatment != null, "Must supply at " + |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 234 | "least a treatment and/or a nextId"); |
| 235 | checkNotNull(appId, "Must supply an application id"); |
| 236 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 237 | timeout, appId, priority, |
| 238 | nextId, treatment, Operation.REMOVE); |
| 239 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 240 | |
| 241 | @Override |
| 242 | public ForwardingObjective add(ObjectiveContext context) { |
| 243 | checkNotNull(selector, "Must have a selector"); |
| 244 | checkNotNull(flag, "A flag must be set"); |
| 245 | checkArgument(nextId != null || treatment != null, "Must supply at " + |
| 246 | "least a treatment and/or a nextId"); |
| 247 | checkNotNull(appId, "Must supply an application id"); |
| 248 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 249 | timeout, appId, priority, |
| 250 | nextId, treatment, |
| 251 | context, Operation.ADD); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public ForwardingObjective remove(ObjectiveContext context) { |
| 256 | checkNotNull(selector, "Must have a selector"); |
| 257 | checkNotNull(flag, "A flag must be set"); |
| 258 | checkArgument(nextId != null || treatment != null, "Must supply at " + |
| 259 | "least a treatment and/or a nextId"); |
| 260 | checkNotNull(appId, "Must supply an application id"); |
| 261 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 262 | timeout, appId, priority, |
| 263 | nextId, treatment, |
| 264 | context, Operation.REMOVE); |
| 265 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 266 | } |
| 267 | } |