Merge "Bug 1029: Remove dead code: sal-schema-repository-api"
[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(String message) {
66         this(message,
67                 NetconfDocumentedException.ErrorType.application,
68                 NetconfDocumentedException.ErrorTag.invalid_value,
69                 NetconfDocumentedException.ErrorSeverity.error
70         );
71     }
72
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());
76     }
77
78     public NetconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
79             final ErrorSeverity errorSeverity, final Map<String, String> errorInfo) {
80         super(message);
81         this.errorType = errorType;
82         this.errorTag = errorTag;
83         this.errorSeverity = errorSeverity;
84         this.errorInfo = errorInfo;
85     }
86
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());
90     }
91
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;
99     }
100
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);
106     }
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);
112     }
113
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);
119     }
120
121     public ErrorType getErrorType() {
122         return this.errorType;
123     }
124
125     public ErrorTag getErrorTag() {
126         return this.errorTag;
127     }
128
129     public ErrorSeverity getErrorSeverity() {
130         return this.errorSeverity;
131     }
132
133     public Map<String, String> getErrorInfo() {
134         return this.errorInfo;
135     }
136
137     @Override
138     public String toString() {
139         return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
140                 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
141                 + this.errorInfo + '}';
142     }
143 }