blob: 798b4d80ddf5784410baa25b9cd5cff590112181 [file] [log] [blame]
khenaidoo1ce37ad2019-03-24 22:07:24 -04001/*
2 * Copyright 2018-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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoo1ce37ad2019-03-24 22:07:24 -040017package utils
18
khenaidoo2c6a0992019-04-29 13:46:56 -040019import (
khenaidoo631fe542019-05-31 15:44:43 -040020 "os"
khenaidoo2c6a0992019-04-29 13:46:56 -040021 "time"
npujar1d86a522019-11-14 17:11:16 +053022
23 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
khenaidoo2c6a0992019-04-29 13:46:56 -040025)
26
khenaidoo442e7c72020-03-10 16:13:48 -040027// ResponseCallback is the function signature for callbacks to execute after a response is received.
28type ResponseCallback func(rpc string, response interface{}, reqArgs ...interface{})
29
npujar1d86a522019-11-14 17:11:16 +053030// DeviceID represent device id attribute
khenaidoo1ce37ad2019-03-24 22:07:24 -040031type DeviceID struct {
npujar1d86a522019-11-14 17:11:16 +053032 ID string
khenaidoo1ce37ad2019-03-24 22:07:24 -040033}
34
npujar1d86a522019-11-14 17:11:16 +053035// LogicalDeviceID rpresent logical device id attribute
khenaidoo1ce37ad2019-03-24 22:07:24 -040036type LogicalDeviceID struct {
npujar1d86a522019-11-14 17:11:16 +053037 ID string
khenaidoo1ce37ad2019-03-24 22:07:24 -040038}
khenaidoo2c6a0992019-04-29 13:46:56 -040039
npujar1d86a522019-11-14 17:11:16 +053040// GetHostName returns host name
khenaidoo631fe542019-05-31 15:44:43 -040041func GetHostName() string {
42 return os.Getenv("HOSTNAME")
43}
44
npujar1d86a522019-11-14 17:11:16 +053045// Response -
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050046type Response struct {
47 *response
48}
49type response struct {
50 err error
51 ch chan struct{}
52 done bool
53}
54
npujar1d86a522019-11-14 17:11:16 +053055// NewResponse -
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050056func NewResponse() Response {
57 return Response{
58 &response{
59 ch: make(chan struct{}),
60 },
61 }
62}
63
A R Karthick5c28f552019-12-11 22:47:44 -080064// Fake a completed response.
65func DoneResponse() Response {
66 r := Response{
67 &response{
68 err: nil,
69 ch: make(chan struct{}),
70 done: true,
71 },
72 }
73 close(r.ch)
74 return r
75}
76
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050077// Error sends a response with the given error. It may only be called once.
78func (r Response) Error(err error) {
79 // if this is called twice, it will panic; this is intentional
80 r.err = err
81 r.done = true
82 close(r.ch)
83}
84
85// Done sends a non-error response unless Error has already been called, in which case this is a no-op.
86func (r Response) Done() {
87 if !r.done {
88 close(r.ch)
89 }
90}
91
khenaidoo2c6a0992019-04-29 13:46:56 -040092//WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error
93//response. If an error is received from a given channel then the returned error array will contain that error.
94//The error will be at the index corresponding to the order in which the channel appear in the parameter list.
95//If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a
96//timeout is obtained then this function will stop waiting for the remaining responses and abort.
khenaidoo442e7c72020-03-10 16:13:48 -040097func WaitForNilOrErrorResponses(timeout time.Duration, responses ...Response) []error {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050098 timedOut := make(chan struct{})
khenaidoo442e7c72020-03-10 16:13:48 -040099 timer := time.AfterFunc(timeout, func() { close(timedOut) })
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500100 defer timer.Stop()
khenaidoo2c6a0992019-04-29 13:46:56 -0400101
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500102 gotError := false
103 errors := make([]error, 0, len(responses))
104 for _, response := range responses {
105 var err error
106 select {
107 case <-response.ch:
108 // if a response is already available, use it
109 err = response.err
110 default:
111 // otherwise, wait for either a response or a timeout
112 select {
113 case <-response.ch:
114 err = response.err
115 case <-timedOut:
116 err = status.Error(codes.Aborted, "timeout")
khenaidoo2c6a0992019-04-29 13:46:56 -0400117 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400118 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500119 gotError = gotError || err != nil
120 errors = append(errors, err)
khenaidoo2c6a0992019-04-29 13:46:56 -0400121 }
122
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500123 if gotError {
khenaidoo2c6a0992019-04-29 13:46:56 -0400124 return errors
125 }
126 return nil
127}