blob: cb8737773b6eb69abd2ad6ff4dc0c00afa5b463a [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
18
19import (
khenaidooca301322019-01-09 23:06:32 -050020 "errors"
21 "fmt"
David Bainbridged1afd662020-03-26 18:27:41 -070022 "math/rand"
khenaidooca301322019-01-09 23:06:32 -050023 "strconv"
David Bainbridged1afd662020-03-26 18:27:41 -070024
25 "github.com/google/uuid"
khenaidoob9203542018-09-17 22:56:37 -040026)
27
David Bainbridged1afd662020-03-26 18:27:41 -070028// CreateDeviceID produces a device ID. The device ID is a UUID
npujar1d86a522019-11-14 17:11:16 +053029func CreateDeviceID() string {
David Bainbridged1afd662020-03-26 18:27:41 -070030 return uuid.New().String()
khenaidoob9203542018-09-17 22:56:37 -040031}
32
David Bainbridged1afd662020-03-26 18:27:41 -070033// CreateLogicalDeviceID produces a logical device ID. The logical device ID is a UUID
npujar1d86a522019-11-14 17:11:16 +053034func CreateLogicalDeviceID() string {
David Bainbridged1afd662020-03-26 18:27:41 -070035 return uuid.New().String()
khenaidoob9203542018-09-17 22:56:37 -040036}
37
npujar1d86a522019-11-14 17:11:16 +053038// CreateLogicalPortID produces a random port ID for a logical device.
39func CreateLogicalPortID() uint32 {
khenaidoob9203542018-09-17 22:56:37 -040040 // A logical port is a uint32
David Bainbridged1afd662020-03-26 18:27:41 -070041 return rand.Uint32()
khenaidoob9203542018-09-17 22:56:37 -040042}
khenaidooca301322019-01-09 23:06:32 -050043
npujar1d86a522019-11-14 17:11:16 +053044// CreateDataPathID creates uint64 pathid from string pathid
45func CreateDataPathID(idInHexString string) (uint64, error) {
khenaidooca301322019-01-09 23:06:32 -050046 if idInHexString == "" {
47 return 0, errors.New("id-empty")
48 }
49 // First prepend 0x to the string
npujar1d86a522019-11-14 17:11:16 +053050 newID := fmt.Sprintf("0x%s", idInHexString)
51 d, err := strconv.ParseUint(newID, 0, 64)
52 if err != nil {
khenaidooca301322019-01-09 23:06:32 -050053 return 0, err
khenaidooca301322019-01-09 23:06:32 -050054 }
npujar1d86a522019-11-14 17:11:16 +053055 return d, nil
khenaidooca301322019-01-09 23:06:32 -050056}