blob: 984c19ca2b5ef3c2842035d34e0829f496f780f1 [file] [log] [blame]
khenaidoo96e37a72017-06-19 17:19:21 -04001#
2# Copyright 2017 the original author or authors.
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# """ ID generation utils """
17
18from uuid import uuid4
19
20def create_cluster_logical_device_ids(core_id, switch_id):
21 """
22 Creates a logical device id and an OpenFlow datapath id that is unique
23 across the Voltha cluster. Both ids represents a 64 bits integer where
24 the lower 48 bits represents the switch id and the upper 16 bits
25 represents the core id.
26 :param core_id: string
27 :return: cluster logical device id and OpenFlow datapath id
28 """
29 switch_id = format(switch_id, '012x')
30 id = '{}{}'.format(format(int(core_id), '04x'), switch_id)
31 hex_int=int(id,16)
32 return id, hex_int
33
34
35def create_cluster_device_id(core_id):
36 """
37 Creates a device id that is unique across the Voltha cluster.
38 The device id is a str of 64 bits. The lower 48 bits refers to the
39 device id while the upper 16 bits refers to the core id.
40 :param core_id: string
41 :return: cluster device id
42 """
43 return '{}{}'.format(format(int(core_id), '04x'), uuid4().hex[:12])
44
45def get_core_id_from_device_id(device_id):
46 # Device id is a string and the first 4 characters represent the core_id
47 assert device_id and device_id.len() == 16
48 return device_id[:4]
49
50
51def get_core_id_from_logical_device_id(logical_device_id):
52 """
53 Logical Device id is a string and the first 4 characters represent the
54 core_id
55 :param logical_device_id:
56 :return: core_id string
57 """
58 assert logical_device_id and logical_device_id.len() == 16
59 return logical_device_id[:4]
60
61def get_core_id_from_datapath_id(datapath_id):
62 """
63 datapath id is a uint64 where:
64 - low 48 bits -> switch_id
65 - high 16 bits -> core id
66 :param datapath_id:
67 :return: core_id string
68 """
69 assert datapath_id
70 # Get the hex string and remove the '0x' prefix
71 id_in_hex_str=hex(datapath_id)[2:]
72 assert id_in_hex_str.len() > 12
73 return id_in_hex_str[:-12]