from common.method_wrappers.ServiceExceptions import InvalidArgumentsException from typing import Optional, Union from uuid import UUID, uuid4, uuid5 # Generate a UUIDv5-like from the SHA-1 of "TFS" and no namespace to be used as the NAMESPACE for all # the context UUIDs generated. For efficiency purposes, the UUID is hardcoded; however, it is produced # using the following code: # from hashlib import sha1 # from uuid import UUID # hash = sha1(bytes('TFS', 'utf-8')).digest() # NAMESPACE_TFS = UUID(bytes=hash[:16], version=5) NAMESPACE_TFS = UUID('200e3a1f-2223-534f-a100-758e29c37f40') def get_uuid_from_string(str_uuid_or_name : Union[str, UUID], prefix_for_name : Optional[str] = None) -> str: # if UUID given, assume it is already a valid UUID if isinstance(str_uuid_or_name, UUID): return str_uuid_or_name if not isinstance(str_uuid_or_name, str): MSG = 'Parameter({:s}) cannot be used to produce a UUID' raise Exception(MSG.format(str(repr(str_uuid_or_name)))) try: # try to parse as UUID return str(UUID(str_uuid_or_name)) except: # pylint: disable=bare-except # produce a UUID within TFS namespace from parameter if prefix_for_name is not None: str_uuid_or_name = '{:s}/{:s}'.format(prefix_for_name, str_uuid_or_name) return str(uuid5(NAMESPACE_TFS, str_uuid_or_name)) def get_uuid_random() -> str: # Generate random UUID. No need to use namespace since "namespace + random = random". return str(uuid4()) def channel_get_uuid( channel_name :str , allow_random : bool = False ) -> str: if len(channel_name) > 0: return get_uuid_from_string(channel_name) if allow_random: return get_uuid_random() raise InvalidArgumentsException([ ('channel uuid', channel_name), ], extra_details=['Channel name is required to produce a channel UUID']) def device_get_uuid (device_name) : if (len(device_name)> 0): return get_uuid_from_string(device_name) raise InvalidArgumentsException([ ('name', device_name), ], extra_details=['Device Name is required to produce Device UUID']) def opticalconfig_get_uuid( device_name : str = '', allow_random : bool = False ) -> str: if len(device_name) > 0: device_uuid= device_get_uuid(device_name=device_name) return get_uuid_from_string(f"{device_uuid}_opticalconfig") if allow_random: return get_uuid_random() raise InvalidArgumentsException([ ('name', device_name), ], extra_details=['At least one is required to produce a OpticalConfig UUID'])