Code Review
/
sim
/
a1-interface.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
Roll version -> 2.8.0 after J-Release
[sim/a1-interface.git]
/
near-rt-ric-simulator
/
src
/
STD_1.1.3
/
main.py
diff --git
a/near-rt-ric-simulator/src/STD_1.1.3/main.py
b/near-rt-ric-simulator/src/STD_1.1.3/main.py
index
18798ab
..
df021d1
100644
(file)
--- a/
near-rt-ric-simulator/src/STD_1.1.3/main.py
+++ b/
near-rt-ric-simulator/src/STD_1.1.3/main.py
@@
-1,5
+1,6
@@
# ============LICENSE_START===============================================
# ============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.
# ========================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@
-21,22
+22,27
@@
import sys
import os
import requests
import os
import requests
+
from pathlib import Path
from pathlib import Path
-from flask import Flask,
escape,
request, Response
+from flask import Flask, request, Response
from jsonschema import validate
from jsonschema import validate
-from var_declaration import policy_instances, policy_status, callbacks, forced_settings, policy_fingerprint
-from maincommon import
*
+from var_declaration import policy_instances, policy_status, callbacks, forced_settings, policy_fingerprint
, hosts_set, app
+from maincommon import
check_apipath, apipath, get_supported_interfaces_response, extract_host_name
+#Constants
+TEXT_PLAIN='text/plain'
check_apipath()
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'])
def test():
#Check alive function
@app.route('/', methods=['GET'])
def test():
- return Response("OK", 200, mimetype=
'text/plain'
)
+ return Response("OK", 200, mimetype=
TEXT_PLAIN
)
#Return the current and all supported yamls for the this container
@app.route('/container_interfaces', methods=['GET'])
#Return the current and all supported yamls for the this container
@app.route('/container_interfaces', methods=['GET'])
@@
-54,7
+60,7
@@
def delete_instances():
forced_settings['code']=None
forced_settings['delay']=None
policy_fingerprint.clear()
forced_settings['code']=None
forced_settings['delay']=None
policy_fingerprint.clear()
- return Response("All policy instances deleted", 200, mimetype=
'text/plain'
)
+ 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)
#Delete all - all reset
#(same as delete_instances but kept to in order to use the same interface as other version of the simulator)
@@
-69,9
+75,9
@@
def forceresponse():
try:
forced_settings['code']=request.args.get('code')
try:
forced_settings['code']=request.args.get('code')
- except:
+ except
Exception
:
forced_settings['code']=None
forced_settings['code']=None
- return Response("Force response code: " + str(forced_settings['code']) + " set for one single A1 response", 200, mimetype=
'text/plain'
)
+ return Response("Force response code: " + str(forced_settings['code']) + " set for one single A1 response", 200, mimetype=
TEXT_PLAIN
)
#Set force delay response, in seconds, for all A1 responses
#/froceesponse?delay=<seconds>
#Set force delay response, in seconds, for all A1 responses
#/froceesponse?delay=<seconds>
@@
-80,24
+86,24
@@
def forcedelay():
try:
forced_settings['delay']=request.args.get('delay')
try:
forced_settings['delay']=request.args.get('delay')
- except:
+ except
Exception
:
forced_settings['delay']=None
forced_settings['delay']=None
- return Response("Force delay: " + str(forced_settings['delay']) + " sec set for all A1 responses", 200, mimetype=
'text/plain'
)
+ return Response("Force delay: " + str(forced_settings['delay']) + " sec set for all A1 responses", 200, mimetype=
TEXT_PLAIN
)
#Set status and reason
#Set status and reason
-#/status?policyid=<policyid>&status=<status>[reason=<reason>]
+#/status?policyid=<policyid>&status=<status>[
&
reason=<reason>]
@app.route('/status', methods=['PUT'])
def setstatus():
@app.route('/status', methods=['PUT'])
def setstatus():
- policy
I
d=request.args.get('policyid')
- if (policy
I
d is None):
- return Response('Parameter <policyid> missing in request', status=400, mimetype=
'text/plain'
)
- if policy
I
d not in policy_instances.keys():
- return Response('Policyid: '+policy
Id+' not found.', status=404, mimetype='text/plain'
)
+ policy
_i
d=request.args.get('policyid')
+ if (policy
_i
d is None):
+ return Response('Parameter <policyid> missing in request', status=400, mimetype=
TEXT_PLAIN
)
+ if policy
_i
d not in policy_instances.keys():
+ return Response('Policyid: '+policy
_id+' not found.', status=404, mimetype=TEXT_PLAIN
)
status=request.args.get('status')
if (status is None):
status=request.args.get('status')
if (status is None):
- return Response('Parameter <status> missing in request', status=400, mimetype=
'text/plain'
)
+ return Response('Parameter <status> missing in request', status=400, mimetype=
TEXT_PLAIN
)
reason=request.args.get('reason')
ps = {}
ps["enforceStatus"] = status
reason=request.args.get('reason')
ps = {}
ps["enforceStatus"] = status
@@
-105,9
+111,9
@@
def setstatus():
if (reason is not None):
ps["enforceReason"] = reason
msg=msg+" and "+reason
if (reason is not None):
ps["enforceReason"] = reason
msg=msg+" and "+reason
- policy_status[policy
I
d] = ps
- msg=msg+" for policy: " + policy
I
d
- return Response(msg, 200, mimetype=
'text/plain'
)
+ policy_status[policy
_i
d] = ps
+ msg=msg+" for policy: " + policy
_i
d
+ return Response(msg, 200, mimetype=
TEXT_PLAIN
)
#Send status
#/status?policyid=<policyid>
#Send status
#/status?policyid=<policyid>
@@
-115,19
+121,19
@@
def setstatus():
def sendstatus():
policyid=request.args.get('policyid')
if (policyid is None):
def sendstatus():
policyid=request.args.get('policyid')
if (policyid is None):
- return Response('Parameter <policyid> missing in request', status=400, mimetype=
'text/plain'
)
+ return Response('Parameter <policyid> missing in request', status=400, mimetype=
TEXT_PLAIN
)
if (policyid not in policy_status.keys()):
if (policyid not in policy_status.keys()):
- return Response('Policyid: '+policyid+' not found.', status=404, mimetype=
'text/plain'
)
+ return Response('Policyid: '+policyid+' not found.', status=404, mimetype=
TEXT_PLAIN
)
ps=policy_status[policyid]
cb=callbacks[policyid]
try:
ps=policy_status[policyid]
cb=callbacks[policyid]
try:
- resp=requests.post(cb,json=json.dumps(ps)
)
- except:
- return Response('Post status failed, could not send to: '+str(cb), status=500, mimetype=
'text/plain'
)
+ resp=requests.post(cb,json=json.dumps(ps)
, verify=False) # NOSONAR
+ 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):
if (resp.status_code<199 & resp.status_code > 299):
- return Response('Post status failed with code: '+resp.status_code, status=500, mimetype=
'text/plain'
)
+ 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')
data = resp.json()
return Response(data, 200, mimetype='application/json')
@@
-139,31
+145,38
@@
def statustest():
try:
data = request.data
data = json.loads(data)
try:
data = request.data
data = json.loads(data)
- except:
- return Response("The status data is corrupt or missing.", 400, mimetype=
'text/plain'
)
+ except
Exception
:
+ return Response("The status data is corrupt or missing.", 400, mimetype=
TEXT_PLAIN
)
return Response(json.dumps(data), 200, mimetype='application/json')
#Metrics function
#Get a named counter
@app.route('/counter/<string:countername>', methods=['GET'])
return Response(json.dumps(data), 200, mimetype='application/json')
#Metrics function
#Get a named counter
@app.route('/counter/<string:countername>', methods=['GET'])
-def get
C
ounter(countername):
+def get
c
ounter(countername):
if (countername == "num_instances"):
if (countername == "num_instances"):
- return Response(str(len(policy_instances)), 200, mimetype=
'text/plain'
)
+ return Response(str(len(policy_instances)), 200, mimetype=
TEXT_PLAIN
)
elif (countername == "num_types"):
elif (countername == "num_types"):
- return Response("0",200, mimetype=
'text/plain'
)
+ return Response("0",200, mimetype=
TEXT_PLAIN
)
elif (countername == "interface"):
p=Path(os.getcwd())
pp=p.parts
elif (countername == "interface"):
p=Path(os.getcwd())
pp=p.parts
- return Response(str(pp[len(pp)-1]),200, mimetype='text/plain')
+ return Response(str(pp[len(pp)-1]),200, mimetype=TEXT_PLAIN)
+ elif (countername == "remote_hosts"):
+ hosts=",".join(hosts_set)
+ return str(hosts),200
+ elif (countername == "datadelivery"):
+ return Response(str(0),200, mimetype=TEXT_PLAIN)
else:
else:
- return Response("Counter name: "+countername+" not found.",404, mimetype=
'text/plain'
)
+ return Response("Counter name: "+countername+" not found.",404, mimetype=
TEXT_PLAIN
)
-port_number =
8085
+port_number =
2222
if len(sys.argv) >= 2:
if isinstance(sys.argv[1], int):
port_number = sys.argv[1]
app.add_api('STD_A1.yaml')
if len(sys.argv) >= 2:
if isinstance(sys.argv[1], int):
port_number = sys.argv[1]
app.add_api('STD_A1.yaml')
-app.run(port=port_number)
+
+if __name__ == '__main__':
+ app.run(port=port_number, host="127.0.0.1")
\ No newline at end of file