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