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(String message) {
67 NetconfDocumentedException.ErrorType.application,
68 NetconfDocumentedException.ErrorTag.invalid_value,
69 NetconfDocumentedException.ErrorSeverity.error
73 public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
74 final ErrorSeverity errorSeverity) {
75 this(message, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
78 public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
79 final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
81 this.errorType = errorType;
82 this.errorTag = errorTag;
83 this.errorSeverity = errorSeverity;
84 this.errorInfo = errorInfo;
87 public NetconfDocumentedException(final String message, final Exception cause, final ErrorType errorType,
88 final ErrorTag errorTag, final ErrorSeverity errorSeverity) {
89 this(message, cause, errorType, errorTag, errorSeverity, Collections.<String, String> emptyMap());
92 public NetconfDocumentedException(final String message, final Exception cause, final ErrorType errorType,
93 final ErrorTag errorTag, final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
94 super(message, cause);
95 this.errorType = errorType;
96 this.errorTag = errorTag;
97 this.errorSeverity = errorSeverity;
98 this.errorInfo = errorInfo;
101 public static <E extends Exception> NetconfDocumentedException wrap(E exception) throws NetconfDocumentedException {
102 final Map<String, String> errorInfo = new HashMap<>();
103 errorInfo.put(ErrorTag.operation_failed.name(), "Exception thrown");
104 throw new NetconfDocumentedException(exception.getMessage(), exception, ErrorType.application, ErrorTag.operation_failed,
105 ErrorSeverity.error, errorInfo);
107 public static NetconfDocumentedException wrap(ValidationException e) throws NetconfDocumentedException {
108 final Map<String, String> errorInfo = new HashMap<>();
109 errorInfo.put(ErrorTag.operation_failed.name(), "Validation failed");
110 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
111 ErrorSeverity.error, errorInfo);
114 public static NetconfDocumentedException wrap(ConflictingVersionException e) throws NetconfDocumentedException {
115 final Map<String, String> errorInfo = new HashMap<>();
116 errorInfo.put(ErrorTag.operation_failed.name(), "Optimistic lock failed");
117 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
118 ErrorSeverity.error, errorInfo);
121 public ErrorType getErrorType() {
122 return this.errorType;
125 public ErrorTag getErrorTag() {
126 return this.errorTag;
129 public ErrorSeverity getErrorSeverity() {
130 return this.errorSeverity;
133 public Map<String, String> getErrorInfo() {
134 return this.errorInfo;
138 public String toString() {
139 return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
140 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
141 + this.errorInfo + '}';