khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 1 | # |
| 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 | |
| 18 | from uuid import uuid4 |
| 19 | |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 20 | |
| 21 | BROADCAST_CORE_ID=hex(0xFFFF)[2:] |
| 22 | |
| 23 | def 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 | |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 35 | def create_cluster_logical_device_ids(core_id, switch_id): |
| 36 | """ |
| 37 | Creates a logical device id and an OpenFlow datapath id that is unique |
khenaidoo | 507d922 | 2017-10-10 16:23:49 -0400 | [diff] [blame] | 38 | across the Voltha cluster. |
| 39 | The returned ids represents a 64 bits integer where the lower 48 bits is |
| 40 | the switch id and the upper 16 bits is the core id. |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 41 | :param core_id: string |
khenaidoo | 507d922 | 2017-10-10 16:23:49 -0400 | [diff] [blame] | 42 | :param switch_id:int |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 43 | :return: cluster logical device id and OpenFlow datapath id |
| 44 | """ |
| 45 | switch_id = format(switch_id, '012x') |
khenaidoo | 507d922 | 2017-10-10 16:23:49 -0400 | [diff] [blame] | 46 | core_in_hex=format(int(core_id, 16), '04x') |
| 47 | id = '{}{}'.format(core_in_hex[-4:], switch_id[-12:]) |
| 48 | return id, int(id, 16) |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 49 | |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 50 | def is_broadcast_core_id(id): |
| 51 | assert id and len(id) == 16 |
| 52 | return id[:4] == BROADCAST_CORE_ID |
| 53 | |
Rachit Shrivastava | a182e91 | 2017-07-28 15:18:34 -0400 | [diff] [blame] | 54 | def create_empty_broadcast_id(): |
| 55 | """ |
| 56 | Returns an empty broadcast id (ffff000000000000). The id is used to |
| 57 | dispatch xPON objects across all the Voltha instances. |
| 58 | :return: An empty broadcast id |
| 59 | """ |
| 60 | return '{}{}'.format(BROADCAST_CORE_ID, '0'*12) |
| 61 | |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 62 | def create_cluster_id(): |
| 63 | """ |
| 64 | Returns an id that is common across all voltha instances. The id |
| 65 | is a str of 64 bits. The lower 48 bits refers to an id specific to that |
| 66 | object while the upper 16 bits refers a broadcast core_id |
| 67 | :return: An common id across all Voltha instances |
| 68 | """ |
| 69 | return '{}{}'.format(BROADCAST_CORE_ID, uuid4().hex[:12]) |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 70 | |
| 71 | def create_cluster_device_id(core_id): |
| 72 | """ |
| 73 | Creates a device id that is unique across the Voltha cluster. |
| 74 | The device id is a str of 64 bits. The lower 48 bits refers to the |
| 75 | device id while the upper 16 bits refers to the core id. |
| 76 | :param core_id: string |
| 77 | :return: cluster device id |
| 78 | """ |
| 79 | return '{}{}'.format(format(int(core_id), '04x'), uuid4().hex[:12]) |
| 80 | |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 81 | |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 82 | def get_core_id_from_device_id(device_id): |
| 83 | # Device id is a string and the first 4 characters represent the core_id |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 84 | assert device_id and len(device_id) == 16 |
| 85 | # Get the leading 4 hexs and remove leading 0's |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 86 | return device_id[:4] |
| 87 | |
| 88 | |
| 89 | def get_core_id_from_logical_device_id(logical_device_id): |
| 90 | """ |
| 91 | Logical Device id is a string and the first 4 characters represent the |
| 92 | core_id |
| 93 | :param logical_device_id: |
| 94 | :return: core_id string |
| 95 | """ |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 96 | assert logical_device_id and len(logical_device_id) == 16 |
| 97 | # Get the leading 4 hexs and remove leading 0's |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 98 | return logical_device_id[:4] |
| 99 | |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 100 | |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 101 | def get_core_id_from_datapath_id(datapath_id): |
| 102 | """ |
| 103 | datapath id is a uint64 where: |
| 104 | - low 48 bits -> switch_id |
| 105 | - high 16 bits -> core id |
| 106 | :param datapath_id: |
| 107 | :return: core_id string |
| 108 | """ |
| 109 | assert datapath_id |
| 110 | # Get the hex string and remove the '0x' prefix |
khenaidoo | 08d48d2 | 2017-06-29 19:42:49 -0400 | [diff] [blame] | 111 | id_in_hex_str = hex(datapath_id)[2:] |
| 112 | assert len(id_in_hex_str) > 12 |
khenaidoo | 96e37a7 | 2017-06-19 17:19:21 -0400 | [diff] [blame] | 113 | return id_in_hex_str[:-12] |