Updating FTC 850 to support a1pms V3 image
[nonrtric.git] / test / common / update_policies_process.py
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
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 json
23 import sys
24 import requests
25 import traceback
26 from time import sleep
27
28 # disable warning about unverified https requests
29 from requests.packages import urllib3
30
31 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
32
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
34 data_out=""
35 url_out=""
36 try:
37
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")
40         print (sys.argv[1:])
41         sys.exit()
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])
46     uuid=str(sys.argv[5])
47     start=int(sys.argv[6])
48     serv=str(sys.argv[7])
49     pt=str(sys.argv[8])
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])
58
59     proxydict=None
60     if httpproxy != "NOPROXY":
61         proxydict = {
62             "http" : httpproxy,
63             "https" : httpproxy
64         }
65     if uuid == "NOUUID":
66         uuid=""
67
68     with open(templatepath, 'r') as file:
69         template = file.read()
70
71         start=start
72         stop=count*num_rics+start
73
74         http_retry_count=0
75         connect_retry_count=0
76
77         with open(str(policy_ids_file_path)) as file:
78             for policyId in file:
79                 if start%pids == (pid_id-1):
80                     payload=template.replace("XXX",str(start))
81                     connect_ok=False
82                     retry_cnt=5
83                     while(retry_cnt>0):
84                         try:
85                             headers = {'Content-type': 'application/json'}
86                             url=baseurl+"/"+str(policyId.strip())
87                             url_out=url
88                             if proxydict is None:
89                                 resp=requests.put(url, payload, headers=headers, verify=False, timeout=90)
90                             else:
91                                 resp=requests.put(url, payload, headers=headers, verify=False, timeout=90, proxies=proxydict)
92                             connect_ok=True
93                         except Exception as e1:
94                             if (retry_cnt > 1):
95                                 sleep(0.1)
96                                 retry_cnt -= 1
97                                 connect_retry_count += 1
98                             else:
99                                 print("1Put failed for id:"+policyId.strip()+ ", "+str(e1) + " "+traceback.format_exc())
100                                 sys.exit()
101
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")
105                                 sys.exit()
106
107                             if (resp.status_code != responsecode):
108                                 if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
109                                     sleep(0.1)
110                                     retry_cnt -= 1
111                                     http_retry_count += 1
112                                 else:
113                                     print("1Put failed for id:"+policyId.strip()+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code))
114                                     print(url_out)
115                                     print(json.loads(payload))
116                                     sys.exit()
117                             else:
118                                 retry_cnt=-1
119                 start  += 1
120     print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
121     sys.exit()
122
123 except Exception as e:
124     print("1"+str(e))
125     traceback.print_exc()
126     print(str(data_out))
127 sys.exit()