blob: 4eee3fed4216ae1bfb8704bde1b267750de5aa61 [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 Moro5e66f982021-06-11 16:41:48 +020022import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
tosinski36ba33a2021-11-22 16:53:00 +010025 * 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 Moro5e66f982021-06-11 16:41:48 +020029 */
tosinski36ba33a2021-11-22 16:53:00 +010030@Beta
31public final class UpfCounter implements UpfEntity {
Daniele Moro5e66f982021-06-11 16:41:48 +020032 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
tosinski36ba33a2021-11-22 16:53:00 +010038 private UpfCounter(int cellId, long ingressPkts, long ingressBytes,
39 long egressPkts, long egressBytes) {
Daniele Moro5e66f982021-06-11 16:41:48 +020040 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() {
tosinski36ba33a2021-11-22 16:53:00 +010053 return String.format("Stats:{ CellID: %d, Ingress:(%dpkts,%dbytes), Egress:(%dpkts,%dbytes) }",
Daniele Moro5e66f982021-06-11 16:41:48 +020054 cellId, ingressPkts, ingressBytes, egressPkts, egressBytes);
55 }
56
57 /**
tosinski36ba33a2021-11-22 16:53:00 +010058 * Get the cell ID (index) of the dataplane counter that produced this set of stats.
Daniele Moro5e66f982021-06-11 16:41:48 +020059 *
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
tosinski36ba33a2021-11-22 16:53:00 +0100102 @Override
103 public UpfEntityType type() {
104 return UpfEntityType.COUNTER;
105 }
106
Daniele Moro5e66f982021-06-11 16:41:48 +0200107 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 /**
tosinski36ba33a2021-11-22 16:53:00 +0100122 * Set the Cell ID (index) of the datalane counter that produced this set of stats.
Daniele Moro5e66f982021-06-11 16:41:48 +0200123 *
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 /**
tosinski36ba33a2021-11-22 16:53:00 +0100133 * Set the number of packets and bytes that hit the counter in the dataplane ingress pipeline.
Daniele Moro5e66f982021-06-11 16:41:48 +0200134 *
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 /**
tosinski36ba33a2021-11-22 16:53:00 +0100146 * Set the number of packets and bytes that hit the counter in the dataplane egress pipeline.
Daniele Moro5e66f982021-06-11 16:41:48 +0200147 *
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
tosinski36ba33a2021-11-22 16:53:00 +0100158 public UpfCounter build() {
Daniele Moro5e66f982021-06-11 16:41:48 +0200159 checkNotNull(cellId, "CellID must be provided");
tosinski36ba33a2021-11-22 16:53:00 +0100160 return new UpfCounter(cellId, ingressPkts, ingressBytes, egressPkts, egressBytes);
Daniele Moro5e66f982021-06-11 16:41:48 +0200161 }
162 }
163}