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 ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 20 | "context" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 21 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | "google.golang.org/grpc/codes" |
| 23 | "google.golang.org/grpc/status" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 24 | "os" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | type contextKey string |
| 29 | |
| 30 | func (c contextKey) String() string { |
| 31 | return string(c) |
| 32 | } |
| 33 | |
| 34 | var ( |
| 35 | // RPCContextKey for keeping rpc name as metadata |
| 36 | rpcContextKey = contextKey("rpc") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 37 | ) |
| 38 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 39 | // ResponseCallback is the function signature for callbacks to execute after a response is received. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 40 | type ResponseCallback func(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 41 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | // DeviceID represent device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 43 | type DeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 44 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 45 | } |
| 46 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 47 | // LogicalDeviceID rpresent logical device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 48 | type LogicalDeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 49 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 50 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 51 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 52 | // GetHostName returns host name |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 53 | func GetHostName() string { |
| 54 | return os.Getenv("HOSTNAME") |
| 55 | } |
| 56 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 57 | // Response - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 58 | type Response struct { |
| 59 | *response |
| 60 | } |
| 61 | type response struct { |
| 62 | err error |
| 63 | ch chan struct{} |
| 64 | done bool |
| 65 | } |
| 66 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 67 | // NewResponse - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 68 | func NewResponse() Response { |
| 69 | return Response{ |
| 70 | &response{ |
| 71 | ch: make(chan struct{}), |
| 72 | }, |
| 73 | } |
| 74 | } |
| 75 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 76 | // Fake a completed response. |
| 77 | func DoneResponse() Response { |
| 78 | r := Response{ |
| 79 | &response{ |
| 80 | err: nil, |
| 81 | ch: make(chan struct{}), |
| 82 | done: true, |
| 83 | }, |
| 84 | } |
| 85 | close(r.ch) |
| 86 | return r |
| 87 | } |
| 88 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 89 | // Error sends a response with the given error. It may only be called once. |
| 90 | func (r Response) Error(err error) { |
| 91 | // if this is called twice, it will panic; this is intentional |
| 92 | r.err = err |
| 93 | r.done = true |
| 94 | close(r.ch) |
| 95 | } |
| 96 | |
| 97 | // Done sends a non-error response unless Error has already been called, in which case this is a no-op. |
| 98 | func (r Response) Done() { |
| 99 | if !r.done { |
| 100 | close(r.ch) |
| 101 | } |
| 102 | } |
| 103 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 104 | //WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error |
| 105 | //response. If an error is received from a given channel then the returned error array will contain that error. |
| 106 | //The error will be at the index corresponding to the order in which the channel appear in the parameter list. |
| 107 | //If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a |
| 108 | //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] | 109 | func WaitForNilOrErrorResponses(timeout time.Duration, responses ...Response) []error { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 110 | timedOut := make(chan struct{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 111 | timer := time.AfterFunc(timeout, func() { close(timedOut) }) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 112 | defer timer.Stop() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 113 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 114 | gotError := false |
| 115 | errors := make([]error, 0, len(responses)) |
| 116 | for _, response := range responses { |
| 117 | var err error |
| 118 | select { |
| 119 | case <-response.ch: |
| 120 | // if a response is already available, use it |
| 121 | err = response.err |
| 122 | default: |
| 123 | // otherwise, wait for either a response or a timeout |
| 124 | select { |
| 125 | case <-response.ch: |
| 126 | err = response.err |
| 127 | case <-timedOut: |
| 128 | err = status.Error(codes.Aborted, "timeout") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 129 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 130 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 131 | gotError = gotError || err != nil |
| 132 | errors = append(errors, err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 135 | if gotError { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 136 | return errors |
| 137 | } |
| 138 | return nil |
| 139 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame^] | 140 | |
| 141 | func WithRPCMetadataContext(ctx context.Context, rpcName string) context.Context { |
| 142 | ctx = context.WithValue(ctx, rpcContextKey, rpcName) |
| 143 | return ctx |
| 144 | } |
| 145 | |
| 146 | func GetRPCMetadataFromContext(ctx context.Context) string { |
| 147 | if ctx != nil { |
| 148 | if val, ok := ctx.Value(rpcContextKey).(string); ok { |
| 149 | return val |
| 150 | } |
| 151 | } |
| 152 | return "" |
| 153 | } |
| 154 | |
| 155 | func WithRPCMetadataFromContext(targetCtx, sourceCtx context.Context) context.Context { |
| 156 | if sourceCtx != nil { |
| 157 | if val, ok := sourceCtx.Value(rpcContextKey).(string); ok { |
| 158 | targetCtx = context.WithValue(targetCtx, rpcContextKey, val) |
| 159 | } |
| 160 | } |
| 161 | return targetCtx |
| 162 | } |
| 163 | |
| 164 | func WithSpanAndRPCMetadataFromContext(sourceCtx context.Context) context.Context { |
| 165 | targetCtx := context.Background() |
| 166 | if sourceCtx != nil { |
| 167 | targetCtx = log.WithSpanFromContext(targetCtx, sourceCtx) |
| 168 | targetCtx = WithRPCMetadataFromContext(targetCtx, sourceCtx) |
| 169 | } |
| 170 | return targetCtx |
| 171 | } |