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=================================================
24 from pathlib import Path
25 from flask import Flask, escape, request, Response, jsonify
26 from jsonschema import validate
27 from var_declaration import policy_instances, policy_types, policy_status, policy_fingerprint, forced_settings, hosts_set
28 from maincommon import check_apipath, apipath, get_supported_interfaces_response, extract_host_name
29 from time import sleep
32 TEXT_PLAIN='text/plain'
36 app = connexion.FlaskApp(__name__, specification_dir=apipath)
39 @app.route('/', methods=['GET'])
42 return Response("OK", 200, mimetype=TEXT_PLAIN)
44 @app.route('/ip', methods=['GET'])
46 if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
47 return jsonify({'ip': request.environ['REMOTE_ADDR']}), 200
49 return jsonify({'ip': request.environ['HTTP_X_FORWARDED_FOR']}), 200
51 #Return the current and all supported yamls for the this container
52 @app.route('/container_interfaces', methods=['GET'])
53 def container_interfaces():
55 return get_supported_interfaces_response()
57 #Delete all created instances and status
58 @app.route('/deleteinstances', methods=['POST'])
59 def deleteinstances():
61 for i in policy_instances.keys():
62 policy_instances[i]={}
65 forced_settings.clear()
66 forced_settings['code']=None
67 forced_settings['delay']=None
68 policy_fingerprint.clear()
69 return Response("All policy instances deleted", 200, mimetype=TEXT_PLAIN)
71 #Delete all - all reset
72 @app.route('/deleteall', methods=['POST'])
75 policy_instances.clear()
78 forced_settings['code']=None
79 forced_settings['delay']=None
80 policy_fingerprint.clear()
81 return Response("All policy instances and types deleted", 200, mimetype=TEXT_PLAIN)
84 @app.route('/policytype', methods=['PUT'])
87 policy_type_id=request.args.get('id')
88 if (policy_type_id is None):
89 return Response('Parameter <id> missing in request', status=400, mimetype=TEXT_PLAIN)
93 return Response("The policy type id is not an int", 400, mimetype=TEXT_PLAIN)
96 data = json.loads(data)
98 return Response("The policy type is corrupt or missing", 400, mimetype=TEXT_PLAIN)
100 if ('name' not in data.keys() or 'description' not in data.keys() or 'policy_type_id' not in data.keys() or'create_schema' not in data.keys()):
101 return Response("The policy type missing atributes", 400, mimetype=TEXT_PLAIN)
104 if (policy_type_id in policy_types.keys()):
106 if (len(policy_instances[policy_type_id]) > 0):
107 return Response("The policy type already exists and instances exists", 400, mimetype=TEXT_PLAIN)
109 policy_types[policy_type_id]=data
110 policy_instances[policy_type_id]={}
111 return Response("Policy type " + policy_type_id + " is OK.", retcode, mimetype=TEXT_PLAIN)
113 #Delete a policy type
114 @app.route('/policytype', methods=['DELETE'])
115 def del_policytype():
117 policy_type_id=request.args.get('id')
118 if (policy_type_id is None):
119 return Response('Parameter <id> missing in request', status=400, mimetype=TEXT_PLAIN)
123 return Response("The policy type id is not an int", 400, mimetype=TEXT_PLAIN)
125 if (policy_type_id in policy_types.keys()):
126 if (len(policy_instances[policy_type_id]) > 0):
127 return Response("The policy type already exists and instances exists", 400, mimetype=TEXT_PLAIN)
129 del policy_types[policy_type_id]
130 del policy_instances[policy_type_id]
131 return Response("Policy type " + policy_type_id + " is OK.", 204, mimetype=TEXT_PLAIN)
133 return Response("Policy type " + policy_type_id + " not found.", 204, mimetype=TEXT_PLAIN)
136 # Get all policy type ids
137 @app.route('/policytypes', methods=['GET'])
138 def get_policytype_ids():
140 return (json.dumps(list(policy_instances.keys())), 200)
142 #Set force response for one A1 response
143 #/forceresponse?code=<responsecode>
144 @app.route('/forceresponse', methods=['POST'])
148 forced_settings['code']=int(request.args.get('code'))
150 forced_settings['code']=None
151 return Response("Force response code: " + str(forced_settings['code']) + " set for one single A1 response", 200, mimetype=TEXT_PLAIN)
153 #Set force delay response, in seconds, for all A1 responses
154 #/froceesponse?delay=<seconds>
155 @app.route('/forcedelay', methods=['POST'])
159 forced_settings['delay']=int(request.args.get('delay'))
161 forced_settings['delay']=None
162 return Response("Force delay: " + str(forced_settings['delay']) + " sec set for all A1 responses", 200, mimetype=TEXT_PLAIN)
165 #Set status and reason
166 #/status?policyid=<policyid>&status=<status>[&deleted=<boolean>][&created_at=<timestamp>]
167 @app.route('/status', methods=['PUT'])
170 policy_id=request.args.get('policyid')
171 if (policy_id is None):
172 return Response('Parameter <policyid> missing in request', status=400, mimetype=TEXT_PLAIN)
174 if policy_id not in policy_status.keys():
175 return Response('Policyid: '+policy_id+' not found.', status=404, mimetype=TEXT_PLAIN)
176 status=request.args.get('status')
178 return Response('Parameter <status> missing in request', status=400, mimetype=TEXT_PLAIN)
179 policy_status[policy_id]["instance_status"]=status
180 msg = "Status set to "+status
181 deleted_policy=request.args.get('deleted')
182 if (deleted_policy is not None):
183 policy_status[policy_id]["has_been_deleted"]=deleted_policy
184 msg = msg + " and has_been_deleted set to "+deleted_policy
185 created_at = request.args.get('created_at')
186 if (created_at is not None):
187 policy_status[policy_id]["created_at"]=created_at
188 msg = msg + " and created_at set to "+created_at
189 msg=msg + " for policy: " + policy_id
190 return Response(msg, 200, mimetype=TEXT_PLAIN)
195 @app.route('/counter/<string:countername>', methods=['GET'])
196 def getcounter(countername):
198 if (countername == "num_instances"):
199 return Response(str(len(policy_fingerprint)), 200, mimetype=TEXT_PLAIN)
200 elif (countername == "num_types"):
201 return Response(str(len(policy_instances)),200, mimetype=TEXT_PLAIN)
202 elif (countername == "interface"):
205 return Response(str(pp[len(pp)-1]),200, mimetype=TEXT_PLAIN)
206 elif (countername == "remote_hosts"):
207 hosts=",".join(hosts_set)
208 return str(hosts),200
209 elif (countername == "datadelivery"):
210 return Response(str(0),200, mimetype=TEXT_PLAIN)
212 return Response("Counter name: "+countername+" not found.",404, mimetype=TEXT_PLAIN)
215 if len(sys.argv) >= 2 :
216 if isinstance(sys.argv[1], int):
217 port_number = sys.argv[1]
219 app.add_api('openapi.yaml')
221 if __name__ == '__main__':
222 app.run(port=port_number, host="127.0.0.1", threaded=False)