5336577aa2a93875e77f3ad4699d6ca6653d95d5
1 # Copyright 2017 Sandvine
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
21 from zipfile
import ZipFile
26 def wait_for_value(func
, result
=True, wait_time
=10, catch_exception
=None):
27 maxtime
= time
.time() + wait_time
28 while time
.time() < maxtime
:
32 except catch_exception
:
36 return func() == result
37 except catch_exception
:
41 def validate_uuid4(uuid_text
):
45 except (ValueError, TypeError):
50 hash_md5
= hashlib
.md5()
51 with
open(fname
, "rb") as f
:
52 for chunk
in iter(lambda: f
.read(4096), b
""):
53 hash_md5
.update(chunk
)
54 return hash_md5
.hexdigest()
57 def get_key_val_from_pkg(descriptor_file
):
58 if descriptor_file
.split(".")[-1] == "zip":
59 return get_key_val_from_pkg_sol004(descriptor_file
)
61 return get_key_val_from_pkg_old(descriptor_file
)
64 def get_key_val_from_pkg_sol004(package_file
):
65 """Method opens up a package and finds the name of the resulting
66 descriptor (vnfd or nsd name), using SOL004 spec
68 with
ZipFile(package_file
) as zipfile
:
70 for filedata
in zipfile
.infolist():
72 re
.match(".*.yaml", filedata
.filename
)
73 and filedata
.filename
.find("Scripts") < 0
75 yamlfile
= filedata
.filename
80 return get_key_val_from_descriptor(zipfile
.open(yamlfile
))
83 def get_key_val_from_pkg_old(descriptor_file
):
84 """Method opens up a package and finds the name of the resulting
85 descriptor (vnfd or nsd name)
87 tar
= tarfile
.open(descriptor_file
)
89 for member
in tar
.getmembers():
90 if re
.match(".*.yaml", member
.name
) and len(member
.name
.split("/")) == 2:
91 yamlfile
= member
.name
96 result
= get_key_val_from_descriptor(tar
.extractfile(yamlfile
))
102 def get_key_val_from_descriptor(descriptor
):
103 dict = yaml
.safe_load(descriptor
)
107 result
["type"] = "nsd"
109 result
["type"] = "vnfd"
111 if "type" not in result
:
112 for k1
, v1
in list(dict.items()):
113 if not k1
.endswith("-catalog"):
115 for k2
, v2
in v1
.items():
116 if not k2
.endswith("nsd") and not k2
.endswith("vnfd"):
119 result
["type"] = "nsd"
121 result
["type"] = "vnfd"
123 for k3
, v3
in list(entry
.items()):
124 # strip off preceeding chars before :
125 key_name
= k3
.split(":").pop()
126 result
[key_name
] = v3