blob: 85601706d1adbb53b6d997b44958edeaa7e47601 [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 }
boyoung27b444122018-09-01 17:28:13 +0900199 return srcIp.toString() + ":" +
200 ((srcPort == null) ? "any" : srcPort.toString()) +
201 " -> " +
202 dstIp.toString() + ":" +
203 ((dstPort == null) ? "any" : dstPort.toString());
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900204 }
205
206 @Override
Jian Li0a5d4d22018-06-08 14:06:56 +0900207 public String toString() {
208 return toStringHelper(this)
209 .add("flowType", flowType)
210 .add("deviceId", deviceId)
211 .add("inputInterfaceId", inputInterfaceId)
212 .add("outputInterfaceId", outputInterfaceId)
213 .add("vlanId", vlanId)
214 .add("vxlanId", vxlanId)
215 .add("srcIp", srcIp)
216 .add("dstIp", dstIp)
217 .add("srcPort", srcPort)
218 .add("dstPort", dstPort)
219 .add("protocol", protocol)
220 .add("srcMac", srcMac)
221 .add("dstMac", dstMac)
222 .add("statsInfo", statsInfo)
223 .toString();
224 }
225
226 /**
Jian Liae3fcff2018-07-30 11:55:44 +0900227 * Obtains a default flow info builder object.
228 *
229 * @return flow info builder object
230 */
231 public static Builder builder() {
232 return new DefaultBuilder();
233 }
234
235 /**
Jian Li0a5d4d22018-06-08 14:06:56 +0900236 * Builder class of DefaultFlowInfo.
237 */
238 public static final class DefaultBuilder implements FlowInfo.Builder {
239
240 private byte flowType;
241 private DeviceId deviceId;
242 private int inputInterfaceId;
243 private int outputInterfaceId;
244 private VlanId vlanId;
245 private short vxlanId;
246 private IpPrefix srcIp;
247 private IpPrefix dstIp;
Jian Li6bd35102018-06-09 00:18:47 +0900248 private TpPort srcPort;
249 private TpPort dstPort;
Jian Li0a5d4d22018-06-08 14:06:56 +0900250 private byte protocol;
251 private MacAddress srcMac;
252 private MacAddress dstMac;
253 private StatsInfo statsInfo;
254
255 @Override
256 public Builder withFlowType(byte flowType) {
257 this.flowType = flowType;
258 return this;
259 }
260
261 @Override
262 public Builder withDeviceId(DeviceId deviceId) {
263 this.deviceId = deviceId;
264 return this;
265 }
266
267 @Override
268 public Builder withInputInterfaceId(int inputInterfaceId) {
269 this.inputInterfaceId = inputInterfaceId;
270 return this;
271 }
272
273 @Override
274 public Builder withOutputInterfaceId(int outputInterfaceId) {
275 this.outputInterfaceId = outputInterfaceId;
276 return this;
277 }
278
279 @Override
280 public Builder withVlanId(VlanId vlanId) {
281 this.vlanId = vlanId;
282 return this;
283 }
284
285 @Override
286 public Builder withVxlanId(short vxlanId) {
287 this.vxlanId = vxlanId;
288 return this;
289 }
290
291 @Override
292 public Builder withSrcIp(IpPrefix srcIp) {
293 this.srcIp = srcIp;
294 return this;
295 }
296
297 @Override
298 public Builder withDstIp(IpPrefix dstIp) {
299 this.dstIp = dstIp;
300 return this;
301 }
302
303 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900304 public Builder withSrcPort(TpPort srcPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900305 this.srcPort = srcPort;
306 return this;
307 }
308
309 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900310 public Builder withDstPort(TpPort dstPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900311 this.dstPort = dstPort;
312 return this;
313 }
314
315 @Override
316 public Builder withProtocol(byte protocol) {
317 this.protocol = protocol;
318 return this;
319 }
320
321 @Override
322 public Builder withSrcMac(MacAddress srcMac) {
323 this.srcMac = srcMac;
324 return this;
325 }
326
327 @Override
328 public Builder withDstMac(MacAddress dstMac) {
329 this.dstMac = dstMac;
330 return this;
331 }
332
333 @Override
334 public Builder withStatsInfo(StatsInfo statsInfo) {
335 this.statsInfo = statsInfo;
336 return this;
337 }
338
339 @Override
340 public FlowInfo build() {
341
342 // TODO: need to check the null value for more properties
343 checkNotNull(srcIp, "Source IP address cannot be null");
344 checkNotNull(dstIp, "Destination IP address cannot be null");
345 checkNotNull(statsInfo, "StatsInfo cannot be null");
346
347 return new DefaultFlowInfo(flowType, deviceId, inputInterfaceId,
348 outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort,
349 protocol, srcMac, dstMac, statsInfo);
350 }
351 }
352}