blob: 584d923dbbb04076544dd3f1a88f54355203f376 [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
npujar1d86a522019-11-14 17:11:16 +053027// DeviceID represent device id attribute
khenaidoo1ce37ad2019-03-24 22:07:24 -040028type DeviceID struct {
npujar1d86a522019-11-14 17:11:16 +053029 ID string
khenaidoo1ce37ad2019-03-24 22:07:24 -040030}
31
npujar1d86a522019-11-14 17:11:16 +053032// LogicalDeviceID rpresent logical device id attribute
khenaidoo1ce37ad2019-03-24 22:07:24 -040033type LogicalDeviceID struct {
npujar1d86a522019-11-14 17:11:16 +053034 ID string
khenaidoo1ce37ad2019-03-24 22:07:24 -040035}
khenaidoo2c6a0992019-04-29 13:46:56 -040036
npujar1d86a522019-11-14 17:11:16 +053037// GetHostName returns host name
khenaidoo631fe542019-05-31 15:44:43 -040038func GetHostName() string {
39 return os.Getenv("HOSTNAME")
40}
41
npujar1d86a522019-11-14 17:11:16 +053042// Response -
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050043type Response struct {
44 *response
45}
46type response struct {
47 err error
48 ch chan struct{}
49 done bool
50}
51
npujar1d86a522019-11-14 17:11:16 +053052// NewResponse -
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050053func 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.
62func (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.
70func (r Response) Done() {
71 if !r.done {
72 close(r.ch)
73 }
74}
75
khenaidoo2c6a0992019-04-29 13:46:56 -040076//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 Hagerman8da2f1e2019-11-25 17:28:09 -050081func 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()
khenaidoo2c6a0992019-04-29 13:46:56 -040085
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050086 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")
khenaidoo2c6a0992019-04-29 13:46:56 -0400101 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400102 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500103 gotError = gotError || err != nil
104 errors = append(errors, err)
khenaidoo2c6a0992019-04-29 13:46:56 -0400105 }
106
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500107 if gotError {
khenaidoo2c6a0992019-04-29 13:46:56 -0400108 return errors
109 }
110 return nil
111}