Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 4 | # |
| 5 | # Adapted from https://github.com/choppsv1/netconf/error.py |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | from netconf.constants import Constants as C |
| 20 | from lxml import etree |
| 21 | |
| 22 | class Error(Exception): |
| 23 | |
| 24 | ### Tag ### |
| 25 | IN_USE = "in-use" |
| 26 | INVALID_VALUE = "invalid-value" |
| 27 | TOO_BIG = "too-big" |
| 28 | MISSING_ATTRIBUTE = "missing-attribute" |
| 29 | BAD_ATTRIBUTE = "bad-attribute" |
| 30 | UNKNOWN_ATTRIBUTE = "unknown-attribute" |
| 31 | MISSING_ELEMENT = "missing-element" |
| 32 | BAD_ELEMENT = "bad-element" |
| 33 | UNKNOWN_ELEMENT = "unknown-element" |
| 34 | UNKNOWN_NAMESPACE = "unknown-namespace" |
| 35 | ACCESS_DENIED = "access-denied" |
| 36 | LOCK_DENIED = "lock-denied" |
| 37 | RESOURCE_DENIED = "resource-denied" |
| 38 | ROLLBACK_FAILED = "rollback-failed" |
| 39 | DATA_EXISTS = "data-exists" |
| 40 | DATA_MISSING = "data-missing" |
| 41 | OPERATION_NOT_SUPPORTED = "operation-not-supported" |
| 42 | OPERATION_FAILED = "operation-failed" |
| 43 | MALFORMED_MESSAGE = "malformed-message" |
| 44 | |
| 45 | ### Error-type ### |
| 46 | TRANSPORT = 0 |
| 47 | RPC = 1 |
| 48 | PROTOCOL = 2 |
| 49 | APPLICATION = 3 |
| 50 | ERROR_TYPE_ENUM = { |
| 51 | TRANSPORT: "transport", |
| 52 | RPC: "rpc", |
| 53 | PROTOCOL: "protocol", |
| 54 | APPLICATION: "application" |
| 55 | } |
| 56 | |
| 57 | ### Severity ### |
| 58 | ERROR = "error" |
| 59 | WARNING = "warning" |
| 60 | |
| 61 | ### Error-info ### |
| 62 | BAD_ATTRIBUTE_INFO = "bad-attribute" |
| 63 | BAD_ELEMENT_INFO = "bad-element" |
| 64 | SESSION_ID_INFO = "session-id" |
| 65 | OK_ELEMENT_INFO = "ok-element" |
| 66 | ERR_ELEMENT_INFO = "err-element" |
| 67 | NOOP_ELEMENT_INFO = "noop-element" |
| 68 | |
| 69 | ### XML Error tags ### |
| 70 | XML_ERROR_TYPE = "error-type" |
| 71 | XML_ERROR_TAG = "error-tag" |
| 72 | XML_ERROR_SEV = "error-severity" |
| 73 | |
| 74 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 75 | class ChannelClosed(Error): |
| 76 | pass |
| 77 | |
| 78 | class FramingError(Error): |
| 79 | pass |
| 80 | |
| 81 | class SessionError(Error): |
| 82 | pass |
| 83 | |
| 84 | |
| 85 | class RPCError(Error): |
| 86 | def __init__(self, origmsg, error_type, error_tag, **kwargs): |
| 87 | # Create the rpc reply |
| 88 | # Append original attributes and namespace |
| 89 | self.reply = etree.Element(C.RPC_REPLY, |
| 90 | attrib=origmsg.attrib, |
| 91 | nsmap=origmsg.nsmap) |
| 92 | |
| 93 | rpcerr = etree.SubElement(self.reply, C.RPC_ERROR) |
| 94 | |
| 95 | if error_type in Error.ERROR_TYPE_ENUM: |
| 96 | error_type = Error.ERROR_TYPE_ENUM[error_type] |
| 97 | else: |
| 98 | error_type = Error.ERROR_TYPE_ENUM[Error.RPC] |
| 99 | |
| 100 | etree.SubElement(rpcerr, Error.XML_ERROR_TYPE).text = str(error_type) |
| 101 | |
| 102 | etree.SubElement(rpcerr, Error.XML_ERROR_TAG).text = error_tag |
| 103 | |
| 104 | if "severity" not in kwargs: |
| 105 | etree.SubElement(rpcerr, Error.XML_ERROR_SEV).text = "error" |
| 106 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 107 | # # Convert all remaining arguments to xml |
| 108 | # for key, value in kwargs.items(): |
| 109 | # key = key.replace('_', '-') |
| 110 | # etree.SubElement(rpcerr, "error-{}".format(key)).text = str(value) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 111 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 112 | |
| 113 | |
| 114 | class BadMsg(RPCError): |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 115 | def __init__(self, origmsg, exception=None): |
| 116 | super(BadMsg, self).__init__( |
| 117 | origmsg, |
| 118 | Error.RPC, |
| 119 | Error.MALFORMED_MESSAGE, |
| 120 | info=str(exception)) |
| 121 | |
| 122 | def get_xml_reply(self): |
| 123 | return etree.tounicode(self.reply) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 124 | |
| 125 | |
| 126 | class InvalidValue(Error): |
| 127 | def __init__(self, origmsg, **kwargs): |
| 128 | RPCError.__init__(self, |
| 129 | origmsg, |
| 130 | Error.RPC, |
| 131 | Error.INVALID_VALUE, |
| 132 | **kwargs) |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 133 | def get_xml_reply(self): |
| 134 | return etree.tounicode(self.reply) |
| 135 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 136 | |
| 137 | |
| 138 | class MissingElement(RPCError): |
| 139 | def __init__(self, origmsg, tag, **kwargs): |
| 140 | RPCError.__init__(self, |
| 141 | origmsg, |
| 142 | Error.RPC, |
| 143 | Error.MISSING_ELEMENT, |
| 144 | info=tag, |
| 145 | **kwargs) |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 146 | def get_xml_reply(self): |
| 147 | return etree.tounicode(self.reply) |
| 148 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 149 | |
| 150 | |
| 151 | class BadElement(RPCError): |
| 152 | def __init__(self, origmsg, element, **kwargs): |
| 153 | RPCError.__init__(self, |
| 154 | origmsg, |
| 155 | Error.RPC, |
| 156 | Error.BAD_ELEMENT, |
| 157 | info=element.tag, |
| 158 | **kwargs) |
| 159 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 160 | def get_xml_reply(self): |
| 161 | return etree.tounicode(self.reply) |
| 162 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 163 | |
| 164 | class UnknownElement(RPCError): |
| 165 | def __init__(self, origmsg, element, **kwargs): |
| 166 | RPCError.__init__(self, |
| 167 | origmsg, |
| 168 | Error.RPC, |
| 169 | Error.UNKNOWN_ELEMENT, |
| 170 | info=element.tag, |
| 171 | **kwargs) |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 172 | def get_xml_reply(self): |
| 173 | return etree.tounicode(self.reply) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 174 | |
| 175 | |
| 176 | class NotImpl(RPCError): |
| 177 | def __init__(self, origmsg, **kwargs): |
| 178 | RPCError.__init__(self, |
| 179 | origmsg, |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 180 | Error.APPLICATION, |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 181 | Error.OPERATION_NOT_SUPPORTED, |
| 182 | **kwargs) |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 183 | def get_xml_reply(self): |
| 184 | return etree.tounicode(self.reply) |
| 185 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 186 | |
| 187 | |
| 188 | class ServerException(RPCError): |
| 189 | def __init__(self, origmsg, exception, **kwargs): |
| 190 | RPCError.__init__(self, |
| 191 | origmsg, |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 192 | Error.APPLICATION, |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 193 | Error.OPERATION_FAILED, |
| 194 | info=str(exception), |
| 195 | **kwargs) |
| 196 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 197 | def get_xml_reply(self): |
| 198 | return etree.tounicode(self.reply) |
| 199 | |