khenaidoo | b920354 | 2018-09-17 22:56:37 -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 | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "crypto/rand" |
| 21 | "encoding/hex" |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 22 | "errors" |
| 23 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 24 | m "math/rand" |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 25 | "strconv" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func randomHex(n int) (string, error) { |
| 29 | bytes := make([]byte, n) |
| 30 | if _, err := rand.Read(bytes); err != nil { |
| 31 | return "", err |
| 32 | } |
| 33 | return hex.EncodeToString(bytes), nil |
| 34 | } |
| 35 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 36 | // CreateDeviceID produces a device ID. DeviceId is 16 hex long - lower 12 hex is the device id. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | // TODO: A cluster unique ID may be required |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | func CreateDeviceID() string { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | val, _ := randomHex(12) |
| 40 | return val |
| 41 | } |
| 42 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 43 | // CreateLogicalDeviceID is not used for now as the logical device ID is derived from the |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | // OLT MAC address |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 45 | func CreateLogicalDeviceID() string { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | // logical device id is 16 hex long - lower 12 hex is the logical device id. For now just generate the 12 hex |
| 47 | val, _ := randomHex(12) |
| 48 | return val |
| 49 | } |
| 50 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 51 | // CreateLogicalPortID produces a random port ID for a logical device. |
| 52 | func CreateLogicalPortID() uint32 { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | // A logical port is a uint32 |
| 54 | return m.Uint32() |
| 55 | } |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 56 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 57 | // CreateDataPathID creates uint64 pathid from string pathid |
| 58 | func CreateDataPathID(idInHexString string) (uint64, error) { |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 59 | if idInHexString == "" { |
| 60 | return 0, errors.New("id-empty") |
| 61 | } |
| 62 | // First prepend 0x to the string |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 63 | newID := fmt.Sprintf("0x%s", idInHexString) |
| 64 | d, err := strconv.ParseUint(newID, 0, 64) |
| 65 | if err != nil { |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 66 | return 0, err |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 67 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 68 | return d, nil |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 69 | } |