1 # ============LICENSE_START===============================================
2 # Copyright (C) 2024 OpenInfra Foundation Europe. 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
8 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
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>"
26 from time import sleep
28 # disable warning about unverified https requests
29 from requests.packages import urllib3
31 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
33 #arg responsecode baseurl ric_base num_rics uuid startid service type transient notification-url templatepath count pids pid_id proxy policy-ids-file-path
38 if len(sys.argv) != 17:
39 print("1Expected 16 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 policy-ids-file-path")
42 responsecode=int(sys.argv[1])
43 baseurl=str(sys.argv[2])
44 ric_base=str(sys.argv[3])
45 num_rics=int(sys.argv[4])
47 start=int(sys.argv[6])
50 trans=str(sys.argv[9])
51 noti=str(sys.argv[10])
52 templatepath=str(sys.argv[11])
53 count=int(sys.argv[12])
54 pids=int(sys.argv[13])
55 pid_id=int(sys.argv[14])
56 httpproxy=str(sys.argv[15])
57 policy_ids_file_path=str(sys.argv[16])
60 if httpproxy != "NOPROXY":
68 with open(templatepath, 'r') as file:
69 template = file.read()
72 stop=count*num_rics+start
77 with open(str(policy_ids_file_path)) as file:
79 if start%pids == (pid_id-1):
80 payload=template.replace("XXX",str(start))
85 headers = {'Content-type': 'application/json'}
86 url=baseurl+"/"+str(policyId.strip())
89 resp=requests.put(url, payload, headers=headers, verify=False, timeout=90)
91 resp=requests.put(url, payload, headers=headers, verify=False, timeout=90, proxies=proxydict)
93 except Exception as e1:
97 connect_retry_count += 1
99 print("1Put failed for id:"+policyId.strip()+ ", "+str(e1) + " "+traceback.format_exc())
102 if (connect_ok == True):
103 if (resp.status_code == None):
104 print("1Put failed for id:"+policyId.strip()+ ", expected response code: "+str(responsecode)+", got: None")
107 if (resp.status_code != responsecode):
108 if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
111 http_retry_count += 1
113 print("1Put failed for id:"+policyId.strip()+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code))
115 print(json.loads(payload))
120 print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
123 except Exception as e:
125 traceback.print_exc()