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