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
18 OSM package API handling
24 from osmclient
.common
.exceptions
import ClientException
25 from osmclient
.common
.exceptions
import NotFound
26 from osmclient
.common
import utils
29 class Package(object):
30 def __init__(self
, http
=None, upload_http
=None, client
=None):
33 self
._upload
_http
= upload_http
35 def _wait_for_package(self
, pkg_type
):
36 if 'vnfd' in pkg_type
['type']:
37 get_method
= self
._client
.vnfd
.get
38 elif 'nsd' in pkg_type
['type']:
39 get_method
= self
._client
.nsd
.get
41 raise ClientException("no valid package type found")
43 # helper method to check if pkg exists
44 def check_exists(func
):
51 return utils
.wait_for_value(lambda:
53 get_method(pkg_type
['name'])))
55 # method opens up a package and finds the name of the resulting
56 # descriptor (vnfd or nsd name)
57 def get_key_val_from_pkg(self
, descriptor_file
):
58 tar
= tarfile
.open(descriptor_file
)
60 for member
in tar
.getmembers():
61 if (re
.match('.*.yaml', member
.name
) and
62 len(member
.name
.split('/')) == 2):
63 yamlfile
= member
.name
68 dict = yaml
.load(tar
.extractfile(yamlfile
))
70 for k1
, v1
in dict.items():
71 if not k1
.endswith('-catalog'):
73 for k2
, v2
in v1
.items():
74 if not k2
.endswith('nsd') and not k2
.endswith('vnfd'):
78 result
['type'] = 'nsd'
80 result
['type'] = 'vnfd'
83 for k3
, v3
in entry
.items():
84 # strip off preceeding chars before :
85 key_name
= k3
.split(':').pop()
91 def wait_for_upload(self
, filename
):
92 """wait(block) for an upload to succeed.
93 The filename passed is assumed to be a descriptor tarball.
95 pkg_type
= self
.get_key_val_from_pkg(filename
)
98 raise ClientException("Cannot determine package type")
100 if not self
._wait
_for
_package
(pkg_type
):
101 raise ClientException("package {} failed to upload"
104 def upload(self
, filename
):
105 resp
= self
._upload
_http
.post_cmd(formfile
=('package', filename
))
106 if not resp
or 'transaction_id' not in resp
:
107 raise ClientException("failed to upload package")