blob: 1ca38641bccdb310d5d1266fcfa76112d9ac4b50 [file] [log] [blame]
garciadeblas017c4fb2018-02-13 11:58:29 +01001# 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"""
18OSM package API handling
19"""
20
garciadeblas017c4fb2018-02-13 11:58:29 +010021#from os import stat
22#from os.path import basename
23from osmclient.common.exceptions import ClientException
Mike Marchettibaa171a2018-08-14 12:15:16 -040024from osmclient.common.exceptions import NotFound
garciadeblas017c4fb2018-02-13 11:58:29 +010025from osmclient.common import utils
garciadeblasf1571282018-05-14 16:06:22 +020026import json
garciadeblas6bc001c2019-11-21 12:02:05 +010027import logging
garciadeblas017c4fb2018-02-13 11:58:29 +010028
29
30class Package(object):
31 def __init__(self, http=None, client=None):
32 self._client = client
33 self._http = http
garciadeblas6bc001c2019-11-21 12:02:05 +010034 self._logger = logging.getLogger('osmclient')
garciadeblas017c4fb2018-02-13 11:58:29 +010035
36 def get_key_val_from_pkg(self, descriptor_file):
garciadeblas6bc001c2019-11-21 12:02:05 +010037 self._logger.debug("")
garciadeblasf36c47a2018-04-18 10:31:58 +020038 return utils.get_key_val_from_pkg(descriptor_file)
garciadeblas017c4fb2018-02-13 11:58:29 +010039
Mike Marchettibaa171a2018-08-14 12:15:16 -040040 def _wait_for_package(self, pkg_type):
garciadeblas6bc001c2019-11-21 12:02:05 +010041 self._logger.debug("")
Mike Marchettibaa171a2018-08-14 12:15:16 -040042 if 'vnfd' in pkg_type['type']:
43 get_method = self._client.vnfd.get
44 elif 'nsd' in pkg_type['type']:
45 get_method = self._client.nsd.get
46 else:
47 raise ClientException("no valid package type found")
48
49 # helper method to check if pkg exists
50 def check_exists(func):
garciadeblas6bc001c2019-11-21 12:02:05 +010051 self._logger.debug("")
Mike Marchettibaa171a2018-08-14 12:15:16 -040052 try:
53 func()
54 except NotFound:
55 return False
56 return True
57
58 return utils.wait_for_value(lambda:
59 check_exists(lambda:
60 get_method(pkg_type['name'])))
61
62 def wait_for_upload(self, filename):
63 """wait(block) for an upload to succeed.
64 The filename passed is assumed to be a descriptor tarball.
65 """
garciadeblas6bc001c2019-11-21 12:02:05 +010066 self._logger.debug("")
pinoa93012ad2019-11-05 14:28:15 +010067 self._client.get_token()
Mike Marchettibaa171a2018-08-14 12:15:16 -040068 pkg_type = utils.get_key_val_from_pkg(filename)
69
70 if pkg_type is None:
71 raise ClientException("Cannot determine package type")
72
73 if not self._wait_for_package(pkg_type):
74 raise ClientException("package {} failed to upload"
75 .format(filename))
76
garciadeblas017c4fb2018-02-13 11:58:29 +010077 def upload(self, filename):
garciadeblas6bc001c2019-11-21 12:02:05 +010078 self._logger.debug("")
pinoa93012ad2019-11-05 14:28:15 +010079 self._client.get_token()
garciadeblas017c4fb2018-02-13 11:58:29 +010080 pkg_type = utils.get_key_val_from_pkg(filename)
81 if pkg_type is None:
82 raise ClientException("Cannot determine package type")
83 if pkg_type['type'] == 'nsd':
garciadeblasa7acd722018-05-03 18:48:12 +020084 endpoint = '/nsd/v1/ns_descriptors_content'
garciadeblas017c4fb2018-02-13 11:58:29 +010085 else:
garciadeblasa7acd722018-05-03 18:48:12 +020086 endpoint = '/vnfpkgm/v1/vnf_packages_content'
garciadeblas017c4fb2018-02-13 11:58:29 +010087 #endpoint = '/nsds' if pkg_type['type'] == 'nsd' else '/vnfds'
garciadeblas09fa3d42019-10-08 18:30:46 +020088 #print('Endpoint: {}'.format(endpoint))
garciadeblas017c4fb2018-02-13 11:58:29 +010089 headers = self._client._headers
90 headers['Content-Type'] = 'application/gzip'
91 #headers['Content-Type'] = 'application/binary'
92 # Next three lines are to be removed in next version
93 #headers['Content-Filename'] = basename(filename)
94 #file_size = stat(filename).st_size
95 #headers['Content-Range'] = 'bytes 0-{}/{}'.format(file_size - 1, file_size)
96 headers["Content-File-MD5"] = utils.md5(filename)
97 http_header = ['{}: {}'.format(key,val)
peustermbe960962018-06-14 21:32:55 +020098 for (key,val) in list(headers.items())]
garciadeblas017c4fb2018-02-13 11:58:29 +010099 self._http.set_http_header(http_header)
garciadeblasf1571282018-05-14 16:06:22 +0200100 http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
garciadeblas09fa3d42019-10-08 18:30:46 +0200101 #print('HTTP CODE: {}'.format(http_code))
102 #print('RESP: {}'.format(resp))
pinoa70d6f182019-12-12 12:10:27 +0100103 #if http_code in (200, 201, 202, 204):
104 if resp:
105 resp = json.loads(resp)
106 if not resp or 'id' not in resp:
tiernobd39b092020-01-21 09:27:09 +0000107 raise ClientException('unexpected response from server - {}'.format(
pinoa70d6f182019-12-12 12:10:27 +0100108 resp))
109 print(resp['id'])
110 # else:
111 # msg = ""
112 # if resp:
113 # try:
114 # msg = json.loads(resp)
115 # except ValueError:
116 # msg = resp
117 # raise ClientException("failed to upload package - {}".format(msg))