blob: b0d3aad0230c54f079c073126daaaabc64283ad3 [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;
boyoung2a8549d22018-11-23 20:42:37 +090030import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getProtocolNameFromType;
Jian Li0a5d4d22018-06-08 14:06:56 +090031
32/**
33 * Implementation class of FlowInfo.
34 */
35public final class DefaultFlowInfo implements FlowInfo {
Boyoung Jeong1cca5e82018-08-01 21:00:08 +090036 private static final String INGRESS_STATS = "Ingress Port :";
37 private static final String EGRESS_STATS = "Egress Port :";
Jian Li0a5d4d22018-06-08 14:06:56 +090038
39 private final byte flowType;
40 private final DeviceId deviceId;
41 private final int inputInterfaceId;
42 private final int outputInterfaceId;
43 private final VlanId vlanId;
44 private final short vxlanId;
45 private final IpPrefix srcIp;
46 private final IpPrefix dstIp;
Jian Li6bd35102018-06-09 00:18:47 +090047 private final TpPort srcPort;
48 private final TpPort dstPort;
Jian Li0a5d4d22018-06-08 14:06:56 +090049 private final byte protocol;
50 private final MacAddress srcMac;
51 private final MacAddress dstMac;
52 private final StatsInfo statsInfo;
53
54 private DefaultFlowInfo(byte flowType, DeviceId deviceId,
55 int inputInterfaceId, int outputInterfaceId,
56 VlanId vlanId, short vxlanId, IpPrefix srcIp,
Jian Li6bd35102018-06-09 00:18:47 +090057 IpPrefix dstIp, TpPort srcPort, TpPort dstPort,
Jian Li0a5d4d22018-06-08 14:06:56 +090058 byte protocol, MacAddress srcMac, MacAddress dstMac,
59 StatsInfo statsInfo) {
60 this.flowType = flowType;
61 this.deviceId = deviceId;
62 this.inputInterfaceId = inputInterfaceId;
63 this.outputInterfaceId = outputInterfaceId;
64 this.vlanId = vlanId;
65 this.vxlanId = vxlanId;
66 this.srcIp = srcIp;
67 this.dstIp = dstIp;
68 this.srcPort = srcPort;
69 this.dstPort = dstPort;
70 this.protocol = protocol;
71 this.srcMac = srcMac;
72 this.dstMac = dstMac;
73 this.statsInfo = statsInfo;
74 }
75
76 @Override
77 public byte flowType() {
78 return flowType;
79 }
80
81 @Override
82 public DeviceId deviceId() {
83 return deviceId;
84 }
85
86 @Override
87 public int inputInterfaceId() {
88 return inputInterfaceId;
89 }
90
91 @Override
92 public int outputInterfaceId() {
93 return outputInterfaceId;
94 }
95
96 @Override
97 public VlanId vlanId() {
98 return vlanId;
99 }
100
101 @Override
102 public short vxlanId() {
103 return vxlanId;
104 }
105
106 @Override
107 public IpPrefix srcIp() {
108 return srcIp;
109 }
110
111 @Override
112 public IpPrefix dstIp() {
113 return dstIp;
114 }
115
116 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900117 public TpPort srcPort() {
Jian Li0a5d4d22018-06-08 14:06:56 +0900118 return srcPort;
119 }
120
121 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900122 public TpPort dstPort() {
Jian Li0a5d4d22018-06-08 14:06:56 +0900123 return dstPort;
124 }
125
126 @Override
127 public byte protocol() {
128 return protocol;
129 }
130
131 @Override
132 public MacAddress srcMac() {
133 return srcMac;
134 }
135
136 @Override
137 public MacAddress dstMac() {
138 return dstMac;
139 }
140
141 @Override
142 public StatsInfo statsInfo() {
143 return statsInfo;
144 }
145
146 @Override
Jian Li0bbbb1c2018-06-22 22:01:17 +0900147 public boolean roughEquals(FlowInfo flowInfo) {
Jian Li85573f42018-06-27 22:29:14 +0900148 final DefaultFlowInfo other = (DefaultFlowInfo) flowInfo;
149 return Objects.equals(this.deviceId, other.deviceId) &&
150 Objects.equals(this.srcIp, other.srcIp) &&
151 Objects.equals(this.dstIp, other.dstIp) &&
152 Objects.equals(this.srcPort, other.srcPort) &&
153 Objects.equals(this.dstPort, other.dstPort) &&
154 Objects.equals(this.protocol, other.protocol);
Jian Li0bbbb1c2018-06-22 22:01:17 +0900155 }
156
157 @Override
Jian Li0a5d4d22018-06-08 14:06:56 +0900158 public boolean equals(Object obj) {
159 if (this == obj) {
160 return true;
161 }
162
163 if (obj instanceof DefaultFlowInfo) {
164 final DefaultFlowInfo other = (DefaultFlowInfo) obj;
165 return Objects.equals(this.flowType, other.flowType) &&
166 Objects.equals(this.deviceId, other.deviceId) &&
167 Objects.equals(this.inputInterfaceId, other.inputInterfaceId) &&
168 Objects.equals(this.outputInterfaceId, other.outputInterfaceId) &&
169 Objects.equals(this.vlanId, other.vlanId) &&
170 Objects.equals(this.vxlanId, other.vxlanId) &&
171 Objects.equals(this.srcIp, other.srcIp) &&
172 Objects.equals(this.dstIp, other.dstIp) &&
173 Objects.equals(this.srcPort, other.srcPort) &&
174 Objects.equals(this.dstPort, other.dstPort) &&
175 Objects.equals(this.protocol, other.protocol) &&
176 Objects.equals(this.srcMac, other.srcMac) &&
177 Objects.equals(this.dstMac, other.dstMac) &&
178 Objects.equals(this.statsInfo, other.statsInfo);
179 }
180 return false;
181 }
182
183 @Override
184 public int hashCode() {
185 return Objects.hash(flowType, deviceId, inputInterfaceId,
186 outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort,
187 protocol, srcMac, dstMac, statsInfo);
188 }
189
190 @Override
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900191 public String uniqueFlowInfoKey() {
192 if (srcIp.address().isZero() || dstIp.address().isZero()) {
193 if (!srcIp.address().isZero()) {
194 return INGRESS_STATS + srcIp.toString();
195 }
196 if (!dstIp.address().isZero()) {
197 return EGRESS_STATS + dstIp.toString();
198 }
199 }
boyoung27b444122018-09-01 17:28:13 +0900200 return srcIp.toString() + ":" +
201 ((srcPort == null) ? "any" : srcPort.toString()) +
202 " -> " +
203 dstIp.toString() + ":" +
boyoung2a8549d22018-11-23 20:42:37 +0900204 ((dstPort == null) ? "any" : dstPort.toString()) +
205 " (" + getProtocolNameFromType(protocol) + ")";
Boyoung Jeong1cca5e82018-08-01 21:00:08 +0900206 }
207
208 @Override
Jian Li0a5d4d22018-06-08 14:06:56 +0900209 public String toString() {
210 return toStringHelper(this)
211 .add("flowType", flowType)
212 .add("deviceId", deviceId)
213 .add("inputInterfaceId", inputInterfaceId)
214 .add("outputInterfaceId", outputInterfaceId)
215 .add("vlanId", vlanId)
216 .add("vxlanId", vxlanId)
217 .add("srcIp", srcIp)
218 .add("dstIp", dstIp)
219 .add("srcPort", srcPort)
220 .add("dstPort", dstPort)
221 .add("protocol", protocol)
222 .add("srcMac", srcMac)
223 .add("dstMac", dstMac)
224 .add("statsInfo", statsInfo)
225 .toString();
226 }
227
228 /**
Jian Liae3fcff2018-07-30 11:55:44 +0900229 * Obtains a default flow info builder object.
230 *
231 * @return flow info builder object
232 */
233 public static Builder builder() {
234 return new DefaultBuilder();
235 }
236
237 /**
Jian Li0a5d4d22018-06-08 14:06:56 +0900238 * Builder class of DefaultFlowInfo.
239 */
240 public static final class DefaultBuilder implements FlowInfo.Builder {
241
242 private byte flowType;
243 private DeviceId deviceId;
244 private int inputInterfaceId;
245 private int outputInterfaceId;
246 private VlanId vlanId;
247 private short vxlanId;
248 private IpPrefix srcIp;
249 private IpPrefix dstIp;
Jian Li6bd35102018-06-09 00:18:47 +0900250 private TpPort srcPort;
251 private TpPort dstPort;
Jian Li0a5d4d22018-06-08 14:06:56 +0900252 private byte protocol;
253 private MacAddress srcMac;
254 private MacAddress dstMac;
255 private StatsInfo statsInfo;
256
257 @Override
258 public Builder withFlowType(byte flowType) {
259 this.flowType = flowType;
260 return this;
261 }
262
263 @Override
264 public Builder withDeviceId(DeviceId deviceId) {
265 this.deviceId = deviceId;
266 return this;
267 }
268
269 @Override
270 public Builder withInputInterfaceId(int inputInterfaceId) {
271 this.inputInterfaceId = inputInterfaceId;
272 return this;
273 }
274
275 @Override
276 public Builder withOutputInterfaceId(int outputInterfaceId) {
277 this.outputInterfaceId = outputInterfaceId;
278 return this;
279 }
280
281 @Override
282 public Builder withVlanId(VlanId vlanId) {
283 this.vlanId = vlanId;
284 return this;
285 }
286
287 @Override
288 public Builder withVxlanId(short vxlanId) {
289 this.vxlanId = vxlanId;
290 return this;
291 }
292
293 @Override
294 public Builder withSrcIp(IpPrefix srcIp) {
295 this.srcIp = srcIp;
296 return this;
297 }
298
299 @Override
300 public Builder withDstIp(IpPrefix dstIp) {
301 this.dstIp = dstIp;
302 return this;
303 }
304
305 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900306 public Builder withSrcPort(TpPort srcPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900307 this.srcPort = srcPort;
308 return this;
309 }
310
311 @Override
Jian Li6bd35102018-06-09 00:18:47 +0900312 public Builder withDstPort(TpPort dstPort) {
Jian Li0a5d4d22018-06-08 14:06:56 +0900313 this.dstPort = dstPort;
314 return this;
315 }
316
317 @Override
318 public Builder withProtocol(byte protocol) {
319 this.protocol = protocol;
320 return this;
321 }
322
323 @Override
324 public Builder withSrcMac(MacAddress srcMac) {
325 this.srcMac = srcMac;
326 return this;
327 }
328
329 @Override
330 public Builder withDstMac(MacAddress dstMac) {
331 this.dstMac = dstMac;
332 return this;
333 }
334
335 @Override
336 public Builder withStatsInfo(StatsInfo statsInfo) {
337 this.statsInfo = statsInfo;
338 return this;
339 }
340
341 @Override
342 public FlowInfo build() {
343
344 // TODO: need to check the null value for more properties
345 checkNotNull(srcIp, "Source IP address cannot be null");
346 checkNotNull(dstIp, "Destination IP address cannot be null");
347 checkNotNull(statsInfo, "StatsInfo cannot be null");
348
349 return new DefaultFlowInfo(flowType, deviceId, inputInterfaceId,
350 outputInterfaceId, vlanId, vxlanId, srcIp, dstIp, srcPort, dstPort,
351 protocol, srcMac, dstMac, statsInfo);
352 }
353 }
354}