Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +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.openstacktelemetry.impl; |
| 17 | |
| 18 | import org.onlab.packet.IpPrefix; |
| 19 | import org.onlab.packet.MacAddress; |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 20 | import org.onlab.packet.TpPort; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.net.DeviceId; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
| 24 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
| 25 | |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Implementation class of FlowInfo. |
| 33 | */ |
| 34 | public final class DefaultFlowInfo implements FlowInfo { |
| 35 | |
| 36 | private final byte flowType; |
| 37 | private final DeviceId deviceId; |
| 38 | private final int inputInterfaceId; |
| 39 | private final int outputInterfaceId; |
| 40 | private final VlanId vlanId; |
| 41 | private final short vxlanId; |
| 42 | private final IpPrefix srcIp; |
| 43 | private final IpPrefix dstIp; |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 44 | private final TpPort srcPort; |
| 45 | private final TpPort dstPort; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 46 | private final byte protocol; |
| 47 | private final MacAddress srcMac; |
| 48 | private final MacAddress dstMac; |
| 49 | private final StatsInfo statsInfo; |
| 50 | |
| 51 | private DefaultFlowInfo(byte flowType, DeviceId deviceId, |
| 52 | int inputInterfaceId, int outputInterfaceId, |
| 53 | VlanId vlanId, short vxlanId, IpPrefix srcIp, |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 54 | IpPrefix dstIp, TpPort srcPort, TpPort dstPort, |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 55 | byte protocol, MacAddress srcMac, MacAddress dstMac, |
| 56 | StatsInfo statsInfo) { |
| 57 | this.flowType = flowType; |
| 58 | this.deviceId = deviceId; |
| 59 | this.inputInterfaceId = inputInterfaceId; |
| 60 | this.outputInterfaceId = outputInterfaceId; |
| 61 | this.vlanId = vlanId; |
| 62 | this.vxlanId = vxlanId; |
| 63 | this.srcIp = srcIp; |
| 64 | this.dstIp = dstIp; |
| 65 | this.srcPort = srcPort; |
| 66 | this.dstPort = dstPort; |
| 67 | this.protocol = protocol; |
| 68 | this.srcMac = srcMac; |
| 69 | this.dstMac = dstMac; |
| 70 | this.statsInfo = statsInfo; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public byte flowType() { |
| 75 | return flowType; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public DeviceId deviceId() { |
| 80 | return deviceId; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int inputInterfaceId() { |
| 85 | return inputInterfaceId; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int outputInterfaceId() { |
| 90 | return outputInterfaceId; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public VlanId vlanId() { |
| 95 | return vlanId; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public short vxlanId() { |
| 100 | return vxlanId; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public IpPrefix srcIp() { |
| 105 | return srcIp; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public IpPrefix dstIp() { |
| 110 | return dstIp; |
| 111 | } |
| 112 | |
| 113 | @Override |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 114 | public TpPort srcPort() { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 115 | return srcPort; |
| 116 | } |
| 117 | |
| 118 | @Override |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 119 | public TpPort dstPort() { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 120 | return dstPort; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public byte protocol() { |
| 125 | return protocol; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public MacAddress srcMac() { |
| 130 | return srcMac; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public MacAddress dstMac() { |
| 135 | return dstMac; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public StatsInfo statsInfo() { |
| 140 | return statsInfo; |
| 141 | } |
| 142 | |
| 143 | @Override |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame^] | 144 | public boolean roughEquals(FlowInfo flowInfo) { |
| 145 | return deviceId.equals(flowInfo.deviceId()) && |
| 146 | srcIp.equals(flowInfo.srcIp()) && |
| 147 | dstIp.equals(flowInfo.dstIp()) && |
| 148 | srcPort.equals(flowInfo.srcPort()) && |
| 149 | dstPort.equals(flowInfo.dstPort()) && |
| 150 | (protocol == flowInfo.protocol()); |
| 151 | } |
| 152 | |
| 153 | @Override |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 154 | public boolean equals(Object obj) { |
| 155 | if (this == obj) { |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | if (obj instanceof DefaultFlowInfo) { |
| 160 | final DefaultFlowInfo other = (DefaultFlowInfo) obj; |
| 161 | return Objects.equals(this.flowType, other.flowType) && |
| 162 | Objects.equals(this.deviceId, other.deviceId) && |
| 163 | Objects.equals(this.inputInterfaceId, other.inputInterfaceId) && |
| 164 | Objects.equals(this.outputInterfaceId, other.outputInterfaceId) && |
| 165 | Objects.equals(this.vlanId, other.vlanId) && |
| 166 | Objects.equals(this.vxlanId, other.vxlanId) && |
| 167 | Objects.equals(this.srcIp, other.srcIp) && |
| 168 | Objects.equals(this.dstIp, other.dstIp) && |
| 169 | Objects.equals(this.srcPort, other.srcPort) && |
| 170 | Objects.equals(this.dstPort, other.dstPort) && |
| 171 | Objects.equals(this.protocol, other.protocol) && |
| 172 | Objects.equals(this.srcMac, other.srcMac) && |
| 173 | Objects.equals(this.dstMac, other.dstMac) && |
| 174 | Objects.equals(this.statsInfo, other.statsInfo); |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int hashCode() { |
| 181 | return Objects.hash(flowType, deviceId, inputInterfaceId, |
| 182 | outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort, |
| 183 | protocol, srcMac, dstMac, statsInfo); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String toString() { |
| 188 | return toStringHelper(this) |
| 189 | .add("flowType", flowType) |
| 190 | .add("deviceId", deviceId) |
| 191 | .add("inputInterfaceId", inputInterfaceId) |
| 192 | .add("outputInterfaceId", outputInterfaceId) |
| 193 | .add("vlanId", vlanId) |
| 194 | .add("vxlanId", vxlanId) |
| 195 | .add("srcIp", srcIp) |
| 196 | .add("dstIp", dstIp) |
| 197 | .add("srcPort", srcPort) |
| 198 | .add("dstPort", dstPort) |
| 199 | .add("protocol", protocol) |
| 200 | .add("srcMac", srcMac) |
| 201 | .add("dstMac", dstMac) |
| 202 | .add("statsInfo", statsInfo) |
| 203 | .toString(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Builder class of DefaultFlowInfo. |
| 208 | */ |
| 209 | public static final class DefaultBuilder implements FlowInfo.Builder { |
| 210 | |
| 211 | private byte flowType; |
| 212 | private DeviceId deviceId; |
| 213 | private int inputInterfaceId; |
| 214 | private int outputInterfaceId; |
| 215 | private VlanId vlanId; |
| 216 | private short vxlanId; |
| 217 | private IpPrefix srcIp; |
| 218 | private IpPrefix dstIp; |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 219 | private TpPort srcPort; |
| 220 | private TpPort dstPort; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 221 | private byte protocol; |
| 222 | private MacAddress srcMac; |
| 223 | private MacAddress dstMac; |
| 224 | private StatsInfo statsInfo; |
| 225 | |
| 226 | @Override |
| 227 | public Builder withFlowType(byte flowType) { |
| 228 | this.flowType = flowType; |
| 229 | return this; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public Builder withDeviceId(DeviceId deviceId) { |
| 234 | this.deviceId = deviceId; |
| 235 | return this; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public Builder withInputInterfaceId(int inputInterfaceId) { |
| 240 | this.inputInterfaceId = inputInterfaceId; |
| 241 | return this; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Builder withOutputInterfaceId(int outputInterfaceId) { |
| 246 | this.outputInterfaceId = outputInterfaceId; |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Builder withVlanId(VlanId vlanId) { |
| 252 | this.vlanId = vlanId; |
| 253 | return this; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public Builder withVxlanId(short vxlanId) { |
| 258 | this.vxlanId = vxlanId; |
| 259 | return this; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Builder withSrcIp(IpPrefix srcIp) { |
| 264 | this.srcIp = srcIp; |
| 265 | return this; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public Builder withDstIp(IpPrefix dstIp) { |
| 270 | this.dstIp = dstIp; |
| 271 | return this; |
| 272 | } |
| 273 | |
| 274 | @Override |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 275 | public Builder withSrcPort(TpPort srcPort) { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 276 | this.srcPort = srcPort; |
| 277 | return this; |
| 278 | } |
| 279 | |
| 280 | @Override |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 281 | public Builder withDstPort(TpPort dstPort) { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 282 | this.dstPort = dstPort; |
| 283 | return this; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public Builder withProtocol(byte protocol) { |
| 288 | this.protocol = protocol; |
| 289 | return this; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public Builder withSrcMac(MacAddress srcMac) { |
| 294 | this.srcMac = srcMac; |
| 295 | return this; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public Builder withDstMac(MacAddress dstMac) { |
| 300 | this.dstMac = dstMac; |
| 301 | return this; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public Builder withStatsInfo(StatsInfo statsInfo) { |
| 306 | this.statsInfo = statsInfo; |
| 307 | return this; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public FlowInfo build() { |
| 312 | |
| 313 | // TODO: need to check the null value for more properties |
| 314 | checkNotNull(srcIp, "Source IP address cannot be null"); |
| 315 | checkNotNull(dstIp, "Destination IP address cannot be null"); |
| 316 | checkNotNull(statsInfo, "StatsInfo cannot be null"); |
| 317 | |
| 318 | return new DefaultFlowInfo(flowType, deviceId, inputInterfaceId, |
| 319 | outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort, |
| 320 | protocol, srcMac, dstMac, statsInfo); |
| 321 | } |
| 322 | } |
| 323 | } |