blob: 2f670504bdc8d1a414587c378de765c8b65f8a43 [file] [log] [blame]
Jian Li0a5d4d22018-06-08 14:06:56 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import org.onlab.packet.IpPrefix;
19import org.onlab.packet.MacAddress;
Jian Li6bd35102018-06-09 00:18:47 +090020import org.onlab.packet.TpPort;
Jian Li0a5d4d22018-06-08 14:06:56 +090021import org.onlab.packet.VlanId;
22import org.onosproject.net.DeviceId;
Jian Li0a5d4d22018-06-08 14:06:56 +090023import org.onosproject.openstacktelemetry.api.FlowInfo;
24import org.onosproject.openstacktelemetry.api.StatsInfo;
25
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Implementation class of FlowInfo.
33 */
34public final class DefaultFlowInfo implements FlowInfo {
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090035 private static final String INGRESS_STATS = "Ingress Port :";
36 private static final String EGRESS_STATS = "Egress Port :";
Jian Li0a5d4d22018-06-08 14:06:56 +090037
38 private final byte flowType;
39 private final DeviceId deviceId;
40 private final int inputInterfaceId;
41 private final int outputInterfaceId;
42 private final VlanId vlanId;
43 private final short vxlanId;
44 private final IpPrefix srcIp;
45 private final IpPrefix dstIp;
Jian Li6bd35102018-06-09 00:18:47 +090046 private final TpPort srcPort;
47 private final TpPort dstPort;
Jian Li0a5d4d22018-06-08 14:06:56 +090048 private final byte protocol;
49 private final MacAddress srcMac;
50 private final MacAddress dstMac;
51 private final StatsInfo statsInfo;
52
53 private DefaultFlowInfo(byte flowType, DeviceId deviceId,
54 int inputInterfaceId, int outputInterfaceId,
55 VlanId vlanId, short vxlanId, IpPrefix srcIp,
Jian Li6bd35102018-06-09 00:18:47 +090056 IpPrefix dstIp, TpPort srcPort, TpPort dstPort,
Jian Li0a5d4d22018-06-08 14:06:56 +090057 byte protocol, MacAddress srcMac, MacAddress dstMac,
58 StatsInfo statsInfo) {
59 this.flowType = flowType;
60 this.deviceId = deviceId;
61 this.inputInterfaceId = inputInterfaceId;
62 this.outputInterfaceId = outputInterfaceId;
63 this.vlanId = vlanId;
64 this.vxlanId = vxlanId;
65 this.srcIp = srcIp;
66 this.dstIp = dstIp;
67 this.srcPort = srcPort;
68 this.dstPort = dstPort;
69 this.protocol = protocol;
70 this.srcMac = srcMac;
71 this.dstMac = dstMac;
72 this.statsInfo = statsInfo;
73 }
74
75 @Override
76 public byte flowType() {
77 return flowType;
78 }
79
80 @Override
81 public DeviceId deviceId() {
82 return deviceId;
83 }
84
85 @Override
86 public int inputInterfaceId() {
87 return inputInterfaceId;
88 }
89
90 @Override
91 public int outputInterfaceId() {
92 return outputInterfaceId;
93 }
94
95 @Override
96 public VlanId vlanId() {
97 return vlanId;
98 }
99
100 @Override
101 public short vxlanId() {
102 return vxlanId;
103 }
104
105 @Override
106 public IpPrefix srcIp() {
107 return srcIp;
108 }
109
110 @Override
111 public IpPrefix dstIp() {
112 return dstIp;
113 }
114
115 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900116 public TpPort srcPort() {
Jian Li0a5d4d22018-06-08 14:06:56 +0900117 return srcPort;
118 }
119
120 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900121 public TpPort dstPort() {
Jian Li0a5d4d22018-06-08 14:06:56 +0900122 return dstPort;
123 }
124
125 @Override
126 public byte protocol() {
127 return protocol;
128 }
129
130 @Override
131 public MacAddress srcMac() {
132 return srcMac;
133 }
134
135 @Override
136 public MacAddress dstMac() {
137 return dstMac;
138 }
139
140 @Override
141 public StatsInfo statsInfo() {
142 return statsInfo;
143 }
144
145 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +0900146 public boolean roughEquals(FlowInfo flowInfo) {
Jian Li85573f42018-06-27 22:29:14 +0900147 final DefaultFlowInfo other = (DefaultFlowInfo) flowInfo;
148 return Objects.equals(this.deviceId, other.deviceId) &&
149 Objects.equals(this.srcIp, other.srcIp) &&
150 Objects.equals(this.dstIp, other.dstIp) &&
151 Objects.equals(this.srcPort, other.srcPort) &&
152 Objects.equals(this.dstPort, other.dstPort) &&
153 Objects.equals(this.protocol, other.protocol);
Jian Li0bbbb1c2018-06-22 22:01:17 +0900154 }
155
156 @Override
Jian Li0a5d4d22018-06-08 14:06:56 +0900157 public boolean equals(Object obj) {
158 if (this == obj) {
159 return true;
160 }
161
162 if (obj instanceof DefaultFlowInfo) {
163 final DefaultFlowInfo other = (DefaultFlowInfo) obj;
164 return Objects.equals(this.flowType, other.flowType) &&
165 Objects.equals(this.deviceId, other.deviceId) &&
166 Objects.equals(this.inputInterfaceId, other.inputInterfaceId) &&
167 Objects.equals(this.outputInterfaceId, other.outputInterfaceId) &&
168 Objects.equals(this.vlanId, other.vlanId) &&
169 Objects.equals(this.vxlanId, other.vxlanId) &&
170 Objects.equals(this.srcIp, other.srcIp) &&
171 Objects.equals(this.dstIp, other.dstIp) &&
172 Objects.equals(this.srcPort, other.srcPort) &&
173 Objects.equals(this.dstPort, other.dstPort) &&
174 Objects.equals(this.protocol, other.protocol) &&
175 Objects.equals(this.srcMac, other.srcMac) &&
176 Objects.equals(this.dstMac, other.dstMac) &&
177 Objects.equals(this.statsInfo, other.statsInfo);
178 }
179 return false;
180 }
181
182 @Override
183 public int hashCode() {
184 return Objects.hash(flowType, deviceId, inputInterfaceId,
185 outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort,
186 protocol, srcMac, dstMac, statsInfo);
187 }
188
189 @Override
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900190 public String uniqueFlowInfoKey() {
191 if (srcIp.address().isZero() || dstIp.address().isZero()) {
192 if (!srcIp.address().isZero()) {
193 return INGRESS_STATS + srcIp.toString();
194 }
195 if (!dstIp.address().isZero()) {
196 return EGRESS_STATS + dstIp.toString();
197 }
198 }
199 return srcIp.toString() + ":" + srcPort.toString() + " -> " +
200 dstIp.toString() + ":" + dstPort.toString();
201 }
202
203 @Override
Jian Li0a5d4d22018-06-08 14:06:56 +0900204 public String toString() {
205 return toStringHelper(this)
206 .add("flowType", flowType)
207 .add("deviceId", deviceId)
208 .add("inputInterfaceId", inputInterfaceId)
209 .add("outputInterfaceId", outputInterfaceId)
210 .add("vlanId", vlanId)
211 .add("vxlanId", vxlanId)
212 .add("srcIp", srcIp)
213 .add("dstIp", dstIp)
214 .add("srcPort", srcPort)
215 .add("dstPort", dstPort)
216 .add("protocol", protocol)
217 .add("srcMac", srcMac)
218 .add("dstMac", dstMac)
219 .add("statsInfo", statsInfo)
220 .toString();
221 }
222
223 /**
Jian Liae3fcff2018-07-30 11:55:44 +0900224 * Obtains a default flow info builder object.
225 *
226 * @return flow info builder object
227 */
228 public static Builder builder() {
229 return new DefaultBuilder();
230 }
231
232 /**
Jian Li0a5d4d22018-06-08 14:06:56 +0900233 * Builder class of DefaultFlowInfo.
234 */
235 public static final class DefaultBuilder implements FlowInfo.Builder {
236
237 private byte flowType;
238 private DeviceId deviceId;
239 private int inputInterfaceId;
240 private int outputInterfaceId;
241 private VlanId vlanId;
242 private short vxlanId;
243 private IpPrefix srcIp;
244 private IpPrefix dstIp;
Jian Li6bd35102018-06-09 00:18:47 +0900245 private TpPort srcPort;
246 private TpPort dstPort;
Jian Li0a5d4d22018-06-08 14:06:56 +0900247 private byte protocol;
248 private MacAddress srcMac;
249 private MacAddress dstMac;
250 private StatsInfo statsInfo;
251
252 @Override
253 public Builder withFlowType(byte flowType) {
254 this.flowType = flowType;
255 return this;
256 }
257
258 @Override
259 public Builder withDeviceId(DeviceId deviceId) {
260 this.deviceId = deviceId;
261 return this;
262 }
263
264 @Override
265 public Builder withInputInterfaceId(int inputInterfaceId) {
266 this.inputInterfaceId = inputInterfaceId;
267 return this;
268 }
269
270 @Override
271 public Builder withOutputInterfaceId(int outputInterfaceId) {
272 this.outputInterfaceId = outputInterfaceId;
273 return this;
274 }
275
276 @Override
277 public Builder withVlanId(VlanId vlanId) {
278 this.vlanId = vlanId;
279 return this;
280 }
281
282 @Override
283 public Builder withVxlanId(short vxlanId) {
284 this.vxlanId = vxlanId;
285 return this;
286 }
287
288 @Override
289 public Builder withSrcIp(IpPrefix srcIp) {
290 this.srcIp = srcIp;
291 return this;
292 }
293
294 @Override
295 public Builder withDstIp(IpPrefix dstIp) {
296 this.dstIp = dstIp;
297 return this;
298 }
299
300 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900301 public Builder withSrcPort(TpPort srcPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900302 this.srcPort = srcPort;
303 return this;
304 }
305
306 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900307 public Builder withDstPort(TpPort dstPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900308 this.dstPort = dstPort;
309 return this;
310 }
311
312 @Override
313 public Builder withProtocol(byte protocol) {
314 this.protocol = protocol;
315 return this;
316 }
317
318 @Override
319 public Builder withSrcMac(MacAddress srcMac) {
320 this.srcMac = srcMac;
321 return this;
322 }
323
324 @Override
325 public Builder withDstMac(MacAddress dstMac) {
326 this.dstMac = dstMac;
327 return this;
328 }
329
330 @Override
331 public Builder withStatsInfo(StatsInfo statsInfo) {
332 this.statsInfo = statsInfo;
333 return this;
334 }
335
336 @Override
337 public FlowInfo build() {
338
339 // TODO: need to check the null value for more properties
340 checkNotNull(srcIp, "Source IP address cannot be null");
341 checkNotNull(dstIp, "Destination IP address cannot be null");
342 checkNotNull(statsInfo, "StatsInfo cannot be null");
343
344 return new DefaultFlowInfo(flowType, deviceId, inputInterfaceId,
345 outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort,
346 protocol, srcMac, dstMac, statsInfo);
347 }
348 }
349}