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 | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 25 | * A structure for compactly passing UPF counter values for a given counter ID. |
| 26 | * Contains four counts: Ingress Packets, Ingress Bytes, Egress Packets, Egress Bytes. |
| 27 | * UpfCounter can be used ONLY on {@code apply} and {@code readAll} calls in the |
| 28 | * {@link UpfDevice} interface. |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 29 | */ |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 30 | @Beta |
| 31 | public final class UpfCounter implements UpfEntity { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 32 | private final int cellId; |
| 33 | private final long ingressPkts; |
| 34 | private final long ingressBytes; |
| 35 | private final long egressPkts; |
| 36 | private final long egressBytes; |
| 37 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 38 | private UpfCounter(int cellId, long ingressPkts, long ingressBytes, |
| 39 | long egressPkts, long egressBytes) { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 40 | this.cellId = cellId; |
| 41 | this.ingressPkts = ingressPkts; |
| 42 | this.ingressBytes = ingressBytes; |
| 43 | this.egressPkts = egressPkts; |
| 44 | this.egressBytes = egressBytes; |
| 45 | } |
| 46 | |
| 47 | public static Builder builder() { |
| 48 | return new Builder(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String toString() { |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 53 | return String.format("Stats:{ CellID: %d, Ingress:(%dpkts,%dbytes), Egress:(%dpkts,%dbytes) }", |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 54 | cellId, ingressPkts, ingressBytes, egressPkts, egressBytes); |
| 55 | } |
| 56 | |
| 57 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 58 | * 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] | 59 | * |
| 60 | * @return counter cell ID |
| 61 | */ |
| 62 | public int getCellId() { |
| 63 | return cellId; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the number of packets that hit this counter in the dataplane ingress pipeline. |
| 68 | * |
| 69 | * @return ingress packet count |
| 70 | */ |
| 71 | public long getIngressPkts() { |
| 72 | return ingressPkts; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the number of packets that hit this counter in the dataplane egress pipeline. |
| 77 | * |
| 78 | * @return egress packet count |
| 79 | */ |
| 80 | public long getEgressPkts() { |
| 81 | return egressPkts; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the number of packet bytes that hit this counter in the dataplane ingress pipeline. |
| 86 | * |
| 87 | * @return ingress byte count |
| 88 | */ |
| 89 | public long getIngressBytes() { |
| 90 | return ingressBytes; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the number of packet bytes that hit this counter in the dataplane egress pipeline. |
| 95 | * |
| 96 | * @return egress byte count |
| 97 | */ |
| 98 | public long getEgressBytes() { |
| 99 | return egressBytes; |
| 100 | } |
| 101 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 102 | @Override |
| 103 | public UpfEntityType type() { |
| 104 | return UpfEntityType.COUNTER; |
| 105 | } |
| 106 | |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 107 | public static class Builder { |
| 108 | private Integer cellId; |
| 109 | private long ingressPkts; |
| 110 | private long ingressBytes; |
| 111 | private long egressPkts; |
| 112 | private long egressBytes; |
| 113 | |
| 114 | public Builder() { |
| 115 | this.ingressPkts = 0; |
| 116 | this.ingressBytes = 0; |
| 117 | this.egressPkts = 0; |
| 118 | this.egressBytes = 0; |
| 119 | } |
| 120 | |
| 121 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 122 | * 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] | 123 | * |
| 124 | * @param cellId the counter cell ID |
| 125 | * @return This builder |
| 126 | */ |
| 127 | public Builder withCellId(int cellId) { |
| 128 | this.cellId = cellId; |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 133 | * 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] | 134 | * |
| 135 | * @param ingressPkts ingress packet count |
| 136 | * @param ingressBytes egress packet count |
| 137 | * @return This builder |
| 138 | */ |
| 139 | public Builder setIngress(long ingressPkts, long ingressBytes) { |
| 140 | this.ingressPkts = ingressPkts; |
| 141 | this.ingressBytes = ingressBytes; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 146 | * 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] | 147 | * |
| 148 | * @param egressPkts egress packet count |
| 149 | * @param egressBytes egress byte count |
| 150 | * @return This builder |
| 151 | */ |
| 152 | public Builder setEgress(long egressPkts, long egressBytes) { |
| 153 | this.egressPkts = egressPkts; |
| 154 | this.egressBytes = egressBytes; |
| 155 | return this; |
| 156 | } |
| 157 | |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 158 | public UpfCounter build() { |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 159 | checkNotNull(cellId, "CellID must be provided"); |
tosinski | 36ba33a | 2021-11-22 16:53:00 +0100 | [diff] [blame] | 160 | return new UpfCounter(cellId, ingressPkts, ingressBytes, egressPkts, egressBytes); |
Daniele Moro | 5e66f98 | 2021-06-11 16:41:48 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |