blob: 64501fe33d8c17833ff1e79267a41cf45b28071e [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
Joey Armstrong5f51f2e2023-01-17 17:06:26 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoob9203542018-09-17 22:56:37 -04003
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 */
npujar1d86a522019-11-14 17:11:16 +053016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package utils
khenaidoob9203542018-09-17 22:56:37 -040018
19import (
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +030020 cryptoRand "crypto/rand"
21 "encoding/binary"
khenaidooca301322019-01-09 23:06:32 -050022 "errors"
23 "fmt"
khenaidooca301322019-01-09 23:06:32 -050024 "strconv"
David Bainbridged1afd662020-03-26 18:27:41 -070025
26 "github.com/google/uuid"
khenaidoob9203542018-09-17 22:56:37 -040027)
28
David Bainbridged1afd662020-03-26 18:27:41 -070029// CreateDeviceID produces a device ID. The device ID is a UUID
npujar1d86a522019-11-14 17:11:16 +053030func CreateDeviceID() string {
David Bainbridged1afd662020-03-26 18:27:41 -070031 return uuid.New().String()
khenaidoob9203542018-09-17 22:56:37 -040032}
33
David Bainbridged1afd662020-03-26 18:27:41 -070034// CreateLogicalDeviceID produces a logical device ID. The logical device ID is a UUID
npujar1d86a522019-11-14 17:11:16 +053035func CreateLogicalDeviceID() string {
David Bainbridged1afd662020-03-26 18:27:41 -070036 return uuid.New().String()
khenaidoob9203542018-09-17 22:56:37 -040037}
38
npujar1d86a522019-11-14 17:11:16 +053039// CreateLogicalPortID produces a random port ID for a logical device.
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +030040func CreateLogicalPortID() (v uint64, err error) {
41 err = binary.Read(cryptoRand.Reader, binary.BigEndian, &v)
42 if err != nil {
43 return v, err
44 }
45
46 return v, nil
khenaidoob9203542018-09-17 22:56:37 -040047}
khenaidooca301322019-01-09 23:06:32 -050048
npujar1d86a522019-11-14 17:11:16 +053049// CreateDataPathID creates uint64 pathid from string pathid
50func CreateDataPathID(idInHexString string) (uint64, error) {
khenaidooca301322019-01-09 23:06:32 -050051 if idInHexString == "" {
52 return 0, errors.New("id-empty")
53 }
54 // First prepend 0x to the string
npujar1d86a522019-11-14 17:11:16 +053055 newID := fmt.Sprintf("0x%s", idInHexString)
56 d, err := strconv.ParseUint(newID, 0, 64)
57 if err != nil {
khenaidooca301322019-01-09 23:06:32 -050058 return 0, err
khenaidooca301322019-01-09 23:06:32 -050059 }
npujar1d86a522019-11-14 17:11:16 +053060 return d, nil
khenaidooca301322019-01-09 23:06:32 -050061}