blob: 1cec54813649abff31d01922b05e3344ccba1c1d [file] [log] [blame]
kdarapu891693b2019-09-16 12:33:49 +05301/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
kdarapu891693b2019-09-16 12:33:49 +05303
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
Joey Armstrong3f0e2422023-07-05 18:25:41 -040017// Package core provides the utility for olt devices, flows and statistics
Scott Bakerdbd960e2020-02-28 08:57:51 -080018package core
kdarapu891693b2019-09-16 12:33:49 +053019
20import (
Neha Sharma96b7bf22020-06-15 10:37:32 +000021 "context"
Kishore Darapuaaf9c102020-05-04 13:06:57 +053022 "fmt"
Girish Kumar2ad402b2020-03-20 19:45:12 +000023 "reflect"
24 "testing"
Kent Hagermane6ff1012020-07-14 15:07:53 -040025
khenaidoo106c61a2021-08-11 18:05:46 -040026 "github.com/opencord/voltha-protos/v5/go/openolt"
27 "github.com/opencord/voltha-protos/v5/go/voltha"
kdarapu891693b2019-09-16 12:33:49 +053028)
29
kdarapu891693b2019-09-16 12:33:49 +053030func TestOpenOltStatisticsMgr_PortStatisticsIndication(t *testing.T) {
31 device := &voltha.Device{
32 Id: "olt",
33 Root: true,
34 ParentId: "logical_device",
kdarapu891693b2019-09-16 12:33:49 +053035 ProxyAddress: &voltha.Device_ProxyAddress{
36 DeviceId: "olt",
37 DeviceType: "onu",
38 ChannelId: 1,
39 ChannelGroupId: 1,
40 },
41 ConnectStatus: 1,
42 }
Naga Manjunath7615e552019-10-11 22:35:47 +053043 dh := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +053044 dh.device = device
Neha Sharma96b7bf22020-06-15 10:37:32 +000045 StatMgr := NewOpenOltStatsMgr(context.Background(), dh)
kdarapu891693b2019-09-16 12:33:49 +053046
47 type args struct {
48 PortStats *openolt.PortStatistics
49 }
50 tests := []struct {
51 name string
52 args args
53 }{
54 // TODO: Add test cases.
55 {"PortStatisticsIndication", args{PortStats: &openolt.PortStatistics{}}},
56 }
57 for _, tt := range tests {
58 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +000059 StatMgr.PortStatisticsIndication(context.Background(), tt.args.PortStats, 16)
Naga Manjunath7615e552019-10-11 22:35:47 +053060 })
61 }
62}
63
64func TestOpenOltStatisticsMgr_publishMetrics(t *testing.T) {
65 type fields struct {
66 Device *DeviceHandler
67 NorthBoundPort map[uint32]*NniPort
68 SouthBoundPort map[uint32]*PonPort
69 }
70 type args struct {
Gamze Abakafcbd6e72020-12-17 13:25:16 +000071 val map[string]float32
72 port *voltha.Port
73 statType string
Naga Manjunath7615e552019-10-11 22:35:47 +053074 }
Naga Manjunath7615e552019-10-11 22:35:47 +053075 ponmap := map[uint32]*PonPort{}
76 ponmap[0] = &PonPort{
77 PONID: 0,
78 DeviceID: "onu1",
79 IntfID: 0,
80 PortNum: 0,
81 PortID: 0,
82 Label: "",
83 ONUs: nil,
84 ONUsByID: nil,
85 RxBytes: 0,
86 RxPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +000087 RxUcastPackets: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +053088 RxMcastPackets: 0,
89 RxBcastPackets: 0,
90 RxErrorPackets: 0,
91 TxBytes: 0,
92 TxPackets: 0,
93 TxUcastPackets: 0,
94 TxMcastPackets: 0,
95 TxBcastPackets: 0,
96 TxErrorPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +000097 RxCrcErrors: 0,
98 BipErrors: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +053099 }
100 nnimap := map[uint32]*NniPort{}
101 nnimap[0] = &NniPort{
102 PortNum: 0,
103 Name: "olt1",
104 LogicalPort: 0,
105 IntfID: 0,
106 RxBytes: 0,
107 RxPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +0000108 RxUcastPackets: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +0530109 RxMcastPackets: uint64(1111),
110 RxBcastPackets: 0,
111 RxErrorPackets: 0,
112 TxBytes: 0,
113 TxPackets: 0,
114 TxUcastPackets: 0,
115 TxMcastPackets: 0,
116 TxBcastPackets: 0,
117 TxErrorPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +0000118 RxCrcErrors: 0,
119 BipErrors: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +0530120 }
121 pval := make(map[string]float32)
122 pval["rx_bytes"] = float32(111)
123 nval := make(map[string]float32)
124 nval["rx_bytes"] = float32(111)
125 dhandlerNNI := newMockDeviceHandler()
126 dhandlerNNI.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: nil, NorthBoundPort: nnimap}
127 dhandlerPON := newMockDeviceHandler()
128 dhandlerPON.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: ponmap, NorthBoundPort: nil}
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000129 dhandlerONU := newMockDeviceHandler()
130 dhandlerGEM := newMockDeviceHandler()
131
Naga Manjunath7615e552019-10-11 22:35:47 +0530132 tests := []struct {
133 name string
134 fields fields
135 args args
136 }{
137 {
138 name: "PublishNNIMetrics-1",
139 fields: fields{
140 Device: dhandlerNNI,
141 NorthBoundPort: nnimap,
142 SouthBoundPort: nil,
143 },
144 args: args{
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000145 val: nval,
146 port: &voltha.Port{PortNo: 0, Label: fmt.Sprintf("%s%d", "nni-", 0), Type: voltha.Port_ETHERNET_NNI},
147 statType: NNIStats,
Naga Manjunath7615e552019-10-11 22:35:47 +0530148 },
149 },
150 {
151 name: "PublishPONMetrics-1",
152 fields: fields{
153 Device: dhandlerPON,
154 NorthBoundPort: nil,
155 SouthBoundPort: ponmap,
156 },
157 args: args{
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000158 val: pval,
159 port: &voltha.Port{PortNo: 1, Label: fmt.Sprintf("%s%d", "pon-", 1), Type: voltha.Port_PON_OLT},
160 statType: PONStats,
161 },
162 },
163 {
164 name: "PublishONUMetrics-1",
165 fields: fields{
166 Device: dhandlerONU,
167 NorthBoundPort: nil,
168 SouthBoundPort: nil,
169 },
170 args: args{
171 port: &voltha.Port{Label: "ONU"},
172 statType: ONUStats,
173 },
174 },
175 {
176 name: "PublishGEMMetrics-1",
177 fields: fields{
178 Device: dhandlerGEM,
179 NorthBoundPort: nil,
180 SouthBoundPort: nil,
181 },
182 args: args{
183 port: &voltha.Port{Label: "GEM"},
184 statType: GEMStats,
Naga Manjunath7615e552019-10-11 22:35:47 +0530185 },
186 },
187 // TODO: Add test cases.
188 }
189 for _, tt := range tests {
190 t.Run(tt.name, func(t *testing.T) {
191 StatMgr := &OpenOltStatisticsMgr{
192 Device: tt.fields.Device,
193 NorthBoundPort: tt.fields.NorthBoundPort,
194 SouthBoundPort: tt.fields.SouthBoundPort,
195 }
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000196 if tt.args.statType == ONUStats {
197 tt.args.val = StatMgr.convertONUStats(&openolt.OnuStatistics{IntfId: 1, OnuId: 1, PositiveDrift: 123, BipErrors: 22})
198 } else if tt.args.statType == GEMStats {
199 tt.args.val = StatMgr.convertGemStats(&openolt.GemPortStatistics{IntfId: 1, GemportId: 1024, RxPackets: 12, TxBytes: 12})
200 }
201 StatMgr.publishMetrics(context.Background(), tt.args.statType, tt.args.val, tt.args.port, "onu1", "openolt")
Naga Manjunath7615e552019-10-11 22:35:47 +0530202 })
203 }
204}
205
206func TestOpenOltStatisticsMgr_collectNNIMetrics(t *testing.T) {
207 type fields struct {
208 Device *DeviceHandler
209 NorthBoundPort map[uint32]*NniPort
210 SouthBoundPort map[uint32]*PonPort
211 }
212 type args struct {
213 nniID uint32
214 }
215 dhandler := newMockDeviceHandler()
216 pmconfig := make(map[string]*voltha.PmConfig)
217 pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"}
218
219 var res map[string]float32
220 nnimap := map[uint32]*NniPort{}
221 nnimap[0] = &NniPort{Name: "olt"}
222 nnimap[1] = &NniPort{Name: "olt"}
223 dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: nil, NorthBoundPort: nnimap}}
224 tests := []struct {
225 name string
226 fields fields
227 args args
228 want map[string]float32
229 }{
230 {"CollectNNIMetrics-1", fields{
231 Device: dh,
232 NorthBoundPort: nnimap,
233 SouthBoundPort: nil,
234 }, args{0}, res},
235 {"CollectNNIMetrics-2", fields{
236 Device: dh,
237 NorthBoundPort: nnimap,
238 SouthBoundPort: nil,
239 }, args{1}, res},
240 // TODO: Add test cases.
241 }
242 for _, tt := range tests {
243 t.Run(tt.name, func(t *testing.T) {
244 StatMgr := &OpenOltStatisticsMgr{
245 Device: tt.fields.Device,
246 NorthBoundPort: tt.fields.NorthBoundPort,
247 SouthBoundPort: tt.fields.SouthBoundPort,
248 }
249 got := StatMgr.collectNNIMetrics(tt.args.nniID)
250 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
251 t.Errorf("collectNNIMetrics() = %v, want %v", got, tt.want)
252 }
253 })
254 }
255}
256
257func TestOpenOltStatisticsMgr_collectPONMetrics(t *testing.T) {
258 type fields struct {
259 Device *DeviceHandler
260 NorthBoundPort map[uint32]*NniPort
261 SouthBoundPort map[uint32]*PonPort
262 }
263 type args struct {
264 pID uint32
265 }
266 dhandler := newMockDeviceHandler()
267 pmconfig := make(map[string]*voltha.PmConfig)
268 pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"}
269
270 var res map[string]float32
271 ponmap := map[uint32]*PonPort{}
272 ponmap[0] = &PonPort{DeviceID: "olt"}
273 ponmap[1] = &PonPort{DeviceID: "olt"}
274 dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: ponmap, NorthBoundPort: nil}}
275
276 tests := []struct {
277 name string
278 fields fields
279 args args
280 want map[string]float32
281 }{
282 {"CollectPONMetrics-1", fields{
283 Device: dh,
284 NorthBoundPort: nil,
285 SouthBoundPort: ponmap,
286 }, args{0}, res},
287 // TODO: Add test cases.
288 }
289 for _, tt := range tests {
290 t.Run(tt.name, func(t *testing.T) {
291 StatMgr := &OpenOltStatisticsMgr{
292 Device: tt.fields.Device,
293 NorthBoundPort: tt.fields.NorthBoundPort,
294 SouthBoundPort: tt.fields.SouthBoundPort,
295 }
296 got := StatMgr.collectPONMetrics(tt.args.pID)
297 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
298 t.Errorf("collectPONMetrics() = %v, want %v", got, tt.want)
299 }
kdarapu891693b2019-09-16 12:33:49 +0530300 })
301 }
302}