Remove DocumentedException.ErrorSeverity
[netconf.git] / netconf / netconf-api / src / main / java / org / opendaylight / netconf / api / DocumentedException.java
index 15d4dfe443882b280f9a6f83db4d4db93b3ea9d5..07636246bb20848c41ab3a0e15445a3b637aa532 100644 (file)
@@ -5,19 +5,18 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.api;
 
+import static java.util.Objects.requireNonNull;
 import static org.opendaylight.netconf.api.xml.XmlNetconfConstants.RPC_REPLY_KEY;
 import static org.opendaylight.netconf.api.xml.XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
 
-import com.google.common.base.Preconditions;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -68,7 +67,7 @@ public class DocumentedException extends Exception {
         private final String typeValue;
 
         ErrorType(final String typeValue) {
-            this.typeValue = Preconditions.checkNotNull(typeValue);
+            this.typeValue = requireNonNull(typeValue);
         }
 
         public String getTypeValue() {
@@ -128,48 +127,22 @@ public class DocumentedException extends Exception {
         }
     }
 
-    public enum ErrorSeverity {
-        ERROR("error"), WARNING("warning");
-
-        private final String severityValue;
-
-        ErrorSeverity(final String severityValue) {
-            this.severityValue = Preconditions.checkNotNull(severityValue);
-        }
-
-        public String getSeverityValue() {
-            return this.severityValue;
-        }
-
-        public static ErrorSeverity from(final String text) {
-            for (ErrorSeverity e : values()) {
-                if (e.getSeverityValue().equalsIgnoreCase(text)) {
-                    return e;
-                }
-            }
-
-            return ERROR;
-        }
-    }
-
     private final ErrorType errorType;
     private final ErrorTag errorTag;
     private final ErrorSeverity errorSeverity;
     private final Map<String, String> errorInfo;
 
     public DocumentedException(final String message) {
-        this(message, DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.INVALID_VALUE,
-                DocumentedException.ErrorSeverity.ERROR);
+        this(message, ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
     }
 
     public DocumentedException(final String message, final Exception cause) {
-        this(message, cause, DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.INVALID_VALUE,
-                DocumentedException.ErrorSeverity.ERROR);
+        this(message, cause, ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
     }
 
     public DocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
             final ErrorSeverity errorSeverity) {
-        this(message, errorType, errorTag, errorSeverity, Collections.emptyMap());
+        this(message, errorType, errorTag, errorSeverity, Map.of());
     }
 
     public DocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
@@ -183,7 +156,7 @@ public class DocumentedException extends Exception {
 
     public DocumentedException(final String message, final Exception cause, final ErrorType errorType,
             final ErrorTag errorTag, final ErrorSeverity errorSeverity) {
-        this(message, cause, errorType, errorTag, errorSeverity, Collections.emptyMap());
+        this(message, cause, errorType, errorTag, errorSeverity, Map.of());
     }
 
     public DocumentedException(final String message, final Exception cause, final ErrorType errorType,
@@ -209,33 +182,48 @@ public class DocumentedException extends Exception {
         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
         Map<String, String> errorInfo = null;
         String errorMessage = "";
+        String allErrorMessages = "";
 
         Node rpcReply = fromDoc.getDocumentElement();
 
-        // FIXME: BUG? - we only handle one rpc-error.
+        // FIXME: BUG? - we only handle one rpc-error. For now, shove extra errorMessages
+        // found in multiple rpc-error in the errorInfo Map to at least let them propagate
+        // back to caller.
+        int rpcErrorCount = 0;
 
         NodeList replyChildren = rpcReply.getChildNodes();
         for (int i = 0; i < replyChildren.getLength(); i++) {
             Node replyChild = replyChildren.item(i);
-            if (RPC_ERROR.equals(replyChild.getNodeName())) {
+            if (RPC_ERROR.equals(replyChild.getLocalName())) {
+                rpcErrorCount++;
                 NodeList rpcErrorChildren = replyChild.getChildNodes();
                 for (int j = 0; j < rpcErrorChildren.getLength(); j++) {
                     Node rpcErrorChild = rpcErrorChildren.item(j);
-                    if (ERROR_TYPE.equals(rpcErrorChild.getNodeName())) {
+
+                    // FIXME: use a switch expression here
+                    if (ERROR_TYPE.equals(rpcErrorChild.getLocalName())) {
                         errorType = ErrorType.from(rpcErrorChild.getTextContent());
-                    } else if (ERROR_TAG.equals(rpcErrorChild.getNodeName())) {
+                    } else if (ERROR_TAG.equals(rpcErrorChild.getLocalName())) {
                         errorTag = ErrorTag.from(rpcErrorChild.getTextContent());
-                    } else if (ERROR_SEVERITY.equals(rpcErrorChild.getNodeName())) {
-                        errorSeverity = ErrorSeverity.from(rpcErrorChild.getTextContent());
-                    } else if (ERROR_MESSAGE.equals(rpcErrorChild.getNodeName())) {
+                    } else if (ERROR_SEVERITY.equals(rpcErrorChild.getLocalName())) {
+                        final ErrorSeverity sev = ErrorSeverity.forElementBody(rpcErrorChild.getTextContent());
+                        // FIXME: this should be a hard error
+                        errorSeverity = sev != null ? sev : ErrorSeverity.ERROR;
+                    } else if (ERROR_MESSAGE.equals(rpcErrorChild.getLocalName())) {
                         errorMessage = rpcErrorChild.getTextContent();
-                    } else if (ERROR_INFO.equals(rpcErrorChild.getNodeName())) {
+                        allErrorMessages = allErrorMessages + errorMessage;
+                    } else if (ERROR_INFO.equals(rpcErrorChild.getLocalName())) {
                         errorInfo = parseErrorInfo(rpcErrorChild);
                     }
                 }
+            }
+        }
 
-                break;
+        if (rpcErrorCount > 1) {
+            if (errorInfo == null) {
+                errorInfo = new HashMap<>();
             }
+            errorInfo.put("Multiple Errors Found", allErrorMessages);
         }
 
         return new DocumentedException(errorMessage, errorType, errorTag, errorSeverity, errorInfo);
@@ -283,7 +271,7 @@ public class DocumentedException extends Exception {
 
             rpcError.appendChild(createTextNode(doc, ERROR_TYPE, getErrorType().getTypeValue()));
             rpcError.appendChild(createTextNode(doc, ERROR_TAG, getErrorTag().getTagValue()));
-            rpcError.appendChild(createTextNode(doc, ERROR_SEVERITY, getErrorSeverity().getSeverityValue()));
+            rpcError.appendChild(createTextNode(doc, ERROR_SEVERITY, getErrorSeverity().elementBody()));
             rpcError.appendChild(createTextNode(doc, ERROR_MESSAGE, getLocalizedMessage()));
 
             Map<String, String> errorInfoMap = getErrorInfo();