blob: 956dbbe87f9cc818f3ad0aa0181f7abcc427ff40 [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;
21import org.onosproject.net.flow.TrafficTreatment;
22
23import java.util.Collection;
24import java.util.List;
alshabib2a441c62015-04-13 18:39:38 -070025import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070026
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a next objective.
32 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070033@Beta
alshabibfaa1e362015-04-02 15:01:54 -070034public final class DefaultNextObjective implements NextObjective {
35
36 private final List<TrafficTreatment> treatments;
37 private final ApplicationId appId;
38 private final Type type;
39 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070040 private final Operation op;
41 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070042
43 private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments,
alshabib2a441c62015-04-13 18:39:38 -070044 ApplicationId appId, Type type, Operation op) {
alshabibfaa1e362015-04-02 15:01:54 -070045 this.treatments = treatments;
46 this.appId = appId;
47 this.type = type;
48 this.id = id;
alshabib2a441c62015-04-13 18:39:38 -070049 this.op = op;
50 this.context = Optional.empty();
51 }
52
53 private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments,
54 ApplicationId appId, ObjectiveContext context,
55 Type type, Operation op) {
56 this.treatments = treatments;
57 this.appId = appId;
58 this.type = type;
59 this.id = id;
60 this.op = op;
61 this.context = Optional.ofNullable(context);
alshabibfaa1e362015-04-02 15:01:54 -070062 }
63
64 @Override
65 public Collection<TrafficTreatment> next() {
66 return treatments;
67 }
68
69 @Override
70 public Type type() {
71 return type;
72 }
73
74 @Override
75 public int id() {
76 return id;
77 }
78
79 @Override
80 public int priority() {
81 return 0;
82 }
83
84 @Override
85 public ApplicationId appId() {
86 return appId;
87 }
88
89 @Override
90 public int timeout() {
91 return 0;
92 }
93
94 @Override
95 public boolean permanent() {
96 return false;
97 }
98
99 @Override
100 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -0700101 return op;
102 }
103
104 @Override
105 public Optional<ObjectiveContext> context() {
106 return context;
alshabibfaa1e362015-04-02 15:01:54 -0700107 }
108
109 /**
110 * Returns a new builder.
111 *
112 * @return new builder
113 */
114 public static Builder builder() {
115 return new Builder();
116 }
117
118 public static final class Builder implements NextObjective.Builder {
119
120 private ApplicationId appId;
121 private Type type;
122 private Integer id;
123
124 private final ImmutableList.Builder<TrafficTreatment> listBuilder
125 = ImmutableList.builder();
126
alshabibfaa1e362015-04-02 15:01:54 -0700127 @Override
128 public NextObjective.Builder withId(int nextId) {
129 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
alshabib2a441c62015-04-13 18:39:38 -0700167 public NextObjective.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
alshabib2a441c62015-04-13 18:39:38 -0700184 public NextObjective add() {
alshabibfaa1e362015-04-02 15:01:54 -0700185 List<TrafficTreatment> treatments = listBuilder.build();
186 checkNotNull(appId, "Must supply an application id");
187 checkNotNull(id, "id cannot be null");
188 checkNotNull(type, "The type cannot be null");
189 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
190
alshabib2a441c62015-04-13 18:39:38 -0700191 return new DefaultNextObjective(id, treatments, appId, type, Operation.ADD);
192 }
193
194 @Override
195 public NextObjective remove() {
196 List<TrafficTreatment> treatments = listBuilder.build();
197 checkNotNull(appId, "Must supply an application id");
198 checkNotNull(id, "id cannot be null");
199 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700200
201 return new DefaultNextObjective(id, treatments, appId, type, Operation.REMOVE);
202 }
203
204 @Override
205 public NextObjective add(ObjectiveContext context) {
206 List<TrafficTreatment> treatments = listBuilder.build();
207 checkNotNull(appId, "Must supply an application id");
208 checkNotNull(id, "id cannot be null");
209 checkNotNull(type, "The type cannot be null");
210 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
211
212 return new DefaultNextObjective(id, treatments, appId,
213 context, type, Operation.ADD);
214 }
215
216 @Override
217 public NextObjective remove(ObjectiveContext context) {
218 List<TrafficTreatment> treatments = listBuilder.build();
219 checkNotNull(appId, "Must supply an application id");
220 checkNotNull(id, "id cannot be null");
221 checkNotNull(type, "The type cannot be null");
222 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
223
224 return new DefaultNextObjective(id, treatments, appId,
225 context, type, Operation.REMOVE);
alshabibfaa1e362015-04-02 15:01:54 -0700226 }
227 }
228}