khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [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 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 19 | "context" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 20 | "fmt" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 21 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 22 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 24 | "math/rand" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | //GetRandomSerialNumber returns a serial number formatted as "HOST:PORT" |
| 29 | func GetRandomSerialNumber() string { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 30 | rand.Seed(time.Now().UnixNano()) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 31 | return fmt.Sprintf("%d.%d.%d.%d:%d", |
| 32 | rand.Intn(255), |
| 33 | rand.Intn(255), |
| 34 | rand.Intn(255), |
| 35 | rand.Intn(255), |
| 36 | rand.Intn(9000)+1000, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | //GetRandomMacAddress returns a random mac address |
| 41 | func GetRandomMacAddress() string { |
khenaidoo | 731697e | 2019-01-29 16:03:29 -0500 | [diff] [blame] | 42 | rand.Seed(time.Now().UnixNano()) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 43 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 44 | rand.Intn(128), |
| 45 | rand.Intn(128), |
| 46 | rand.Intn(128), |
| 47 | rand.Intn(128), |
| 48 | rand.Intn(128), |
| 49 | rand.Intn(128), |
| 50 | ) |
| 51 | } |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 52 | |
| 53 | const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 54 | const ( |
| 55 | letterIdxBits = 6 // 6 bits to represent a letter index |
| 56 | letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits |
| 57 | letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits |
| 58 | ) |
| 59 | |
| 60 | var src = rand.NewSource(time.Now().UnixNano()) |
| 61 | |
| 62 | func GetRandomString(n int) string { |
| 63 | b := make([]byte, n) |
| 64 | // A src.Int63() generates 63 random bits, enough for letterIdxMax characters! |
| 65 | for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { |
| 66 | if remain == 0 { |
| 67 | cache, remain = src.Int63(), letterIdxMax |
| 68 | } |
| 69 | if idx := int(cache & letterIdxMask); idx < len(letterBytes) { |
| 70 | b[i] = letterBytes[idx] |
| 71 | i-- |
| 72 | } |
| 73 | cache >>= letterIdxBits |
| 74 | remain-- |
| 75 | } |
| 76 | return string(b) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 77 | } |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame] | 78 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 79 | func ICProxyErrorCodeToGrpcErrorCode(ctx context.Context, icErr ic.ErrorCodeCodes) codes.Code { |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame] | 80 | switch icErr { |
| 81 | case ic.ErrorCode_INVALID_PARAMETERS: |
| 82 | return codes.InvalidArgument |
| 83 | case ic.ErrorCode_UNSUPPORTED_REQUEST: |
| 84 | return codes.Unavailable |
| 85 | case ic.ErrorCode_DEADLINE_EXCEEDED: |
| 86 | return codes.DeadlineExceeded |
| 87 | default: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 88 | logger.Warnw(ctx, "cannnot-map-ic-error-code-to-grpc-error-code", log.Fields{"err": icErr}) |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame] | 89 | return codes.Internal |
| 90 | } |
| 91 | } |