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.TrafficTreatment;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Optional;
27 import static com.google.common.base.Preconditions.checkArgument;
28 import static com.google.common.base.Preconditions.checkNotNull;
31 * Default implementation of a next objective.
34 public final class DefaultNextObjective implements NextObjective {
36 private final List<TrafficTreatment> treatments;
37 private final ApplicationId appId;
38 private final Type type;
39 private final Integer id;
40 private final Operation op;
41 private final Optional<ObjectiveContext> context;
43 private DefaultNextObjective(Builder builder) {
44 this.treatments = builder.treatments;
45 this.appId = builder.appId;
46 this.type = builder.type;
49 this.context = Optional.ofNullable(builder.context);
53 public Collection<TrafficTreatment> next() {
68 public int priority() {
73 public ApplicationId appId() {
78 public int timeout() {
83 public boolean permanent() {
88 public Operation op() {
93 public Optional<ObjectiveContext> context() {
98 * Returns a new builder.
100 * @return new builder
102 public static Builder builder() {
103 return new Builder();
106 public static final class Builder implements NextObjective.Builder {
108 private ApplicationId appId;
111 private List<TrafficTreatment> treatments;
112 private Operation op;
113 private ObjectiveContext context;
115 private final ImmutableList.Builder<TrafficTreatment> listBuilder
116 = ImmutableList.builder();
119 public Builder withId(int nextId) {
125 public Builder withType(Type type) {
131 public Builder addTreatment(TrafficTreatment treatment) {
132 listBuilder.add(treatment);
137 * Noop. This method has no effect.
139 * @param timeout a timeout
140 * @return a next objective builder
143 public Builder makeTemporary(int timeout) {
148 * Noop. This method has no effect.
150 * @return a next objective builder
153 public Builder makePermanent() {
158 public Builder fromApp(ApplicationId appId) {
164 * Noop. This method has no effect.
166 * @param priority an integer
167 * @return a next objective builder
170 public Builder withPriority(int priority) {
175 public NextObjective add() {
176 treatments = listBuilder.build();
178 checkNotNull(appId, "Must supply an application id");
179 checkNotNull(id, "id cannot be null");
180 checkNotNull(type, "The type cannot be null");
181 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
183 return new DefaultNextObjective(this);
187 public NextObjective remove() {
188 treatments = listBuilder.build();
189 op = Operation.REMOVE;
190 checkNotNull(appId, "Must supply an application id");
191 checkNotNull(id, "id cannot be null");
192 checkNotNull(type, "The type cannot be null");
194 return new DefaultNextObjective(this);
198 public NextObjective add(ObjectiveContext context) {
199 treatments = listBuilder.build();
201 this.context = context;
202 checkNotNull(appId, "Must supply an application id");
203 checkNotNull(id, "id cannot be null");
204 checkNotNull(type, "The type cannot be null");
205 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
207 return new DefaultNextObjective(this);
211 public NextObjective remove(ObjectiveContext context) {
212 treatments = listBuilder.build();
213 op = Operation.REMOVE;
214 this.context = context;
215 checkNotNull(appId, "Must supply an application id");
216 checkNotNull(id, "id cannot be null");
217 checkNotNull(type, "The type cannot be null");
219 return new DefaultNextObjective(this);