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
21 from flask import Flask
22 from flask import jsonify
23 from flask import render_template
24 from flask import request
26 from flask_socketio import emit
27 from flask_socketio import SocketIO
28 from summarizer import NFVBenchSummarizer
31 from utils import byteify
32 from utils import RunLock
34 # this global cannot reside in Ctx because of the @app and @socketio decorators
39 STATUS_ERROR = 'ERROR'
40 STATUS_PENDING = 'PENDING'
41 STATUS_NOT_FOUND = 'NOT_FOUND'
44 def result_json(status, message, request_id=None):
47 'error_message': message
50 if request_id is not None:
51 body['request_id'] = request_id
57 return json.loads(json.dumps(data), object_hook=byteify)
61 return uuid.uuid4().hex
66 run_queue = Queue.Queue()
69 request_from_socketio = False
75 def enqueue(config, request_id, from_socketio=False):
77 Ctx.request_from_socketio = from_socketio
78 config['request_id'] = request_id
79 Ctx.run_queue.put(config)
81 if len(Ctx.ids) >= Ctx.MAXLEN:
83 del Ctx.results[Ctx.ids.pop(0)]
86 Ctx.ids.append(request_id)
90 config = Ctx.run_queue.get()
91 Ctx.current_id = config['request_id']
101 res['request_id'] = Ctx.current_id
102 Ctx.results[Ctx.current_id] = res
106 def get_result(request_id=None):
109 res = Ctx.results[request_id]
113 if Ctx.result and request_id == Ctx.result['request_id']:
128 def get_current_request_id():
129 return Ctx.current_id
132 def setup_flask(root_path):
135 app = Flask(__name__)
136 app.root_path = root_path
137 socketio = SocketIO(app, async_mode='threading')
138 busy_json = result_json(STATUS_ERROR, 'there is already an NFVbench request running')
139 not_busy_json = result_json(STATUS_ERROR, 'no pending NFVbench run')
140 not_found_msg = 'results not found'
141 pending_msg = 'NFVbench run still pending'
143 # --------- socketio requests ------------
145 @socketio.on('start_run')
146 def _socketio_start_run(config):
147 if not Ctx.is_busy():
148 Ctx.enqueue(config, get_uuid(), from_socketio=True)
150 emit('error', {'reason': 'there is already an NFVbench request running'})
153 def _socketio_echo(config):
156 # --------- HTTP requests ------------
160 return render_template('index.html')
162 @app.route('/echo', methods=['GET'])
164 config = request.json
165 return jsonify(config)
167 @app.route('/start_run', methods=['POST'])
169 config = load_json(request.json)
173 return jsonify(busy_json)
174 request_id = get_uuid()
175 Ctx.enqueue(config, request_id)
176 return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
178 @app.route('/status', defaults={'request_id': None}, methods=['GET'])
179 @app.route('/status/<request_id>', methods=['GET'])
180 def _get_status(request_id):
182 if Ctx.is_busy() and request_id == Ctx.get_current_request_id():
183 # task with request_id still pending
184 return jsonify(result_json(STATUS_PENDING, pending_msg, request_id))
186 res = Ctx.get_result(request_id)
188 # found result for given request_id
190 # result for given request_id not found
191 return jsonify(result_json(STATUS_NOT_FOUND, not_found_msg, request_id))
194 # task still pending, return with request_id
195 return jsonify(result_json(STATUS_PENDING,
197 Ctx.get_current_request_id()))
199 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
211 def __init__(self, http_root, runner, fluent_logger):
212 self.nfvbench_runner = runner
213 setup_flask(http_root)
214 self.fluent_logger = fluent_logger
216 def run(self, host='127.0.0.1', port=7556):
218 # socketio.run will not return so we need to run it in a background thread so that
219 # the calling thread (main thread) can keep doing work
220 socketio.start_background_task(target=socketio.run, app=app, host=host, port=port)
222 # wait for run requests
223 # the runner must be executed from the main thread (Trex client library requirement)
226 # print 'main thread waiting for requests...'
227 config = Ctx.dequeue()
228 # print 'main thread processing request...'
231 # remove unfilled values as we do not want them to override default values with None
232 config = {k: v for k, v in config.items() if v is not None}
234 if self.fluent_logger:
235 self.fluent_logger.start_new_run()
236 results = self.nfvbench_runner.run(config, config)
237 except Exception as exc:
238 results = result_json(STATUS_ERROR, str(exc))
239 LOG.exception('NFVbench runner exception:')
241 if Ctx.request_from_socketio:
242 socketio.emit('run_end', results)
244 # this might overwrite a previously unfetched result
245 Ctx.set_result(results)
246 summary = NFVBenchSummarizer(results['result'], self.fluent_logger)
247 LOG.info(str(summary))
249 if self.fluent_logger:
250 self.fluent_logger.send_run_summary(True)
252 def send_interval_stats(self, time_ms, tx_pps, rx_pps, drop_pct):
253 stats = {'time_ms': time_ms, 'tx_pps': tx_pps, 'rx_pps': rx_pps, 'drop_pct': drop_pct}
254 socketio.emit('run_interval_stats', stats)
256 def send_ndr_found(self, ndr_pps):
257 socketio.emit('ndr_found', {'rate_pps': ndr_pps})
259 def send_pdr_found(self, pdr_pps):
260 socketio.emit('pdr_found', {'rate_pps': pdr_pps})