Merge "BUG-832 Add initial configuration for controller self mount"
[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 NetconfDocumentedException wrap(ValidationException e) throws NetconfDocumentedException {
94         final Map<String, String> errorInfo = new HashMap<>();
95         errorInfo.put(ErrorTag.operation_failed.name(), "Validation failed");
96         throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
97                 ErrorSeverity.error, errorInfo);
98     }
99
100     public static NetconfDocumentedException wrap(ConflictingVersionException e) throws NetconfDocumentedException {
101         final Map<String, String> errorInfo = new HashMap<>();
102         errorInfo.put(ErrorTag.operation_failed.name(), "Optimistic lock failed");
103         throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
104                 ErrorSeverity.error, errorInfo);
105     }
106
107     public ErrorType getErrorType() {
108         return this.errorType;
109     }
110
111     public ErrorTag getErrorTag() {
112         return this.errorTag;
113     }
114
115     public ErrorSeverity getErrorSeverity() {
116         return this.errorSeverity;
117     }
118
119     public Map<String, String> getErrorInfo() {
120         return this.errorInfo;
121     }
122
123     @Override
124     public String toString() {
125         return "NetconfDocumentedException{" + "message=" + getMessage() + ", errorType=" + this.errorType
126                 + ", errorTag=" + this.errorTag + ", errorSeverity=" + this.errorSeverity + ", errorInfo="
127                 + this.errorInfo + '}';
128     }
129 }