def run(self): log.info("Starting REST server on: %s:%s" % self.__werkzeug.server_address) try: self.__werkzeug.serve_forever() except Exception as e: log.exception("Got exception in REST-API loop: %s" % e)
def __init__(self, standalone=False, **kwargs): log.info("Starting REST-API Sublayer...") self.__server = MainApiServer() self.__entry_point_cache = {} self.mgrs = {} self.is_up = False self.prefix = CONFIG.get_rest_api_prefix() super(RestInterfaceAPI, self).__init__(standalone, **kwargs)
def update(param=None): call_as_coop_task(func=quit_with_code, ret_code=UPDATE_VALUE) if param is not None: with open(".checkout", 'w') as f: f.write(param) log.info("Update task scheduled with explicit checkpoint: %s" % param) return Response("UPDATE accepted (%s).\n" % param, httplib.ACCEPTED) else: log.info("Update task scheduled") return Response("UPDATE accepted.\n", httplib.ACCEPTED)
def dispatch_request(self): """ :return: """ log.info("Received edit-config deploy request...") if not request.data: log.error("No data received!") return Response("Request data is missing!", httplib.BAD_REQUEST) # Get message-id unique = "ESCAPE-%s-edit-config" % self.mgr.LAYER_NAME # Trailing stats.init_request_measurement(request_id=unique) MessageDumper().dump_to_file(data=request.data, unique=unique) # Parsing log.debug("Parsing request (body_size: %s)..." % len(request.data)) if CONFIG.get_rest_api_config(self.mgr.LAYER_NAME)['unify_interface']: req = Virtualizer.parse_from_text(text=request.data) else: req = NFFG.parse(raw_data=request.data) if req.mode: log.info("Detected mapping mode in request body: %s" % req.mode) else: if request.method == 'POST': req.mode = req.MODE_ADD log.debug('Add mapping mode: %s based on HTTP verb: %s' % (req.mode, request.method)) elif request.method == 'PUT': req.mode = NFFG.MODE_DEL log.debug('Add mapping mode: %s based on HTTP verb: %s' % (req.mode, request.method)) else: log.info('No mode parameter has been defined in body!') log.debug("Request parsing ended...") # Scheduling params = request.args.to_dict(flat=True) msg_id = self.get_message_id() log.info("Acquired message-id: %s" % msg_id) params[self.MESSAGE_ID_NAME] = msg_id entry_point = self.mgr.layer_api.get_entry_point( layer=self.mgr.LAYER_NAME, rpc=self.name) self.mgr.scheduler.schedule_request(id=msg_id, layer=self.mgr.LAYER_NAME, hook=entry_point, data=req, params=params) return Response(status=httplib.ACCEPTED, headers={"message-id": msg_id})
def shutdown(self, event): log.info("REST-API Sublayer is going down...") self.__server.stop()
def restart(): call_as_coop_task(func=quit_with_code, ret_code=RESTART_VALUE) log.info("Restart task scheduled") return Response("RESTART accepted.\n", httplib.ACCEPTED)
def stop(): call_as_coop_task(func=quit_with_code, ret_code=STOP_VALUE) log.info("Shutdown task scheduled") return Response("SHUTDOWN accepted.\n", httplib.ACCEPTED)
def shutdown(): call_as_coop_task(func=quit_with_ok) log.info("Shutdown task scheduled") return Response("SHUTDOWN accepted.\n", httplib.ACCEPTED)