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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 27 | // DeviceID represent device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 28 | type DeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 29 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 30 | } |
| 31 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 32 | // LogicalDeviceID rpresent logical device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 33 | type LogicalDeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 34 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 35 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 36 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 37 | // GetHostName returns host name |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 38 | func GetHostName() string { |
| 39 | return os.Getenv("HOSTNAME") |
| 40 | } |
| 41 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | // Response - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 43 | type Response struct { |
| 44 | *response |
| 45 | } |
| 46 | type response struct { |
| 47 | err error |
| 48 | ch chan struct{} |
| 49 | done bool |
| 50 | } |
| 51 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 52 | // NewResponse - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 53 | func NewResponse() Response { |
| 54 | return Response{ |
| 55 | &response{ |
| 56 | ch: make(chan struct{}), |
| 57 | }, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Error sends a response with the given error. It may only be called once. |
| 62 | func (r Response) Error(err error) { |
| 63 | // if this is called twice, it will panic; this is intentional |
| 64 | r.err = err |
| 65 | r.done = true |
| 66 | close(r.ch) |
| 67 | } |
| 68 | |
| 69 | // Done sends a non-error response unless Error has already been called, in which case this is a no-op. |
| 70 | func (r Response) Done() { |
| 71 | if !r.done { |
| 72 | close(r.ch) |
| 73 | } |
| 74 | } |
| 75 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 76 | //WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error |
| 77 | //response. If an error is received from a given channel then the returned error array will contain that error. |
| 78 | //The error will be at the index corresponding to the order in which the channel appear in the parameter list. |
| 79 | //If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a |
| 80 | //timeout is obtained then this function will stop waiting for the remaining responses and abort. |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 81 | func WaitForNilOrErrorResponses(timeout int64, responses ...Response) []error { |
| 82 | timedOut := make(chan struct{}) |
| 83 | timer := time.AfterFunc(time.Duration(timeout)*time.Millisecond, func() { close(timedOut) }) |
| 84 | defer timer.Stop() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 85 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 86 | gotError := false |
| 87 | errors := make([]error, 0, len(responses)) |
| 88 | for _, response := range responses { |
| 89 | var err error |
| 90 | select { |
| 91 | case <-response.ch: |
| 92 | // if a response is already available, use it |
| 93 | err = response.err |
| 94 | default: |
| 95 | // otherwise, wait for either a response or a timeout |
| 96 | select { |
| 97 | case <-response.ch: |
| 98 | err = response.err |
| 99 | case <-timedOut: |
| 100 | err = status.Error(codes.Aborted, "timeout") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 101 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 102 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 103 | gotError = gotError || err != nil |
| 104 | errors = append(errors, err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 107 | if gotError { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 108 | return errors |
| 109 | } |
| 110 | return nil |
| 111 | } |