khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [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 | */ |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 16 | package api |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 17 | |
| 18 | import ( |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 19 | "context" |
| 20 | "fmt" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | "time" |
| 22 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes/empty" |
| 24 | "github.com/google/uuid" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 26 | "google.golang.org/grpc/metadata" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 30 | volthaSerialNumberKey = "voltha_serial_number" |
| 31 | retryInterval = 50 * time.Millisecond |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 32 | ) |
| 33 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 34 | var ( |
| 35 | coreInCompeteMode bool |
| 36 | ) |
| 37 | |
| 38 | type isLogicalDeviceConditionSatisfied func(ld *voltha.LogicalDevice) bool |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 39 | type isLogicalDevicePortsConditionSatisfied func(ports []*voltha.LogicalPort) bool |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 40 | type isDeviceConditionSatisfied func(ld *voltha.Device) bool |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 41 | type isDevicePortsConditionSatisfied func(ports *voltha.Ports) bool |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 42 | type isDevicesConditionSatisfied func(ds *voltha.Devices) bool |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 43 | type isLogicalDevicesConditionSatisfied func(lds *voltha.LogicalDevices) bool |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 44 | type isConditionSatisfied func() bool |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 45 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 46 | func init() { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 47 | //Default mode is two rw-core running in a pair of competing cores |
| 48 | coreInCompeteMode = true |
| 49 | } |
| 50 | |
| 51 | func setCoreCompeteMode(mode bool) { |
| 52 | coreInCompeteMode = mode |
| 53 | } |
| 54 | |
| 55 | func getContext() context.Context { |
| 56 | if coreInCompeteMode { |
| 57 | return metadata.NewIncomingContext(context.Background(), metadata.Pairs(volthaSerialNumberKey, uuid.New().String())) |
| 58 | } |
| 59 | return context.Background() |
| 60 | } |
| 61 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 62 | func waitUntilDeviceReadiness(deviceID string, |
| 63 | timeout time.Duration, |
| 64 | verificationFunction isDeviceConditionSatisfied, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 65 | nbi *NBIHandler) error { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 66 | ch := make(chan int, 1) |
| 67 | done := false |
| 68 | go func() { |
| 69 | for { |
| 70 | device, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: deviceID}) |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 71 | if verificationFunction(device) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 72 | ch <- 1 |
| 73 | break |
| 74 | } |
| 75 | if done { |
| 76 | break |
| 77 | } |
| 78 | time.Sleep(retryInterval) |
| 79 | } |
| 80 | }() |
| 81 | timer := time.NewTimer(timeout) |
| 82 | defer timer.Stop() |
| 83 | select { |
| 84 | case <-ch: |
| 85 | return nil |
| 86 | case <-timer.C: |
| 87 | done = true |
| 88 | return fmt.Errorf("expected-states-not-reached-for-device%s", deviceID) |
| 89 | } |
| 90 | } |
| 91 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 92 | func waitUntilDevicePortsReadiness(deviceID string, |
| 93 | timeout time.Duration, |
| 94 | verificationFunction isDevicePortsConditionSatisfied, |
| 95 | nbi *NBIHandler) error { |
| 96 | ch := make(chan int, 1) |
| 97 | done := false |
| 98 | go func() { |
| 99 | for { |
| 100 | ports, _ := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: deviceID}) |
| 101 | if verificationFunction(ports) { |
| 102 | ch <- 1 |
| 103 | break |
| 104 | } |
| 105 | if done { |
| 106 | break |
| 107 | } |
| 108 | time.Sleep(retryInterval) |
| 109 | } |
| 110 | }() |
| 111 | timer := time.NewTimer(timeout) |
| 112 | defer timer.Stop() |
| 113 | select { |
| 114 | case <-ch: |
| 115 | return nil |
| 116 | case <-timer.C: |
| 117 | done = true |
| 118 | return fmt.Errorf("expected-states-not-reached-for-device%s", deviceID) |
| 119 | } |
| 120 | } |
| 121 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 122 | func waitUntilLogicalDeviceReadiness(oltDeviceID string, |
| 123 | timeout time.Duration, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 124 | nbi *NBIHandler, |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 125 | verificationFunction isLogicalDeviceConditionSatisfied, |
| 126 | ) error { |
| 127 | ch := make(chan int, 1) |
| 128 | done := false |
| 129 | go func() { |
| 130 | for { |
| 131 | // Get the logical device from the olt device |
| 132 | d, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDeviceID}) |
| 133 | if d != nil && d.ParentId != "" { |
| 134 | ld, _ := nbi.GetLogicalDevice(getContext(), &voltha.ID{Id: d.ParentId}) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 135 | if verificationFunction(ld) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 136 | ch <- 1 |
| 137 | break |
| 138 | } |
| 139 | if done { |
| 140 | break |
| 141 | } |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 142 | } else if d != nil && d.ParentId == "" { // case where logical device deleted |
| 143 | if verificationFunction(nil) { |
| 144 | ch <- 1 |
| 145 | break |
| 146 | } |
| 147 | if done { |
| 148 | break |
| 149 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 150 | } |
| 151 | time.Sleep(retryInterval) |
| 152 | } |
| 153 | }() |
| 154 | timer := time.NewTimer(timeout) |
| 155 | defer timer.Stop() |
| 156 | select { |
| 157 | case <-ch: |
| 158 | return nil |
| 159 | case <-timer.C: |
| 160 | done = true |
| 161 | return fmt.Errorf("timeout-waiting-for-logical-device-readiness%s", oltDeviceID) |
| 162 | } |
| 163 | } |
| 164 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 165 | func waitUntilLogicalDevicePortsReadiness(oltDeviceID string, |
| 166 | timeout time.Duration, |
| 167 | nbi *NBIHandler, |
| 168 | verificationFunction isLogicalDevicePortsConditionSatisfied, |
| 169 | ) error { |
| 170 | ch := make(chan int, 1) |
| 171 | done := false |
| 172 | go func() { |
| 173 | for { |
| 174 | // Get the logical device from the olt device |
| 175 | d, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDeviceID}) |
| 176 | if d != nil && d.ParentId != "" { |
| 177 | ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: d.ParentId}) |
| 178 | if err == nil && verificationFunction(ports.Items) { |
| 179 | ch <- 1 |
| 180 | break |
| 181 | } |
| 182 | if done { |
| 183 | break |
| 184 | } |
| 185 | } |
| 186 | time.Sleep(retryInterval) |
| 187 | } |
| 188 | }() |
| 189 | timer := time.NewTimer(timeout) |
| 190 | defer timer.Stop() |
| 191 | select { |
| 192 | case <-ch: |
| 193 | return nil |
| 194 | case <-timer.C: |
| 195 | done = true |
| 196 | return fmt.Errorf("timeout-waiting-for-logical-device-readiness%s", oltDeviceID) |
| 197 | } |
| 198 | } |
| 199 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 200 | func waitUntilConditionForDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isDevicesConditionSatisfied) error { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 201 | ch := make(chan int, 1) |
| 202 | done := false |
| 203 | go func() { |
| 204 | for { |
| 205 | devices, _ := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 206 | if verificationFunction(devices) { |
| 207 | ch <- 1 |
| 208 | break |
| 209 | } |
| 210 | if done { |
| 211 | break |
| 212 | } |
| 213 | |
| 214 | time.Sleep(retryInterval) |
| 215 | } |
| 216 | }() |
| 217 | timer := time.NewTimer(timeout) |
| 218 | defer timer.Stop() |
| 219 | select { |
| 220 | case <-ch: |
| 221 | return nil |
| 222 | case <-timer.C: |
| 223 | done = true |
| 224 | return fmt.Errorf("timeout-waiting-devices") |
| 225 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 226 | } |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 227 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 228 | func waitUntilConditionForLogicalDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isLogicalDevicesConditionSatisfied) error { |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 229 | ch := make(chan int, 1) |
| 230 | done := false |
| 231 | go func() { |
| 232 | for { |
| 233 | lDevices, _ := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 234 | if verificationFunction(lDevices) { |
| 235 | ch <- 1 |
| 236 | break |
| 237 | } |
| 238 | if done { |
| 239 | break |
| 240 | } |
| 241 | |
| 242 | time.Sleep(retryInterval) |
| 243 | } |
| 244 | }() |
| 245 | timer := time.NewTimer(timeout) |
| 246 | defer timer.Stop() |
| 247 | select { |
| 248 | case <-ch: |
| 249 | return nil |
| 250 | case <-timer.C: |
| 251 | done = true |
| 252 | return fmt.Errorf("timeout-waiting-logical-devices") |
| 253 | } |
| 254 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 255 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 256 | func waitUntilCondition(timeout time.Duration, nbi *NBIHandler, verificationFunction isConditionSatisfied) error { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 257 | ch := make(chan int, 1) |
| 258 | done := false |
| 259 | go func() { |
| 260 | for { |
| 261 | if verificationFunction() { |
| 262 | ch <- 1 |
| 263 | break |
| 264 | } |
| 265 | if done { |
| 266 | break |
| 267 | } |
| 268 | time.Sleep(retryInterval) |
| 269 | } |
| 270 | }() |
| 271 | timer := time.NewTimer(timeout) |
| 272 | defer timer.Stop() |
| 273 | select { |
| 274 | case <-ch: |
| 275 | return nil |
| 276 | case <-timer.C: |
| 277 | done = true |
| 278 | return fmt.Errorf("timeout-waiting-for-condition") |
| 279 | } |
| 280 | } |