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 | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 27 | |
| 28 | # disable warning about unverified https requests |
| 29 | from requests.packages import urllib3 |
| 30 | |
| 31 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 32 | |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 33 | #arg responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 34 | data_out="" |
| 35 | url_out="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 36 | try: |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 37 | |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 38 | if len(sys.argv) < 11: |
| 39 | print("1Expected 11/14 args, got "+str(len(sys.argv)-1)) |
| 40 | print (sys.argv[1:]) |
| 41 | sys.exit() |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 42 | responsecode=int(sys.argv[1]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 43 | baseurl=str(sys.argv[2]) |
| 44 | ric_base=str(sys.argv[3]) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 45 | num_rics=int(sys.argv[4]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 46 | uuid=str(sys.argv[5]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 47 | start=int(sys.argv[6]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 48 | if ("/v2/" in baseurl): |
| 49 | if len(sys.argv) != 15: |
| 50 | print("1Expected 14 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") |
| 51 | print (sys.argv[1:]) |
| 52 | sys.exit() |
| 53 | |
| 54 | serv=str(sys.argv[7]) |
| 55 | pt=str(sys.argv[8]) |
| 56 | trans=str(sys.argv[9]) |
| 57 | noti=str(sys.argv[10]) |
| 58 | templatepath=str(sys.argv[11]) |
| 59 | count=int(sys.argv[12]) |
| 60 | pids=int(sys.argv[13]) |
| 61 | pid_id=int(sys.argv[14]) |
| 62 | else: |
| 63 | if len(sys.argv) != 11: |
| 64 | print("1Expected 10 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id") |
| 65 | print (sys.argv[1:]) |
| 66 | sys.exit() |
| 67 | |
| 68 | templatepath=str(sys.argv[7]) |
| 69 | count=int(sys.argv[8]) |
| 70 | pids=int(sys.argv[9]) |
| 71 | pid_id=int(sys.argv[10]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 72 | |
| 73 | if uuid == "NOUUID": |
| 74 | uuid="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 75 | |
| 76 | with open(templatepath, 'r') as file: |
| 77 | template = file.read() |
| 78 | |
| 79 | start=start |
| 80 | stop=count*num_rics+start |
| 81 | |
| 82 | for i in range(start,stop): |
| 83 | if (i%pids == (pid_id-1)): |
| 84 | payload=template.replace("XXX",str(i)) |
| 85 | ric_id=(i%num_rics)+1 |
| 86 | ric=ric_base+str(ric_id) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 87 | |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 88 | try: |
| 89 | headers = {'Content-type': 'application/json'} |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 90 | if ("/v2/" in baseurl): |
| 91 | url=baseurl |
| 92 | |
| 93 | data={} |
| 94 | data["ric_id"]=ric |
| 95 | data["policy_id"]=uuid+str(i) |
| 96 | data["service_id"]=serv |
| 97 | if (trans != "NOTRANSIENT"): |
| 98 | data["transient"]=trans |
| 99 | if (pt != "NOTYPE"): |
| 100 | data["policy_type_id"]=pt |
| 101 | else: |
| 102 | data["policy_type_id"]="" |
| 103 | if (noti != "NOURL"): |
| 104 | data["status_notification_uri"]=noti |
| 105 | data["policy_data"]=json.loads(payload) |
| 106 | |
| 107 | url_out=url |
| 108 | data_out=json.dumps(data) |
| 109 | resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90) |
| 110 | else: |
| 111 | url=baseurl+"&id="+uuid+str(i)+"&ric="+str(ric) |
| 112 | url_out=url |
| 113 | data_out=json.dumps(json.loads(payload)) |
| 114 | resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 115 | except Exception as e1: |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 116 | print("1Put failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc()) |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 117 | sys.exit() |
| 118 | if (resp.status_code == None): |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 119 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None") |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 120 | sys.exit() |
| 121 | if (resp.status_code != responsecode): |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 122 | print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code)) |
| 123 | print(url_out) |
| 124 | print(str(data_out)) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 125 | sys.exit() |
| 126 | |
| 127 | print("0") |
| 128 | sys.exit() |
| 129 | |
| 130 | except Exception as e: |
| 131 | print("1"+str(e)) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame^] | 132 | traceback.print_exc() |
| 133 | print(str(data_out)) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 134 | sys.exit() |