blob: b28151f706cdfb0d1c97135f7812b4bed5b1d2c2 [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 */
16package core
17
18import (
19 "crypto/rand"
20 "encoding/hex"
21 m "math/rand"
22)
23
24func randomHex(n int) (string, error) {
25 bytes := make([]byte, n)
26 if _, err := rand.Read(bytes); err != nil {
27 return "", err
28 }
29 return hex.EncodeToString(bytes), nil
30}
31
32// CreateDeviceId produces a device ID. DeviceId is 16 hex long - lower 12 hex is the device id.
33// TODO: A cluster unique ID may be required
34func CreateDeviceId() string {
35 val, _ := randomHex(12)
36 return val
37}
38
39// CreateLogicalDeviceId is not used for now as the logical device ID is derived from the
40// OLT MAC address
41func CreateLogicalDeviceId() string {
42 // logical device id is 16 hex long - lower 12 hex is the logical device id. For now just generate the 12 hex
43 val, _ := randomHex(12)
44 return val
45}
46
khenaidoo9a468962018-09-19 15:33:13 -040047// CreateLogicalPortId produces a random port ID for a logical device.
khenaidoob9203542018-09-17 22:56:37 -040048func CreateLogicalPortId() uint32 {
49 // A logical port is a uint32
50 return m.Uint32()
51}