Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.behaviour.upf; |
| 18 | |
| 19 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 20 | import com.google.common.annotations.Beta; |
| 21 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame^] | 22 | import java.util.Objects; |
| 23 | |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 27 | * 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 Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 31 | */ |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 32 | @Beta |
| 33 | public final class UpfCounter implements UpfEntity { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 34 | 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 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 40 | private UpfCounter(int cellId, long ingressPkts, long ingressBytes, |
| 41 | long egressPkts, long egressBytes) { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 42 | 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 Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame^] | 55 | return String.format("UpfStats(cell_id=%d, ingress=(%dpkts,%dbytes), egress=(%dpkts,%dbytes))", |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 56 | cellId, ingressPkts, ingressBytes, egressPkts, egressBytes); |
| 57 | } |
| 58 | |
Daniele Moro | f3a5ab0 | 2022-01-26 16:47:08 +0100 | [diff] [blame^] | 59 | @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 Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 95 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 96 | * Get the cell ID (index) of the dataplane counter that produced this set of stats. |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 97 | * |
| 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 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 140 | @Override |
| 141 | public UpfEntityType type() { |
| 142 | return UpfEntityType.COUNTER; |
| 143 | } |
| 144 | |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 145 | 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 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 160 | * Set the Cell ID (index) of the datalane counter that produced this set of stats. |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 161 | * |
| 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 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 171 | * Set the number of packets and bytes that hit the counter in the dataplane ingress pipeline. |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 172 | * |
| 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 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 184 | * Set the number of packets and bytes that hit the counter in the dataplane egress pipeline. |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 185 | * |
| 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 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 196 | public UpfCounter build() { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 197 | checkNotNull(cellId, "CellID must be provided"); |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 198 | return new UpfCounter(cellId, ingressPkts, ingressBytes, egressPkts, egressBytes); |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |