Doc update with j-release links (master branch)
[nonrtric.git] / test / common / delete_policies_process_v3.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 delete 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 sys
23 import requests
24 import traceback
25 from time import sleep
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 num_rics uuid startid count pids pid_id proxy
33
34 try:
35     if len(sys.argv) != 8:
36         print("1Expected 7 args, got "+str(len(sys.argv)-1)+ ". Args: responseCode baseurl policyIdsFilePath startId pids pidID proxy")
37         sys.exit()
38
39     responseCode=int(sys.argv[1])
40     baseurl=str(sys.argv[2])
41     policyIdsFilePath=str(sys.argv[3])
42     startId=int(sys.argv[4])
43     pids=int(sys.argv[5])
44     pidId=int(sys.argv[6])
45     httpproxy=str(sys.argv[7])
46
47     proxydict=None
48     if httpproxy != "NOPROXY":
49         proxydict = {
50             "http" : httpproxy,
51             "https" : httpproxy
52         }
53
54     http_retry_count=0
55     connect_retry_count=0
56
57     with open(str(policyIdsFilePath)) as file:
58         for policyId in file:
59             if startId%pids == (pidId - 1):
60                 connect_ok=False
61                 retry_cnt=5
62                 while(retry_cnt>0):
63                     url=str(baseurl+policyId.strip())
64                     try:
65                         if proxydict is None:
66                             resp=requests.delete(url, verify=False, timeout=90)
67                         else:
68                             resp=requests.delete(url, verify=False, timeout=90, proxies=proxydict)
69                         connect_ok=True
70                     except Exception as e1:
71                         if (retry_cnt > 1):
72                             sleep(0.1)
73                             retry_cnt -= 1
74                             connect_retry_count += 1
75                         else:
76                             print("1DELETE failed for id:"+policyId.strip()+ ", "+str(e1) + " "+traceback.format_exc())
77                             sys.exit()
78
79                     if (connect_ok == True):
80                         if (resp.status_code == None):
81                             print("1DELETE failed for id:"+policyId.strip()+ ", expected response code: "+str(responseCode)+", got: None")
82                             sys.exit()
83                         if (resp.status_code != responseCode):
84                             if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
85                                 sleep(0.1)
86                                 retry_cnt -= 1
87                                 http_retry_count += 1
88                             else:
89                                 print("1DELETE failed for id:"+policyId.strip()+ ", expected response code: "+str(responseCode)+", got: "+str(resp.status_code)+str(resp.raw))
90                                 sys.exit()
91                         else:
92                             retry_cnt=-1
93             startId  += 1
94     print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
95     sys.exit()
96
97 except Exception as e:
98     print("1"+str(e))
99     traceback.print_exc()
100 sys.exit()