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