| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 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 | |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 21 | #from os import stat |
| 22 | #from os.path import basename |
| 23 | from osmclient.common.exceptions import ClientException |
| Mike Marchetti | baa171a | 2018-08-14 12:15:16 -0400 | [diff] [blame] | 24 | from osmclient.common.exceptions import NotFound |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 25 | from osmclient.common import utils |
| garciadeblas | f157128 | 2018-05-14 16:06:22 +0200 | [diff] [blame] | 26 | import json |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 27 | import logging |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class Package(object): |
| 31 | def __init__(self, http=None, client=None): |
| 32 | self._client = client |
| 33 | self._http = http |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 34 | self._logger = logging.getLogger('osmclient') |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 35 | |
| 36 | def get_key_val_from_pkg(self, descriptor_file): |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 37 | self._logger.debug("") |
| garciadeblas | f36c47a | 2018-04-18 10:31:58 +0200 | [diff] [blame] | 38 | return utils.get_key_val_from_pkg(descriptor_file) |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 39 | |
| Mike Marchetti | baa171a | 2018-08-14 12:15:16 -0400 | [diff] [blame] | 40 | def _wait_for_package(self, pkg_type): |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 41 | self._logger.debug("") |
| Mike Marchetti | baa171a | 2018-08-14 12:15:16 -0400 | [diff] [blame] | 42 | 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): |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 51 | self._logger.debug("") |
| Mike Marchetti | baa171a | 2018-08-14 12:15:16 -0400 | [diff] [blame] | 52 | 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 | """ |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 66 | self._logger.debug("") |
| pinoa | 93012ad | 2019-11-05 14:28:15 +0100 | [diff] [blame] | 67 | self._client.get_token() |
| Mike Marchetti | baa171a | 2018-08-14 12:15:16 -0400 | [diff] [blame] | 68 | 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 | |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 77 | def upload(self, filename): |
| garciadeblas | 6bc001c | 2019-11-21 12:02:05 +0100 | [diff] [blame] | 78 | self._logger.debug("") |
| pinoa | 93012ad | 2019-11-05 14:28:15 +0100 | [diff] [blame] | 79 | self._client.get_token() |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 80 | 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': |
| garciadeblas | a7acd72 | 2018-05-03 18:48:12 +0200 | [diff] [blame] | 84 | endpoint = '/nsd/v1/ns_descriptors_content' |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 85 | else: |
| garciadeblas | a7acd72 | 2018-05-03 18:48:12 +0200 | [diff] [blame] | 86 | endpoint = '/vnfpkgm/v1/vnf_packages_content' |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 87 | #endpoint = '/nsds' if pkg_type['type'] == 'nsd' else '/vnfds' |
| garciadeblas | 09fa3d4 | 2019-10-08 18:30:46 +0200 | [diff] [blame] | 88 | #print('Endpoint: {}'.format(endpoint)) |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 89 | 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) |
| peusterm | be96096 | 2018-06-14 21:32:55 +0200 | [diff] [blame] | 98 | for (key,val) in list(headers.items())] |
| garciadeblas | 017c4fb | 2018-02-13 11:58:29 +0100 | [diff] [blame] | 99 | self._http.set_http_header(http_header) |
| garciadeblas | f157128 | 2018-05-14 16:06:22 +0200 | [diff] [blame] | 100 | http_code, resp = self._http.post_cmd(endpoint=endpoint, filename=filename) |
| garciadeblas | 09fa3d4 | 2019-10-08 18:30:46 +0200 | [diff] [blame] | 101 | #print('HTTP CODE: {}'.format(http_code)) |
| 102 | #print('RESP: {}'.format(resp)) |
| pinoa | 70d6f18 | 2019-12-12 12:10:27 +0100 | [diff] [blame] | 103 | #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: |
| tierno | bd39b09 | 2020-01-21 09:27:09 +0000 | [diff] [blame] | 107 | raise ClientException('unexpected response from server - {}'.format( |
| pinoa | 70d6f18 | 2019-12-12 12:10:27 +0100 | [diff] [blame] | 108 | 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)) |