X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fusecases%2Fhealthcheck%2Fsrc%2Fmain.py;h=fdf6d422642390d31e1202ea8411aaa5a04ae67e;hb=0b74e0b18aee730623a668e10702300d2ae0e564;hp=d3fed3ad285f11f9890672346eda97567b7fb537;hpb=233225c6c7632eea0e8ab08d56bd05d8100c9289;p=nonrtric.git
diff --git a/test/usecases/healthcheck/src/main.py b/test/usecases/healthcheck/src/main.py
index d3fed3ad..fdf6d422 100644
--- a/test/usecases/healthcheck/src/main.py
+++ b/test/usecases/healthcheck/src/main.py
@@ -17,6 +17,10 @@
import argparse
from datetime import datetime
+from jinja2 import Template
+from flask import Flask, request
+import os.path
+from os import path
from pygments.util import xrange
from requests import ConnectionError
import requests
@@ -32,6 +36,58 @@ TIME_BETWEEN_CHECKS = 60
type_to_use = ''
policy_body = ''
+app = Flask(__name__)
+
+# Server info
+HOST_IP = "::"
+HOST_PORT = 9990
+APP_URL = "/stats"
+
+stat_page_template = """
+
+
+
+
+ Non-RealTime RIC Health Check
+
+
+ General
+
+ Policy type ID:...............................{{policyTypeId}}
+ Policy body path:.............................{{policyBodyPath}}
+ Time of last check:...........................{{time}}
+ Duration of check:............................{{duration}}
+ Number of checks:.............................{{noOfChecks}}
+
+ Near-RT RICs
+
+ Number of unavailable Near-RT RICS:...........{{noOfUnavailableRics}}
+ Number of Near-RT RICS not supporting type....{{noOfNotSupportingRics}}
+ Number of Near-RT RICS supporting type:.......{{noOfSupportingRics}}
+
+ Policies
+
+ Number of created policies:...................{{noOfCreatedPolicies}}
+ Number of read policies:......................{{noOfReadPolicies}}
+ Number of updated policies:...................{{noOfUpdatedPolicies}}
+ Number of deleted policies:...................{{noOfDeletedPolicies}}
+
+
+
+"""
+type_to_use = "2"
+test_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
+policy_body_path = os.path.join(test_dir, 'auto-test','testdata','OSC','pihw_template.json')
+
+duration = 0
+no_of_checks = 0
+no_of_unavailable_rics = 0
+no_of_rics_not_supporting_type = 0
+no_of_rics_supporting_type = 0
+no_of_created_policies = 0
+no_of_read_policies = 0
+no_of_updated_policies = 0
+no_of_deleted_policies = 0
class Ric:
@@ -79,6 +135,27 @@ class PolicyCheckThread (threading.Thread):
self.ric.no_of_deleted_policies += 1
+class MonitorServer (threading.Thread):
+ def __init__(self):
+ threading.Thread.__init__(self)
+
+ def run(self):
+ verboseprint('Staring monitor server')
+ app.run(port=HOST_PORT, host=HOST_IP)
+
+
+@app.route(APP_URL,
+ methods=['GET'])
+def produceStatsPage():
+ t = Template(stat_page_template)
+ page = t.render(refreshTime=TIME_BETWEEN_CHECKS, policyTypeId=type_to_use, policyBodyPath=policy_body_path,
+ time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), duration=duration, noOfChecks=no_of_checks,
+ noOfUnavailableRics=no_of_unavailable_rics, noOfNotSupportingRics=no_of_rics_not_supporting_type,
+ noOfSupportingRics=no_of_rics_supporting_type, noOfCreatedPolicies=no_of_created_policies,
+ noOfReadPolicies=no_of_read_policies, noOfUpdatedPolicies=no_of_updated_policies,
+ noOfDeletedPolicies=no_of_deleted_policies)
+ return page,200
+
def get_rics_from_agent():
resp = requests.get(BASE_URL + '/rics')
if not resp.ok:
@@ -146,14 +223,21 @@ def delete_policy(thread_id):
return True
-def statistics(duration):
+def statistics():
+ global duration
+ global no_of_checks
+ global no_of_unavailable_rics
+ global no_of_rics_not_supporting_type
+ global no_of_rics_supporting_type
+ global no_of_created_policies
+ global no_of_read_policies
+ global no_of_updated_policies
+ global no_of_deleted_policies
+
+ # Clear ric data between checks as it may have changed since last check.
no_of_unavailable_rics = 0
no_of_rics_not_supporting_type = 0
no_of_rics_supporting_type = 0
- no_of_created_policies = 0
- no_of_read_policies = 0
- no_of_updated_policies = 0
- no_of_deleted_policies = 0
for ric in rics.values():
if not (ric.state == 'AVAILABLE' or ric.state == 'CONSISTENCY_CHECK'):
@@ -167,17 +251,17 @@ def statistics(duration):
else:
no_of_rics_not_supporting_type += 1
- print(f'*********** Statistics {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ***********')
- print(f'Duration of check: {duration.total_seconds()} seconds')
- print(f'Number of checks: {no_of_checks}')
- print(f'Number of unavailable rics: {no_of_unavailable_rics}')
- print(f'Number of rics not supporting type: {no_of_rics_not_supporting_type}')
- print(f'Number of rics supporting type: {no_of_rics_supporting_type}')
- print(f'Number of created policies: {no_of_created_policies}')
- print(f'Number of read policies: {no_of_read_policies}')
- print(f'Number of updated policies: {no_of_updated_policies}')
- print(f'Number of deleted policies: {no_of_deleted_policies}')
- print('******************************************************')
+ print(f'*********** Statistics {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} *******************')
+ print(f'Duration of check: {duration.total_seconds()} seconds')
+ print(f'Number of checks: {no_of_checks}')
+ print(f'Number of unavailable Near-RT RICS: {no_of_unavailable_rics}')
+ print(f'Number of Near-RT RICS not supporting type: {no_of_rics_not_supporting_type}')
+ print(f'Number of Near-RT RICS supporting type: {no_of_rics_supporting_type}')
+ print(f'Number of created policies: {no_of_created_policies}')
+ print(f'Number of read policies: {no_of_read_policies}')
+ print(f'Number of updated policies: {no_of_updated_policies}')
+ print(f'Number of deleted policies: {no_of_deleted_policies}')
+ print('**************************************************************')
def run_check_threads(rics):
@@ -197,6 +281,9 @@ def run_check_threads(rics):
def split_rics_equally(chunks):
# prep with empty dicts
return_list = [dict() for _ in xrange(chunks)]
+ if len(rics) < RIC_CHUNK_SIZE:
+ return [rics]
+
idx = 0
for k,v in rics.items():
return_list[idx][k] = v
@@ -214,8 +301,8 @@ def get_no_of_chunks(size_of_chunks, size_to_chunk):
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='PROG')
- parser.add_argument('policyTypeId', help='The ID of the policy type to use')
- parser.add_argument('policyBodyPath', help='The path to the JSON body of the policy to create')
+ parser.add_argument('--policyTypeId', help='The ID of the policy type to use')
+ parser.add_argument('--policyBodyPath', help='The path to the JSON body of the policy to create')
parser.add_argument('-v', '--verbose', action='store_true', help='Turn on verbose printing')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = vars(parser.parse_args())
@@ -226,11 +313,19 @@ if __name__ == '__main__':
else:
verboseprint = lambda *a, **k: None # do-nothing function
- verboseprint(f'Using policy type {args["policyTypeId"]}')
- verboseprint(f'Using policy file {args["policyBodyPath"]}')
+ if args["policyTypeId"]:
+ type_to_use = args["policyTypeId"]
+
+ if args["policyBodyPath"]:
+ policy_body_path = args["policyBodyPath"]
+ if not os.path.exists(policy_body_path):
+ print(f'Policy body {policy_body_path} does not exist.')
+ sys.exit(1)
- type_to_use = args["policyTypeId"]
- with open(args["policyBodyPath"]) as json_file:
+ verboseprint(f'Using policy type {type_to_use}')
+ verboseprint(f'Using policy file {policy_body_path}')
+
+ with open(policy_body_path) as json_file:
policy_body = json_file.read()
verboseprint(f'Policy body: {policy_body}')
@@ -242,7 +337,9 @@ if __name__ == '__main__':
rics = create_ric_dict(rics_from_agent)
- no_of_checks = 0
+ monitor_server = MonitorServer()
+ monitor_server.start()
+
while True:
start_time = datetime.now()
chunked_rics = split_rics_equally(get_no_of_chunks(RIC_CHUNK_SIZE, rics.__len__()))
@@ -252,7 +349,7 @@ if __name__ == '__main__':
no_of_checks += 1
finish_time = datetime.now()
duration = finish_time - start_time
- statistics(duration)
+ statistics()
sleep_time = TIME_BETWEEN_CHECKS - duration.total_seconds()
verboseprint(f'Sleeping {sleep_time} seconds')
time.sleep(sleep_time)