Doc update with j-release links (master branch)
[nonrtric.git] / test / common / get_policies_process_v3.py
1 #  ============LICENSE_START===============================================
2 #  Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
3 #  Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
4 #  ========================================================================
5 #  Licensed under the Apache License, Version 2.0 (the "License");
6 #  you may not use this file except in compliance with the License.
7 #  You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under the License is distributed on an "AS IS" BASIS,
13 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #  See the License for the specific language governing permissions and
15 #  limitations under the License.
16 #  ============LICENSE_END=================================================
17 #
18
19 # This script gets policies spread over a number rics
20 # Intended for parallel processing
21 # Returns a string with result, either "0" for ok, or "1<fault description>"
22
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 policyIdsFilePath proxy
34
35 try:
36     if len(sys.argv) != 8:
37         print("1Expected 7 args, got "+str(len(sys.argv)-1)+ ". Args: responseCode baseurl policyIdsFilePath startId pids pidID proxy")
38         sys.exit()
39
40     responseCode=int(sys.argv[1])
41     baseurl=str(sys.argv[2])
42     policyIdsFilePath=str(sys.argv[3])
43     startId=int(sys.argv[4])
44     pids=int(sys.argv[5])
45     pidId=int(sys.argv[6])
46     httpproxy=str(sys.argv[7])
47
48     proxydict=None
49     if httpproxy != "NOPROXY":
50         proxydict = {
51             "http" : httpproxy,
52             "https" : httpproxy
53         }
54
55     http_retry_count=0
56     connect_retry_count=0
57
58     with open(str(policyIdsFilePath)) as file:
59         for policyId in file:
60             if startId%pids == (pidId - 1):
61                 connect_ok=False
62                 retry_cnt=5
63                 while(retry_cnt>0):
64                     url=str(baseurl+policyId.strip())
65                     try:
66                         if proxydict is None:
67                             resp=requests.get(url, verify=False, timeout=90)
68                         else:
69                             resp=requests.get(url, verify=False, timeout=90, proxies=proxydict)
70                         connect_ok=True
71                     except Exception as e1:
72                         if (retry_cnt > 1):
73                             sleep(0.1)
74                             retry_cnt -= 1
75                             connect_retry_count += 1
76                         else:
77                             print("1Get failed for id:"+policyId.strip()+ ", "+str(e1) + " "+traceback.format_exc())
78                             sys.exit()
79
80                     if (connect_ok == True):
81                         if (resp.status_code == None):
82                             print("1Get failed for id:"+policyId.strip()+ ", expected response code: "+str(responseCode)+", got: None")
83                             sys.exit()
84                         if (resp.status_code != responseCode):
85                             if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
86                                 sleep(0.1)
87                                 retry_cnt -= 1
88                                 http_retry_count += 1
89                             else:
90                                 print("1Get failed for id:"+policyId.strip()+ ", expected response code: "+str(responseCode)+", got: "+str(resp.status_code)+str(resp.raw))
91                                 sys.exit()
92                         else:
93                             retry_cnt=-1
94             startId  += 1
95     print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
96     sys.exit()
97
98 except Exception as e:
99     print("1"+str(e))
100     traceback.print_exc()
101 sys.exit()