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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "crypto/rand" |
| 20 | "encoding/hex" |
| 21 | m "math/rand" |
| 22 | ) |
| 23 | |
| 24 | func 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 |
| 34 | func 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 |
| 41 | func 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 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 47 | // CreateLogicalPortId produces a random port ID for a logical device. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 48 | func CreateLogicalPortId() uint32 { |
| 49 | // A logical port is a uint32 |
| 50 | return m.Uint32() |
| 51 | } |