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 org.onosproject.core.ApplicationId;
20 import org.onosproject.net.flow.TrafficSelector;
21 import org.onosproject.net.flow.TrafficTreatment;
23 import java.util.Objects;
24 import java.util.Optional;
26 import static com.google.common.base.Preconditions.checkArgument;
27 import static com.google.common.base.Preconditions.checkNotNull;
30 * Default implementation of a forwarding objective.
33 public final class DefaultForwardingObjective implements ForwardingObjective {
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;
41 private final Integer nextId;
42 private final TrafficTreatment treatment;
43 private final Operation op;
44 private final Optional<ObjectiveContext> context;
48 private DefaultForwardingObjective(Builder builder) {
49 this.selector = builder.selector;
50 this.flag = builder.flag;
51 this.permanent = builder.permanent;
52 this.timeout = builder.timeout;
53 this.appId = builder.appId;
54 this.priority = builder.priority;
55 this.nextId = builder.nextId;
56 this.treatment = builder.treatment;
58 this.context = Optional.ofNullable(builder.context);
60 this.id = Objects.hash(selector, flag, permanent,
61 timeout, appId, priority, nextId,
67 public TrafficSelector selector() {
72 public Integer nextId() {
77 public TrafficTreatment treatment() {
93 public int priority() {
98 public ApplicationId appId() {
103 public int timeout() {
108 public boolean permanent() {
113 public Operation op() {
118 public Optional<ObjectiveContext> context() {
123 * Returns a new builder.
125 * @return new builder
127 public static Builder builder() {
128 return new Builder();
131 public static final class Builder implements ForwardingObjective.Builder {
133 private TrafficSelector selector;
135 private boolean permanent = DEFAULT_PERMANENT;
136 private int timeout = DEFAULT_TIMEOUT;
137 private int priority = DEFAULT_PRIORITY;
138 private ApplicationId appId;
139 private Integer nextId;
140 private TrafficTreatment treatment;
141 private Operation op;
142 private ObjectiveContext context;
145 public Builder withSelector(TrafficSelector selector) {
146 this.selector = selector;
151 public Builder nextStep(int nextId) {
152 this.nextId = nextId;
157 public Builder withTreatment(TrafficTreatment treatment) {
158 this.treatment = treatment;
163 public Builder withFlag(Flag flag) {
169 public Builder makeTemporary(int timeout) {
170 this.timeout = timeout;
171 this.permanent = false;
176 public Builder makePermanent() {
177 this.permanent = true;
182 public Builder fromApp(ApplicationId appId) {
188 public Builder withPriority(int priority) {
189 this.priority = priority;
194 public ForwardingObjective add() {
195 checkNotNull(selector, "Must have a selector");
196 checkNotNull(flag, "A flag must be set");
197 checkArgument(nextId != null || treatment != null, "Must supply at " +
198 "least a treatment and/or a nextId");
199 checkNotNull(appId, "Must supply an application id");
201 return new DefaultForwardingObjective(this);
205 public ForwardingObjective remove() {
206 checkNotNull(selector, "Must have a selector");
207 checkNotNull(flag, "A flag must be set");
208 checkArgument(nextId != null || treatment != null, "Must supply at " +
209 "least a treatment and/or a nextId");
210 checkNotNull(appId, "Must supply an application id");
211 op = Operation.REMOVE;
212 return new DefaultForwardingObjective(this);
216 public ForwardingObjective add(ObjectiveContext context) {
217 checkNotNull(selector, "Must have a selector");
218 checkNotNull(flag, "A flag must be set");
219 checkArgument(nextId != null || treatment != null, "Must supply at " +
220 "least a treatment and/or a nextId");
221 checkNotNull(appId, "Must supply an application id");
223 this.context = context;
225 return new DefaultForwardingObjective(this);
229 public ForwardingObjective remove(ObjectiveContext context) {
230 checkNotNull(selector, "Must have a selector");
231 checkNotNull(flag, "A flag must be set");
232 checkArgument(nextId != null || treatment != null, "Must supply at " +
233 "least a treatment and/or a nextId");
234 checkNotNull(appId, "Must supply an application id");
235 op = Operation.REMOVE;
236 this.context = context;
238 return new DefaultForwardingObjective(this);