blob: 82465efb3d0eb9d64810583ae9be5bfa2bd111dd [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
A R Karthick5c28f552019-12-11 22:47:44 -080061// Fake a completed response.
62func DoneResponse() Response {
63 r := Response{
64 &response{
65 err: nil,
66 ch: make(chan struct{}),
67 done: true,
68 },
69 }
70 close(r.ch)
71 return r
72}
73
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050074// Error sends a response with the given error. It may only be called once.
75func (r Response) Error(err error) {
76 // if this is called twice, it will panic; this is intentional
77 r.err = err
78 r.done = true
79 close(r.ch)
80}
81
82// Done sends a non-error response unless Error has already been called, in which case this is a no-op.
83func (r Response) Done() {
84 if !r.done {
85 close(r.ch)
86 }
87}
88
khenaidoo2c6a0992019-04-29 13:46:56 -040089//WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error
90//response. If an error is received from a given channel then the returned error array will contain that error.
91//The error will be at the index corresponding to the order in which the channel appear in the parameter list.
92//If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a
93//timeout is obtained then this function will stop waiting for the remaining responses and abort.
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050094func WaitForNilOrErrorResponses(timeout int64, responses ...Response) []error {
95 timedOut := make(chan struct{})
96 timer := time.AfterFunc(time.Duration(timeout)*time.Millisecond, func() { close(timedOut) })
97 defer timer.Stop()
khenaidoo2c6a0992019-04-29 13:46:56 -040098
Kent Hagerman8da2f1e2019-11-25 17:28:09 -050099 gotError := false
100 errors := make([]error, 0, len(responses))
101 for _, response := range responses {
102 var err error
103 select {
104 case <-response.ch:
105 // if a response is already available, use it
106 err = response.err
107 default:
108 // otherwise, wait for either a response or a timeout
109 select {
110 case <-response.ch:
111 err = response.err
112 case <-timedOut:
113 err = status.Error(codes.Aborted, "timeout")
khenaidoo2c6a0992019-04-29 13:46:56 -0400114 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400115 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500116 gotError = gotError || err != nil
117 errors = append(errors, err)
khenaidoo2c6a0992019-04-29 13:46:56 -0400118 }
119
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500120 if gotError {
khenaidoo2c6a0992019-04-29 13:46:56 -0400121 return errors
122 }
123 return nil
124}