BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1 | # ============LICENSE_START=============================================== |
| 2 | # Copyright (C) 2020 Nordix Foundation. All rights reserved. |
| 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 implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # ============LICENSE_END================================================= |
| 16 | # |
| 17 | |
| 18 | # This script create/update policies spread over a number rics |
| 19 | # Intended for parallel processing |
| 20 | # Returns a string with result, either "0" for ok, or "1<fault description>" |
| 21 | |
| 22 | import os |
| 23 | import json |
| 24 | import sys |
| 25 | import requests |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 26 | import traceback |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 27 | from time import sleep |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 28 | |
| 29 | # disable warning about unverified https requests |
| 30 | from requests.packages import urllib3 |
| 31 | |
| 32 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 33 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 34 | #arg responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id proxy |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 35 | data_out="" |
| 36 | url_out="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 37 | try: |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 38 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 39 | if len(sys.argv) < 12: |
| 40 | print("1Expected 12/15 args, got "+str(len(sys.argv)-1)) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 41 | print (sys.argv[1:]) |
| 42 | sys.exit() |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 43 | responsecode=int(sys.argv[1]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 44 | baseurl=str(sys.argv[2]) |
| 45 | ric_base=str(sys.argv[3]) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 46 | num_rics=int(sys.argv[4]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 47 | uuid=str(sys.argv[5]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 48 | start=int(sys.argv[6]) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 49 | httpproxy="NOPROXY" |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 50 | if ("/v2/" in baseurl): |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 51 | if len(sys.argv) != 16: |
| 52 | print("1Expected 15 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics uuid startid service type transient notification-url templatepath count pids pid_id proxy") |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 53 | print (sys.argv[1:]) |
| 54 | sys.exit() |
| 55 | |
| 56 | serv=str(sys.argv[7]) |
| 57 | pt=str(sys.argv[8]) |
| 58 | trans=str(sys.argv[9]) |
| 59 | noti=str(sys.argv[10]) |
| 60 | templatepath=str(sys.argv[11]) |
| 61 | count=int(sys.argv[12]) |
| 62 | pids=int(sys.argv[13]) |
| 63 | pid_id=int(sys.argv[14]) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 64 | httpproxy=str(sys.argv[15]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 65 | else: |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 66 | if len(sys.argv) != 12: |
| 67 | print("1Expected 11 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id proxy") |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 68 | print (sys.argv[1:]) |
| 69 | sys.exit() |
| 70 | |
| 71 | templatepath=str(sys.argv[7]) |
| 72 | count=int(sys.argv[8]) |
| 73 | pids=int(sys.argv[9]) |
| 74 | pid_id=int(sys.argv[10]) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 75 | httpproxy=str(sys.argv[11]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 76 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 77 | proxydict=None |
| 78 | if httpproxy != "NOPROXY": |
| 79 | proxydict = { |
| 80 | "http" : httpproxy, |
| 81 | "https" : httpproxy |
| 82 | } |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 83 | if uuid == "NOUUID": |
| 84 | uuid="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 85 | |
| 86 | with open(templatepath, 'r') as file: |
| 87 | template = file.read() |
| 88 | |
| 89 | start=start |
| 90 | stop=count*num_rics+start |
| 91 | |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 92 | http_retry_count=0 |
| 93 | connect_retry_count=0 |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 94 | |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 95 | for i in range(start,stop): |
| 96 | if (i%pids == (pid_id-1)): |
| 97 | payload=template.replace("XXX",str(i)) |
| 98 | ric_id=(i%num_rics)+1 |
| 99 | ric=ric_base+str(ric_id) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 100 | |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 101 | connect_ok=False |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 102 | retry_cnt=5 |
| 103 | while(retry_cnt>0): |
| 104 | try: |
| 105 | headers = {'Content-type': 'application/json'} |
| 106 | if ("/v2/" in baseurl): |
| 107 | url=baseurl |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 108 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 109 | data={} |
| 110 | data["ric_id"]=ric |
| 111 | data["policy_id"]=uuid+str(i) |
| 112 | data["service_id"]=serv |
| 113 | if (trans != "NOTRANSIENT"): |
| 114 | data["transient"]=trans |
| 115 | if (pt != "NOTYPE"): |
| 116 | data["policytype_id"]=pt |
| 117 | else: |
| 118 | data["policytype_id"]="" |
| 119 | if (noti != "NOURL"): |
| 120 | data["status_notification_uri"]=noti |
| 121 | data["policy_data"]=json.loads(payload) |
| 122 | |
| 123 | url_out=url |
| 124 | data_out=json.dumps(data) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 125 | else: |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 126 | url=baseurl+"&id="+uuid+str(i)+"&ric="+str(ric) |
| 127 | url_out=url |
| 128 | data_out=json.dumps(json.loads(payload)) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 129 | if proxydict is None: |
| 130 | resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90) |
| 131 | else: |
| 132 | resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90, proxies=proxydict) |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 133 | connect_ok=True |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 134 | except Exception as e1: |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 135 | if (retry_cnt > 1): |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 136 | sleep(0.1) |
| 137 | retry_cnt -= 1 |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 138 | connect_retry_count += 1 |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 139 | else: |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 140 | print("1Put failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc()) |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 141 | sys.exit() |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 142 | |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame^] | 143 | if (connect_ok == True): |
| 144 | if (resp.status_code == None): |
| 145 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None") |
| 146 | sys.exit() |
| 147 | |
| 148 | if (resp.status_code != responsecode): |
| 149 | if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1): |
| 150 | sleep(0.1) |
| 151 | retry_cnt -= 1 |
| 152 | http_retry_count += 1 |
| 153 | else: |
| 154 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code)) |
| 155 | print(url_out) |
| 156 | print(str(data_out)) |
| 157 | sys.exit() |
| 158 | else: |
| 159 | retry_cnt=-1 |
| 160 | |
| 161 | print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count)) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 162 | sys.exit() |
| 163 | |
| 164 | except Exception as e: |
| 165 | print("1"+str(e)) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 166 | traceback.print_exc() |
| 167 | print(str(data_out)) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 168 | sys.exit() |