06305bf792c5a34e2fe7734ffa36642a4f7145c2
[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 import org.onosproject.net.flow.criteria.Criteria;
23 import org.onosproject.net.flow.criteria.Criterion;
24
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29
30 import static com.google.common.base.Preconditions.checkArgument;
31 import static com.google.common.base.Preconditions.checkNotNull;
32
33 /**
34  * Default implementation of a filtering objective.
35  */
36 @Beta
37 public final class DefaultFilteringObjective implements FilteringObjective {
38
39
40     private final Type type;
41     private final boolean permanent;
42     private final int timeout;
43     private final ApplicationId appId;
44     private final int priority;
45     private final Criterion key;
46     private final List<Criterion> conditions;
47     private final int id;
48     private final Operation op;
49     private final Optional<ObjectiveContext> context;
50     private final TrafficTreatment meta;
51
52     private DefaultFilteringObjective(Builder builder) {
53         this.key = builder.key;
54         this.type = builder.type;
55         this.permanent = builder.permanent;
56         this.timeout = builder.timeout;
57         this.appId = builder.appId;
58         this.priority = builder.priority;
59         this.conditions = builder.conditions;
60         this.op = builder.op;
61         this.context = Optional.ofNullable(builder.context);
62         this.meta = builder.meta;
63
64         this.id = Objects.hash(type, key, conditions, permanent,
65                 timeout, appId, priority);
66     }
67
68     @Override
69     public Criterion key() {
70         return key;
71     }
72
73     @Override
74     public Type type() {
75         return this.type;
76     }
77
78     @Override
79     public Collection<Criterion> conditions() {
80         return conditions;
81     }
82
83     @Override
84     public int id() {
85         return id;
86     }
87
88     @Override
89     public TrafficTreatment meta() {
90         return meta;
91     }
92
93
94     @Override
95     public int priority() {
96         return priority;
97     }
98
99     @Override
100     public ApplicationId appId() {
101         return appId;
102     }
103
104     @Override
105     public int timeout() {
106         return timeout;
107     }
108
109     @Override
110     public boolean permanent() {
111         return permanent;
112     }
113
114     @Override
115     public Operation op() {
116         return op;
117     }
118
119     @Override
120     public Optional<ObjectiveContext> context() {
121         return context;
122     }
123
124     /**
125      * Returns a new builder.
126      *
127      * @return new builder
128      */
129     public static Builder builder() {
130         return new Builder();
131     }
132
133
134     public static final class Builder implements FilteringObjective.Builder {
135         private final ImmutableList.Builder<Criterion> listBuilder
136                 = ImmutableList.builder();
137
138         private Type type;
139         private boolean permanent = DEFAULT_PERMANENT;
140         private int timeout = DEFAULT_TIMEOUT;
141         private ApplicationId appId;
142         private int priority = DEFAULT_PRIORITY;
143         private Criterion key = Criteria.dummy();
144         private List<Criterion> conditions;
145         private Operation op;
146         private ObjectiveContext context;
147         private TrafficTreatment meta;
148
149         @Override
150         public Builder withKey(Criterion key) {
151             this.key = key;
152             return this;
153         }
154
155         @Override
156         public Builder addCondition(Criterion criterion) {
157             listBuilder.add(criterion);
158             return this;
159         }
160
161         @Override
162         public Builder permit() {
163             this.type = Type.PERMIT;
164             return this;
165         }
166
167         @Override
168         public Builder deny() {
169             this.type = Type.DENY;
170             return this;
171         }
172
173         @Override
174         public Builder makeTemporary(int timeout) {
175             this.timeout = timeout;
176             permanent = false;
177             return this;
178         }
179
180         @Override
181         public Builder makePermanent() {
182             permanent = true;
183             return this;
184         }
185
186         @Override
187         public Builder fromApp(ApplicationId appId) {
188             this.appId = appId;
189             return this;
190         }
191
192         @Override
193         public Builder withPriority(int priority) {
194             this.priority = priority;
195             return this;
196         }
197
198         @Override
199         public Builder setMeta(TrafficTreatment treatment) {
200             this.meta = treatment;
201             return this;
202         }
203
204         @Override
205         public FilteringObjective add() {
206             conditions = listBuilder.build();
207             op = Operation.ADD;
208             checkNotNull(type, "Must have a type.");
209             checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
210             checkNotNull(appId, "Must supply an application id");
211
212             return new DefaultFilteringObjective(this);
213
214         }
215
216         @Override
217         public FilteringObjective remove() {
218             conditions = listBuilder.build();
219             checkNotNull(type, "Must have a type.");
220             checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
221             checkNotNull(appId, "Must supply an application id");
222             op = Operation.REMOVE;
223
224             return new DefaultFilteringObjective(this);
225
226         }
227
228         @Override
229         public FilteringObjective add(ObjectiveContext context) {
230             conditions = listBuilder.build();
231             checkNotNull(type, "Must have a type.");
232             checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
233             checkNotNull(appId, "Must supply an application id");
234             op = Operation.ADD;
235             this.context = context;
236
237             return new DefaultFilteringObjective(this);
238         }
239
240         @Override
241         public FilteringObjective remove(ObjectiveContext context) {
242             conditions = listBuilder.build();
243             checkNotNull(type, "Must have a type.");
244             checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
245             checkNotNull(appId, "Must supply an application id");
246             op = Operation.REMOVE;
247             this.context = context;
248
249             return new DefaultFilteringObjective(this);
250         }
251
252
253     }
254
255 }