khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 17 | package utils |
| 18 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 19 | import ( |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 20 | "os" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 21 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | |
| 23 | "google.golang.org/grpc/codes" |
| 24 | "google.golang.org/grpc/status" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 27 | // ResponseCallback is the function signature for callbacks to execute after a response is received. |
| 28 | type ResponseCallback func(rpc string, response interface{}, reqArgs ...interface{}) |
| 29 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 30 | // DeviceID represent device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 31 | type DeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 32 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 33 | } |
| 34 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 35 | // LogicalDeviceID rpresent logical device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 36 | type LogicalDeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 37 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 38 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 39 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 40 | // GetHostName returns host name |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 41 | func GetHostName() string { |
| 42 | return os.Getenv("HOSTNAME") |
| 43 | } |
| 44 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 45 | // Response - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 46 | type Response struct { |
| 47 | *response |
| 48 | } |
| 49 | type response struct { |
| 50 | err error |
| 51 | ch chan struct{} |
| 52 | done bool |
| 53 | } |
| 54 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 55 | // NewResponse - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 56 | func NewResponse() Response { |
| 57 | return Response{ |
| 58 | &response{ |
| 59 | ch: make(chan struct{}), |
| 60 | }, |
| 61 | } |
| 62 | } |
| 63 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 64 | // Fake a completed response. |
| 65 | func 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 Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 77 | // Error sends a response with the given error. It may only be called once. |
| 78 | func (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. |
| 86 | func (r Response) Done() { |
| 87 | if !r.done { |
| 88 | close(r.ch) |
| 89 | } |
| 90 | } |
| 91 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 92 | //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. |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 97 | func WaitForNilOrErrorResponses(timeout time.Duration, responses ...Response) []error { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 98 | timedOut := make(chan struct{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 99 | timer := time.AfterFunc(timeout, func() { close(timedOut) }) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 100 | defer timer.Stop() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 101 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 102 | 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") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 117 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 118 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 119 | gotError = gotError || err != nil |
| 120 | errors = append(errors, err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 123 | if gotError { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 124 | return errors |
| 125 | } |
| 126 | return nil |
| 127 | } |