blob: bd58050798a5e0e12ae8cf4abf1e507042c9b22f [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 com.google.common.collect.ImmutableList;
20import org.onosproject.core.ApplicationId;
Saurav Das8a0732e2015-11-20 15:27:53 -080021import org.onosproject.net.flow.TrafficSelector;
alshabibfaa1e362015-04-02 15:01:54 -070022import org.onosproject.net.flow.TrafficTreatment;
23
24import java.util.Collection;
25import java.util.List;
alshabib2a441c62015-04-13 18:39:38 -070026import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070027
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Default implementation of a next objective.
33 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070034@Beta
alshabibfaa1e362015-04-02 15:01:54 -070035public 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;
alshabib2a441c62015-04-13 18:39:38 -070041 private final Operation op;
42 private final Optional<ObjectiveContext> context;
Saurav Das8a0732e2015-11-20 15:27:53 -080043 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070044
Ray Milkey810d6e72015-08-14 10:35:06 -070045 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 Das8a0732e2015-11-20 15:27:53 -080052 this.meta = builder.meta;
alshabibfaa1e362015-04-02 15:01:54 -070053 }
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() {
alshabib2a441c62015-04-13 18:39:38 -070092 return op;
93 }
94
95 @Override
96 public Optional<ObjectiveContext> context() {
97 return context;
alshabibfaa1e362015-04-02 15:01:54 -070098 }
99
Saurav Das8a0732e2015-11-20 15:27:53 -0800100 @Override
101 public TrafficSelector meta() {
102 return meta;
103 }
104
alshabibfaa1e362015-04-02 15:01:54 -0700105 /**
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 Milkey810d6e72015-08-14 10:35:06 -0700119 private List<TrafficTreatment> treatments;
120 private Operation op;
121 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800122 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700123
124 private final ImmutableList.Builder<TrafficTreatment> listBuilder
125 = ImmutableList.builder();
126
alshabibfaa1e362015-04-02 15:01:54 -0700127 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700128 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700129 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 Milkey810d6e72015-08-14 10:35:06 -0700167 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700168 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 Das4ce45962015-11-24 23:21:05 -0800184 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800185 this.meta = meta;
186 return this;
187 }
188
189 @Override
alshabib2a441c62015-04-13 18:39:38 -0700190 public NextObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700191 treatments = listBuilder.build();
192 op = Operation.ADD;
alshabibfaa1e362015-04-02 15:01:54 -0700193 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 Milkey810d6e72015-08-14 10:35:06 -0700198 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700199 }
200
201 @Override
202 public NextObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700203 treatments = listBuilder.build();
204 op = Operation.REMOVE;
alshabib2a441c62015-04-13 18:39:38 -0700205 checkNotNull(appId, "Must supply an application id");
206 checkNotNull(id, "id cannot be null");
207 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700208
Ray Milkey810d6e72015-08-14 10:35:06 -0700209 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700210 }
211
212 @Override
213 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700214 treatments = listBuilder.build();
215 op = Operation.ADD;
216 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700217 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 Milkey810d6e72015-08-14 10:35:06 -0700222 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700223 }
224
225 @Override
226 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700227 treatments = listBuilder.build();
228 op = Operation.REMOVE;
229 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700230 checkNotNull(appId, "Must supply an application id");
231 checkNotNull(id, "id cannot be null");
232 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700233
Ray Milkey810d6e72015-08-14 10:35:06 -0700234 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700235 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800236
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
alshabibfaa1e362015-04-02 15:01:54 -0700285 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800286
alshabibfaa1e362015-04-02 15:01:54 -0700287}