- public static NetconfDocumentedException wrap(ConflictingVersionException e) throws NetconfDocumentedException {
- final Map<String, String> errorInfo = new HashMap<>();
- errorInfo.put(ErrorTag.operation_failed.name(), "Optimistic lock failed");
- throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
- ErrorSeverity.error, errorInfo);
- }
-
- public static NetconfDocumentedException fromXMLDocument( Document fromDoc ) {
-
- ErrorType errorType = ErrorType.application;
- ErrorTag errorTag = ErrorTag.operation_failed;
- ErrorSeverity errorSeverity = ErrorSeverity.error;
- Map<String, String> errorInfo = null;
- String errorMessage = "";
-
- Node rpcReply = fromDoc.getDocumentElement();
-
- // FIXME: BUG? - we only handle one rpc-error.
-
- NodeList replyChildren = rpcReply.getChildNodes();
- for( int i = 0; i < replyChildren.getLength(); i++ ) {
- Node replyChild = replyChildren.item( i );
- if( RPC_ERROR.equals( replyChild.getNodeName() ) )
- {
- NodeList rpcErrorChildren = replyChild.getChildNodes();
- for( int j = 0; j < rpcErrorChildren.getLength(); j++ )
- {
- Node rpcErrorChild = rpcErrorChildren.item( j );
- if( ERROR_TYPE.equals( rpcErrorChild.getNodeName() ) ) {
- errorType = ErrorType.from( rpcErrorChild.getTextContent() );
- }
- else if( ERROR_TAG.equals( rpcErrorChild.getNodeName() ) ) {
- errorTag = ErrorTag.from( rpcErrorChild.getTextContent() );
- }
- else if( ERROR_SEVERITY.equals( rpcErrorChild.getNodeName() ) ) {
- errorSeverity = ErrorSeverity.from( rpcErrorChild.getTextContent() );
- }
- else if( ERROR_MESSAGE.equals( rpcErrorChild.getNodeName() ) ) {
- errorMessage = rpcErrorChild.getTextContent();
- }
- else if( ERROR_INFO.equals( rpcErrorChild.getNodeName() ) ) {
- errorInfo = parseErrorInfo( rpcErrorChild );
- }
- }
-
- break;
- }
- }
-
- return new NetconfDocumentedException( errorMessage, errorType, errorTag, errorSeverity, errorInfo );
- }
-
- private static Map<String, String> parseErrorInfo( Node node ) {
- Map<String, String> infoMap = new HashMap<>();
- NodeList children = node.getChildNodes();
- for( int i = 0; i < children.getLength(); i++ ) {
- Node child = children.item( i );
- if( child.getNodeType() == Node.ELEMENT_NODE ) {
- infoMap.put( child.getNodeName(), child.getTextContent() );
- }
- }
-
- return infoMap;
- }
-
- public ErrorType getErrorType() {
- return this.errorType;
- }
-
- public ErrorTag getErrorTag() {
- return this.errorTag;
- }
-
- public ErrorSeverity getErrorSeverity() {
- return this.errorSeverity;
- }
-
- public Map<String, String> getErrorInfo() {
- return this.errorInfo;
- }
-
- public Document toXMLDocument() {
- Document doc = null;
- try {
- doc = BUILDER_FACTORY.newDocumentBuilder().newDocument();
-
- Node rpcReply = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_REPLY_KEY );
- doc.appendChild( rpcReply );
-
- Node rpcError = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_ERROR );
- rpcReply.appendChild( rpcError );
-
- rpcError.appendChild( createTextNode( doc, ERROR_TYPE, getErrorType().getTagValue() ) );
- rpcError.appendChild( createTextNode( doc, ERROR_TAG, getErrorTag().getTagValue() ) );
- rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getTagValue() ) );
- rpcError.appendChild( createTextNode( doc, ERROR_MESSAGE, getLocalizedMessage() ) );
-
- Map<String, String> errorInfoMap = getErrorInfo();
- if( errorInfoMap != null && !errorInfoMap.isEmpty() ) {
- /*
- * <error-info>
- * <bad-attribute>message-id</bad-attribute>
- * <bad-element>rpc</bad-element>
- * </error-info>
- */
-
- Node errorInfoNode = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, ERROR_INFO );
- errorInfoNode.setPrefix( rpcReply.getPrefix() );
- rpcError.appendChild( errorInfoNode );
-
- for ( Entry<String, String> entry : errorInfoMap.entrySet() ) {
- errorInfoNode.appendChild( createTextNode( doc, entry.getKey(), entry.getValue() ) );
- }
- }
- }
- catch( ParserConfigurationException e ) {
- LOG.error( "Error outputting to XML document", e ); // this shouldn't happen
- }
-
- return doc;