Bharat saraswal
Committed by Gerrit Code Review

[ONOS-3116] Implementation of Flow classifier for service function chain.

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