blob: 097b1c13016251048d576b99cedd04cbc5c69288 [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
39type isDeviceConditionSatisfied func(ld *voltha.Device) bool
40type isDevicesConditionSatisfied func(ds *voltha.Devices) bool
khenaidoo93d5a3d2020-01-15 12:37:05 -050041type isLogicalDevicesConditionSatisfied func(lds *voltha.LogicalDevices) bool
khenaidoo67b22152020-03-02 16:01:25 -050042type isConditionSatisfied func() bool
khenaidoob64fc8a2019-11-27 15:08:19 -050043
khenaidooab1f7bd2019-11-14 14:00:27 -050044func init() {
khenaidoob64fc8a2019-11-27 15:08:19 -050045 //Default mode is two rw-core running in a pair of competing cores
46 coreInCompeteMode = true
47}
48
49func setCoreCompeteMode(mode bool) {
50 coreInCompeteMode = mode
51}
52
53func 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
khenaidoob64fc8a2019-11-27 15:08:19 -050060func waitUntilDeviceReadiness(deviceID string,
61 timeout time.Duration,
62 verificationFunction isDeviceConditionSatisfied,
Kent Hagerman2b216042020-04-03 18:28:56 -040063 nbi *NBIHandler) error {
khenaidoob64fc8a2019-11-27 15:08:19 -050064 ch := make(chan int, 1)
65 done := false
66 go func() {
67 for {
68 device, _ := nbi.GetDevice(getContext(), &voltha.ID{Id: deviceID})
Chaitrashree G Se8ad0202020-02-27 18:48:00 -050069 if verificationFunction(device) {
khenaidoob64fc8a2019-11-27 15:08:19 -050070 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
90func waitUntilLogicalDeviceReadiness(oltDeviceID string,
91 timeout time.Duration,
Kent Hagerman2b216042020-04-03 18:28:56 -040092 nbi *NBIHandler,
khenaidoob64fc8a2019-11-27 15:08:19 -050093 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})
khenaidoo67b22152020-03-02 16:01:25 -0500103 if verificationFunction(ld) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500104 ch <- 1
105 break
106 }
107 if done {
108 break
109 }
Girish Gowdra408cd962020-03-11 14:31:31 -0700110 } 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 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500118 }
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 Hagerman2b216042020-04-03 18:28:56 -0400133func waitUntilConditionForDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isDevicesConditionSatisfied) error {
khenaidoob64fc8a2019-11-27 15:08:19 -0500134 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 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500159}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500160
Kent Hagerman2b216042020-04-03 18:28:56 -0400161func waitUntilConditionForLogicalDevices(timeout time.Duration, nbi *NBIHandler, verificationFunction isLogicalDevicesConditionSatisfied) error {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500162 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}
khenaidoo67b22152020-03-02 16:01:25 -0500188
Kent Hagerman2b216042020-04-03 18:28:56 -0400189func waitUntilCondition(timeout time.Duration, nbi *NBIHandler, verificationFunction isConditionSatisfied) error {
khenaidoo67b22152020-03-02 16:01:25 -0500190 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}