blob: 399bf1df5bc1742d5247124564989e0497bcca90 [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 (
20 "crypto/rand"
21 "encoding/hex"
khenaidooca301322019-01-09 23:06:32 -050022 "errors"
23 "fmt"
khenaidoob9203542018-09-17 22:56:37 -040024 m "math/rand"
khenaidooca301322019-01-09 23:06:32 -050025 "strconv"
khenaidoob9203542018-09-17 22:56:37 -040026)
27
28func 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
npujar1d86a522019-11-14 17:11:16 +053036// CreateDeviceID produces a device ID. DeviceId is 16 hex long - lower 12 hex is the device id.
khenaidoob9203542018-09-17 22:56:37 -040037// TODO: A cluster unique ID may be required
npujar1d86a522019-11-14 17:11:16 +053038func CreateDeviceID() string {
khenaidoob9203542018-09-17 22:56:37 -040039 val, _ := randomHex(12)
40 return val
41}
42
npujar1d86a522019-11-14 17:11:16 +053043// CreateLogicalDeviceID is not used for now as the logical device ID is derived from the
khenaidoob9203542018-09-17 22:56:37 -040044// OLT MAC address
npujar1d86a522019-11-14 17:11:16 +053045func CreateLogicalDeviceID() string {
khenaidoob9203542018-09-17 22:56:37 -040046 // 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
npujar1d86a522019-11-14 17:11:16 +053051// CreateLogicalPortID produces a random port ID for a logical device.
52func CreateLogicalPortID() uint32 {
khenaidoob9203542018-09-17 22:56:37 -040053 // A logical port is a uint32
54 return m.Uint32()
55}
khenaidooca301322019-01-09 23:06:32 -050056
npujar1d86a522019-11-14 17:11:16 +053057// CreateDataPathID creates uint64 pathid from string pathid
58func CreateDataPathID(idInHexString string) (uint64, error) {
khenaidooca301322019-01-09 23:06:32 -050059 if idInHexString == "" {
60 return 0, errors.New("id-empty")
61 }
62 // First prepend 0x to the string
npujar1d86a522019-11-14 17:11:16 +053063 newID := fmt.Sprintf("0x%s", idInHexString)
64 d, err := strconv.ParseUint(newID, 0, 64)
65 if err != nil {
khenaidooca301322019-01-09 23:06:32 -050066 return 0, err
khenaidooca301322019-01-09 23:06:32 -050067 }
npujar1d86a522019-11-14 17:11:16 +053068 return d, nil
khenaidooca301322019-01-09 23:06:32 -050069}