blob: 0cf900d697275727a7568ed55b80cd1db0f746e9 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/usr/bin/env python
2#
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -05003# Copyright 2017 the original author or authors.
Khen Nursimulua7b842a2016-12-03 23:28:42 -05004#
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#
19from netconf.constants import Constants as C
20from lxml import etree
21
22class 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 Nursimulua7b842a2016-12-03 23:28:42 -050075class ChannelClosed(Error):
76 pass
77
78class FramingError(Error):
79 pass
80
81class SessionError(Error):
82 pass
83
84
85class 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500107 # # 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 Nursimulua7b842a2016-12-03 23:28:42 -0500111
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500112
113
114class BadMsg(RPCError):
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500115 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 Nursimulua7b842a2016-12-03 23:28:42 -0500124
125
126class InvalidValue(Error):
127 def __init__(self, origmsg, **kwargs):
128 RPCError.__init__(self,
129 origmsg,
130 Error.RPC,
131 Error.INVALID_VALUE,
132 **kwargs)
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500133 def get_xml_reply(self):
134 return etree.tounicode(self.reply)
135
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500136
137
138class 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500146 def get_xml_reply(self):
147 return etree.tounicode(self.reply)
148
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500149
150
151class 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500160 def get_xml_reply(self):
161 return etree.tounicode(self.reply)
162
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500163
164class 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500172 def get_xml_reply(self):
173 return etree.tounicode(self.reply)
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500174
175
176class NotImpl(RPCError):
177 def __init__(self, origmsg, **kwargs):
178 RPCError.__init__(self,
179 origmsg,
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500180 Error.APPLICATION,
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500181 Error.OPERATION_NOT_SUPPORTED,
182 **kwargs)
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500183 def get_xml_reply(self):
184 return etree.tounicode(self.reply)
185
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500186
187
188class ServerException(RPCError):
189 def __init__(self, origmsg, exception, **kwargs):
190 RPCError.__init__(self,
191 origmsg,
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500192 Error.APPLICATION,
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500193 Error.OPERATION_FAILED,
194 info=str(exception),
195 **kwargs)
196
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500197 def get_xml_reply(self):
198 return etree.tounicode(self.reply)
199