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