Remove yang-test
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / xml / DocumentedException.java
index 736a4fc035eff377863c2d60e7683194f2477ff8..6554c6fed513b6b921c69358506f565668e8096e 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.config.util.xml;
 
 import static org.opendaylight.controller.config.util.xml.XmlMappingConstants.RPC_REPLY_KEY;
 import static org.opendaylight.controller.config.util.xml.XmlMappingConstants.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;
@@ -53,7 +55,7 @@ public class DocumentedException extends Exception {
             BUILDER_FACTORY.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
             BUILDER_FACTORY.setXIncludeAware(false);
             BUILDER_FACTORY.setExpandEntityReferences(false);
-        } catch (ParserConfigurationException e) {
+        } catch (final ParserConfigurationException e) {
             throw new ExceptionInInitializerError(e);
         }
         BUILDER_FACTORY.setNamespaceAware(true);
@@ -63,19 +65,37 @@ public class DocumentedException extends Exception {
     }
 
     public enum ErrorType {
-        TRANSPORT, RPC, PROTOCOL, APPLICATION;
+        TRANSPORT("transport"),
+        RPC("rpc"),
+        PROTOCOL("protocol"),
+        APPLICATION("application");
+
+        private final String typeValue;
+
+        ErrorType(final String typeValue) {
+            this.typeValue = Preconditions.checkNotNull(typeValue);
+        }
+
+        public String getTypeValue() {
+            return this.typeValue;
+        }
 
+        /**
+         * @deprecated Use {@link #getTypeValue()} instead.
+         */
+        @Deprecated
         public String getTagValue() {
-            return name();
+            return this.typeValue;
         }
 
-        public static ErrorType from( String text ) {
-            try {
-                return valueOf( text.toUpperCase() );
-            }
-            catch( Exception e ) {
-                return APPLICATION;
+        public static ErrorType from(final String text) {
+            for (ErrorType e : values()) {
+               if (e.getTypeValue().equalsIgnoreCase(text)) {
+                   return e;
+               }
             }
+
+            return APPLICATION;
         }
     }
 
@@ -110,7 +130,7 @@ public class DocumentedException extends Exception {
             return this.tagValue;
         }
 
-        public static ErrorTag from( String text ) {
+        public static ErrorTag from( final String text ) {
             for( ErrorTag e: values() )
             {
                 if( e.getTagValue().equals( text ) ) {
@@ -123,19 +143,35 @@ public class DocumentedException extends Exception {
     }
 
     public enum ErrorSeverity {
-        ERROR, WARNING;
+        ERROR("error"),
+        WARNING("warning");
+
+        private final String severityValue;
 
+        ErrorSeverity(final String severityValue) {
+            this.severityValue = Preconditions.checkNotNull(severityValue);
+        }
+
+        public String getSeverityValue() {
+            return this.severityValue;
+        }
+
+        /**
+         * @deprecated Use {@link #getSeverityValue()} instead.
+         */
+        @Deprecated
         public String getTagValue() {
-            return name();
+            return this.severityValue;
         }
 
-        public static ErrorSeverity from( String text ) {
-            try {
-                return valueOf( text.toUpperCase() );
-            }
-            catch( Exception e ) {
-                return ERROR;
+        public static ErrorSeverity from(final String text) {
+            for (ErrorSeverity e : values()) {
+                if (e.getSeverityValue().equalsIgnoreCase(text)) {
+                    return e;
+                }
             }
+
+            return ERROR;
         }
     }
 
@@ -144,7 +180,7 @@ public class DocumentedException extends Exception {
     private final ErrorSeverity errorSeverity;
     private final Map<String, String> errorInfo;
 
-    public DocumentedException(String message) {
+    public DocumentedException(final String message) {
         this(message,
                 DocumentedException.ErrorType.APPLICATION,
                 DocumentedException.ErrorTag.INVALID_VALUE,
@@ -152,6 +188,14 @@ public class DocumentedException extends Exception {
         );
     }
 
+    public DocumentedException(final String message, final Exception cause) {
+        this(message, cause,
+                DocumentedException.ErrorType.APPLICATION,
+                DocumentedException.ErrorTag.INVALID_VALUE,
+                DocumentedException.ErrorSeverity.ERROR
+        );
+    }
+
     public DocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
                                final ErrorSeverity errorSeverity) {
         this(message, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
@@ -180,27 +224,27 @@ public class DocumentedException extends Exception {
         this.errorInfo = errorInfo;
     }
 
-    public static <E extends Exception> DocumentedException wrap(E exception) throws DocumentedException {
+    public static <E extends Exception> DocumentedException wrap(final E exception) throws DocumentedException {
         final Map<String, String> errorInfo = new HashMap<>();
         errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Exception thrown");
         throw new DocumentedException(exception.getMessage(), exception, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
                 ErrorSeverity.ERROR, errorInfo);
     }
-    public static DocumentedException wrap(ValidationException e) throws DocumentedException {
+    public static DocumentedException wrap(final ValidationException e) throws DocumentedException {
         final Map<String, String> errorInfo = new HashMap<>();
         errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Validation failed");
         throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
                 ErrorSeverity.ERROR, errorInfo);
     }
 
-    public static DocumentedException wrap(ConflictingVersionException e) throws DocumentedException {
+    public static DocumentedException wrap(final ConflictingVersionException e) throws DocumentedException {
         final Map<String, String> errorInfo = new HashMap<>();
         errorInfo.put(ErrorTag.OPERATION_FAILED.name(), "Optimistic lock failed");
         throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
                 ErrorSeverity.ERROR, errorInfo);
     }
 
-    public static DocumentedException fromXMLDocument( Document fromDoc ) {
+    public static DocumentedException fromXMLDocument( final Document fromDoc ) {
 
         ErrorType errorType = ErrorType.APPLICATION;
         ErrorTag errorTag = ErrorTag.OPERATION_FAILED;
@@ -245,7 +289,7 @@ public class DocumentedException extends Exception {
         return new DocumentedException( errorMessage, errorType, errorTag, errorSeverity, errorInfo );
     }
 
-    private static Map<String, String> parseErrorInfo( Node node ) {
+    private static Map<String, String> parseErrorInfo( final Node node ) {
         Map<String, String> infoMap = new HashMap<>();
         NodeList children = node.getChildNodes();
         for( int i = 0; i < children.getLength(); i++ ) {
@@ -285,9 +329,9 @@ public class DocumentedException extends Exception {
             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_TYPE, getErrorType().getTypeValue() ) );
             rpcError.appendChild( createTextNode( doc, ERROR_TAG, getErrorTag().getTagValue() ) );
-            rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getTagValue() ) );
+            rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getSeverityValue() ) );
             rpcError.appendChild( createTextNode( doc, ERROR_MESSAGE, getLocalizedMessage() ) );
 
             Map<String, String> errorInfoMap = getErrorInfo();
@@ -308,7 +352,7 @@ public class DocumentedException extends Exception {
                 }
             }
         }
-        catch( ParserConfigurationException e ) {
+        catch( final ParserConfigurationException e ) {
             // this shouldn't happen
             LOG.error("Error outputting to XML document", e);
         }
@@ -316,7 +360,7 @@ public class DocumentedException extends Exception {
         return doc;
     }
 
-    private Node createTextNode( Document doc, String tag, String textContent ) {
+    private Node createTextNode( final Document doc, final String tag, final String textContent ) {
         Node node = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, tag );
         node.setTextContent( textContent );
         return node;