Merge "Bug 451 - Fix netconf exception handling"
[controller.git] / opendaylight / netconf / netconf-api / src / main / java / org / opendaylight / controller / netconf / api / NetconfDocumentedException.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.api;
10
11 import org.opendaylight.controller.config.api.ConflictingVersionException;
12 import org.opendaylight.controller.config.api.ValidationException;
13
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /**
19  * Checked exception to communicate an error that needs to be sent to the
20  * netconf client.
21  */
22 public class NetconfDocumentedException extends Exception {
23
24     private static final long serialVersionUID = 1L;
25
26
27
28     public enum ErrorType {
29         transport, rpc, protocol, application;
30
31         public String getTagValue() {
32             return name();
33         }
34     }
35
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");
40
41         private final String tagValue;
42
43         ErrorTag(final String tagValue) {
44             this.tagValue = tagValue;
45         }
46
47         public String getTagValue() {
48             return this.tagValue;
49         }
50     }
51
52     public enum ErrorSeverity {
53         error, warning;
54
55         public String getTagValue() {
56             return name();
57         }
58     }
59
60     private final ErrorType errorType;
61     private final ErrorTag errorTag;
62     private final ErrorSeverity errorSeverity;
63     private final Map<String, String> errorInfo;
64
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());
68     }
69
70     public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
71             final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
72         super(message);
73         this.errorType = errorType;
74         this.errorTag = errorTag;
75         this.errorSeverity = errorSeverity;
76         this.errorInfo = errorInfo;
77     }
78
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());
82     }
83
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;
91     }
92
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);
98     }
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);
104     }
105
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);
111     }
112
113     public ErrorType getErrorType() {
114         return this.errorType;
115     }
116
117     public ErrorTag getErrorTag() {
118         return this.errorTag;
119     }
120
121     public ErrorSeverity getErrorSeverity() {
122         return this.errorSeverity;
123     }
124
125     public Map<String, String> getErrorInfo() {
126         return this.errorInfo;
127     }
128
129     @Override
130     public String toString() {
131         return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
132                 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
133                 + this.errorInfo + '}';
134     }
135 }