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