def _process_set(self, req_msg): """Process an incoming Set and generate a SetResp""" resp_msg = usp_msg.Msg() path_to_set_dict = {} update_obj_result_list = [] set_failure_param_err_list = [] self._logger.info("Processing a Set Request...") # Populate the Response's Header information resp_msg.header.msg_id = req_msg.header.msg_id resp_msg.header.msg_type = usp_msg.Header.SET_RESP # Validate the Set Request and populate the dictionaries and lists appropriately self._validate_set(req_msg, path_to_set_dict, update_obj_result_list, set_failure_param_err_list) # Finished with all validation, process the errors or make the updates if set_failure_param_err_list: usp_err_msg = utils.UspErrMsg(req_msg.header.msg_id) err_msg = "Invalid Path Found, Allow Partial Updates = False :: Fail the entire Set" resp_msg = usp_err_msg.generate_error(9000, err_msg) resp_msg.body.error.param_errs.extend(set_failure_param_err_list) else: # Process the Updates against the database for param_path in path_to_set_dict: self._db.update(param_path, path_to_set_dict[param_path]) resp_msg.body.response.set_resp.updated_obj_results.extend(update_obj_result_list) return resp_msg
def _process_operation(self, req_rec, req): """Process an incoming Operate and generate a OperateResp""" resp_rec = usp_record.Record() resp = usp.Msg() op_result_list = [] command = req.body.request.operate.command product_class = self._db.get("Device.DeviceInfo.ProductClass") self._logger.info("Processing an Operate Request...") #TODO: This is hard-coded for the Camera, but needs to be dynamic # Populate the Response's Header information self._populate_resp_header(req_rec, req, resp_rec, resp, usp.Header.OPERATE_RESP) if product_class == "RPi_Camera" or product_class == "RPiZero_Camera": # Validate that the Operate.command is supported if command == TAKE_PICTURE_CAMERA_OP: op_result = usp.OperateResp.OperationResult() op_result.executed_command = req.body.request.operate.command out_arg_map = op_result.req_output_args.output_args camera = self._service_map[product_class] param_map = camera.take_picture() for param in param_map: out_arg_map[param] = param_map[param] op_result_list.append(op_result) resp.body.response.operate_resp.operation_results.extend( op_result_list) else: # Invalid Command - return an Error to_id = req_rec.from_id err_msg = "Operate Failure: invalid command - {}".format( command) usp_err_msg = utils.UspErrMsg(req.header.msg_id, to_id, self._id) resp_rec, resp = usp_err_msg.generate_error(9000, err_msg) else: # Unknown agent product class - return an Error to_id = req_rec.from_id err_msg = "Operate Failure: unknown product class - {}".format( product_class) usp_err_msg = utils.UspErrMsg(req.header.msg_id, to_id, self._id) resp_rec, resp = usp_err_msg.generate_error(9000, err_msg) return resp_rec, resp
def _process_request(self, req_rec, req): """Processing the incoming Message and return a Response""" to_id = req_rec.from_id err_msg = "Message Failure: Request body does not match Header msg_type" usp_err_msg = utils.UspErrMsg(req.header.msg_id, to_id, self._id) resp_rec, resp = usp_err_msg.generate_error(9000, err_msg) if req.header.msg_type == usp.Header.GET: # Validate that the Request body matches the Header's msg_type if req.body.request.WhichOneof("req_type") == "get": resp_rec, resp = self._process_get(req_rec, req) # elif req.header.msg_type == usp.Header.GET_INSTANCES: # # Validate that the Request body matches the Header's msg_type # if req.body.request.WhichOneof("req_type") == "get_instances": # resp_rec, resp = self._process_get_instances(req_rec, req) # elif req.header.msg_type == usp.Header.GET_IMPL_OBJECTS: # # Validate that the Request body matches the Header's msg_type # if req.body.request.WhichOneof("req_type") == "get_impl_objects": # resp_rec, resp = self._process_get_impl_objects(req_rec, req) elif req.header.msg_type == usp.Header.SET: # Validate that the Request body matches the Header's msg_type if req.body.request.WhichOneof("req_type") == "set": resp_rec, resp = self._process_set(req_rec, req) elif req.header.msg_type == usp.Header.OPERATE: # Validate that the Request body matches the Header's msg_type if req.body.request.WhichOneof("req_type") == "operate": resp_rec, resp = self._process_operation(req_rec, req) elif req.header.msg_type == usp.Header.GET_SUPPORTED_PROTO: # Validate that the Request body matches the Header's msg_type if req.body.request.WhichOneof( "req_type") == "get_supported_protocol": resp_rec, resp = self._process_get_supported_protocol( req_rec, req) else: err_msg = "Invalid USP Message: unknown command" resp_rec, resp = usp_err_msg.generate_error(9000, err_msg) return resp_rec, resp
def _process_get_supported_protocol(self, req_rec, req): """Process an incoming GetSupportedProtocol and generate a GetSupportedProtocolResp""" resp_rec = usp_record.Record() resp = usp.Msg() self._logger.info("Processing a GetSupportedProtocol Request...") # Populate the Response's Header information self._populate_resp_header(req_rec, req, resp_rec, resp, usp.Header.GET_SUPPORTED_PROTO_RESP) # Process the supported versions in the GetSupportedProtocol Request if not ("1.0" in req.body.request.get_supported_protocol. controller_supported_protocol_versions.split(",")): to_id = req_rec.from_id err_msg = "GetSupportedProtocol Failure: incompatible versions" usp_err_msg = utils.UspErrMsg(req.header.msg_id, to_id, self._id) resp_rec, resp = usp_err_msg.generate_error(9000, err_msg) return resp_rec, resp resp.body.response.get_supported_protocol_resp.agent_supported_protocol_versions = "1.0" return resp_rec, resp
def _process_request(self, req_as_record, req_as_msg): """Processing the incoming Message and return a Response""" to_id = req_as_record.from_id resp_record = usp_record.Record() err_msg = "Message Failure: Request body does not match Header msg_type" usp_err_msg = utils.UspErrMsg(req_as_msg.header.msg_id) resp_msg = usp_err_msg.generate_error(9000, err_msg) if req_as_msg.header.msg_type == usp_msg.Header.GET: # Validate that the Request body matches the Header's msg_type if req_as_msg.body.request.WhichOneof("req_type") == "get": NUM_GET_MSGS_METRIC.inc() resp_msg = self._process_get(req_as_msg) elif req_as_msg.header.msg_type == usp_msg.Header.SET: # Validate that the Request body matches the Header's msg_type if req_as_msg.body.request.WhichOneof("req_type") == "set": NUM_SET_MSGS_METRIC.inc() resp_msg = self._process_set(req_as_msg) elif req_as_msg.header.msg_type == usp_msg.Header.OPERATE: # Validate that the Request body matches the Header's msg_type if req_as_msg.body.request.WhichOneof("req_type") == "operate": NUM_OPERATE_MSGS_METRIC.inc() resp_msg = self._process_operation(req_as_msg) else: err_msg = "Invalid USP Message: unknown command" resp_msg = usp_err_msg.generate_error(9000, err_msg) NUM_UNKNOWN_MSGS_METRIC.inc() # Wrap the USP Message response into a USP Record resp_record.version = "1.0" resp_record.to_id = to_id resp_record.from_id = self._id resp_record.payload_security = usp_record.Record.PLAINTEXT resp_record.no_session_context.payload = resp_msg.SerializeToString() return resp_msg, resp_record