blob: 9341ee4cab5f2b8574335f00539d3ea3ee14c17a [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42:18 -06001#
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
Zack Williams84a71e92019-11-15 09:00:19 -070018from __future__ import absolute_import
Chip Boling67b674a2019-02-08 11:42:18 -060019from uuid import uuid4
20
21
22BROADCAST_CORE_ID=hex(0xFFFF)[2:]
23
24def get_next_core_id(current_id_in_hex_str):
25 """
26 :param current_id_in_hex_str: a hex string of the maximum core id
27 assigned without the leading 0x characters
28 :return: current_id_in_hex_str + 1 in hex string
29 """
30 if not current_id_in_hex_str or current_id_in_hex_str == '':
31 return '0001'
32 else:
33 return format(int(current_id_in_hex_str, 16) + 1, '04x')
34
35
36def create_cluster_logical_device_ids(core_id, switch_id):
37 """
38 Creates a logical device id and an OpenFlow datapath id that is unique
39 across the Voltha cluster.
40 The returned logical device id represents a 64 bits integer where the
41 lower 48 bits is the switch id and the upper 16 bits is the core id. For
42 the datapath id the core id is set to '0000' as it is not used for voltha
43 core routing
44 :param core_id: string
45 :param switch_id:int
46 :return: cluster logical device id and OpenFlow datapath id
47 """
48 switch_id = format(switch_id, '012x')
49 core_in_hex=format(int(core_id, 16), '04x')
50 ld_id = '{}{}'.format(core_in_hex[-4:], switch_id[-12:])
51 dpid_id = '{}{}'.format('0000', switch_id[-12:])
52 return ld_id, int(dpid_id, 16)
53
54def is_broadcast_core_id(id):
55 assert id and len(id) == 16
56 return id[:4] == BROADCAST_CORE_ID
57
58def create_empty_broadcast_id():
59 """
60 Returns an empty broadcast id (ffff000000000000). The id is used to
61 dispatch xPON objects across all the Voltha instances.
62 :return: An empty broadcast id
63 """
64 return '{}{}'.format(BROADCAST_CORE_ID, '0'*12)
65
66def create_cluster_id():
67 """
68 Returns an id that is common across all voltha instances. The id
69 is a str of 64 bits. The lower 48 bits refers to an id specific to that
70 object while the upper 16 bits refers a broadcast core_id
71 :return: An common id across all Voltha instances
72 """
73 return '{}{}'.format(BROADCAST_CORE_ID, uuid4().hex[:12])
74
75def create_cluster_device_id(core_id):
76 """
77 Creates a device id that is unique across the Voltha cluster.
78 The device id is a str of 64 bits. The lower 48 bits refers to the
79 device id while the upper 16 bits refers to the core id.
80 :param core_id: string
81 :return: cluster device id
82 """
83 return '{}{}'.format(format(int(core_id), '04x'), uuid4().hex[:12])
84
85
86def get_core_id_from_device_id(device_id):
87 # Device id is a string and the first 4 characters represent the core_id
88 assert device_id and len(device_id) == 16
89 # Get the leading 4 hexs and remove leading 0's
90 return device_id[:4]
91
92
93def get_core_id_from_logical_device_id(logical_device_id):
94 """
95 Logical Device id is a string and the first 4 characters represent the
96 core_id
97 :param logical_device_id:
98 :return: core_id string
99 """
100 assert logical_device_id and len(logical_device_id) == 16
101 # Get the leading 4 hexs and remove leading 0's
102 return logical_device_id[:4]
103
104
105def get_core_id_from_datapath_id(datapath_id):
106 """
107 datapath id is a uint64 where:
108 - low 48 bits -> switch_id
109 - high 16 bits -> core id
110 :param datapath_id:
111 :return: core_id string
112 """
113 assert datapath_id
114 # Get the hex string and remove the '0x' prefix
115 id_in_hex_str = hex(datapath_id)[2:]
116 assert len(id_in_hex_str) > 12
117 return id_in_hex_str[:-12]