blob: 94e8bd6caef92268fb2edb7216f94efa843c9b16 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 */
16package common
17
18import (
19 "fmt"
Matteo Scandolo1530d412020-01-30 14:57:46 -080020 "github.com/opencord/voltha-lib-go/v3/pkg/log"
21 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
22 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070023 "math/rand"
24 "time"
25)
26
27//GetRandomSerialNumber returns a serial number formatted as "HOST:PORT"
28func GetRandomSerialNumber() string {
29 rand.Seed(time.Now().UnixNano())
30 return fmt.Sprintf("%d.%d.%d.%d:%d",
31 rand.Intn(255),
32 rand.Intn(255),
33 rand.Intn(255),
34 rand.Intn(255),
35 rand.Intn(9000)+1000,
36 )
37}
38
39//GetRandomMacAddress returns a random mac address
40func GetRandomMacAddress() string {
41 rand.Seed(time.Now().UnixNano())
42 return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
43 rand.Intn(128),
44 rand.Intn(128),
45 rand.Intn(128),
46 rand.Intn(128),
47 rand.Intn(128),
48 rand.Intn(128),
49 )
50}
51
52const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
53const (
54 letterIdxBits = 6 // 6 bits to represent a letter index
55 letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
56 letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
57)
58
59var src = rand.NewSource(time.Now().UnixNano())
60
61func GetRandomString(n int) string {
62 b := make([]byte, n)
63 // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
64 for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
65 if remain == 0 {
66 cache, remain = src.Int63(), letterIdxMax
67 }
68 if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
69 b[i] = letterBytes[idx]
70 i--
71 }
72 cache >>= letterIdxBits
73 remain--
74 }
75 return string(b)
76}
Matteo Scandolo1530d412020-01-30 14:57:46 -080077
Scott Bakere6685952020-06-23 04:05:39 +000078func ICProxyErrorCodeToGrpcErrorCode(icErr ic.ErrorCodeCodes) codes.Code {
Matteo Scandolo1530d412020-01-30 14:57:46 -080079 switch icErr {
80 case ic.ErrorCode_INVALID_PARAMETERS:
81 return codes.InvalidArgument
82 case ic.ErrorCode_UNSUPPORTED_REQUEST:
83 return codes.Unavailable
84 case ic.ErrorCode_DEADLINE_EXCEEDED:
85 return codes.DeadlineExceeded
86 default:
Scott Bakere6685952020-06-23 04:05:39 +000087 logger.Warnw("cannnot-map-ic-error-code-to-grpc-error-code", log.Fields{"err": icErr})
Matteo Scandolo1530d412020-01-30 14:57:46 -080088 return codes.Internal
89 }
90}