2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnrsc.sfc;
18 import java.util.Objects;
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;
25 import com.google.common.base.MoreObjects;
26 import static com.google.common.base.Preconditions.checkNotNull;
28 * Provides Default flow classifier.
30 public final class DefaultFlowClassifier implements FlowClassifier {
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;
49 * Constructor to create default flow classifier.
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
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;
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;
87 public FlowClassifierId flowClassifierId() {
88 return flowClassifierId;
92 public TenantId tenantId() {
97 public String flowclassifierName() {
102 public String flowClassifierDescription() {
107 public EtherType etherType() {
112 public String ipProtocol() {
117 public int minSrcPortRange() {
118 return minSrcPortRange;
122 public int maxSrcPortRange() {
123 return maxSrcPortRange;
127 public int minDstPortRange() {
128 return minDstPortRange;
132 public int maxDstPortRange() {
133 return maxDstPortRange;
137 public IpPrefix srcIpPrefix() {
142 public IpPrefix dstIpPrefix() {
147 public VirtualPortId srcVirtualPort() {
152 public VirtualPortId dstVirtualPort() {
157 * Builder class for constructing Flow classifier.
159 public static class Builder implements FlowClassifier.Builder {
161 private FlowClassifierId flowClassifierId;
162 private TenantId tenantId;
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;
189 public FlowClassifier build() {
191 checkNotNull(flowClassifierId, "FlowClassifier id can not be null.");
192 checkNotNull(tenantId, "Tenant id can not be 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;
206 if (isFlowClassifierNameSet) {
209 if (isFlowClassifierDescriptionSet) {
210 description = this.description;
212 if (isEtherTypeSet) {
213 etherType = this.etherType;
216 protocol = this.protocol;
218 if (isMinSrcPortRangeSet) {
219 minSrcPortRange = this.minSrcPortRange;
221 if (isMaxSrcPortRangeSet) {
222 maxSrcPortRange = this.maxSrcPortRange;
224 if (isMinDstPortRangeSet) {
225 minDstPortRange = this.minDstPortRange;
227 if (isMaxDstPortRangeSet) {
228 maxDstPortRange = this.maxDstPortRange;
230 if (isSrcIpPrefixSet) {
231 srcIpPrefix = this.srcIpPrefix;
233 if (isDstIpPrefixSet) {
234 dstIpPrefix = this.dstIpPrefix;
237 srcPort = this.srcPort;
240 dstPort = this.dstPort;
243 return new DefaultFlowClassifier(flowClassifierId, tenantId, name, description, etherType, protocol,
244 minSrcPortRange, maxSrcPortRange, minDstPortRange, maxDstPortRange, srcIpPrefix, dstIpPrefix,
249 public Builder setFlowClassifierId(FlowClassifierId flowClassifierId) {
250 this.flowClassifierId = flowClassifierId;
255 public Builder setTenantId(TenantId tenantId) {
256 this.tenantId = tenantId;
261 public Builder setFlowClassifierName(String name) {
263 this.isFlowClassifierNameSet = true;
268 public Builder setFlowClassifierDescription(String description) {
269 this.description = description;
270 this.isFlowClassifierDescriptionSet = true;
275 public Builder setEtherType(EtherType etherType) {
276 this.etherType = etherType;
277 this.isEtherTypeSet = true;
282 public Builder setIpProtocol(String protocol) {
283 this.protocol = protocol;
284 this.isProtocolSet = true;
289 public Builder setMinSrcPortRange(int minSrcPortRange) {
290 this.minSrcPortRange = minSrcPortRange;
291 this.isMinSrcPortRangeSet = true;
296 public Builder setMaxSrcPortRange(int maxSrcPortRange) {
297 this.maxSrcPortRange = maxSrcPortRange;
298 this.isMaxSrcPortRangeSet = true;
303 public Builder setMinDstPortRange(int minDstPortRange) {
304 this.minDstPortRange = minDstPortRange;
305 this.isMinDstPortRangeSet = true;
310 public Builder setMaxDstPortRange(int maxDstPortRange) {
311 this.maxDstPortRange = maxDstPortRange;
312 this.isMaxDstPortRangeSet = true;
317 public Builder setSrcIpPrefix(IpPrefix srcIpPrefix) {
318 this.srcIpPrefix = srcIpPrefix;
319 this.isSrcIpPrefixSet = true;
324 public Builder setDstIpPrefix(IpPrefix dstIpPrefix) {
325 this.dstIpPrefix = dstIpPrefix;
326 this.isDstIpPrefixSet = true;
331 public Builder setSrcVirtualPort(VirtualPortId srcPort) {
332 this.srcPort = srcPort;
333 this.isSrcPortSet = true;
338 public Builder setDstVirtualPort(VirtualPortId dstPort) {
339 this.dstPort = dstPort;
340 this.isDstPortSet = true;
346 public int hashCode() {
347 return Objects.hash(flowClassifierId, tenantId, name, description, etherType, protocol, minSrcPortRange,
348 maxSrcPortRange, minDstPortRange, maxDstPortRange, srcIpPrefix, dstIpPrefix, srcPort, dstPort);
352 public boolean equals(Object obj) {
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);
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());
396 public String toString() {
397 return MoreObjects.toStringHelper(getClass())
398 .add("FlowClassifierId", flowClassifierId)
399 .add("TenantId", tenantId)
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)