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