Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstackvtap.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of an immutable openstack vTap criterion. |
| 28 | */ |
| 29 | public final class DefaultOpenstackVtapCriterion implements OpenstackVtapCriterion { |
| 30 | private final IpPrefix srcIpPrefix; |
| 31 | private final IpPrefix dstIpPrefix; |
| 32 | private final byte ipProtocol; |
| 33 | private final TpPort srcTpPort; |
| 34 | private final TpPort dstTpPort; |
| 35 | |
| 36 | private static final String NOT_NULL_MSG = "Element % cannot be null"; |
| 37 | |
| 38 | // private constructor not intended to use from external |
| 39 | private DefaultOpenstackVtapCriterion(IpPrefix srcIpPrefix, |
| 40 | IpPrefix dstIpPrefix, |
| 41 | byte ipProtocol, |
| 42 | TpPort srcTpPort, |
| 43 | TpPort dstTpPort) { |
| 44 | this.srcIpPrefix = srcIpPrefix; |
| 45 | this.dstIpPrefix = dstIpPrefix; |
| 46 | this.ipProtocol = ipProtocol; |
| 47 | this.srcTpPort = srcTpPort; |
| 48 | this.dstTpPort = dstTpPort; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public IpPrefix srcIpPrefix() { |
| 53 | return srcIpPrefix; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public IpPrefix dstIpPrefix() { |
| 58 | return dstIpPrefix; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public byte ipProtocol() { |
| 63 | return ipProtocol; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public TpPort srcTpPort() { |
| 68 | return srcTpPort; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public TpPort dstTpPort() { |
| 73 | return dstTpPort; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(getClass()) |
| 79 | .add("srcIpPrefix", srcIpPrefix) |
| 80 | .add("dstIpPrefix", dstIpPrefix) |
| 81 | .add("ipProtocol", ipProtocol) |
| 82 | .add("srcTpPort", srcTpPort) |
| 83 | .add("dstTpPort", dstTpPort) |
| 84 | .toString(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 89 | return Objects.hashCode(srcIpPrefix, dstIpPrefix, |
| 90 | ipProtocol, srcTpPort, dstTpPort); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 94 | public boolean equals(Object o) { |
| 95 | if (this == o) { |
| 96 | return true; |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 97 | } |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 98 | if (o == null || getClass() != o.getClass()) { |
| 99 | return false; |
| 100 | } |
| 101 | DefaultOpenstackVtapCriterion that = (DefaultOpenstackVtapCriterion) o; |
| 102 | return Objects.equal(this.srcIpPrefix, that.srcIpPrefix) && |
| 103 | Objects.equal(this.dstIpPrefix, that.dstIpPrefix) && |
| 104 | Objects.equal(this.ipProtocol, that.ipProtocol) && |
| 105 | Objects.equal(this.srcTpPort, that.srcTpPort) && |
| 106 | Objects.equal(this.dstTpPort, that.dstTpPort); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a new default openstack vTap criterion builder. |
| 111 | * |
| 112 | * @return default openstack vTap criterion builder |
| 113 | */ |
| 114 | public static Builder builder() { |
| 115 | return new Builder(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * A builder class for openstack vTap criterion builder. |
| 120 | */ |
| 121 | public static final class Builder implements OpenstackVtapCriterion.Builder { |
| 122 | private IpPrefix srcIpPrefix; |
| 123 | private IpPrefix dstIpPrefix; |
| 124 | private byte ipProtocol; |
| 125 | private TpPort srcTpPort; |
| 126 | private TpPort dstTpPort; |
| 127 | |
| 128 | // private constructor not intended to use from external |
| 129 | Builder() { |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public DefaultOpenstackVtapCriterion build() { |
| 134 | checkArgument(srcIpPrefix != null, NOT_NULL_MSG, "Source IP Prefix"); |
| 135 | checkArgument(dstIpPrefix != null, NOT_NULL_MSG, "Destination IP Prefix"); |
| 136 | |
| 137 | return new DefaultOpenstackVtapCriterion(srcIpPrefix, dstIpPrefix, |
| 138 | ipProtocol, srcTpPort, dstTpPort); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Builder srcIpPrefix(IpPrefix srcIpPrefix) { |
| 143 | this.srcIpPrefix = srcIpPrefix; |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Builder dstIpPrefix(IpPrefix dstIpPrefix) { |
| 149 | this.dstIpPrefix = dstIpPrefix; |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Builder ipProtocol(byte ipProtocol) { |
| 155 | this.ipProtocol = ipProtocol; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public Builder srcTpPort(TpPort srcTpPort) { |
| 161 | this.srcTpPort = srcTpPort; |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Builder dstTpPort(TpPort dstTpPort) { |
| 167 | this.dstTpPort = dstTpPort; |
| 168 | return this; |
| 169 | } |
| 170 | } |
| 171 | } |