blob: e0fea1c95fae20a26243620674bcebd40bd14df7 [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
khenaidoo08d48d22017-06-29 19:42:49 -040020
21BROADCAST_CORE_ID=hex(0xFFFF)[2:]
22
23def get_next_core_id(current_id_in_hex_str):
24 """
25 :param current_id_in_hex_str: a hex string of the maximum core id
26 assigned without the leading 0x characters
27 :return: current_id_in_hex_str + 1 in hex string
28 """
29 if not current_id_in_hex_str or current_id_in_hex_str == '':
30 return '0001'
31 else:
32 return format(int(current_id_in_hex_str, 16) + 1, '04x')
33
34
khenaidoo96e37a72017-06-19 17:19:21 -040035def create_cluster_logical_device_ids(core_id, switch_id):
36 """
37 Creates a logical device id and an OpenFlow datapath id that is unique
khenaidoo507d9222017-10-10 16:23:49 -040038 across the Voltha cluster.
khenaidooe9677d42018-02-20 17:41:05 -050039 The returned logical device id represents a 64 bits integer where the
40 lower 48 bits is the switch id and the upper 16 bits is the core id. For
41 the datapath id the core id is set to '0000' as it is not used for voltha
42 core routing
khenaidoo96e37a72017-06-19 17:19:21 -040043 :param core_id: string
khenaidoo507d9222017-10-10 16:23:49 -040044 :param switch_id:int
khenaidoo96e37a72017-06-19 17:19:21 -040045 :return: cluster logical device id and OpenFlow datapath id
46 """
47 switch_id = format(switch_id, '012x')
khenaidoo507d9222017-10-10 16:23:49 -040048 core_in_hex=format(int(core_id, 16), '04x')
khenaidooe9677d42018-02-20 17:41:05 -050049 ld_id = '{}{}'.format(core_in_hex[-4:], switch_id[-12:])
50 dpid_id = '{}{}'.format('0000', switch_id[-12:])
51 return ld_id, int(dpid_id, 16)
khenaidoo96e37a72017-06-19 17:19:21 -040052
khenaidoo08d48d22017-06-29 19:42:49 -040053def is_broadcast_core_id(id):
54 assert id and len(id) == 16
55 return id[:4] == BROADCAST_CORE_ID
56
Rachit Shrivastavaa182e912017-07-28 15:18:34 -040057def create_empty_broadcast_id():
58 """
59 Returns an empty broadcast id (ffff000000000000). The id is used to
60 dispatch xPON objects across all the Voltha instances.
61 :return: An empty broadcast id
62 """
63 return '{}{}'.format(BROADCAST_CORE_ID, '0'*12)
64
khenaidoo08d48d22017-06-29 19:42:49 -040065def create_cluster_id():
66 """
67 Returns an id that is common across all voltha instances. The id
68 is a str of 64 bits. The lower 48 bits refers to an id specific to that
69 object while the upper 16 bits refers a broadcast core_id
70 :return: An common id across all Voltha instances
71 """
72 return '{}{}'.format(BROADCAST_CORE_ID, uuid4().hex[:12])
khenaidoo96e37a72017-06-19 17:19:21 -040073
74def create_cluster_device_id(core_id):
75 """
76 Creates a device id that is unique across the Voltha cluster.
77 The device id is a str of 64 bits. The lower 48 bits refers to the
78 device id while the upper 16 bits refers to the core id.
79 :param core_id: string
80 :return: cluster device id
81 """
82 return '{}{}'.format(format(int(core_id), '04x'), uuid4().hex[:12])
83
khenaidoo08d48d22017-06-29 19:42:49 -040084
khenaidoo96e37a72017-06-19 17:19:21 -040085def get_core_id_from_device_id(device_id):
86 # Device id is a string and the first 4 characters represent the core_id
khenaidoo08d48d22017-06-29 19:42:49 -040087 assert device_id and len(device_id) == 16
88 # Get the leading 4 hexs and remove leading 0's
khenaidoo96e37a72017-06-19 17:19:21 -040089 return device_id[:4]
90
91
92def get_core_id_from_logical_device_id(logical_device_id):
93 """
94 Logical Device id is a string and the first 4 characters represent the
95 core_id
96 :param logical_device_id:
97 :return: core_id string
98 """
khenaidoo08d48d22017-06-29 19:42:49 -040099 assert logical_device_id and len(logical_device_id) == 16
100 # Get the leading 4 hexs and remove leading 0's
khenaidoo96e37a72017-06-19 17:19:21 -0400101 return logical_device_id[:4]
102
khenaidoo08d48d22017-06-29 19:42:49 -0400103
khenaidoo96e37a72017-06-19 17:19:21 -0400104def get_core_id_from_datapath_id(datapath_id):
105 """
106 datapath id is a uint64 where:
107 - low 48 bits -> switch_id
108 - high 16 bits -> core id
109 :param datapath_id:
110 :return: core_id string
111 """
112 assert datapath_id
113 # Get the hex string and remove the '0x' prefix
khenaidoo08d48d22017-06-29 19:42:49 -0400114 id_in_hex_str = hex(datapath_id)[2:]
115 assert len(id_in_hex_str) > 12
khenaidoo96e37a72017-06-19 17:19:21 -0400116 return id_in_hex_str[:-12]