X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fcommon%2Futils.py;h=937e12175ee2942169a26dfadf695cdd6f53659a;hb=1f3e889846ff574daf98fc699d81279f5f302e71;hp=7228e5c542dc6095a7606f60564ae56292686ef1;hpb=4c7e237e45bc6fe209006b4837a8e6b031e78e1c;p=osm%2Fosmclient.git diff --git a/osmclient/common/utils.py b/osmclient/common/utils.py index 7228e5c..937e121 100644 --- a/osmclient/common/utils.py +++ b/osmclient/common/utils.py @@ -15,6 +15,11 @@ # under the License. import time +from uuid import UUID +import hashlib +import tarfile +import re +import yaml def wait_for_value(func, result=True, wait_time=10, catch_exception=None): @@ -25,8 +30,61 @@ def wait_for_value(func, result=True, wait_time=10, catch_exception=None): return True except catch_exception: pass - time.sleep(1) + time.sleep(5) try: return func() == result except catch_exception: return False + + +def validate_uuid4(uuid_text): + try: + UUID(uuid_text) + return True + except (ValueError, TypeError): + return False + + +def md5(fname): + hash_md5 = hashlib.md5() + with open(fname, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() + + +def get_key_val_from_pkg(descriptor_file): + # method opens up a package and finds the name of the resulting + # descriptor (vnfd or nsd name) + tar = tarfile.open(descriptor_file) + yamlfile = None + for member in tar.getmembers(): + if (re.match('.*.yaml', member.name) and + len(member.name.split('/')) == 2): + yamlfile = member.name + break + if yamlfile is None: + return None + + dict = yaml.load(tar.extractfile(yamlfile)) + result = {} + for k1, v1 in list(dict.items()): + if not k1.endswith('-catalog'): + continue + for k2, v2 in list(v1.items()): + if not k2.endswith('nsd') and not k2.endswith('vnfd'): + continue + + if 'nsd' in k2: + result['type'] = 'nsd' + else: + result['type'] = 'vnfd' + + for entry in v2: + for k3, v3 in list(entry.items()): + # strip off preceeding chars before : + key_name = k3.split(':').pop() + + result[key_name] = v3 + tar.close() + return result