adding a test script to send kafka messages
[osm/NBI.git] / osm_nbi / tests / upload.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import getopt
18 import sys
19 import requests
20 from os.path import getsize, basename
21 from hashlib import md5
22
23 __author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com"
24 __date__ = "$2018-01-01$"
25 __version__ = "0.1"
26 version_date = "Jan 2018"
27
28
29 def usage():
30 print("Usage: ", sys.argv[0], "[options]")
31 print(" --version: prints current version")
32 print(" -f|--file FILE: file to be sent")
33 print(" -h|--help: shows this help")
34 print(" -u|--url URL: complete server URL")
35 print(" -s|--chunk-size SIZE: size of chunks, by default 1000")
36 print(" -t|--token TOKEN: Authorizaton token, previously obtained from server")
37 print(" -v|--verbose print debug information, can be used several times")
38 return
39
40
41 if __name__ == "__main__":
42 try:
43 # load parameters and configuration
44 opts, args = getopt.getopt(sys.argv[1:], "hvu:s:f:t:",
45 ["url=", "help", "version", "verbose", "file=", "chunk-size=", "token="])
46 url = None
47 chunk_size = 500
48 pkg_file = None
49 verbose = 0
50 token = None
51
52 for o, a in opts:
53 if o == "--version":
54 print("upload version " + __version__ + ' ' + version_date)
55 sys.exit()
56 elif o in ("-v", "--verbose"):
57 verbose += 1
58 elif o in ("-h", "--help"):
59 usage()
60 sys.exit()
61 elif o in ("-u", "--url"):
62 url = a
63 elif o in ("-s", "--chunk-size"):
64 chunk_size = int(a)
65 elif o in ("-f", "--file"):
66 pkg_file = a
67 elif o in ("-t", "--token"):
68 token = a
69 else:
70 assert False, "Unhandled option"
71 total_size = getsize(pkg_file)
72 index = 0
73 transaction_id = None
74 file_md5 = md5()
75 with open(pkg_file, 'rb') as f:
76 headers = {
77 "Content-type": "application/gzip",
78 "Content-Filename": basename(pkg_file),
79 "Accept": "application/json",
80 }
81 if token:
82 headers["Authorization"] = token
83 while index < total_size:
84 chunk_data = f.read(chunk_size)
85 file_md5.update(chunk_data)
86 # payload = {"file_name": pkg_file, "chunk_data": base64.b64encode(chunk_data).decode("utf-8"),
87 # "chunk_size": chunk_size}
88 if transaction_id:
89 headers["Transaction-Id"] = transaction_id
90 if index+len(chunk_data) == total_size:
91 headers["Content-File-MD5"] = file_md5.hexdigest()
92 # payload["id"] = transaction_id
93 headers["Content-range"] = "bytes {}-{}/{}".format(index, index+len(chunk_data)-1, total_size)
94 # refers to rfc2616: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
95 if verbose:
96 print("TX chunk Headers: {}".format(headers))
97 r = requests.post(url, data=chunk_data, headers=headers, verify=False)
98 if r.status_code not in (200, 201):
99 print("Got {}: {}".format(r.status_code, r.text))
100 exit(1)
101 if verbose > 1:
102 print("RX {}: {}".format(r.status_code, r.text))
103 response = r.json()
104 if not transaction_id:
105 transaction_id = response["id"]
106 index += len(chunk_data)
107 if verbose <= 1:
108 print("RX {}: {}".format(r.status_code, r.text))
109 if "id" in response:
110 print("---\nid: {}".format(response["id"]))
111 except Exception:
112 raise