kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 17 | //Package core provides the utility for olt devices, flows and statistics |
| 18 | package core |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 19 | |
| 20 | import ( |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 21 | "context" |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 22 | "fmt" |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 23 | "reflect" |
| 24 | "testing" |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 25 | |
| 26 | "github.com/opencord/voltha-protos/v3/go/openolt" |
| 27 | "github.com/opencord/voltha-protos/v3/go/voltha" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 28 | ) |
| 29 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 30 | func TestOpenOltStatisticsMgr_PortStatisticsIndication(t *testing.T) { |
| 31 | device := &voltha.Device{ |
| 32 | Id: "olt", |
| 33 | Root: true, |
| 34 | ParentId: "logical_device", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 35 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 36 | DeviceId: "olt", |
| 37 | DeviceType: "onu", |
| 38 | ChannelId: 1, |
| 39 | ChannelGroupId: 1, |
| 40 | }, |
| 41 | ConnectStatus: 1, |
| 42 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 43 | dh := newMockDeviceHandler() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 44 | dh.device = device |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 45 | StatMgr := NewOpenOltStatsMgr(context.Background(), dh) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 46 | |
| 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 Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 60 | StatMgr.PortStatisticsIndication(context.Background(), tt.args.PortStats, 16) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func 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 { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 72 | val map[string]float32 |
| 73 | port *voltha.Port |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 74 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 75 | 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 Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 87 | RxUcastPackets: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 88 | 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 Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 97 | RxCrcErrors: 0, |
| 98 | BipErrors: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 99 | } |
| 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 Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 108 | RxUcastPackets: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 109 | 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 Kuchhangi | 52cfbe1 | 2020-01-15 20:16:21 +0000 | [diff] [blame] | 118 | RxCrcErrors: 0, |
| 119 | BipErrors: 0, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 120 | } |
| 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} |
| 129 | tests := []struct { |
| 130 | name string |
| 131 | fields fields |
| 132 | args args |
| 133 | }{ |
| 134 | { |
| 135 | name: "PublishNNIMetrics-1", |
| 136 | fields: fields{ |
| 137 | Device: dhandlerNNI, |
| 138 | NorthBoundPort: nnimap, |
| 139 | SouthBoundPort: nil, |
| 140 | }, |
| 141 | args: args{ |
Girish Gowdra | 34815db | 2020-05-11 17:18:04 -0700 | [diff] [blame] | 142 | val: nval, |
| 143 | port: &voltha.Port{PortNo: 0, Label: fmt.Sprintf("%s%d", "nni-", 0), Type: voltha.Port_ETHERNET_NNI}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 144 | }, |
| 145 | }, |
| 146 | { |
| 147 | name: "PublishPONMetrics-1", |
| 148 | fields: fields{ |
| 149 | Device: dhandlerPON, |
| 150 | NorthBoundPort: nil, |
| 151 | SouthBoundPort: ponmap, |
| 152 | }, |
| 153 | args: args{ |
Girish Gowdra | 34815db | 2020-05-11 17:18:04 -0700 | [diff] [blame] | 154 | val: pval, |
| 155 | port: &voltha.Port{PortNo: 1, Label: fmt.Sprintf("%s%d", "pon-", 1), Type: voltha.Port_PON_OLT}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 156 | }, |
| 157 | }, |
| 158 | // TODO: Add test cases. |
| 159 | } |
| 160 | for _, tt := range tests { |
| 161 | t.Run(tt.name, func(t *testing.T) { |
| 162 | StatMgr := &OpenOltStatisticsMgr{ |
| 163 | Device: tt.fields.Device, |
| 164 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 165 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 166 | } |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 167 | StatMgr.publishMetrics(context.Background(), tt.args.val, tt.args.port, "onu1", "openolt") |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 168 | }) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestOpenOltStatisticsMgr_collectNNIMetrics(t *testing.T) { |
| 173 | type fields struct { |
| 174 | Device *DeviceHandler |
| 175 | NorthBoundPort map[uint32]*NniPort |
| 176 | SouthBoundPort map[uint32]*PonPort |
| 177 | } |
| 178 | type args struct { |
| 179 | nniID uint32 |
| 180 | } |
| 181 | dhandler := newMockDeviceHandler() |
| 182 | pmconfig := make(map[string]*voltha.PmConfig) |
| 183 | pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"} |
| 184 | |
| 185 | var res map[string]float32 |
| 186 | nnimap := map[uint32]*NniPort{} |
| 187 | nnimap[0] = &NniPort{Name: "olt"} |
| 188 | nnimap[1] = &NniPort{Name: "olt"} |
| 189 | dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: nil, NorthBoundPort: nnimap}} |
| 190 | tests := []struct { |
| 191 | name string |
| 192 | fields fields |
| 193 | args args |
| 194 | want map[string]float32 |
| 195 | }{ |
| 196 | {"CollectNNIMetrics-1", fields{ |
| 197 | Device: dh, |
| 198 | NorthBoundPort: nnimap, |
| 199 | SouthBoundPort: nil, |
| 200 | }, args{0}, res}, |
| 201 | {"CollectNNIMetrics-2", fields{ |
| 202 | Device: dh, |
| 203 | NorthBoundPort: nnimap, |
| 204 | SouthBoundPort: nil, |
| 205 | }, args{1}, res}, |
| 206 | // TODO: Add test cases. |
| 207 | } |
| 208 | for _, tt := range tests { |
| 209 | t.Run(tt.name, func(t *testing.T) { |
| 210 | StatMgr := &OpenOltStatisticsMgr{ |
| 211 | Device: tt.fields.Device, |
| 212 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 213 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 214 | } |
| 215 | got := StatMgr.collectNNIMetrics(tt.args.nniID) |
| 216 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 217 | t.Errorf("collectNNIMetrics() = %v, want %v", got, tt.want) |
| 218 | } |
| 219 | }) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestOpenOltStatisticsMgr_collectPONMetrics(t *testing.T) { |
| 224 | type fields struct { |
| 225 | Device *DeviceHandler |
| 226 | NorthBoundPort map[uint32]*NniPort |
| 227 | SouthBoundPort map[uint32]*PonPort |
| 228 | } |
| 229 | type args struct { |
| 230 | pID uint32 |
| 231 | } |
| 232 | dhandler := newMockDeviceHandler() |
| 233 | pmconfig := make(map[string]*voltha.PmConfig) |
| 234 | pmconfig["rx_bytes"] = &voltha.PmConfig{Name: "olt"} |
| 235 | |
| 236 | var res map[string]float32 |
| 237 | ponmap := map[uint32]*PonPort{} |
| 238 | ponmap[0] = &PonPort{DeviceID: "olt"} |
| 239 | ponmap[1] = &PonPort{DeviceID: "olt"} |
| 240 | dh := &DeviceHandler{portStats: &OpenOltStatisticsMgr{Device: dhandler, SouthBoundPort: ponmap, NorthBoundPort: nil}} |
| 241 | |
| 242 | tests := []struct { |
| 243 | name string |
| 244 | fields fields |
| 245 | args args |
| 246 | want map[string]float32 |
| 247 | }{ |
| 248 | {"CollectPONMetrics-1", fields{ |
| 249 | Device: dh, |
| 250 | NorthBoundPort: nil, |
| 251 | SouthBoundPort: ponmap, |
| 252 | }, args{0}, res}, |
| 253 | // TODO: Add test cases. |
| 254 | } |
| 255 | for _, tt := range tests { |
| 256 | t.Run(tt.name, func(t *testing.T) { |
| 257 | StatMgr := &OpenOltStatisticsMgr{ |
| 258 | Device: tt.fields.Device, |
| 259 | NorthBoundPort: tt.fields.NorthBoundPort, |
| 260 | SouthBoundPort: tt.fields.SouthBoundPort, |
| 261 | } |
| 262 | got := StatMgr.collectPONMetrics(tt.args.pID) |
| 263 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 264 | t.Errorf("collectPONMetrics() = %v, want %v", got, tt.want) |
| 265 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 266 | }) |
| 267 | } |
| 268 | } |