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