blob: 537b3de3339d7d640db1e25f46975b8e8c664e6d [file] [log] [blame]
kdarapu891693b2019-09-16 12:33:49 +05301/*
Joey Armstronga6af1522023-01-17 16:06:16 -05002 * Copyright 2019-2023 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) {
59
Neha Sharma96b7bf22020-06-15 10:37:32 +000060 StatMgr.PortStatisticsIndication(context.Background(), tt.args.PortStats, 16)
Naga Manjunath7615e552019-10-11 22:35:47 +053061 })
62 }
63}
64
65func TestOpenOltStatisticsMgr_publishMetrics(t *testing.T) {
66 type fields struct {
67 Device *DeviceHandler
68 NorthBoundPort map[uint32]*NniPort
69 SouthBoundPort map[uint32]*PonPort
70 }
71 type args struct {
Gamze Abakafcbd6e72020-12-17 13:25:16 +000072 val map[string]float32
73 port *voltha.Port
74 statType string
Naga Manjunath7615e552019-10-11 22:35:47 +053075 }
Naga Manjunath7615e552019-10-11 22:35:47 +053076 ponmap := map[uint32]*PonPort{}
77 ponmap[0] = &PonPort{
78 PONID: 0,
79 DeviceID: "onu1",
80 IntfID: 0,
81 PortNum: 0,
82 PortID: 0,
83 Label: "",
84 ONUs: nil,
85 ONUsByID: nil,
86 RxBytes: 0,
87 RxPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +000088 RxUcastPackets: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +053089 RxMcastPackets: 0,
90 RxBcastPackets: 0,
91 RxErrorPackets: 0,
92 TxBytes: 0,
93 TxPackets: 0,
94 TxUcastPackets: 0,
95 TxMcastPackets: 0,
96 TxBcastPackets: 0,
97 TxErrorPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +000098 RxCrcErrors: 0,
99 BipErrors: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +0530100 }
101 nnimap := map[uint32]*NniPort{}
102 nnimap[0] = &NniPort{
103 PortNum: 0,
104 Name: "olt1",
105 LogicalPort: 0,
106 IntfID: 0,
107 RxBytes: 0,
108 RxPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +0000109 RxUcastPackets: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +0530110 RxMcastPackets: uint64(1111),
111 RxBcastPackets: 0,
112 RxErrorPackets: 0,
113 TxBytes: 0,
114 TxPackets: 0,
115 TxUcastPackets: 0,
116 TxMcastPackets: 0,
117 TxBcastPackets: 0,
118 TxErrorPackets: 0,
Dileep Kuchhangi52cfbe12020-01-15 20:16:21 +0000119 RxCrcErrors: 0,
120 BipErrors: 0,
Naga Manjunath7615e552019-10-11 22:35:47 +0530121 }
122 pval := make(map[string]float32)
123 pval["rx_bytes"] = float32(111)
124 nval := make(map[string]float32)
125 nval["rx_bytes"] = float32(111)
126 dhandlerNNI := newMockDeviceHandler()
127 dhandlerNNI.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: nil, NorthBoundPort: nnimap}
128 dhandlerPON := newMockDeviceHandler()
129 dhandlerPON.portStats = &OpenOltStatisticsMgr{Device: nil, SouthBoundPort: ponmap, NorthBoundPort: nil}
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000130 dhandlerONU := newMockDeviceHandler()
131 dhandlerGEM := newMockDeviceHandler()
132
Naga Manjunath7615e552019-10-11 22:35:47 +0530133 tests := []struct {
134 name string
135 fields fields
136 args args
137 }{
138 {
139 name: "PublishNNIMetrics-1",
140 fields: fields{
141 Device: dhandlerNNI,
142 NorthBoundPort: nnimap,
143 SouthBoundPort: nil,
144 },
145 args: args{
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000146 val: nval,
147 port: &voltha.Port{PortNo: 0, Label: fmt.Sprintf("%s%d", "nni-", 0), Type: voltha.Port_ETHERNET_NNI},
148 statType: NNIStats,
Naga Manjunath7615e552019-10-11 22:35:47 +0530149 },
150 },
151 {
152 name: "PublishPONMetrics-1",
153 fields: fields{
154 Device: dhandlerPON,
155 NorthBoundPort: nil,
156 SouthBoundPort: ponmap,
157 },
158 args: args{
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000159 val: pval,
160 port: &voltha.Port{PortNo: 1, Label: fmt.Sprintf("%s%d", "pon-", 1), Type: voltha.Port_PON_OLT},
161 statType: PONStats,
162 },
163 },
164 {
165 name: "PublishONUMetrics-1",
166 fields: fields{
167 Device: dhandlerONU,
168 NorthBoundPort: nil,
169 SouthBoundPort: nil,
170 },
171 args: args{
172 port: &voltha.Port{Label: "ONU"},
173 statType: ONUStats,
174 },
175 },
176 {
177 name: "PublishGEMMetrics-1",
178 fields: fields{
179 Device: dhandlerGEM,
180 NorthBoundPort: nil,
181 SouthBoundPort: nil,
182 },
183 args: args{
184 port: &voltha.Port{Label: "GEM"},
185 statType: GEMStats,
Naga Manjunath7615e552019-10-11 22:35:47 +0530186 },
187 },
188 // TODO: Add test cases.
189 }
190 for _, tt := range tests {
191 t.Run(tt.name, func(t *testing.T) {
192 StatMgr := &OpenOltStatisticsMgr{
193 Device: tt.fields.Device,
194 NorthBoundPort: tt.fields.NorthBoundPort,
195 SouthBoundPort: tt.fields.SouthBoundPort,
196 }
Gamze Abakafcbd6e72020-12-17 13:25:16 +0000197 if tt.args.statType == ONUStats {
198 tt.args.val = StatMgr.convertONUStats(&openolt.OnuStatistics{IntfId: 1, OnuId: 1, PositiveDrift: 123, BipErrors: 22})
199 } else if tt.args.statType == GEMStats {
200 tt.args.val = StatMgr.convertGemStats(&openolt.GemPortStatistics{IntfId: 1, GemportId: 1024, RxPackets: 12, TxBytes: 12})
201 }
202 StatMgr.publishMetrics(context.Background(), tt.args.statType, tt.args.val, tt.args.port, "onu1", "openolt")
Naga Manjunath7615e552019-10-11 22:35:47 +0530203 })
204 }
205}
206
207func TestOpenOltStatisticsMgr_collectNNIMetrics(t *testing.T) {
208 type fields struct {
209 Device *DeviceHandler
210 NorthBoundPort map[uint32]*NniPort
211 SouthBoundPort map[uint32]*PonPort
212 }
213 type args struct {
214 nniID uint32
215 }
216 dhandler := newMockDeviceHandler()
217 pmconfig := make(map[string]*voltha.PmConfig)
218 pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"}
219
220 var res map[string]float32
221 nnimap := map[uint32]*NniPort{}
222 nnimap[0] = &NniPort{Name: "olt"}
223 nnimap[1] = &NniPort{Name: "olt"}
224 dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: nil, NorthBoundPort: nnimap}}
225 tests := []struct {
226 name string
227 fields fields
228 args args
229 want map[string]float32
230 }{
231 {"CollectNNIMetrics-1", fields{
232 Device: dh,
233 NorthBoundPort: nnimap,
234 SouthBoundPort: nil,
235 }, args{0}, res},
236 {"CollectNNIMetrics-2", fields{
237 Device: dh,
238 NorthBoundPort: nnimap,
239 SouthBoundPort: nil,
240 }, args{1}, res},
241 // TODO: Add test cases.
242 }
243 for _, tt := range tests {
244 t.Run(tt.name, func(t *testing.T) {
245 StatMgr := &OpenOltStatisticsMgr{
246 Device: tt.fields.Device,
247 NorthBoundPort: tt.fields.NorthBoundPort,
248 SouthBoundPort: tt.fields.SouthBoundPort,
249 }
250 got := StatMgr.collectNNIMetrics(tt.args.nniID)
251 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
252 t.Errorf("collectNNIMetrics() = %v, want %v", got, tt.want)
253 }
254 })
255 }
256}
257
258func TestOpenOltStatisticsMgr_collectPONMetrics(t *testing.T) {
259 type fields struct {
260 Device *DeviceHandler
261 NorthBoundPort map[uint32]*NniPort
262 SouthBoundPort map[uint32]*PonPort
263 }
264 type args struct {
265 pID uint32
266 }
267 dhandler := newMockDeviceHandler()
268 pmconfig := make(map[string]*voltha.PmConfig)
269 pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"}
270
271 var res map[string]float32
272 ponmap := map[uint32]*PonPort{}
273 ponmap[0] = &PonPort{DeviceID: "olt"}
274 ponmap[1] = &PonPort{DeviceID: "olt"}
275 dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: ponmap, NorthBoundPort: nil}}
276
277 tests := []struct {
278 name string
279 fields fields
280 args args
281 want map[string]float32
282 }{
283 {"CollectPONMetrics-1", fields{
284 Device: dh,
285 NorthBoundPort: nil,
286 SouthBoundPort: ponmap,
287 }, args{0}, res},
288 // TODO: Add test cases.
289 }
290 for _, tt := range tests {
291 t.Run(tt.name, func(t *testing.T) {
292 StatMgr := &OpenOltStatisticsMgr{
293 Device: tt.fields.Device,
294 NorthBoundPort: tt.fields.NorthBoundPort,
295 SouthBoundPort: tt.fields.SouthBoundPort,
296 }
297 got := StatMgr.collectPONMetrics(tt.args.pID)
298 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
299 t.Errorf("collectPONMetrics() = %v, want %v", got, tt.want)
300 }
kdarapu891693b2019-09-16 12:33:49 +0530301 })
302 }
303}