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
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>"
27 from time import sleep
29 # disable warning about unverified https requests
30 from requests.packages import urllib3
32 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
34 #arg responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id proxy
39 if len(sys.argv) < 12:
40 print("1Expected 12/15 args, got "+str(len(sys.argv)-1))
43 responsecode=int(sys.argv[1])
44 baseurl=str(sys.argv[2])
45 ric_base=str(sys.argv[3])
46 num_rics=int(sys.argv[4])
48 start=int(sys.argv[6])
50 if ("/v2/" in baseurl):
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")
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])
64 httpproxy=str(sys.argv[15])
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")
71 templatepath=str(sys.argv[7])
72 count=int(sys.argv[8])
74 pid_id=int(sys.argv[10])
75 httpproxy=str(sys.argv[11])
78 if httpproxy != "NOPROXY":
86 with open(templatepath, 'r') as file:
87 template = file.read()
90 stop=count*num_rics+start
95 for i in range(start,stop):
96 if (i%pids == (pid_id-1)):
97 payload=template.replace("XXX",str(i))
99 ric=ric_base+str(ric_id)
105 headers = {'Content-type': 'application/json'}
106 if ("/v2/" in baseurl):
111 data["policy_id"]=uuid+str(i)
112 data["service_id"]=serv
113 if (trans != "NOTRANSIENT"):
114 data["transient"]=trans
116 data["policytype_id"]=pt
118 data["policytype_id"]=""
119 if (noti != "NOURL"):
120 data["status_notification_uri"]=noti
121 data["policy_data"]=json.loads(payload)
124 data_out=json.dumps(data)
126 url=baseurl+"&id="+uuid+str(i)+"&ric="+str(ric)
128 data_out=json.dumps(json.loads(payload))
129 if proxydict is None:
130 resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90)
132 resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90, proxies=proxydict)
134 except Exception as e1:
138 connect_retry_count += 1
140 print("1Put failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc())
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")
148 if (resp.status_code != responsecode):
149 if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
152 http_retry_count += 1
154 print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code))
161 print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
164 except Exception as e:
166 traceback.print_exc()