blob: e1f2d5656b465f7a0ebc2ac95a3789b756cccfd2 [file] [log] [blame]
maximesson98f822c2019-12-11 16:53:33 +01001import connexion
2import fileinput
3import json
4import sys
5
6from flask import Flask, escape, request, make_response
7from var_declaration import policy_instances, policy_types, policy_status
8
9app = connexion.App(__name__, specification_dir='.')
10
11@app.route('/policytypes/<string:policyTypeId>', methods=['PUT','DELETE'])
12def policy_type(policyTypeId):
13 if request.method == 'PUT':
14 data = request.data.decode("utf-8")
15 data = data.replace("'", "\"")
16 data = json.loads(data)
17 policy_types[policyTypeId] = data
18 return ('The policy type was either created or updated for policy type id: ' + policyTypeId)
19 elif request.method == 'DELETE':
20 if policyTypeId in policy_types.keys():
21 policy_types.pop(policyTypeId)
22 return make_response("policy type successfully deleted for policy type id: " + policyTypeId, 200)
23 else:
24 return make_response("No policy type defined for the specified id", 404)
25
26@app.route('/', methods=['GET'])
27def test():
28 if "STD_QoSNud_0.1.0" in policy_types.keys():
29 return("Something fishy!", 200)
30 else:
31 return(str(list(policy_types.keys())), 200)
32
33@app.route('/deleteinstances', methods=['DELETE'])
34def delete_instances():
35 global policy_instances
36 global policy_status
37 policy_instances.clear()
38 policy_status.clear()
39 return("All policy instances deleted", 200)
40
41@app.route('/deletetypes', methods=['DELETE'])
42def delete_types():
43 global policy_types
44 policy_types.clear()
45 return("All policy types deleted", 200)
46
47@app.route('/<string:policyId>/<string:enforceStatus>', methods=['PUT'])
48def set_status(policyId, enforceStatus):
49 if policyId in policy_instances.keys():
50 if enforceStatus in ["UNDEFINED", "ENFORCED", "NOT_ENFORCED"]:
51 policy_status.pop(policyId)
52 ps = {}
53 ps["policyId"] = policyId
54 ps["enforceStatus"] = enforceStatus
55 policy_status[policyId] = ps
56 return("Status updated for policy: " + policyId, 200)
57 else:
58 return("enforceStatus should be one of \"UNDEFINED\", \"ENFORCED\" or \"NOT_ENFORCED\"", 400)
59 else:
60 return("The policy id does not correspond to any existing policy instance", 400)
61
62@app.route('/<string:policyId>/<string:enforceStatus>/<string:enforceReason>', methods=['PUT'])
63def set_status_with_reason(policyId, enforceStatus, enforceReason):
64 if policyId in policy_instances.keys():
65 if enforceStatus == "NOT_ENFORCED":
66 if enforceReason in ["100", "200", "300", "800"]:
67 policy_status.pop(policyId)
68 ps = {}
69 ps["policyId"] = policyId
70 ps["enforceStatus"] = enforceStatus
71 ps["enforceReason"] = enforceReason
72 policy_status[policyId] = ps
73 return("Status updated for policy: " + policyId, 200)
74 else:
75 return("enforceReason should be one of \"100\", \"200\", \"300\" or \"800\"", 400)
76 else:
77 return("A status provided together with an enforcement reason should be \"NOT_ENFORCED\"", 400)
78 else:
79 return("The policy id does not correspond to any existing policy instance", 404)
80
81
82port_number = 8085
83if len(sys.argv) >= 2:
84 if isinstance(sys.argv[1], int):
85 port_number = sys.argv[1]
86
87app.add_api('a1-openapi.yaml')
88app.run(port=port_number)
89