def get_rpc_handler(self, rpc_node, msg, grpc_channel, session, capabilities): try: # Parse the request into a dictionary log.info("rpc-node", node=etree.tostring(rpc_node, pretty_print=True)) request = self.parse_xml_request(rpc_node) if not request: log.error("request-bad-format") raise ncerror.BadMsg(rpc_node) log.info("parsed-request", request=request) if not request.has_key('message_id'): log.error("request-no-message-id") raise ncerror.BadMsg(rpc_node) class_handler = self._get_rpc_handler(request['command']) if class_handler is not None: return class_handler(request, rpc_node, grpc_channel, session, capabilities) log.error("rpc-not-implemented", rpc=request['command']) except ncerror.BadMsg as err: log.info('ncerror.BadMsg') raise ncerror.BadMsg(rpc_node) except Exception as e: log.exception('exception', e=e) raise ncerror.ServerException(rpc_node, exception=e)
def _validate_parameters(self): if self.request: try: if self.request['command'] != 'close-session': self.rpc_response.is_error = True self.rpc_response.node = ncerror.BadMsg(self.request_xml) return except Exception as e: self.rpc_response.is_error = True self.rpc_response.node = ncerror.ServerException( self.request_xml) return
def _validate_parameters(self): log.info('validate-parameters', session=self.session.session_id) # For now just validate that the command is presenf if self.request: try: if self.request['command'] is None: self.rpc_response.is_error = True self.rpc_response.node = ncerror.BadMsg(self.request_xml) return except Exception as e: self.rpc_response.is_error = True self.rpc_response.node = ncerror.ServerException( self.request_xml) return
def _validate_parameters(self): log.info('validate-parameters', session=self.session.session_id) # Validate the GET command if self.request: try: if self.request['command'] != 'get': self.rpc_response.is_error = True self.rpc_response.node = ncerror.BadMsg(self.request_xml) return if self.request.has_key('filter'): if not self.request.has_key('class'): self.rpc_response.is_error = True self.rpc_response.node = ncerror.NotImpl( self.request_xml) return except Exception as e: self.rpc_response.is_error = True self.rpc_response.node = ncerror.ServerException( self.request_xml) return
def handle_request(self, msg): if not self.session.session_opened: return # Any error with XML encoding here is going to cause a session close try: tree = etree.parse(io.BytesIO(msg.encode('utf-8'))) if not tree: raise ncerror.SessionError(msg, "Invalid XML from client.") except etree.XMLSyntaxError: log.error("malformed-message", msg=msg) try: error = ncerror.BadMsg(msg) self.send_message(error.get_reply_msg()) except AttributeError: log.error("attribute-error", msg=msg) # close session self.close() return rpcs = tree.xpath(C.RPC_XPATH, namespaces=C.NS_MAP) if not rpcs: raise ncerror.SessionError(msg, "No rpc found") # A message can have multiple rpc requests rpc_factory = get_rpc_factory_instance() for rpc in rpcs: try: # Validate message id is received try: msg_id = rpc.get(C.MESSAGE_ID) log.info("Received-rpc-message-id", msg_id=msg_id) except (TypeError, ValueError): log.error('no-message-id', rpc=rpc) raise ncerror.MissingElement(msg, C.MESSAGE_ID) # Get a rpc handler rpc_handler = rpc_factory.get_rpc_handler( rpc, msg, self.grpc_client, self.session, self.capabilities) if rpc_handler: # set the parameters for this handler response = yield rpc_handler.execute() log.info('handler', rpc_handler=rpc_handler, is_error=response.is_error, custom_rpc=response.custom_rpc, response=response) if not response.is_error: if response.custom_rpc: self.send_custom_rpc_reply(response.node, rpc) else: self.send_rpc_reply(response.node, rpc) # self.send_rpc_reply(self.get_mock_volthainstance(), rpc) else: self.send_message(response.node.get_xml_reply()) if response.close_session: log.info('response-closing-session', response=response) self.close() else: log.error('no-rpc-handler', request=msg, session_id=self.session.session_id) error = ncerror.NotImpl(rpc) self.send_message(error.get_xml_reply()) except ncerror.BadMsg as err: log.info('ncerror.BadMsg') if self.new_framing: self.send_message(err.get_xml_reply()) else: # If we are 1.0 we have to simply close the connection # as we are not allowed to send this error log.error("Closing-1-0-session--malformed-message") self.close() except (ncerror.NotImpl, ncerror.MissingElement) as e: log.exception('error', e=e) self.send_message(e.get_reply_msg()) except Exception as e: log.exception('Exception', e=e) error = ncerror.ServerException(rpc, e) self.send_message(error.get_xml_reply())