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 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package utils |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 20 | "errors" |
| 21 | "fmt" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 22 | "math/rand" |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 23 | "strconv" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 24 | |
| 25 | "github.com/google/uuid" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 28 | // CreateDeviceID produces a device ID. The device ID is a UUID |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 29 | func CreateDeviceID() string { |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 30 | return uuid.New().String() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 31 | } |
| 32 | |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 33 | // CreateLogicalDeviceID produces a logical device ID. The logical device ID is a UUID |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 34 | func CreateLogicalDeviceID() string { |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 35 | return uuid.New().String() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | } |
| 37 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | // CreateLogicalPortID produces a random port ID for a logical device. |
| 39 | func CreateLogicalPortID() uint32 { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | // A logical port is a uint32 |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 41 | return rand.Uint32() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | } |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 43 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 44 | // CreateDataPathID creates uint64 pathid from string pathid |
| 45 | func CreateDataPathID(idInHexString string) (uint64, error) { |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 46 | if idInHexString == "" { |
| 47 | return 0, errors.New("id-empty") |
| 48 | } |
| 49 | // First prepend 0x to the string |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 50 | newID := fmt.Sprintf("0x%s", idInHexString) |
| 51 | d, err := strconv.ParseUint(newID, 0, 64) |
| 52 | if err != nil { |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 53 | return 0, err |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 54 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 55 | return d, nil |
khenaidoo | ca30132 | 2019-01-09 23:06:32 -0500 | [diff] [blame] | 56 | } |