2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.flowobjective;
18 import com.google.common.annotations.Beta;
19 import com.google.common.collect.ImmutableList;
20 import org.onosproject.core.ApplicationId;
21 import org.onosproject.net.flow.TrafficSelector;
22 import org.onosproject.net.flow.TrafficTreatment;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Optional;
28 import static com.google.common.base.Preconditions.checkArgument;
29 import static com.google.common.base.Preconditions.checkNotNull;
32 * Default implementation of a next objective.
35 public final class DefaultNextObjective implements NextObjective {
37 private final List<TrafficTreatment> treatments;
38 private final ApplicationId appId;
39 private final Type type;
40 private final Integer id;
41 private final Operation op;
42 private final Optional<ObjectiveContext> context;
43 private final TrafficSelector meta;
45 private DefaultNextObjective(Builder builder) {
46 this.treatments = builder.treatments;
47 this.appId = builder.appId;
48 this.type = builder.type;
51 this.context = Optional.ofNullable(builder.context);
52 this.meta = builder.meta;
56 public Collection<TrafficTreatment> next() {
71 public int priority() {
76 public ApplicationId appId() {
81 public int timeout() {
86 public boolean permanent() {
91 public Operation op() {
96 public Optional<ObjectiveContext> context() {
101 public TrafficSelector meta() {
106 * Returns a new builder.
108 * @return new builder
110 public static Builder builder() {
111 return new Builder();
114 public static final class Builder implements NextObjective.Builder {
116 private ApplicationId appId;
119 private List<TrafficTreatment> treatments;
120 private Operation op;
121 private ObjectiveContext context;
122 private TrafficSelector meta;
124 private final ImmutableList.Builder<TrafficTreatment> listBuilder
125 = ImmutableList.builder();
128 public Builder withId(int nextId) {
134 public Builder withType(Type type) {
140 public Builder addTreatment(TrafficTreatment treatment) {
141 listBuilder.add(treatment);
146 * Noop. This method has no effect.
148 * @param timeout a timeout
149 * @return a next objective builder
152 public Builder makeTemporary(int timeout) {
157 * Noop. This method has no effect.
159 * @return a next objective builder
162 public Builder makePermanent() {
167 public Builder fromApp(ApplicationId appId) {
173 * Noop. This method has no effect.
175 * @param priority an integer
176 * @return a next objective builder
179 public Builder withPriority(int priority) {
184 public Builder setMeta(TrafficSelector meta) {
190 public NextObjective add() {
191 treatments = listBuilder.build();
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");
198 return new DefaultNextObjective(this);
202 public NextObjective remove() {
203 treatments = listBuilder.build();
204 op = Operation.REMOVE;
205 checkNotNull(appId, "Must supply an application id");
206 checkNotNull(id, "id cannot be null");
207 checkNotNull(type, "The type cannot be null");
209 return new DefaultNextObjective(this);
213 public NextObjective add(ObjectiveContext context) {
214 treatments = listBuilder.build();
216 this.context = context;
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");
222 return new DefaultNextObjective(this);
226 public NextObjective remove(ObjectiveContext context) {
227 treatments = listBuilder.build();
228 op = Operation.REMOVE;
229 this.context = context;
230 checkNotNull(appId, "Must supply an application id");
231 checkNotNull(id, "id cannot be null");
232 checkNotNull(type, "The type cannot be null");
234 return new DefaultNextObjective(this);
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");
246 return new DefaultNextObjective(this);
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");
257 return new DefaultNextObjective(this);
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");
270 return new DefaultNextObjective(this);
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");
282 return new DefaultNextObjective(this);