NONRTRIC-955: Fix unit test 404 issue
[sim/a1-interface.git] / near-rt-ric-simulator / src / STD_2.0.0 / main.py
index dcf835e..c16b81e 100644 (file)
@@ -1,5 +1,6 @@
 #  ============LICENSE_START===============================================
-#  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+#  Copyright (C) 2023 Nordix Foundation. All rights reserved.
+#  Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
 #  ========================================================================
 #  Licensed under the Apache License, Version 2.0 (the "License");
 #  you may not use this file except in compliance with the License.
@@ -23,17 +24,21 @@ import requests
 
 
 from pathlib import Path
-from flask import Flask, escape, request, Response
+from flask import Flask, request, Response
 from jsonschema import validate
-from var_declaration import policy_instances, policy_types, policy_status, callbacks, forced_settings, policy_fingerprint, hosts_set, data_delivery_counter
+from var_declaration import policy_instances, policy_types, policy_status, callbacks, forced_settings, policy_fingerprint, hosts_set, data_delivery_counter, app
 from maincommon import check_apipath, apipath, get_supported_interfaces_response, extract_host_name
 
-#Constants
+# Constants
 TEXT_PLAIN='text/plain'
+APPL_JSON='application/json'
+
 
 check_apipath()
 
-app = connexion.App(__name__, specification_dir=apipath)
+# app is created in var_declarations
+
+import payload_logging   # app var need to be initialized
 
 #Check alive function
 @app.route('/', methods=['GET'])
@@ -61,9 +66,9 @@ def delete_instances():
   return Response("All policy instances deleted", 200, mimetype=TEXT_PLAIN)
 
 #Delete all - all reset
-#(same as delete_instances but kept to in order to use the same interface as other version of the simulator)
 @app.route('/deleteall', methods=['POST'])
 def delete_all():
+  global data_delivery_counter
 
   policy_instances.clear()
   policy_types.clear()
@@ -72,6 +77,7 @@ def delete_all():
   forced_settings['code']=None
   forced_settings['delay']=None
   policy_fingerprint.clear()
+  data_delivery_counter=0
   return Response("All policy instances and types deleted", 200, mimetype=TEXT_PLAIN)
 
 #Load a policy type
@@ -188,15 +194,13 @@ def sendstatus():
   ps=policy_status[policyid]
   cb=callbacks[policyid]
   try:
-    print("Callback url: " + str(cb))
     resp=requests.post(cb,json=json.dumps(ps), verify=False) # NOSONAR
-  except:
+  except Exception:
     return Response('Post status failed, could not send to: '+str(cb), status=500, mimetype=TEXT_PLAIN)
   if (resp.status_code<199 & resp.status_code > 299):
     return Response('Post status failed with code: '+resp.status_code, status=500, mimetype=TEXT_PLAIN)
 
-  data = resp.json()
-  return Response(data, 200, mimetype='application/json')
+  return Response(None, 204, mimetype=APPL_JSON)
 
 #Receive status (only for testing callbacks)
 #/statustest
@@ -208,7 +212,7 @@ def statustest():
   except Exception:
     return Response("The status data is corrupt or missing.", 400, mimetype=TEXT_PLAIN)
 
-  return Response(json.dumps(data), 200, mimetype='application/json')
+  return Response(json.dumps(data), 200, mimetype=APPL_JSON)
 
 #Receive a data delivery package
 #/datadelivery
@@ -252,4 +256,4 @@ if len(sys.argv) >= 2:
 app.add_api('ORAN_A1-p_V2.0.0_api.yaml')
 
 if __name__ == '__main__':
-  app.run(port=port_number, host="127.0.0.1", threaded=False)
\ No newline at end of file
+  app.run(port=port_number, host="127.0.0.1")
\ No newline at end of file