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;
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);
}
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;
}
}
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 ) ) {
}
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;
}
}
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,
);
}
+ 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());
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;
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++ ) {
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();
}
}
}
- catch( ParserConfigurationException e ) {
+ catch( final ParserConfigurationException e ) {
// this shouldn't happen
LOG.error("Error outputting to XML document", e);
}
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;