def process(self, data): if not self.been_greeted: self.logger.info("Client's greeting received") self.been_greeted = True return xml_request_root = remove_namespaces(etree.fromstring(data.encode())) message_id = xml_request_root.get("message-id") operation = xml_request_root[0] self.logger.info("Operation requested %s" % repr(operation.tag)) handled = False operation_name = normalize_operation_name(operation) for capability in self.capabilities: if hasattr(capability, operation_name): try: self.reply(message_id, getattr(capability, operation_name)(operation)) except NetconfError as e: self.reply(message_id, Response(e.to_etree())) handled = True if not handled: self.reply( message_id, Response(OperationNotSupported(operation_name).to_etree()))
def get_config(self, request): source = first(request.xpath("source")) content = self.datastore.to_etree(resolve_source_name(source[0].tag)) filtering = first(request.xpath("filter")) if filtering is not None: filter_content(content, filtering) return Response(content)
def get_interface_information(self, request): if len(request) == 1 and request[0].tag == "terse": return Response(self.datastore.get_interface_information_terse()) raise NetconfError("syntax error", err_type="protocol", tag="operation-failed")
def commit_results_error_to_response(commit_results_error): return Response( dict_2_etree({ 'commit-results': [ error_to_rpcerror_dict(e) for e in commit_results_error.netconf_errors ] }))
def error_to_response(error): error_specs = {"error-message": error.message} if error.type: error_specs["error-type"] = error.type if error.tag: error_specs["error-tag"] = error.tag if error.severity: error_specs["error-severity"] = error.severity if error.info: error_specs["error-info"] = error.info return Response(dict_2_etree({"rpc-error": error_specs}))
def get_configuration(self, request): if "compare" not in request.attrib: raise OperationNotSupported("get_configuration without a compare") running = self.datastore.to_etree(RUNNING) candidate = self.datastore.to_etree(CANDIDATE) data = etree.fromstring(textwrap.dedent(""" <configuration-information> <configuration-output> {0}</configuration-output> </configuration-information> """).format("There were some changes" if not xml_equals(running, candidate) else ""), parser=etree.XMLParser(recover=True)) return Response(data)
def get_configuration(self, request): if "compare" not in request.attrib: raise OperationNotSupported("get_configuration without a compare") running = self.datastore.to_etree(RUNNING) candidate = self.datastore.to_etree(CANDIDATE) # NOTE(lindycoder): Not actually computing the diff, not needed so far, just show that there's a change data_string = textwrap.dedent(""" <configuration-information> <configuration-output>{0}</configuration-output> </configuration-information> """).format("There were some changes" if not xml_equals(running, candidate) else "") data = etree.fromstring(data_string, parser=etree.XMLParser(recover=True)) return Response(data)
def commit(self, *args, **kwargs): self.datastore.commit_candidate() self.datastore.configurations.get('candidate').commit() return Response(etree.Element("ok"))
def edit_config(self, request): target = first(request.xpath("target")) config = first(request.xpath("config")) self.datastore.edit(resolve_source_name(target[0].tag), config[0]) return Response(etree.Element("ok"))
def discard_changes(self, _): self.datastore.reset() return Response(etree.Element("ok"))
def load_configuration(self, request): return Response(etree.Element("ok"), require_disconnect=False)
def unlock(self, request): target = first(request.xpath("target")) self.datastore.unlock(resolve_source_name(target[0].tag)) return Response(etree.Element("ok"))
def close_session(self, _): return Response(etree.Element("ok"), require_disconnect=True)
def errors_to_response(errors): return Response( [dict_2_etree(error_to_rpcerror_dict(error)) for error in errors])
def error_to_response(error): return Response(dict_2_etree(error_to_rpcerror_dict(error)))
def commit(self, _): self.datastore.commit_candidate() return Response(etree.Element("ok"))