khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 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 | "os" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 22 | "os/signal" |
| 23 | "syscall" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 24 | "time" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/peer" |
| 29 | "google.golang.org/grpc/status" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type contextKey string |
| 33 | |
| 34 | func (c contextKey) String() string { |
| 35 | return string(c) |
| 36 | } |
| 37 | |
| 38 | var ( |
| 39 | // RPCContextKey for keeping rpc name as metadata |
| 40 | rpcContextKey = contextKey("rpc") |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 41 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 42 | // endpointKey for keeping the client endpoint making an RPC request |
| 43 | endpointKey = contextKey("endpoint") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 44 | ) |
| 45 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 46 | // 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] | 47 | type ResponseCallback func(ctx context.Context, rpc string, response interface{}, reqArgs ...interface{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 48 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 49 | // DeviceID represent device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 50 | type DeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 51 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 52 | } |
| 53 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 54 | // LogicalDeviceID rpresent logical device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 55 | type LogicalDeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 56 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 57 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 58 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 59 | // GetHostName returns host name |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 60 | func GetHostName() string { |
| 61 | return os.Getenv("HOSTNAME") |
| 62 | } |
| 63 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 64 | // Response - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 65 | type Response struct { |
| 66 | *response |
| 67 | } |
| 68 | type response struct { |
| 69 | err error |
| 70 | ch chan struct{} |
| 71 | done bool |
| 72 | } |
| 73 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 74 | // NewResponse - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 75 | func NewResponse() Response { |
| 76 | return Response{ |
| 77 | &response{ |
| 78 | ch: make(chan struct{}), |
| 79 | }, |
| 80 | } |
| 81 | } |
| 82 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 83 | // Fake a completed response. |
| 84 | func DoneResponse() Response { |
| 85 | r := Response{ |
| 86 | &response{ |
| 87 | err: nil, |
| 88 | ch: make(chan struct{}), |
| 89 | done: true, |
| 90 | }, |
| 91 | } |
| 92 | close(r.ch) |
| 93 | return r |
| 94 | } |
| 95 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 96 | // Error sends a response with the given error. It may only be called once. |
| 97 | func (r Response) Error(err error) { |
| 98 | // if this is called twice, it will panic; this is intentional |
| 99 | r.err = err |
| 100 | r.done = true |
| 101 | close(r.ch) |
| 102 | } |
| 103 | |
| 104 | // Done sends a non-error response unless Error has already been called, in which case this is a no-op. |
| 105 | func (r Response) Done() { |
| 106 | if !r.done { |
| 107 | close(r.ch) |
| 108 | } |
| 109 | } |
| 110 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 111 | //WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error |
| 112 | //response. If an error is received from a given channel then the returned error array will contain that error. |
| 113 | //The error will be at the index corresponding to the order in which the channel appear in the parameter list. |
| 114 | //If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a |
| 115 | //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] | 116 | func WaitForNilOrErrorResponses(timeout time.Duration, responses ...Response) []error { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 117 | timedOut := make(chan struct{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 118 | timer := time.AfterFunc(timeout, func() { close(timedOut) }) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 119 | defer timer.Stop() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 120 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 121 | gotError := false |
| 122 | errors := make([]error, 0, len(responses)) |
| 123 | for _, response := range responses { |
| 124 | var err error |
| 125 | select { |
| 126 | case <-response.ch: |
| 127 | // if a response is already available, use it |
| 128 | err = response.err |
| 129 | default: |
| 130 | // otherwise, wait for either a response or a timeout |
| 131 | select { |
| 132 | case <-response.ch: |
| 133 | err = response.err |
| 134 | case <-timedOut: |
| 135 | err = status.Error(codes.Aborted, "timeout") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 136 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 137 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 138 | gotError = gotError || err != nil |
| 139 | errors = append(errors, err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 142 | if gotError { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 143 | return errors |
| 144 | } |
| 145 | return nil |
| 146 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 147 | |
| 148 | func WithRPCMetadataContext(ctx context.Context, rpcName string) context.Context { |
| 149 | ctx = context.WithValue(ctx, rpcContextKey, rpcName) |
| 150 | return ctx |
| 151 | } |
| 152 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 153 | func WithCoreEndpointContext(ctx context.Context) context.Context { |
| 154 | ctx = context.WithValue(ctx, endpointKey, "CORE") |
| 155 | return ctx |
| 156 | } |
| 157 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 158 | func GetRPCMetadataFromContext(ctx context.Context) string { |
| 159 | if ctx != nil { |
| 160 | if val, ok := ctx.Value(rpcContextKey).(string); ok { |
| 161 | return val |
| 162 | } |
| 163 | } |
| 164 | return "" |
| 165 | } |
| 166 | |
| 167 | func WithRPCMetadataFromContext(targetCtx, sourceCtx context.Context) context.Context { |
| 168 | if sourceCtx != nil { |
| 169 | if val, ok := sourceCtx.Value(rpcContextKey).(string); ok { |
| 170 | targetCtx = context.WithValue(targetCtx, rpcContextKey, val) |
| 171 | } |
| 172 | } |
| 173 | return targetCtx |
| 174 | } |
| 175 | |
| 176 | func WithSpanAndRPCMetadataFromContext(sourceCtx context.Context) context.Context { |
| 177 | targetCtx := context.Background() |
| 178 | if sourceCtx != nil { |
| 179 | targetCtx = log.WithSpanFromContext(targetCtx, sourceCtx) |
| 180 | targetCtx = WithRPCMetadataFromContext(targetCtx, sourceCtx) |
| 181 | } |
| 182 | return targetCtx |
| 183 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 184 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 185 | func WithRPCMetadataAndEndpointFromContext(targetCtx, sourceCtx context.Context) context.Context { |
| 186 | if sourceCtx != nil { |
| 187 | targetCtx = WithRPCMetadataFromContext(targetCtx, sourceCtx) |
| 188 | targetCtx = WithCoreEndpointContext(targetCtx) |
| 189 | } |
| 190 | return targetCtx |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 191 | } |
| 192 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 193 | func WithAllMetadataFromContext(sourceCtx context.Context) context.Context { |
| 194 | targetCtx := context.Background() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 195 | if sourceCtx != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 196 | targetCtx = log.WithSpanFromContext(targetCtx, sourceCtx) |
| 197 | targetCtx = WithRPCMetadataFromContext(targetCtx, sourceCtx) |
| 198 | targetCtx = WithCoreEndpointContext(targetCtx) |
| 199 | } |
| 200 | return targetCtx |
| 201 | } |
| 202 | |
| 203 | func WithEndpointMetadataFromContext(targetCtx, sourceCtx context.Context) context.Context { |
| 204 | if sourceCtx != nil { |
| 205 | if val, ok := sourceCtx.Value(endpointKey).(string); ok { |
| 206 | targetCtx = context.WithValue(targetCtx, endpointKey, val) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | return targetCtx |
| 210 | } |
| 211 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 212 | func WithNewSpanAndRPCMetadataContext(sourceCtx context.Context, rpcName string) context.Context { |
| 213 | targetCtx := context.Background() |
| 214 | if sourceCtx != nil { |
| 215 | sourceEndpoint := "" |
| 216 | if p, ok := peer.FromContext(sourceCtx); ok { |
| 217 | sourceEndpoint = p.Addr.String() |
| 218 | } |
| 219 | targetCtx = log.WithSpanFromContext(targetCtx, sourceCtx) |
| 220 | targetCtx = context.WithValue(targetCtx, rpcContextKey, rpcName) |
| 221 | targetCtx = context.WithValue(targetCtx, endpointKey, sourceEndpoint) |
| 222 | } |
| 223 | return targetCtx |
| 224 | } |
| 225 | |
| 226 | func GetEndpointMetadataFromContext(ctx context.Context) string { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 227 | if ctx != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 228 | if val, ok := ctx.Value(endpointKey).(string); ok { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 229 | return val |
| 230 | } |
| 231 | } |
| 232 | return "" |
| 233 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 234 | |
| 235 | func WaitForExit(ctx context.Context) int { |
| 236 | signalChannel := make(chan os.Signal, 1) |
| 237 | signal.Notify(signalChannel, |
| 238 | syscall.SIGHUP, |
| 239 | syscall.SIGINT, |
| 240 | syscall.SIGTERM, |
| 241 | syscall.SIGQUIT) |
| 242 | |
| 243 | s := <-signalChannel |
| 244 | switch s { |
| 245 | case syscall.SIGHUP, |
| 246 | syscall.SIGINT, |
| 247 | syscall.SIGTERM, |
| 248 | syscall.SIGQUIT: |
| 249 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
| 250 | return 0 |
| 251 | default: |
| 252 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
| 253 | return 1 |
| 254 | } |
| 255 | } |