27b3ad48ed88c9511afaa0ffaa3ebd610d0dfbb2
1 # Copyright 2018 Telefonica
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
22 #from os.path import basename
23 from osmclient
.common
.exceptions
import OsmHttpException
24 from osmclient
.common
.exceptions
import ClientException
25 from osmclient
.common
.exceptions
import NotFound
26 from osmclient
.common
import utils
31 class Package(object):
32 def __init__(self
, http
=None, client
=None):
35 self
._logger
= logging
.getLogger('osmclient')
37 def get_key_val_from_pkg(self
, descriptor_file
):
38 self
._logger
.debug("")
39 return utils
.get_key_val_from_pkg(descriptor_file
)
41 def _wait_for_package(self
, pkg_type
):
42 self
._logger
.debug("")
43 if 'vnfd' in pkg_type
['type']:
44 get_method
= self
._client
.vnfd
.get
45 elif 'nsd' in pkg_type
['type']:
46 get_method
= self
._client
.nsd
.get
48 raise ClientException("no valid package type found")
50 # helper method to check if pkg exists
51 def check_exists(func
):
52 self
._logger
.debug("")
59 return utils
.wait_for_value(lambda:
61 get_method(pkg_type
['name'])))
63 def wait_for_upload(self
, filename
):
64 """wait(block) for an upload to succeed.
65 The filename passed is assumed to be a descriptor tarball.
67 self
._logger
.debug("")
68 self
._client
.get_token()
69 pkg_type
= utils
.get_key_val_from_pkg(filename
)
72 raise ClientException("Cannot determine package type")
74 if not self
._wait
_for
_package
(pkg_type
):
75 raise ClientException("package {} failed to upload"
78 def upload(self
, filename
):
79 self
._logger
.debug("")
80 self
._client
.get_token()
81 pkg_type
= utils
.get_key_val_from_pkg(filename
)
83 raise ClientException("Cannot determine package type")
84 if pkg_type
['type'] == 'nsd':
85 endpoint
= '/nsd/v1/ns_descriptors_content'
87 endpoint
= '/vnfpkgm/v1/vnf_packages_content'
88 #endpoint = '/nsds' if pkg_type['type'] == 'nsd' else '/vnfds'
89 #print('Endpoint: {}'.format(endpoint))
90 headers
= self
._client
._headers
91 headers
['Content-Type'] = 'application/gzip'
92 #headers['Content-Type'] = 'application/binary'
93 # Next three lines are to be removed in next version
94 #headers['Content-Filename'] = basename(filename)
95 #file_size = stat(filename).st_size
96 #headers['Content-Range'] = 'bytes 0-{}/{}'.format(file_size - 1, file_size)
97 headers
["Content-File-MD5"] = utils
.md5(filename
)
98 http_header
= ['{}: {}'.format(key
,val
)
99 for (key
,val
) in list(headers
.items())]
100 self
._http
.set_http_header(http_header
)
101 http_code
, resp
= self
._http
.post_cmd(endpoint
=endpoint
, filename
=filename
)
102 #print('HTTP CODE: {}'.format(http_code))
103 #print('RESP: {}'.format(resp))
104 #if http_code in (200, 201, 202, 204):
106 resp
= json
.loads(resp
)
107 if not resp
or 'id' not in resp
:
108 raise OsmHttpException('unexpected response from server - {}'.format(
115 # msg = json.loads(resp)
118 # raise ClientException("failed to upload package - {}".format(msg))