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