blob: 3a4fe8ede9ff63e4c21f9f2cf3295ab818990008 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
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 Hagerman2b216042020-04-03 18:28:56 -040016package api
khenaidooab1f7bd2019-11-14 14:00:27 -050017
18import (
khenaidoob64fc8a2019-11-27 15:08:19 -050019 "context"
20 "fmt"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080021 "time"
22
khenaidoob64fc8a2019-11-27 15:08:19 -050023 "github.com/golang/protobuf/ptypes/empty"
24 "github.com/google/uuid"
Maninderdfadc982020-10-28 14:04:33 +053025 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050026 "google.golang.org/grpc/metadata"
khenaidooab1f7bd2019-11-14 14:00:27 -050027)
28
29const (
khenaidoob64fc8a2019-11-27 15:08:19 -050030 volthaSerialNumberKey = "voltha_serial_number"
31 retryInterval = 50 * time.Millisecond
khenaidooab1f7bd2019-11-14 14:00:27 -050032)
33
khenaidoob64fc8a2019-11-27 15:08:19 -050034var (
35 coreInCompeteMode bool
36)
37
38type isLogicalDeviceConditionSatisfied func(ld *voltha.LogicalDevice) bool
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040039type isLogicalDevicePortsConditionSatisfied func(ports []*voltha.LogicalPort) bool
khenaidoob64fc8a2019-11-27 15:08:19 -050040type isDeviceConditionSatisfied func(ld *voltha.Device) bool
Kent Hagerman2a07b862020-06-19 15:23:07 -040041type isDevicePortsConditionSatisfied func(ports *voltha.Ports) bool
khenaidoob64fc8a2019-11-27 15:08:19 -050042type isDevicesConditionSatisfied func(ds *voltha.Devices) bool
khenaidoo93d5a3d2020-01-15 12:37:05 -050043type isLogicalDevicesConditionSatisfied func(lds *voltha.LogicalDevices) bool
khenaidoo67b22152020-03-02 16:01:25 -050044type isConditionSatisfied func() bool
khenaidoob64fc8a2019-11-27 15:08:19 -050045
khenaidooab1f7bd2019-11-14 14:00:27 -050046func init() {
khenaidoob64fc8a2019-11-27 15:08:19 -050047 //Default mode is two rw-core running in a pair of competing cores
48 coreInCompeteMode = true
49}
50
51func setCoreCompeteMode(mode bool) {
52 coreInCompeteMode = mode
53}
54
55func 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
khenaidoob64fc8a2019-11-27 15:08:19 -050062func waitUntilDeviceReadiness(deviceID string,
63 timeout time.Duration,
64 verificationFunction isDeviceConditionSatisfied,
Kent Hagerman2b216042020-04-03 18:28:56 -040065 nbi *NBIHandler) error {
khenaidoob64fc8a2019-11-27 15:08:19 -050066 ch := make(chan int, 1)
67 done := false
68 go func() {
69 for {
70 device, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: deviceID})
Chaitrashree G Se8ad0202020-02-27 18:48:00 -050071 if verificationFunction(device) {
khenaidoob64fc8a2019-11-27 15:08:19 -050072 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 Hagerman2a07b862020-06-19 15:23:07 -040092func 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
khenaidoob64fc8a2019-11-27 15:08:19 -0500122func waitUntilLogicalDeviceReadiness(oltDeviceID string,
123 timeout time.Duration,
Kent Hagerman2b216042020-04-03 18:28:56 -0400124 nbi *NBIHandler,
khenaidoob64fc8a2019-11-27 15:08:19 -0500125 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})
khenaidoo67b22152020-03-02 16:01:25 -0500135 if verificationFunction(ld) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500136 ch <- 1
137 break
138 }
139 if done {
140 break
141 }
Girish Gowdra408cd962020-03-11 14:31:31 -0700142 } 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 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500150 }
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 Hagermanfa9d6d42020-05-25 11:49:40 -0400165func 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 Hagerman2b216042020-04-03 18:28:56 -0400200func waitUntilConditionForDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isDevicesConditionSatisfied) error {
khenaidoob64fc8a2019-11-27 15:08:19 -0500201 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 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500226}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500227
Kent Hagerman2b216042020-04-03 18:28:56 -0400228func waitUntilConditionForLogicalDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isLogicalDevicesConditionSatisfied) error {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500229 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}
khenaidoo67b22152020-03-02 16:01:25 -0500255
Kent Hagerman2b216042020-04-03 18:28:56 -0400256func waitUntilCondition(timeout time.Duration, nbi *NBIHandler, verificationFunction isConditionSatisfied) error {
khenaidoo67b22152020-03-02 16:01:25 -0500257 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}