blob: cc316fe546666f662f3024ba8d22187c2dc64fbc [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
18import com.google.common.collect.ImmutableList;
19import org.onosproject.core.ApplicationId;
20import org.onosproject.net.flow.TrafficTreatment;
21
22import java.util.Collection;
23import java.util.List;
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 next objective.
31 */
32public final class DefaultNextObjective implements NextObjective {
33
34 private final List<TrafficTreatment> treatments;
35 private final ApplicationId appId;
36 private final Type type;
37 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070038 private final Operation op;
39 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070040
41 private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments,
alshabib2a441c62015-04-13 18:39:38 -070042 ApplicationId appId, Type type, Operation op) {
alshabibfaa1e362015-04-02 15:01:54 -070043 this.treatments = treatments;
44 this.appId = appId;
45 this.type = type;
46 this.id = id;
alshabib2a441c62015-04-13 18:39:38 -070047 this.op = op;
48 this.context = Optional.empty();
49 }
50
51 private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments,
52 ApplicationId appId, ObjectiveContext context,
53 Type type, Operation op) {
54 this.treatments = treatments;
55 this.appId = appId;
56 this.type = type;
57 this.id = id;
58 this.op = op;
59 this.context = Optional.ofNullable(context);
alshabibfaa1e362015-04-02 15:01:54 -070060 }
61
62 @Override
63 public Collection<TrafficTreatment> next() {
64 return treatments;
65 }
66
67 @Override
68 public Type type() {
69 return type;
70 }
71
72 @Override
73 public int id() {
74 return id;
75 }
76
77 @Override
78 public int priority() {
79 return 0;
80 }
81
82 @Override
83 public ApplicationId appId() {
84 return appId;
85 }
86
87 @Override
88 public int timeout() {
89 return 0;
90 }
91
92 @Override
93 public boolean permanent() {
94 return false;
95 }
96
97 @Override
98 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -070099 return op;
100 }
101
102 @Override
103 public Optional<ObjectiveContext> context() {
104 return context;
alshabibfaa1e362015-04-02 15:01:54 -0700105 }
106
107 /**
108 * Returns a new builder.
109 *
110 * @return new builder
111 */
112 public static Builder builder() {
113 return new Builder();
114 }
115
116 public static final class Builder implements NextObjective.Builder {
117
118 private ApplicationId appId;
119 private Type type;
120 private Integer id;
121
122 private final ImmutableList.Builder<TrafficTreatment> listBuilder
123 = ImmutableList.builder();
124
alshabibfaa1e362015-04-02 15:01:54 -0700125 @Override
126 public NextObjective.Builder withId(int nextId) {
127 this.id = nextId;
128 return this;
129 }
130
131 @Override
132 public Builder withType(Type type) {
133 this.type = type;
134 return this;
135 }
136
137 @Override
138 public Builder addTreatment(TrafficTreatment treatment) {
139 listBuilder.add(treatment);
140 return this;
141 }
142
143 /**
144 * Noop. This method has no effect.
145 *
146 * @param timeout a timeout
147 * @return a next objective builder
148 */
149 @Override
150 public Builder makeTemporary(int timeout) {
151 return this;
152 }
153
154 /**
155 * Noop. This method has no effect.
156 *
157 * @return a next objective builder
158 */
159 @Override
160 public Builder makePermanent() {
161 return this;
162 }
163
164 @Override
alshabib2a441c62015-04-13 18:39:38 -0700165 public NextObjective.Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700166 this.appId = appId;
167 return this;
168 }
169
170 /**
171 * Noop. This method has no effect.
172 *
173 * @param priority an integer
174 * @return a next objective builder
175 */
176 @Override
177 public Builder withPriority(int priority) {
178 return this;
179 }
180
181 @Override
alshabib2a441c62015-04-13 18:39:38 -0700182 public NextObjective add() {
alshabibfaa1e362015-04-02 15:01:54 -0700183 List<TrafficTreatment> treatments = listBuilder.build();
184 checkNotNull(appId, "Must supply an application id");
185 checkNotNull(id, "id cannot be null");
186 checkNotNull(type, "The type cannot be null");
187 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
188
alshabib2a441c62015-04-13 18:39:38 -0700189 return new DefaultNextObjective(id, treatments, appId, type, Operation.ADD);
190 }
191
192 @Override
193 public NextObjective remove() {
194 List<TrafficTreatment> treatments = listBuilder.build();
195 checkNotNull(appId, "Must supply an application id");
196 checkNotNull(id, "id cannot be null");
197 checkNotNull(type, "The type cannot be null");
198 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
199
200 return new DefaultNextObjective(id, treatments, appId, type, Operation.REMOVE);
201 }
202
203 @Override
204 public NextObjective add(ObjectiveContext context) {
205 List<TrafficTreatment> treatments = listBuilder.build();
206 checkNotNull(appId, "Must supply an application id");
207 checkNotNull(id, "id cannot be null");
208 checkNotNull(type, "The type cannot be null");
209 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
210
211 return new DefaultNextObjective(id, treatments, appId,
212 context, type, Operation.ADD);
213 }
214
215 @Override
216 public NextObjective remove(ObjectiveContext context) {
217 List<TrafficTreatment> treatments = listBuilder.build();
218 checkNotNull(appId, "Must supply an application id");
219 checkNotNull(id, "id cannot be null");
220 checkNotNull(type, "The type cannot be null");
221 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
222
223 return new DefaultNextObjective(id, treatments, appId,
224 context, type, Operation.REMOVE);
alshabibfaa1e362015-04-02 15:01:54 -0700225 }
226 }
227}