blob: 43287e957ed4fa35731e93a84e87118f6171c965 [file] [log] [blame]
Mike Marchettie84eb312017-05-04 15:06:26 -04001# Copyright 2017 Sandvine
2#
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import time
garciadeblas017c4fb2018-02-13 11:58:29 +010018from uuid import UUID
19import hashlib
20import tarfile
21import re
22import yaml
Mike Marchettie84eb312017-05-04 15:06:26 -040023
Eduardo Sousaa63fb3c2019-03-31 23:49:14 +010024
Mike Marchetti4c7e2372017-05-08 16:07:20 -040025def wait_for_value(func, result=True, wait_time=10, catch_exception=None):
Mike Marchettie84eb312017-05-04 15:06:26 -040026 maxtime = time.time() + wait_time
27 while time.time() < maxtime:
28 try:
29 if func() == result:
Mike Marchetti4c7e2372017-05-08 16:07:20 -040030 return True
31 except catch_exception:
Mike Marchettie84eb312017-05-04 15:06:26 -040032 pass
garciadeblasd6eb17b2019-06-06 17:55:09 +020033 time.sleep(5)
Mike Marchettie84eb312017-05-04 15:06:26 -040034 try:
35 return func() == result
Mike Marchetti4c7e2372017-05-08 16:07:20 -040036 except catch_exception:
Mike Marchettie84eb312017-05-04 15:06:26 -040037 return False
garciadeblas017c4fb2018-02-13 11:58:29 +010038
39
40def validate_uuid4(uuid_text):
41 try:
42 UUID(uuid_text)
43 return True
44 except (ValueError, TypeError):
45 return False
46
47
48def md5(fname):
49 hash_md5 = hashlib.md5()
50 with open(fname, "rb") as f:
51 for chunk in iter(lambda: f.read(4096), b""):
52 hash_md5.update(chunk)
53 return hash_md5.hexdigest()
54
55
56def get_key_val_from_pkg(descriptor_file):
Eduardo Sousaa63fb3c2019-03-31 23:49:14 +010057 # method opens up a package and finds the name of the resulting
58 # descriptor (vnfd or nsd name)
garciadeblas017c4fb2018-02-13 11:58:29 +010059 tar = tarfile.open(descriptor_file)
60 yamlfile = None
61 for member in tar.getmembers():
62 if (re.match('.*.yaml', member.name) and
Eduardo Sousaa63fb3c2019-03-31 23:49:14 +010063 len(member.name.split('/')) == 2):
garciadeblas017c4fb2018-02-13 11:58:29 +010064 yamlfile = member.name
65 break
66 if yamlfile is None:
67 return None
68
garciadeblasdd006fa2019-11-07 09:30:30 +010069 dict = yaml.safe_load(tar.extractfile(yamlfile))
garciadeblas017c4fb2018-02-13 11:58:29 +010070 result = {}
peustermbe960962018-06-14 21:32:55 +020071 for k1, v1 in list(dict.items()):
garciadeblas017c4fb2018-02-13 11:58:29 +010072 if not k1.endswith('-catalog'):
73 continue
peustermbe960962018-06-14 21:32:55 +020074 for k2, v2 in list(v1.items()):
garciadeblas017c4fb2018-02-13 11:58:29 +010075 if not k2.endswith('nsd') and not k2.endswith('vnfd'):
76 continue
77
78 if 'nsd' in k2:
79 result['type'] = 'nsd'
80 else:
81 result['type'] = 'vnfd'
82
83 for entry in v2:
peustermbe960962018-06-14 21:32:55 +020084 for k3, v3 in list(entry.items()):
garciadeblas017c4fb2018-02-13 11:58:29 +010085 # strip off preceeding chars before :
86 key_name = k3.split(':').pop()
87
88 result[key_name] = v3
89 tar.close()
90 return result