4701589f622bbe938c063ebdce6c46ae6410ff9a
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.net.flowobjective;
17
18 import com.google.common.annotations.Beta;
19 import com.google.common.collect.ImmutableList;
20 import org.onosproject.core.ApplicationId;
21 import org.onosproject.net.flow.TrafficSelector;
22 import org.onosproject.net.flow.TrafficTreatment;
23
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Optional;
27
28 import static com.google.common.base.Preconditions.checkArgument;
29 import static com.google.common.base.Preconditions.checkNotNull;
30
31 /**
32  * Default implementation of a next objective.
33  */
34 @Beta
35 public 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;
41     private final Operation op;
42     private final Optional<ObjectiveContext> context;
43     private final TrafficSelector meta;
44
45     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);
52         this.meta = builder.meta;
53     }
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() {
92         return op;
93     }
94
95     @Override
96     public Optional<ObjectiveContext> context() {
97         return context;
98     }
99
100     @Override
101     public TrafficSelector meta() {
102         return meta;
103     }
104
105     /**
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;
119         private List<TrafficTreatment> treatments;
120         private Operation op;
121         private ObjectiveContext context;
122         private TrafficSelector meta;
123
124         private final ImmutableList.Builder<TrafficTreatment> listBuilder
125                 = ImmutableList.builder();
126
127         @Override
128         public 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
167         public Builder fromApp(ApplicationId appId) {
168             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
184         public Builder setMeta(TrafficSelector meta) {
185             this.meta = meta;
186             return this;
187         }
188
189         @Override
190         public NextObjective add() {
191             treatments = listBuilder.build();
192             op = Operation.ADD;
193             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
198             return new DefaultNextObjective(this);
199         }
200
201         @Override
202         public NextObjective remove() {
203             treatments = listBuilder.build();
204             op = Operation.REMOVE;
205             checkNotNull(appId, "Must supply an application id");
206             checkNotNull(id, "id cannot be null");
207             checkNotNull(type, "The type cannot be null");
208
209             return new DefaultNextObjective(this);
210         }
211
212         @Override
213         public NextObjective add(ObjectiveContext context) {
214             treatments = listBuilder.build();
215             op = Operation.ADD;
216             this.context = context;
217             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
222             return new DefaultNextObjective(this);
223         }
224
225         @Override
226         public NextObjective remove(ObjectiveContext context) {
227             treatments = listBuilder.build();
228             op = Operation.REMOVE;
229             this.context = context;
230             checkNotNull(appId, "Must supply an application id");
231             checkNotNull(id, "id cannot be null");
232             checkNotNull(type, "The type cannot be null");
233
234             return new DefaultNextObjective(this);
235         }
236
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
285     }
286
287 }