blob: 6fcd511b53f71a568f18cf4334544558011eb666 [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-protos/v3/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
41type isDevicesConditionSatisfied func(ds *voltha.Devices) bool
khenaidoo93d5a3d2020-01-15 12:37:05 -050042type isLogicalDevicesConditionSatisfied func(lds *voltha.LogicalDevices) bool
khenaidoo67b22152020-03-02 16:01:25 -050043type isConditionSatisfied func() bool
khenaidoob64fc8a2019-11-27 15:08:19 -050044
khenaidooab1f7bd2019-11-14 14:00:27 -050045func init() {
khenaidoob64fc8a2019-11-27 15:08:19 -050046 //Default mode is two rw-core running in a pair of competing cores
47 coreInCompeteMode = true
48}
49
50func setCoreCompeteMode(mode bool) {
51 coreInCompeteMode = mode
52}
53
54func 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
khenaidoob64fc8a2019-11-27 15:08:19 -050061func waitUntilDeviceReadiness(deviceID string,
62 timeout time.Duration,
63 verificationFunction isDeviceConditionSatisfied,
Kent Hagerman2b216042020-04-03 18:28:56 -040064 nbi *NBIHandler) error {
khenaidoob64fc8a2019-11-27 15:08:19 -050065 ch := make(chan int, 1)
66 done := false
67 go func() {
68 for {
69 device, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: deviceID})
Chaitrashree G Se8ad0202020-02-27 18:48:00 -050070 if verificationFunction(device) {
khenaidoob64fc8a2019-11-27 15:08:19 -050071 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
91func waitUntilLogicalDeviceReadiness(oltDeviceID string,
92 timeout time.Duration,
Kent Hagerman2b216042020-04-03 18:28:56 -040093 nbi *NBIHandler,
khenaidoob64fc8a2019-11-27 15:08:19 -050094 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})
khenaidoo67b22152020-03-02 16:01:25 -0500104 if verificationFunction(ld) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500105 ch <- 1
106 break
107 }
108 if done {
109 break
110 }
Girish Gowdra408cd962020-03-11 14:31:31 -0700111 } 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 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500119 }
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 Hagermanfa9d6d42020-05-25 11:49:40 -0400134func 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 Hagerman2b216042020-04-03 18:28:56 -0400169func waitUntilConditionForDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isDevicesConditionSatisfied) error {
khenaidoob64fc8a2019-11-27 15:08:19 -0500170 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 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500195}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500196
Kent Hagerman2b216042020-04-03 18:28:56 -0400197func waitUntilConditionForLogicalDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isLogicalDevicesConditionSatisfied) error {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500198 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}
khenaidoo67b22152020-03-02 16:01:25 -0500224
Kent Hagerman2b216042020-04-03 18:28:56 -0400225func waitUntilCondition(timeout time.Duration, nbi *NBIHandler, verificationFunction isConditionSatisfied) error {
khenaidoo67b22152020-03-02 16:01:25 -0500226 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}