1 # ============LICENSE_START===============================================
2 # Copyright (C) 2020 Nordix Foundation. 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
8 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
25 from pathlib import Path
26 from flask import Flask, escape, request, Response
27 from jsonschema import validate
28 from var_declaration import policy_instances, policy_status, callbacks, forced_settings, policy_fingerprint, hosts_set
29 from maincommon import *
34 app = connexion.App(__name__, specification_dir=apipath)
37 @app.route('/', methods=['GET'])
40 return Response("OK", 200, mimetype='text/plain')
42 #Return the current and all supported yamls for the this container
43 @app.route('/container_interfaces', methods=['GET'])
44 def container_interfaces():
46 return get_supported_interfaces_response()
48 #Delete all created instances and status
49 @app.route('/deleteinstances', methods=['POST'])
50 def delete_instances():
52 policy_instances.clear()
55 forced_settings['code']=None
56 forced_settings['delay']=None
57 policy_fingerprint.clear()
58 return Response("All policy instances deleted", 200, mimetype='text/plain')
60 #Delete all - all reset
61 #(same as delete_instances but kept to in order to use the same interface as other version of the simulator)
62 @app.route('/deleteall', methods=['POST'])
64 return delete_instances()
66 #Set force response for one A1 response
67 #/forceresponse?code=<responsecode>
68 @app.route('/forceresponse', methods=['POST'])
72 forced_settings['code']=request.args.get('code')
74 forced_settings['code']=None
75 return Response("Force response code: " + str(forced_settings['code']) + " set for one single A1 response", 200, mimetype='text/plain')
77 #Set force delay response, in seconds, for all A1 responses
78 #/froceesponse?delay=<seconds>
79 @app.route('/forcedelay', methods=['POST'])
83 forced_settings['delay']=request.args.get('delay')
85 forced_settings['delay']=None
86 return Response("Force delay: " + str(forced_settings['delay']) + " sec set for all A1 responses", 200, mimetype='text/plain')
89 #Set status and reason
90 #/status?policyid=<policyid>&status=<status>[&reason=<reason>]
91 @app.route('/status', methods=['PUT'])
94 policyId=request.args.get('policyid')
95 if (policyId is None):
96 return Response('Parameter <policyid> missing in request', status=400, mimetype='text/plain')
97 if policyId not in policy_instances.keys():
98 return Response('Policyid: '+policyId+' not found.', status=404, mimetype='text/plain')
99 status=request.args.get('status')
101 return Response('Parameter <status> missing in request', status=400, mimetype='text/plain')
102 reason=request.args.get('reason')
104 ps["enforceStatus"] = status
105 msg="Status set to "+status
106 if (reason is not None):
107 ps["enforceReason"] = reason
108 msg=msg+" and "+reason
109 policy_status[policyId] = ps
110 msg=msg+" for policy: " + policyId
111 return Response(msg, 200, mimetype='text/plain')
114 #/status?policyid=<policyid>
115 @app.route('/sendstatus', methods=['POST'])
117 policyid=request.args.get('policyid')
118 if (policyid is None):
119 return Response('Parameter <policyid> missing in request', status=400, mimetype='text/plain')
121 if (policyid not in policy_status.keys()):
122 return Response('Policyid: '+policyid+' not found.', status=404, mimetype='text/plain')
124 ps=policy_status[policyid]
125 cb=callbacks[policyid]
127 resp=requests.post(cb,json=json.dumps(ps), verify=False)
129 return Response('Post status failed, could not send to: '+str(cb), status=500, mimetype='text/plain')
130 if (resp.status_code<199 & resp.status_code > 299):
131 return Response('Post status failed with code: '+resp.status_code, status=500, mimetype='text/plain')
134 return Response(data, 200, mimetype='application/json')
136 #Receive status (only for testing callbacks)
138 @app.route('/statustest', methods=['POST', 'PUT'])
142 data = json.loads(data)
144 return Response("The status data is corrupt or missing.", 400, mimetype='text/plain')
146 return Response(json.dumps(data), 200, mimetype='application/json')
150 @app.route('/counter/<string:countername>', methods=['GET'])
151 def getCounter(countername):
153 if (countername == "num_instances"):
154 return Response(str(len(policy_instances)), 200, mimetype='text/plain')
155 elif (countername == "num_types"):
156 return Response("0",200, mimetype='text/plain')
157 elif (countername == "interface"):
160 return Response(str(pp[len(pp)-1]),200, mimetype='text/plain')
161 elif (countername == "remote_hosts"):
162 hosts=",".join(hosts_set)
163 return str(hosts),200
165 return Response("Counter name: "+countername+" not found.",404, mimetype='text/plain')
168 if len(sys.argv) >= 2:
169 if isinstance(sys.argv[1], int):
170 port_number = sys.argv[1]
172 port_number_secure=8185
174 app.add_api('STD_A1.yaml')
175 context=get_security_context()
176 if (context == None):
177 print("Start on non-secure port: "+str(port_number))
178 app.run(port=port_number, host="::")
180 print("Start on secure port: "+str(port_number_secure))
181 app.run(port=port_number_secure, host="::", ssl_context=context)