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 |
| 26 | |
| 27 | # disable warning about unverified https requests |
| 28 | from requests.packages import urllib3 |
| 29 | |
| 30 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 31 | |
| 32 | #arg responsecode baseurl ric_base num_rics startid templatepath count pids pid_id |
| 33 | try: |
| 34 | if len(sys.argv) != 10: |
| 35 | print("1Expected 9 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics startid templatepath count pids pid_id") |
| 36 | sys.exit() |
| 37 | |
| 38 | responsecode=int(sys.argv[1]) |
| 39 | baseurl=sys.argv[2] |
| 40 | ric_base=sys.argv[3] |
| 41 | num_rics=int(sys.argv[4]) |
| 42 | start=int(sys.argv[5]) |
| 43 | templatepath=sys.argv[6] |
| 44 | count=int(sys.argv[7]) |
| 45 | pids=int(sys.argv[8]) |
| 46 | pid_id=int(sys.argv[9]) |
| 47 | |
| 48 | with open(templatepath, 'r') as file: |
| 49 | template = file.read() |
| 50 | |
| 51 | start=start |
| 52 | stop=count*num_rics+start |
| 53 | |
| 54 | for i in range(start,stop): |
| 55 | if (i%pids == (pid_id-1)): |
| 56 | payload=template.replace("XXX",str(i)) |
| 57 | ric_id=(i%num_rics)+1 |
| 58 | ric=ric_base+str(ric_id) |
| 59 | url=baseurl+"&id="+str(i)+"&ric="+str(ric) |
| 60 | try: |
| 61 | headers = {'Content-type': 'application/json'} |
| 62 | resp=requests.put(url, json.dumps(json.loads(payload)), headers=headers, verify=False, timeout=90) |
| 63 | except Exception as e1: |
| 64 | print("1Put failed for id:"+str(i)+ ", "+str(e1)) |
| 65 | sys.exit() |
| 66 | if (resp.status_code != responsecode): |
| 67 | print("1Put failed for id:"+str(i)+ ", expected response code: "+responsecode+", got: "+str(resp.status_code)) |
| 68 | sys.exit() |
| 69 | |
| 70 | print("0") |
| 71 | sys.exit() |
| 72 | |
| 73 | except Exception as e: |
| 74 | print("1"+str(e)) |
| 75 | sys.exit() |