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