2 * Copyright (c) 2013 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.netconf.api;
11 import org.opendaylight.controller.config.api.ConflictingVersionException;
12 import org.opendaylight.controller.config.api.ValidationException;
14 import java.util.Collections;
15 import java.util.HashMap;
19 * Checked exception to communicate an error that needs to be sent to the
22 public class NetconfDocumentedException extends Exception {
24 private static final long serialVersionUID = 1L;
28 public enum ErrorType {
29 transport, rpc, protocol, application;
31 public String getTagValue() {
36 public enum ErrorTag {
37 missing_attribute("missing-attribute"), unknown_element("unknown-element"), operation_not_supported(
38 "operation-not-supported"), bad_attribute("bad-attribute"), data_missing("data-missing"), operation_failed(
39 "operation-failed"), invalid_value("invalid-value"), malformed_message("malformed-message");
41 private final String tagValue;
43 ErrorTag(final String tagValue) {
44 this.tagValue = tagValue;
47 public String getTagValue() {
52 public enum ErrorSeverity {
55 public String getTagValue() {
60 private final ErrorType errorType;
61 private final ErrorTag errorTag;
62 private final ErrorSeverity errorSeverity;
63 private final Map<String, String> errorInfo;
65 public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
66 final ErrorSeverity errorSeverity) {
67 this(message, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
70 public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
71 final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
73 this.errorType = errorType;
74 this.errorTag = errorTag;
75 this.errorSeverity = errorSeverity;
76 this.errorInfo = errorInfo;
79 public NetconfDocumentedException(final String message, final Exception cause, final ErrorType errorType,
80 final ErrorTag errorTag, final ErrorSeverity errorSeverity) {
81 this(message, cause, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
84 public NetconfDocumentedException(final String message, final Exception cause, final ErrorType errorType,
85 final ErrorTag errorTag, final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
86 super(message, cause);
87 this.errorType = errorType;
88 this.errorTag = errorTag;
89 this.errorSeverity = errorSeverity;
90 this.errorInfo = errorInfo;
93 public static <E extends Exception> NetconfDocumentedException wrap(E exception) throws NetconfDocumentedException {
94 final Map<String, String> errorInfo = new HashMap<>();
95 errorInfo.put(ErrorTag.operation_failed.name(), "Exception thrown");
96 throw new NetconfDocumentedException(exception.getMessage(), exception, ErrorType.application, ErrorTag.operation_failed,
97 ErrorSeverity.error, errorInfo);
99 public static NetconfDocumentedException wrap(ValidationException e) throws NetconfDocumentedException {
100 final Map<String, String> errorInfo = new HashMap<>();
101 errorInfo.put(ErrorTag.operation_failed.name(), "Validation failed");
102 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
103 ErrorSeverity.error, errorInfo);
106 public static NetconfDocumentedException wrap(ConflictingVersionException e) throws NetconfDocumentedException {
107 final Map<String, String> errorInfo = new HashMap<>();
108 errorInfo.put(ErrorTag.operation_failed.name(), "Optimistic lock failed");
109 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
110 ErrorSeverity.error, errorInfo);
113 public ErrorType getErrorType() {
114 return this.errorType;
117 public ErrorTag getErrorTag() {
118 return this.errorTag;
121 public ErrorSeverity getErrorSeverity() {
122 return this.errorSeverity;
125 public Map<String, String> getErrorInfo() {
126 return this.errorInfo;
130 public String toString() {
131 return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
132 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
133 + this.errorInfo + '}';