blob: eb41a04cdd200fda7071f354022be3902c06181e [file] [log] [blame]
Daniele Moro5e66f982021-06-11 16:41:48 +02001/*
2 * Copyright 2021-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
17package org.onosproject.net.behaviour.upf;
18
19
tosinski36ba33a2021-11-22 16:53:00 +010020import com.google.common.annotations.Beta;
21
Daniele Morof3a5ab02022-01-26 16:47:08 +010022import java.util.Objects;
23
Daniele Moro5e66f982021-06-11 16:41:48 +020024import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
tosinski36ba33a2021-11-22 16:53:00 +010027 * A structure for compactly passing UPF counter values for a given counter ID.
28 * Contains four counts: Ingress Packets, Ingress Bytes, Egress Packets, Egress Bytes.
29 * UpfCounter can be used ONLY on {@code apply} and {@code readAll} calls in the
30 * {@link UpfDevice} interface.
Daniele Moro5e66f982021-06-11 16:41:48 +020031 */
tosinski36ba33a2021-11-22 16:53:00 +010032@Beta
33public final class UpfCounter implements UpfEntity {
Daniele Moro5e66f982021-06-11 16:41:48 +020034 private final int cellId;
35 private final long ingressPkts;
36 private final long ingressBytes;
37 private final long egressPkts;
38 private final long egressBytes;
39
tosinski36ba33a2021-11-22 16:53:00 +010040 private UpfCounter(int cellId, long ingressPkts, long ingressBytes,
41 long egressPkts, long egressBytes) {
Daniele Moro5e66f982021-06-11 16:41:48 +020042 this.cellId = cellId;
43 this.ingressPkts = ingressPkts;
44 this.ingressBytes = ingressBytes;
45 this.egressPkts = egressPkts;
46 this.egressBytes = egressBytes;
47 }
48
49 public static Builder builder() {
50 return new Builder();
51 }
52
53 @Override
54 public String toString() {
Daniele Morof3a5ab02022-01-26 16:47:08 +010055 return String.format("UpfStats(cell_id=%d, ingress=(%dpkts,%dbytes), egress=(%dpkts,%dbytes))",
Daniele Moro5e66f982021-06-11 16:41:48 +020056 cellId, ingressPkts, ingressBytes, egressPkts, egressBytes);
57 }
58
Daniele Morof3a5ab02022-01-26 16:47:08 +010059 @Override
60 public boolean equals(Object object) {
61 if (object == this) {
62 return true;
63 }
64 if (object == null) {
65 return false;
66 }
67 if (getClass() != object.getClass()) {
68 return false;
69 }
70 UpfCounter that = (UpfCounter) object;
71 return this.cellId == that.cellId;
72 }
73
74 /**
75 * Returns whether this UpfCounter is exactly equal to the given UpfCounter,
76 * including their packets and bytes values.
77 *
78 * @param that other {@link UpfCounter} instance to compare
79 * @return true if exactly equals, false otherwise
80 */
81 public boolean exactlyEquals(UpfCounter that) {
82 return this.equals(that) &&
83 this.ingressPkts == that.ingressPkts &&
84 this.ingressBytes == that.ingressBytes &&
85 this.egressPkts == that.egressPkts &&
86 this.egressBytes == that.egressBytes;
87 }
88
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(cellId);
93 }
94
Daniele Moro5e66f982021-06-11 16:41:48 +020095 /**
tosinski36ba33a2021-11-22 16:53:00 +010096 * Get the cell ID (index) of the dataplane counter that produced this set of stats.
Daniele Moro5e66f982021-06-11 16:41:48 +020097 *
98 * @return counter cell ID
99 */
100 public int getCellId() {
101 return cellId;
102 }
103
104 /**
105 * Get the number of packets that hit this counter in the dataplane ingress pipeline.
106 *
107 * @return ingress packet count
108 */
109 public long getIngressPkts() {
110 return ingressPkts;
111 }
112
113 /**
114 * Get the number of packets that hit this counter in the dataplane egress pipeline.
115 *
116 * @return egress packet count
117 */
118 public long getEgressPkts() {
119 return egressPkts;
120 }
121
122 /**
123 * Get the number of packet bytes that hit this counter in the dataplane ingress pipeline.
124 *
125 * @return ingress byte count
126 */
127 public long getIngressBytes() {
128 return ingressBytes;
129 }
130
131 /**
132 * Get the number of packet bytes that hit this counter in the dataplane egress pipeline.
133 *
134 * @return egress byte count
135 */
136 public long getEgressBytes() {
137 return egressBytes;
138 }
139
tosinski36ba33a2021-11-22 16:53:00 +0100140 @Override
141 public UpfEntityType type() {
142 return UpfEntityType.COUNTER;
143 }
144
Daniele Moro5e66f982021-06-11 16:41:48 +0200145 public static class Builder {
146 private Integer cellId;
147 private long ingressPkts;
148 private long ingressBytes;
149 private long egressPkts;
150 private long egressBytes;
151
152 public Builder() {
153 this.ingressPkts = 0;
154 this.ingressBytes = 0;
155 this.egressPkts = 0;
156 this.egressBytes = 0;
157 }
158
159 /**
tosinski36ba33a2021-11-22 16:53:00 +0100160 * Set the Cell ID (index) of the datalane counter that produced this set of stats.
Daniele Moro5e66f982021-06-11 16:41:48 +0200161 *
162 * @param cellId the counter cell ID
163 * @return This builder
164 */
165 public Builder withCellId(int cellId) {
166 this.cellId = cellId;
167 return this;
168 }
169
170 /**
tosinski36ba33a2021-11-22 16:53:00 +0100171 * Set the number of packets and bytes that hit the counter in the dataplane ingress pipeline.
Daniele Moro5e66f982021-06-11 16:41:48 +0200172 *
173 * @param ingressPkts ingress packet count
174 * @param ingressBytes egress packet count
175 * @return This builder
176 */
177 public Builder setIngress(long ingressPkts, long ingressBytes) {
178 this.ingressPkts = ingressPkts;
179 this.ingressBytes = ingressBytes;
180 return this;
181 }
182
183 /**
tosinski36ba33a2021-11-22 16:53:00 +0100184 * Set the number of packets and bytes that hit the counter in the dataplane egress pipeline.
Daniele Moro5e66f982021-06-11 16:41:48 +0200185 *
186 * @param egressPkts egress packet count
187 * @param egressBytes egress byte count
188 * @return This builder
189 */
190 public Builder setEgress(long egressPkts, long egressBytes) {
191 this.egressPkts = egressPkts;
192 this.egressBytes = egressBytes;
193 return this;
194 }
195
tosinski36ba33a2021-11-22 16:53:00 +0100196 public UpfCounter build() {
Daniele Moro5e66f982021-06-11 16:41:48 +0200197 checkNotNull(cellId, "CellID must be provided");
tosinski36ba33a2021-11-22 16:53:00 +0100198 return new UpfCounter(cellId, ingressPkts, ingressBytes, egressPkts, egressBytes);
Daniele Moro5e66f982021-06-11 16:41:48 +0200199 }
200 }
201}