Merge "Initial docs skeleton"
[it/test.git] / ons_2019_demo / a1_med / a1_med_http_server / a1med.py
1 #!flask/bin/python
2 #
3 #
4 # Copyright 2019 AT&T Intellectual Property
5 # Copyright 2019 Nokia
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 from flask import Flask, jsonify, request
21 from datetime import timedelta
22 from flask import make_response, request, current_app
23 from flask_cors import CORS, cross_origin
24 from functools import update_wrapper
25 import json
26
27 def crossdomain(origin=None, methods=None, headers=None, max_age=21600,
28                 attach_to_all=True, automatic_options=True):
29     """Decorator function that allows crossdomain requests.
30       Courtesy of
31       https://blog.skyred.fi/articles/better-crossdomain-snippet-for-flask.html
32     """
33     if methods is not None:
34         methods = ', '.join(sorted(x.upper() for x in methods))
35     if headers is not None and not isinstance(headers, list):
36         headers = ', '.join(x.upper() for x in headers)
37     if not isinstance(origin, list):
38         origin = ', '.join(origin)
39     if isinstance(max_age, timedelta):
40         max_age = max_age.total_seconds()
41
42     def get_methods():
43         """ Determines which methods are allowed
44         """
45         if methods is not None:
46             return methods
47
48         options_resp = current_app.make_default_options_response()
49         return options_resp.headers['allow']
50
51     def decorator(f):
52         """The decorator function
53         """
54         def wrapped_function(*args, **kwargs):
55             """Caries out the actual cross domain code
56             """
57             if automatic_options and request.method == 'OPTIONS':
58                 resp = current_app.make_default_options_response()
59             else:
60                 resp = make_response(f(*args, **kwargs))
61             if not attach_to_all and request.method != 'OPTIONS':
62                 return resp
63
64             h = resp.headers
65             h['Access-Control-Allow-Origin'] = origin
66             h['Access-Control-Allow-Methods'] = get_methods()
67             h['Access-Control-Max-Age'] = str(max_age)
68             h['Access-Control-Allow-Credentials'] = 'true'
69             h['Access-Control-Allow-Headers'] = \
70                 "Origin, X-Requested-With, Content-Type, Accept, Authorization"
71             if headers is not None:
72                 h['Access-Control-Allow-Headers'] = headers
73             return resp
74
75         f.provide_automatic_options = False
76         return update_wrapper(wrapped_function, f)
77     return decorator
78
79 app = Flask(__name__)
80 cors = CORS(app)
81
82 @app.route('/a1ric/metrics', methods=['GET'])
83 def get_metrics():
84     with open('metrics.json') as json_file:
85         metrics = json.load(json_file)
86     return jsonify(metrics)
87
88 @app.route('/a1ric/delay', methods=['GET'])
89 def get_delay():
90     with open('delay.json') as json_file:
91         delay = json.load(json_file)
92     return jsonify(delay)
93
94 @app.route('/a1ric/load', methods=['GET'])
95 def get_load():
96     with open('load.json') as json_file:
97         load = json.load(json_file)
98     return jsonify(load)
99
100 @app.route('/a1ric/delay', methods=['PUT'])
101 def write_delay_file():
102     if not request.json or not 'delay' in request.json:
103         abort(400)
104     delay = {
105         'delay': request.json['delay'],
106     }
107     delay_json = json.dumps(delay)
108     f = open("delay.json","w")
109     f.write(delay_json)
110
111     f = open("delay.txt","w")
112     print (request.json['delay'])
113     f.write(str(request.json['delay']))
114
115     return jsonify(delay), 201
116
117 @app.route('/a1ric/load', methods=['PUT'])
118 def write_load_file():
119     if not request.json or not 'load' in request.json:
120         abort(400)
121     load = {
122         'load': request.json['load'],
123     }
124     load_json = json.dumps(load)
125     f = open("load.json","w")
126     f.write(load_json)
127
128     f = open("load.txt","w")
129     print (request.json['load'])
130     #f.write(str(request.json['load']*80000))
131     f.write(str(request.json['load']*(8/1000)))
132
133     return jsonify(load), 201
134
135 if __name__ == '__main__':
136     app.run(debug=True, host='0.0.0.0', port=10080)
137     #app.run(debug=True, host='0.0.0.0', port=3000)