blob: 89bfde8c1c9619df2a51c89e4346f6fd89463be7 [file] [log] [blame]
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001# ============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
22import os
23import json
24import sys
25import requests
BjornMagnussonXA048aaa12020-06-04 07:48:37 +020026import traceback
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010027from time import sleep
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020028
29# disable warning about unverified https requests
30from requests.packages import urllib3
31
32urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
33
BjornMagnussonXAad047782020-06-08 15:54:11 +020034#arg responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id
BjornMagnussonXA4207b832020-11-03 09:52:49 +010035data_out=""
36url_out=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020037try:
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020038
BjornMagnussonXA4207b832020-11-03 09:52:49 +010039 if len(sys.argv) < 11:
40 print("1Expected 11/14 args, got "+str(len(sys.argv)-1))
41 print (sys.argv[1:])
42 sys.exit()
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020043 responsecode=int(sys.argv[1])
BjornMagnussonXA4207b832020-11-03 09:52:49 +010044 baseurl=str(sys.argv[2])
45 ric_base=str(sys.argv[3])
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020046 num_rics=int(sys.argv[4])
BjornMagnussonXA4207b832020-11-03 09:52:49 +010047 uuid=str(sys.argv[5])
BjornMagnussonXAad047782020-06-08 15:54:11 +020048 start=int(sys.argv[6])
BjornMagnussonXA4207b832020-11-03 09:52:49 +010049 if ("/v2/" in baseurl):
50 if len(sys.argv) != 15:
51 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")
52 print (sys.argv[1:])
53 sys.exit()
54
55 serv=str(sys.argv[7])
56 pt=str(sys.argv[8])
57 trans=str(sys.argv[9])
58 noti=str(sys.argv[10])
59 templatepath=str(sys.argv[11])
60 count=int(sys.argv[12])
61 pids=int(sys.argv[13])
62 pid_id=int(sys.argv[14])
63 else:
64 if len(sys.argv) != 11:
65 print("1Expected 10 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl ric_base num_rics uuid startid templatepath count pids pid_id")
66 print (sys.argv[1:])
67 sys.exit()
68
69 templatepath=str(sys.argv[7])
70 count=int(sys.argv[8])
71 pids=int(sys.argv[9])
72 pid_id=int(sys.argv[10])
BjornMagnussonXAad047782020-06-08 15:54:11 +020073
74 if uuid == "NOUUID":
75 uuid=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020076
77 with open(templatepath, 'r') as file:
78 template = file.read()
79
80 start=start
81 stop=count*num_rics+start
82
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010083 total_retry_count=0
84
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020085 for i in range(start,stop):
86 if (i%pids == (pid_id-1)):
87 payload=template.replace("XXX",str(i))
88 ric_id=(i%num_rics)+1
89 ric=ric_base+str(ric_id)
BjornMagnussonXA4207b832020-11-03 09:52:49 +010090
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010091 retry_cnt=5
92 while(retry_cnt>0):
93 try:
94 headers = {'Content-type': 'application/json'}
95 if ("/v2/" in baseurl):
96 url=baseurl
BjornMagnussonXA4207b832020-11-03 09:52:49 +010097
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010098 data={}
99 data["ric_id"]=ric
100 data["policy_id"]=uuid+str(i)
101 data["service_id"]=serv
102 if (trans != "NOTRANSIENT"):
103 data["transient"]=trans
104 if (pt != "NOTYPE"):
105 data["policytype_id"]=pt
106 else:
107 data["policytype_id"]=""
108 if (noti != "NOURL"):
109 data["status_notification_uri"]=noti
110 data["policy_data"]=json.loads(payload)
111
112 url_out=url
113 data_out=json.dumps(data)
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100114 else:
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100115 url=baseurl+"&id="+uuid+str(i)+"&ric="+str(ric)
116 url_out=url
117 data_out=json.dumps(json.loads(payload))
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100118
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100119 resp=requests.put(url, data_out, headers=headers, verify=False, timeout=90)
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100120 except Exception as e1:
121 print("1Put failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc())
122 sys.exit()
123
124 if (resp.status_code == None):
125 print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None")
126 sys.exit()
127
128 if (resp.status_code != responsecode):
129 if (resp.status_code == 503 ) and (retry_cnt > 1):
130 sleep(0.1)
131 retry_cnt -= 1
132 total_retry_count += 1
133 else:
134 print("1Put failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code))
135 print(url_out)
136 print(str(data_out))
137 sys.exit()
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100138 else:
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100139 retry_cnt=-1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200140
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100141 if (total_retry_count > 0):
142 print("0 retries:"+str(total_retry_count))
143 else:
144 print("0")
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200145 sys.exit()
146
147except Exception as e:
148 print("1"+str(e))
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100149 traceback.print_exc()
150 print(str(data_out))
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200151sys.exit()