2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.util.xml;
11 import static org.opendaylight.controller.config.util.xml.XmlMappingConstants.RPC_REPLY_KEY;
12 import static org.opendaylight.controller.config.util.xml.XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
14 import com.google.common.base.Preconditions;
15 import java.util.Collections;
16 import java.util.HashMap;
18 import java.util.Map.Entry;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.ParserConfigurationException;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
30 * Checked exception to communicate an error that needs to be sent to the
33 public class DocumentedException extends Exception {
35 public static final String RPC_ERROR = "rpc-error";
36 public static final String ERROR_TYPE = "error-type";
37 public static final String ERROR_TAG = "error-tag";
38 public static final String ERROR_SEVERITY = "error-severity";
39 public static final String ERROR_APP_TAG = "error-app-tag";
40 public static final String ERROR_PATH = "error-path";
41 public static final String ERROR_MESSAGE = "error-message";
42 public static final String ERROR_INFO = "error-info";
44 private static final long serialVersionUID = 1L;
46 private static final Logger LOG = LoggerFactory.getLogger(DocumentedException.class);
48 private static final DocumentBuilderFactory BUILDER_FACTORY;
51 BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
53 BUILDER_FACTORY.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
54 BUILDER_FACTORY.setFeature("http://xml.org/sax/features/external-general-entities", false);
55 BUILDER_FACTORY.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
56 BUILDER_FACTORY.setXIncludeAware(false);
57 BUILDER_FACTORY.setExpandEntityReferences(false);
58 } catch (ParserConfigurationException e) {
59 throw new ExceptionInInitializerError(e);
61 BUILDER_FACTORY.setNamespaceAware(true);
62 BUILDER_FACTORY.setCoalescing(true);
63 BUILDER_FACTORY.setIgnoringElementContentWhitespace(true);
64 BUILDER_FACTORY.setIgnoringComments(true);
67 public enum ErrorType {
68 TRANSPORT("transport"),
71 APPLICATION("application");
73 private final String typeValue;
75 ErrorType(String typeValue) {
76 this.typeValue = Preconditions.checkNotNull(typeValue);
79 public String getTypeValue() {
80 return this.typeValue;
84 * @deprecated Use {@link #getTypeValue()} instead.
87 public String getTagValue() {
88 return this.typeValue;
91 public static ErrorType from(String text) {
92 for (ErrorType e : values()) {
93 if (e.getTypeValue().equalsIgnoreCase(text)) {
102 public enum ErrorTag {
103 ACCESS_DENIED("access-denied"),
104 BAD_ATTRIBUTE("bad-attribute"),
105 BAD_ELEMENT("bad-element"),
106 DATA_EXISTS("data-exists"),
107 DATA_MISSING("data-missing"),
109 INVALID_VALUE("invalid-value"),
110 LOCK_DENIED("lock-denied"),
111 MALFORMED_MESSAGE("malformed-message"),
112 MISSING_ATTRIBUTE("missing-attribute"),
113 MISSING_ELEMENT("missing-element"),
114 OPERATION_FAILED("operation-failed"),
115 OPERATION_NOT_SUPPORTED("operation-not-supported"),
116 RESOURCE_DENIED("resource-denied"),
117 ROLLBCK_FAILED("rollback-failed"),
119 UNKNOWN_ATTRIBUTE("unknown-attribute"),
120 UNKNOWN_ELEMENT("unknown-element"),
121 UNKNOWN_NAMESPACE("unknown-namespace");
123 private final String tagValue;
125 ErrorTag(final String tagValue) {
126 this.tagValue = tagValue;
129 public String getTagValue() {
130 return this.tagValue;
133 public static ErrorTag from( String text ) {
134 for( ErrorTag e: values() )
136 if( e.getTagValue().equals( text ) ) {
141 return OPERATION_FAILED;
145 public enum ErrorSeverity {
149 private final String severityValue;
151 ErrorSeverity(String severityValue) {
152 this.severityValue = Preconditions.checkNotNull(severityValue);
155 public String getSeverityValue() {
156 return this.severityValue;
160 * @deprecated Use {@link #getSeverityValue()} instead.
163 public String getTagValue() {
164 return this.severityValue;
167 public static ErrorSeverity from(String text) {
168 for (ErrorSeverity e : values()) {
169 if (e.getSeverityValue().equalsIgnoreCase(text)) {
178 private final ErrorType errorType;
179 private final ErrorTag errorTag;
180 private final ErrorSeverity errorSeverity;
181 private final Map<String, String> errorInfo;
183 public DocumentedException(String message) {
185 DocumentedException.ErrorType.APPLICATION,
186 DocumentedException.ErrorTag.INVALID_VALUE,
187 DocumentedException.ErrorSeverity.ERROR
191 public DocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
192 final ErrorSeverity errorSeverity) {
193 this(message, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
196 public DocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
197 final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
199 this.errorType = errorType;
200 this.errorTag = errorTag;
201 this.errorSeverity = errorSeverity;
202 this.errorInfo = errorInfo;
205 public DocumentedException(final String message, final Exception cause, final ErrorType errorType,
206 final ErrorTag errorTag, final ErrorSeverity errorSeverity) {
207 this(message, cause, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
210 public DocumentedException(final String message, final Exception cause, final ErrorType errorType,
211 final ErrorTag errorTag, final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
212 super(message, cause);
213 this.errorType = errorType;
214 this.errorTag = errorTag;
215 this.errorSeverity = errorSeverity;
216 this.errorInfo = errorInfo;
219 public static <E extends Exception> DocumentedException wrap(E exception) throws DocumentedException {
220 final Map<String, String> errorInfo = new HashMap<>();
221 errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Exception thrown");
222 throw new DocumentedException(exception.getMessage(), exception, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
223 ErrorSeverity.ERROR, errorInfo);
225 public static DocumentedException wrap(ValidationException e) throws DocumentedException {
226 final Map<String, String> errorInfo = new HashMap<>();
227 errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Validation failed");
228 throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
229 ErrorSeverity.ERROR, errorInfo);
232 public static DocumentedException wrap(ConflictingVersionException e) throws DocumentedException {
233 final Map<String, String> errorInfo = new HashMap<>();
234 errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Optimistic lock failed");
235 throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
236 ErrorSeverity.ERROR, errorInfo);
239 public static DocumentedException fromXMLDocument( Document fromDoc ) {
241 ErrorType errorType = ErrorType.APPLICATION;
242 ErrorTag errorTag = ErrorTag.OPERATION_FAILED;
243 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
244 Map<String, String> errorInfo = null;
245 String errorMessage = "";
247 Node rpcReply = fromDoc.getDocumentElement();
249 // FIXME: BUG? - we only handle one rpc-error.
251 NodeList replyChildren = rpcReply.getChildNodes();
252 for( int i = 0; i < replyChildren.getLength(); i++ ) {
253 Node replyChild = replyChildren.item( i );
254 if( RPC_ERROR.equals( replyChild.getNodeName() ) )
256 NodeList rpcErrorChildren = replyChild.getChildNodes();
257 for( int j = 0; j < rpcErrorChildren.getLength(); j++ )
259 Node rpcErrorChild = rpcErrorChildren.item( j );
260 if( ERROR_TYPE.equals( rpcErrorChild.getNodeName() ) ) {
261 errorType = ErrorType.from(rpcErrorChild.getTextContent());
263 else if( ERROR_TAG.equals( rpcErrorChild.getNodeName() ) ) {
264 errorTag = ErrorTag.from(rpcErrorChild.getTextContent());
266 else if( ERROR_SEVERITY.equals( rpcErrorChild.getNodeName() ) ) {
267 errorSeverity = ErrorSeverity.from(rpcErrorChild.getTextContent());
269 else if( ERROR_MESSAGE.equals( rpcErrorChild.getNodeName() ) ) {
270 errorMessage = rpcErrorChild.getTextContent();
272 else if( ERROR_INFO.equals( rpcErrorChild.getNodeName() ) ) {
273 errorInfo = parseErrorInfo( rpcErrorChild );
281 return new DocumentedException( errorMessage, errorType, errorTag, errorSeverity, errorInfo );
284 private static Map<String, String> parseErrorInfo( Node node ) {
285 Map<String, String> infoMap = new HashMap<>();
286 NodeList children = node.getChildNodes();
287 for( int i = 0; i < children.getLength(); i++ ) {
288 Node child = children.item( i );
289 if( child.getNodeType() == Node.ELEMENT_NODE ) {
290 infoMap.put( child.getNodeName(), child.getTextContent() );
297 public ErrorType getErrorType() {
298 return this.errorType;
301 public ErrorTag getErrorTag() {
302 return this.errorTag;
305 public ErrorSeverity getErrorSeverity() {
306 return this.errorSeverity;
309 public Map<String, String> getErrorInfo() {
310 return this.errorInfo;
313 public Document toXMLDocument() {
316 doc = BUILDER_FACTORY.newDocumentBuilder().newDocument();
318 Node rpcReply = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_REPLY_KEY);
319 doc.appendChild( rpcReply );
321 Node rpcError = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_ERROR );
322 rpcReply.appendChild( rpcError );
324 rpcError.appendChild( createTextNode( doc, ERROR_TYPE, getErrorType().getTypeValue() ) );
325 rpcError.appendChild( createTextNode( doc, ERROR_TAG, getErrorTag().getTagValue() ) );
326 rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getSeverityValue() ) );
327 rpcError.appendChild( createTextNode( doc, ERROR_MESSAGE, getLocalizedMessage() ) );
329 Map<String, String> errorInfoMap = getErrorInfo();
330 if( errorInfoMap != null && !errorInfoMap.isEmpty() ) {
333 * <bad-attribute>message-id</bad-attribute>
334 * <bad-element>rpc</bad-element>
338 Node errorInfoNode = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, ERROR_INFO );
339 errorInfoNode.setPrefix( rpcReply.getPrefix() );
340 rpcError.appendChild( errorInfoNode );
342 for ( Entry<String, String> entry : errorInfoMap.entrySet() ) {
343 errorInfoNode.appendChild( createTextNode( doc, entry.getKey(), entry.getValue() ) );
347 catch( ParserConfigurationException e ) {
348 // this shouldn't happen
349 LOG.error("Error outputting to XML document", e);
355 private Node createTextNode( Document doc, String tag, String textContent ) {
356 Node node = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, tag );
357 node.setTextContent( textContent );
362 public String toString() {
363 return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
364 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
365 + this.errorInfo + '}';