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