2 # Copyright 2017 Cisco Systems, Inc. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
17 from flask import Flask
18 from flask import jsonify
19 from flask import render_template
20 from flask import request
22 from flask_socketio import emit
23 from flask_socketio import SocketIO
29 from utils import byteify
30 from utils import RunLock
33 # this global cannot reside in Ctx because of the @app and @socketio decorators
38 STATUS_ERROR = 'ERROR'
39 STATUS_PENDING = 'PENDING'
40 STATUS_NOT_FOUND = 'NOT_FOUND'
43 def result_json(status, message, request_id=None):
46 'error_message': message
49 if request_id is not None:
50 body['request_id'] = request_id
56 return json.loads(json.dumps(data), object_hook=byteify)
60 return uuid.uuid4().hex
65 run_queue = Queue.Queue()
68 request_from_socketio = False
74 def enqueue(config, request_id, from_socketio=False):
76 Ctx.request_from_socketio = from_socketio
77 config['request_id'] = request_id
78 Ctx.run_queue.put(config)
80 if len(Ctx.ids) >= Ctx.MAXLEN:
82 del Ctx.results[Ctx.ids.pop(0)]
85 Ctx.ids.append(request_id)
89 config = Ctx.run_queue.get()
90 Ctx.current_id = config['request_id']
100 res['request_id'] = Ctx.current_id
101 Ctx.results[Ctx.current_id] = res
105 def get_result(request_id=None):
108 res = Ctx.results[request_id]
112 if Ctx.result and request_id == Ctx.result['request_id']:
127 def get_current_request_id():
128 return Ctx.current_id
131 def setup_flask(root_path):
134 app = Flask(__name__)
135 app.root_path = root_path
136 socketio = SocketIO(app, async_mode='threading')
137 busy_json = result_json(STATUS_ERROR, 'there is already an NFVbench request running')
138 not_busy_json = result_json(STATUS_ERROR, 'no pending NFVbench run')
139 not_found_msg = 'results not found'
140 pending_msg = 'NFVbench run still pending'
142 # --------- socketio requests ------------
144 @socketio.on('start_run')
145 def socketio_start_run(config):
146 if not Ctx.is_busy():
147 Ctx.enqueue(config, get_uuid(), from_socketio=True)
149 emit('error', {'reason': 'there is already an NFVbench request running'})
152 def socketio_echo(config):
155 # --------- HTTP requests ------------
159 return render_template('index.html')
161 @app.route('/echo', methods=['GET'])
163 config = request.json
164 return jsonify(config)
166 @app.route('/start_run', methods=['POST'])
168 config = load_json(request.json)
170 return jsonify(busy_json)
172 request_id = get_uuid()
173 Ctx.enqueue(config, request_id)
174 return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
176 @app.route('/status', defaults={'request_id': None}, methods=['GET'])
177 @app.route('/status/<request_id>', methods=['GET'])
178 def get_status(request_id):
180 if Ctx.is_busy() and request_id == Ctx.get_current_request_id():
181 # task with request_id still pending
182 return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
184 res = Ctx.get_result(request_id)
186 # found result for given request_id
189 # result for given request_id not found
190 return jsonify(result_json(STATUS_NOT_FOUND, not_found_msg, request_id))
193 # task still pending, return with request_id
194 return jsonify(result_json(STATUS_PENDING,
196 Ctx.get_current_request_id()))
198 res = Ctx.get_result()
202 return jsonify(not_busy_json)
205 class WebSocketIoServer(object):
206 """This class takes care of the web socketio server, accepts websocket events, and sends back
207 notifications using websocket events (send_ methods). Caller should simply create an instance
208 of this class and pass a runner object then invoke the run method
210 def __init__(self, http_root, runner, logger):
211 self.nfvbench_runner = runner
212 setup_flask(http_root)
213 self.fluent_logger = logger
215 def run(self, host='127.0.0.1', port=7556):
217 # socketio.run will not return so we need to run it in a background thread so that
218 # the calling thread (main thread) can keep doing work
219 socketio.start_background_task(target=socketio.run, app=app, host=host, port=port)
221 # wait for run requests
222 # the runner must be executed from the main thread (Trex client library requirement)
225 # print 'main thread waiting for requests...'
226 config = Ctx.dequeue()
227 # print 'main thread processing request...'
230 # remove unfilled values as we do not want them to override default values with None
231 config = {k: v for k, v in config.items() if v is not None}
233 results = self.nfvbench_runner.run(config, config)
234 except Exception as exc:
235 print 'NFVbench runner exception:'
236 traceback.print_exc()
237 results = result_json(STATUS_ERROR, str(exc))
240 if Ctx.request_from_socketio:
241 socketio.emit('run_end', results)
243 # this might overwrite a previously unfetched result
244 Ctx.set_result(results)
246 self.fluent_logger.send_run_summary(True)
248 def send_interval_stats(self, time_ms, tx_pps, rx_pps, drop_pct):
249 stats = {'time_ms': time_ms, 'tx_pps': tx_pps, 'rx_pps': rx_pps, 'drop_pct': drop_pct}
250 socketio.emit('run_interval_stats', stats)
252 def send_ndr_found(self, ndr_pps):
253 socketio.emit('ndr_found', {'rate_pps': ndr_pps})
255 def send_pdr_found(self, pdr_pps):
256 socketio.emit('pdr_found', {'rate_pps': pdr_pps})