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; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 21 | import org.onosproject.net.flow.TrafficSelector; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.TrafficTreatment; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.List; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 26 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Default implementation of a next objective. |
| 33 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 34 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 35 | public final class DefaultNextObjective implements NextObjective { |
| 36 | |
| 37 | private final List<TrafficTreatment> treatments; |
| 38 | private final ApplicationId appId; |
| 39 | private final Type type; |
| 40 | private final Integer id; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 41 | private final Operation op; |
| 42 | private final Optional<ObjectiveContext> context; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 43 | private final TrafficSelector meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 44 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 45 | private DefaultNextObjective(Builder builder) { |
| 46 | this.treatments = builder.treatments; |
| 47 | this.appId = builder.appId; |
| 48 | this.type = builder.type; |
| 49 | this.id = builder.id; |
| 50 | this.op = builder.op; |
| 51 | this.context = Optional.ofNullable(builder.context); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 52 | this.meta = builder.meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Collection<TrafficTreatment> next() { |
| 57 | return treatments; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Type type() { |
| 62 | return type; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int id() { |
| 67 | return id; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int priority() { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public ApplicationId appId() { |
| 77 | return appId; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int timeout() { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean permanent() { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Operation op() { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 92 | return op; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Optional<ObjectiveContext> context() { |
| 97 | return context; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 100 | @Override |
| 101 | public TrafficSelector meta() { |
| 102 | return meta; |
| 103 | } |
| 104 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Returns a new builder. |
| 107 | * |
| 108 | * @return new builder |
| 109 | */ |
| 110 | public static Builder builder() { |
| 111 | return new Builder(); |
| 112 | } |
| 113 | |
| 114 | public static final class Builder implements NextObjective.Builder { |
| 115 | |
| 116 | private ApplicationId appId; |
| 117 | private Type type; |
| 118 | private Integer id; |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 119 | private List<TrafficTreatment> treatments; |
| 120 | private Operation op; |
| 121 | private ObjectiveContext context; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 122 | private TrafficSelector meta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 123 | |
| 124 | private final ImmutableList.Builder<TrafficTreatment> listBuilder |
| 125 | = ImmutableList.builder(); |
| 126 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 127 | @Override |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 128 | public Builder withId(int nextId) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 129 | this.id = nextId; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Builder withType(Type type) { |
| 135 | this.type = type; |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Builder addTreatment(TrafficTreatment treatment) { |
| 141 | listBuilder.add(treatment); |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Noop. This method has no effect. |
| 147 | * |
| 148 | * @param timeout a timeout |
| 149 | * @return a next objective builder |
| 150 | */ |
| 151 | @Override |
| 152 | public Builder makeTemporary(int timeout) { |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Noop. This method has no effect. |
| 158 | * |
| 159 | * @return a next objective builder |
| 160 | */ |
| 161 | @Override |
| 162 | public Builder makePermanent() { |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 167 | public Builder fromApp(ApplicationId appId) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 168 | this.appId = appId; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Noop. This method has no effect. |
| 174 | * |
| 175 | * @param priority an integer |
| 176 | * @return a next objective builder |
| 177 | */ |
| 178 | @Override |
| 179 | public Builder withPriority(int priority) { |
| 180 | return this; |
| 181 | } |
| 182 | |
| 183 | @Override |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 184 | public Builder withMeta(TrafficSelector meta) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 185 | this.meta = meta; |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 190 | public NextObjective add() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 191 | treatments = listBuilder.build(); |
| 192 | op = Operation.ADD; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 193 | checkNotNull(appId, "Must supply an application id"); |
| 194 | checkNotNull(id, "id cannot be null"); |
| 195 | checkNotNull(type, "The type cannot be null"); |
| 196 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 197 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 198 | return new DefaultNextObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public NextObjective remove() { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 203 | treatments = listBuilder.build(); |
| 204 | op = Operation.REMOVE; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 205 | checkNotNull(appId, "Must supply an application id"); |
| 206 | checkNotNull(id, "id cannot be null"); |
| 207 | checkNotNull(type, "The type cannot be null"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 208 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 209 | return new DefaultNextObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public NextObjective add(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 214 | treatments = listBuilder.build(); |
| 215 | op = Operation.ADD; |
| 216 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 217 | checkNotNull(appId, "Must supply an application id"); |
| 218 | checkNotNull(id, "id cannot be null"); |
| 219 | checkNotNull(type, "The type cannot be null"); |
| 220 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 221 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 222 | return new DefaultNextObjective(this); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public NextObjective remove(ObjectiveContext context) { |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 227 | treatments = listBuilder.build(); |
| 228 | op = Operation.REMOVE; |
| 229 | this.context = context; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 230 | checkNotNull(appId, "Must supply an application id"); |
| 231 | checkNotNull(id, "id cannot be null"); |
| 232 | checkNotNull(type, "The type cannot be null"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 233 | |
Ray Milkey | 810d6e7 | 2015-08-14 10:35:06 -0700 | [diff] [blame] | 234 | return new DefaultNextObjective(this); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 235 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 236 | |
| 237 | @Override |
| 238 | public NextObjective addToExisting() { |
| 239 | treatments = listBuilder.build(); |
| 240 | op = Operation.ADD_TO_EXISTING; |
| 241 | checkNotNull(appId, "Must supply an application id"); |
| 242 | checkNotNull(id, "id cannot be null"); |
| 243 | checkNotNull(type, "The type cannot be null"); |
| 244 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 245 | |
| 246 | return new DefaultNextObjective(this); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public NextObjective removeFromExisting() { |
| 251 | treatments = listBuilder.build(); |
| 252 | op = Operation.REMOVE_FROM_EXISTING; |
| 253 | checkNotNull(appId, "Must supply an application id"); |
| 254 | checkNotNull(id, "id cannot be null"); |
| 255 | checkNotNull(type, "The type cannot be null"); |
| 256 | |
| 257 | return new DefaultNextObjective(this); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public NextObjective addToExisting(ObjectiveContext context) { |
| 262 | treatments = listBuilder.build(); |
| 263 | op = Operation.ADD_TO_EXISTING; |
| 264 | this.context = context; |
| 265 | checkNotNull(appId, "Must supply an application id"); |
| 266 | checkNotNull(id, "id cannot be null"); |
| 267 | checkNotNull(type, "The type cannot be null"); |
| 268 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 269 | |
| 270 | return new DefaultNextObjective(this); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public NextObjective removeFromExisting(ObjectiveContext context) { |
| 275 | treatments = listBuilder.build(); |
| 276 | op = Operation.REMOVE_FROM_EXISTING; |
| 277 | this.context = context; |
| 278 | checkNotNull(appId, "Must supply an application id"); |
| 279 | checkNotNull(id, "id cannot be null"); |
| 280 | checkNotNull(type, "The type cannot be null"); |
| 281 | |
| 282 | return new DefaultNextObjective(this); |
| 283 | } |
| 284 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 285 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 286 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 287 | } |