c383862f4b754b3cd09254ca7a548e74e66b4036
[osm/osmclient.git] / osmclient / sol005 / package.py
1 # Copyright 2018 Telefonica
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
17 """
18 OSM package API handling
19 """
20
21 #from os import stat
22 #from os.path import basename
23 from osmclient.common.exceptions import ClientException
24 from osmclient.common.exceptions import NotFound
25 from osmclient.common import utils
26 import json
27
28
29 class Package(object):
30 def __init__(self, http=None, client=None):
31 self._client = client
32 self._http = http
33
34 def get_key_val_from_pkg(self, descriptor_file):
35 return utils.get_key_val_from_pkg(descriptor_file)
36
37 def _wait_for_package(self, pkg_type):
38 if 'vnfd' in pkg_type['type']:
39 get_method = self._client.vnfd.get
40 elif 'nsd' in pkg_type['type']:
41 get_method = self._client.nsd.get
42 else:
43 raise ClientException("no valid package type found")
44
45 # helper method to check if pkg exists
46 def check_exists(func):
47 try:
48 func()
49 except NotFound:
50 return False
51 return True
52
53 return utils.wait_for_value(lambda:
54 check_exists(lambda:
55 get_method(pkg_type['name'])))
56
57 def wait_for_upload(self, filename):
58 """wait(block) for an upload to succeed.
59 The filename passed is assumed to be a descriptor tarball.
60 """
61 self._client.get_token()
62 pkg_type = utils.get_key_val_from_pkg(filename)
63
64 if pkg_type is None:
65 raise ClientException("Cannot determine package type")
66
67 if not self._wait_for_package(pkg_type):
68 raise ClientException("package {} failed to upload"
69 .format(filename))
70
71 def upload(self, filename):
72 self._client.get_token()
73 pkg_type = utils.get_key_val_from_pkg(filename)
74 if pkg_type is None:
75 raise ClientException("Cannot determine package type")
76 if pkg_type['type'] == 'nsd':
77 endpoint = '/nsd/v1/ns_descriptors_content'
78 else:
79 endpoint = '/vnfpkgm/v1/vnf_packages_content'
80 #endpoint = '/nsds' if pkg_type['type'] == 'nsd' else '/vnfds'
81 #print('Endpoint: {}'.format(endpoint))
82 headers = self._client._headers
83 headers['Content-Type'] = 'application/gzip'
84 #headers['Content-Type'] = 'application/binary'
85 # Next three lines are to be removed in next version
86 #headers['Content-Filename'] = basename(filename)
87 #file_size = stat(filename).st_size
88 #headers['Content-Range'] = 'bytes 0-{}/{}'.format(file_size - 1, file_size)
89 headers["Content-File-MD5"] = utils.md5(filename)
90 http_header = ['{}: {}'.format(key,val)
91 for (key,val) in list(headers.items())]
92 self._http.set_http_header(http_header)
93 http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
94 #print('HTTP CODE: {}'.format(http_code))
95 #print('RESP: {}'.format(resp))
96 if http_code in (200, 201, 202, 204):
97 if resp:
98 resp = json.loads(resp)
99 if not resp or 'id' not in resp:
100 raise ClientException('unexpected response from server - {}'.format(
101 resp))
102 print(resp['id'])
103 else:
104 msg = ""
105 if resp:
106 try:
107 msg = json.loads(resp)
108 except ValueError:
109 msg = resp
110 raise ClientException("failed to upload package - {}".format(msg))
111