7e06f332f6e5505c8ce56ef78bc379676e1d6b72
[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.vtnrsc.sfc;
17
18 import java.util.Objects;
19
20 import org.onlab.packet.EthType.EtherType;
21 import org.onlab.packet.IpPrefix;
22 import org.onosproject.vtnrsc.TenantId;
23 import org.onosproject.vtnrsc.VirtualPortId;
24
25 import com.google.common.base.MoreObjects;
26 import static com.google.common.base.Preconditions.checkNotNull;
27 /**
28  * Provides Default flow classifier.
29  */
30 public final class DefaultFlowClassifier implements FlowClassifier {
31
32     private final FlowClassifierId flowClassifierId;
33     private final TenantId tenantId;
34     private final String name;
35     private final String description;
36     private final EtherType etherType;
37     private final String protocol;
38     private final int minSrcPortRange;
39     private final int maxSrcPortRange;
40     private final int minDstPortRange;
41     private final int maxDstPortRange;
42     private final IpPrefix srcIpPrefix;
43     private final IpPrefix dstIpPrefix;
44     private final VirtualPortId srcPort;
45     private final VirtualPortId dstPort;
46     private static final int NULL_PORT = 0;
47
48     /**
49      * Constructor to create default flow classifier.
50      *
51      * @param flowClassifierId      flow classifier Id
52      * @param tenantId              Tenant ID
53      * @param name                  flow classifier name
54      * @param description           flow classifier description
55      * @param etherType             etherType
56      * @param protocol              IP protocol
57      * @param minSrcPortRange       Minimum Source port range
58      * @param maxSrcPortRange       Maximum Source port range
59      * @param minDstPortRange       Minimum destination port range
60      * @param maxDstPortRange       Maximum destination port range
61      * @param srcIpPrefix           Source IP prefix
62      * @param dstIpPrefix           destination IP prefix
63      * @param srcPort               Source VirtualPort
64      * @param dstPort               destination VirtualPort
65      */
66     private DefaultFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name,
67             String description, EtherType etherType, String protocol, int minSrcPortRange, int maxSrcPortRange,
68             int minDstPortRange, int maxDstPortRange, IpPrefix srcIpPrefix, IpPrefix dstIpPrefix,
69             VirtualPortId srcPort, VirtualPortId dstPort) {
70         this.flowClassifierId = flowClassifierId;
71         this.tenantId = tenantId;
72         this.name = name;
73         this.description = description;
74         this.etherType = etherType;
75         this.protocol = protocol;
76         this.minSrcPortRange = minSrcPortRange;
77         this.maxSrcPortRange = maxSrcPortRange;
78         this.minDstPortRange = minDstPortRange;
79         this.maxDstPortRange = maxDstPortRange;
80         this.srcIpPrefix = srcIpPrefix;
81         this.dstIpPrefix = dstIpPrefix;
82         this.srcPort = srcPort;
83         this.dstPort = dstPort;
84     }
85
86     @Override
87     public FlowClassifierId flowClassifierId() {
88         return flowClassifierId;
89     }
90
91     @Override
92     public TenantId tenantId() {
93         return tenantId;
94     }
95
96     @Override
97     public String flowclassifierName() {
98         return name;
99     }
100
101     @Override
102     public String flowClassifierDescription() {
103         return description;
104     }
105
106     @Override
107     public EtherType etherType() {
108         return etherType;
109     }
110
111     @Override
112     public String ipProtocol() {
113         return protocol;
114     }
115
116     @Override
117     public int minSrcPortRange() {
118         return minSrcPortRange;
119     }
120
121     @Override
122     public int maxSrcPortRange() {
123         return maxSrcPortRange;
124     }
125
126     @Override
127     public int minDstPortRange() {
128         return minDstPortRange;
129     }
130
131     @Override
132     public int maxDstPortRange() {
133         return maxDstPortRange;
134     }
135
136     @Override
137     public IpPrefix srcIpPrefix() {
138         return srcIpPrefix;
139     }
140
141     @Override
142     public IpPrefix dstIpPrefix() {
143         return dstIpPrefix;
144     }
145
146     @Override
147     public VirtualPortId srcVirtualPort() {
148         return srcPort;
149     }
150
151     @Override
152     public VirtualPortId dstVirtualPort() {
153         return dstPort;
154     }
155
156     /**
157      * Builder class for constructing Flow classifier.
158      */
159     public static class Builder implements FlowClassifier.Builder {
160
161         private FlowClassifierId flowClassifierId;
162         private TenantId tenantId;
163         private String name;
164         private boolean isFlowClassifierNameSet = false;
165         private String description;
166         private boolean isFlowClassifierDescriptionSet = false;
167         private EtherType etherType;
168         private boolean isEtherTypeSet = false;
169         private String protocol;
170         private boolean isProtocolSet = false;
171         private int minSrcPortRange;
172         private boolean isMinSrcPortRangeSet = false;
173         private int maxSrcPortRange;
174         private boolean isMaxSrcPortRangeSet = false;
175         private int minDstPortRange;
176         private boolean isMinDstPortRangeSet = false;
177         private int maxDstPortRange;
178         private boolean isMaxDstPortRangeSet = false;
179         private IpPrefix srcIpPrefix;
180         private boolean isSrcIpPrefixSet = false;
181         private IpPrefix dstIpPrefix;
182         private boolean isDstIpPrefixSet = false;
183         private VirtualPortId srcPort;
184         private boolean isSrcPortSet = false;
185         private VirtualPortId dstPort;
186         private boolean isDstPortSet = false;
187
188         @Override
189         public FlowClassifier build() {
190
191             checkNotNull(flowClassifierId, "FlowClassifier id can not be null.");
192             checkNotNull(tenantId, "Tenant id can not be null.");
193             String name = null;
194             String description = null;
195             EtherType etherType = null;
196             String protocol = null;
197             int minSrcPortRange = NULL_PORT;
198             int maxSrcPortRange = NULL_PORT;
199             int minDstPortRange = NULL_PORT;
200             int maxDstPortRange = NULL_PORT;
201             IpPrefix srcIpPrefix = null;
202             IpPrefix dstIpPrefix = null;
203             VirtualPortId srcPort = null;
204             VirtualPortId dstPort = null;
205
206             if (isFlowClassifierNameSet) {
207                 name = this.name;
208             }
209             if (isFlowClassifierDescriptionSet) {
210                 description = this.description;
211             }
212             if (isEtherTypeSet) {
213                 etherType = this.etherType;
214             }
215             if (isProtocolSet) {
216                 protocol = this.protocol;
217             }
218             if (isMinSrcPortRangeSet) {
219                 minSrcPortRange = this.minSrcPortRange;
220             }
221             if (isMaxSrcPortRangeSet) {
222                 maxSrcPortRange = this.maxSrcPortRange;
223             }
224             if (isMinDstPortRangeSet) {
225                 minDstPortRange = this.minDstPortRange;
226             }
227             if (isMaxDstPortRangeSet) {
228                 maxDstPortRange = this.maxDstPortRange;
229             }
230             if (isSrcIpPrefixSet) {
231                 srcIpPrefix = this.srcIpPrefix;
232             }
233             if (isDstIpPrefixSet) {
234                 dstIpPrefix = this.dstIpPrefix;
235             }
236             if (isSrcPortSet) {
237                 srcPort = this.srcPort;
238             }
239             if (isDstPortSet) {
240                 dstPort = this.dstPort;
241             }
242
243             return new DefaultFlowClassifier(flowClassifierId, tenantId, name, description, etherType, protocol,
244                     minSrcPortRange, maxSrcPortRange, minDstPortRange, maxDstPortRange, srcIpPrefix, dstIpPrefix,
245                     srcPort, dstPort);
246         }
247
248         @Override
249         public Builder setFlowClassifierId(FlowClassifierId flowClassifierId) {
250             this.flowClassifierId = flowClassifierId;
251             return this;
252         }
253
254         @Override
255         public Builder setTenantId(TenantId tenantId) {
256             this.tenantId = tenantId;
257             return this;
258         }
259
260         @Override
261         public Builder setFlowClassifierName(String name) {
262             this.name = name;
263             this.isFlowClassifierNameSet = true;
264             return this;
265         }
266
267         @Override
268         public Builder setFlowClassifierDescription(String description) {
269             this.description = description;
270             this.isFlowClassifierDescriptionSet = true;
271             return this;
272         }
273
274         @Override
275         public Builder setEtherType(EtherType etherType) {
276             this.etherType = etherType;
277             this.isEtherTypeSet = true;
278             return this;
279         }
280
281         @Override
282         public Builder setIpProtocol(String protocol) {
283             this.protocol = protocol;
284             this.isProtocolSet = true;
285             return this;
286         }
287
288         @Override
289         public Builder setMinSrcPortRange(int minSrcPortRange) {
290             this.minSrcPortRange = minSrcPortRange;
291             this.isMinSrcPortRangeSet = true;
292             return this;
293         }
294
295         @Override
296         public Builder setMaxSrcPortRange(int maxSrcPortRange) {
297             this.maxSrcPortRange = maxSrcPortRange;
298             this.isMaxSrcPortRangeSet = true;
299             return this;
300         }
301
302         @Override
303         public Builder setMinDstPortRange(int minDstPortRange) {
304             this.minDstPortRange = minDstPortRange;
305             this.isMinDstPortRangeSet = true;
306             return this;
307         }
308
309         @Override
310         public Builder setMaxDstPortRange(int maxDstPortRange) {
311             this.maxDstPortRange = maxDstPortRange;
312             this.isMaxDstPortRangeSet = true;
313             return this;
314         }
315
316         @Override
317         public Builder setSrcIpPrefix(IpPrefix srcIpPrefix) {
318             this.srcIpPrefix = srcIpPrefix;
319             this.isSrcIpPrefixSet = true;
320             return this;
321         }
322
323         @Override
324         public Builder setDstIpPrefix(IpPrefix dstIpPrefix) {
325             this.dstIpPrefix = dstIpPrefix;
326             this.isDstIpPrefixSet = true;
327             return this;
328         }
329
330         @Override
331         public Builder setSrcVirtualPort(VirtualPortId srcPort) {
332             this.srcPort = srcPort;
333             this.isSrcPortSet = true;
334             return this;
335         }
336
337         @Override
338         public Builder setDstVirtualPort(VirtualPortId dstPort) {
339             this.dstPort = dstPort;
340             this.isDstPortSet = true;
341             return this;
342         }
343     }
344
345     @Override
346     public int hashCode() {
347         return Objects.hash(flowClassifierId, tenantId, name, description, etherType, protocol, minSrcPortRange,
348                 maxSrcPortRange, minDstPortRange, maxDstPortRange, srcIpPrefix, dstIpPrefix, srcPort, dstPort);
349     }
350
351     @Override
352     public boolean equals(Object obj) {
353         if (this == obj) {
354             return true;
355         }
356         if (obj instanceof DefaultFlowClassifier) {
357             DefaultFlowClassifier other = (DefaultFlowClassifier) obj;
358             return Objects.equals(this.flowClassifierId, other.flowClassifierId)
359                     && Objects.equals(this.tenantId, other.tenantId)
360                     && Objects.equals(this.name, other.name)
361                     && Objects.equals(this.description, other.description)
362                     && Objects.equals(this.etherType, other.etherType)
363                     && Objects.equals(this.protocol, other.protocol)
364                     && Objects.equals(this.minSrcPortRange, other.minSrcPortRange)
365                     && Objects.equals(this.maxSrcPortRange, other.maxSrcPortRange)
366                     && Objects.equals(this.minDstPortRange, other.minDstPortRange)
367                     && Objects.equals(this.maxDstPortRange, other.maxDstPortRange)
368                     && Objects.equals(this.srcIpPrefix, other.srcIpPrefix)
369                     && Objects.equals(this.dstIpPrefix, other.dstIpPrefix)
370                     && Objects.equals(this.srcPort, other.srcPort)
371                     && Objects.equals(this.dstPort, other.dstPort);
372         }
373         return false;
374     }
375
376     @Override
377     public boolean exactMatch(FlowClassifier flowClassifier) {
378         return this.equals(flowClassifier)
379                 && Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId())
380                 && Objects.equals(this.tenantId, flowClassifier.tenantId())
381                 && Objects.equals(this.name, flowClassifier.flowclassifierName())
382                 && Objects.equals(this.description, flowClassifier.flowClassifierDescription())
383                 && Objects.equals(this.etherType, flowClassifier.etherType())
384                 && Objects.equals(this.protocol, flowClassifier.ipProtocol())
385                 && Objects.equals(this.minSrcPortRange, flowClassifier.minSrcPortRange())
386                 && Objects.equals(this.maxSrcPortRange, flowClassifier.maxSrcPortRange())
387                 && Objects.equals(this.minDstPortRange, flowClassifier.minDstPortRange())
388                 && Objects.equals(this.maxDstPortRange, flowClassifier.maxDstPortRange())
389                 && Objects.equals(this.srcIpPrefix, flowClassifier.srcIpPrefix())
390                 && Objects.equals(this.dstIpPrefix, flowClassifier.dstIpPrefix())
391                 && Objects.equals(this.srcPort, flowClassifier.srcVirtualPort())
392                 && Objects.equals(this.dstPort, flowClassifier.dstVirtualPort());
393     }
394
395     @Override
396     public String toString() {
397         return MoreObjects.toStringHelper(getClass())
398                 .add("FlowClassifierId", flowClassifierId)
399                 .add("TenantId", tenantId)
400                 .add("Name", name)
401                 .add("Description", description)
402                 .add("EtherType", etherType)
403                 .add("Protocol", protocol)
404                 .add("MinSrcPortRange", minSrcPortRange)
405                 .add("MaxSrcPortRange", maxSrcPortRange)
406                 .add("MinDstPortRange", minDstPortRange)
407                 .add("MaxDstPortRange", maxDstPortRange)
408                 .add("SrcIpPrefix", srcIpPrefix)
409                 .add("DstIpPrefix", dstIpPrefix)
410                 .add("SrcPort", srcPort)
411                 .add("DstPort", dstPort)
412                 .toString();
413     }
414 }