Remove the option to deliver streams over WebSockets
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / RestconfError.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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 package org.opendaylight.restconf.common.errors;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.Serializable;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.ErrorTag;
15 import org.opendaylight.yangtools.yang.common.ErrorType;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18
19 /**
20  * Encapsulates a single {@code error} within the
21  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.9">"errors" YANG Data Template</a>.
22  *
23  * @author Devin Avery
24  */
25 @Deprecated
26 public class RestconfError implements Serializable {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     private final @NonNull ErrorType errorType;
31     private final @NonNull ErrorTag errorTag;
32     // FIXME: 'errorInfo' is defined as a formatted XML string. We need a better representation to enable reasonable
33     //        interop with JSON (and others)
34     private final String errorInfo;
35     private final String errorAppTag;
36     private final String errorMessage;
37     private final YangInstanceIdentifier errorPath;
38
39     /**
40      * Constructs a RestconfError.
41      *
42      * @param errorType The enumerated type indicating the layer where the error occurred.
43      * @param errorTag The enumerated tag representing a more specific error cause.
44      * @param errorMessage A string which provides a plain text string describing the error.
45      */
46     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
47         this(errorType, errorTag, errorMessage, null, null, null);
48     }
49
50     /**
51      * Constructs a RestconfError object.
52      *
53      * @param errorType The enumerated type indicating the layer where the error occurred.
54      * @param errorTag The enumerated tag representing a more specific error cause.
55      * @param errorMessage A string which provides a plain text string describing the error.
56      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
57      *                    cause.
58      */
59     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
60             final String errorAppTag) {
61         this(errorType, errorTag, errorMessage, errorAppTag, null, null);
62     }
63
64     /**
65      * Constructs a RestconfError object.
66      *
67      * @param errorType The enumerated type indicating the layer where the error occurred.
68      * @param errorTag The enumerated tag representing a more specific error cause.
69      * @param errorMessage A string which provides a plain text string describing the error.
70      * @param errorPath An instance identifier which contains error path
71      */
72     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
73             final YangInstanceIdentifier errorPath) {
74         this(errorType, errorTag, errorMessage, null, null, errorPath);
75     }
76
77     /**
78      * Constructs a RestconfError object.
79      *
80      * @param errorType The enumerated type indicating the layer where the error occurred.
81      * @param errorTag The enumerated tag representing a more specific error cause.
82      * @param errorMessage A string which provides a plain text string describing the error.
83      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
84      *                    cause.
85      * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
86      */
87     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
88             final String errorAppTag, final String errorInfo) {
89         this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
90     }
91
92     /**
93      * Constructs a RestConfError object.
94      *
95      * @param errorType The enumerated type indicating the layer where the error occurred.
96      * @param errorTag The enumerated tag representing a more specific error cause.
97      * @param errorMessage A string which provides a plain text string describing the error.
98      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
99      *                    cause.
100      * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
101      * @param errorPath An instance identifier which contains error path
102      */
103     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
104             final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
105         this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
106         this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
107         this.errorMessage = errorMessage;
108         this.errorAppTag = errorAppTag;
109         this.errorInfo = errorInfo;
110         this.errorPath = errorPath;
111     }
112
113     /**
114      * Constructs a RestConfError object from an RpcError.
115      */
116     public RestconfError(final RpcError rpcError) {
117         errorType = rpcError.getErrorType();
118
119         final var tag = rpcError.getTag();
120         errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
121
122         errorMessage = rpcError.getMessage();
123         errorAppTag = rpcError.getApplicationTag();
124         errorInfo = rpcErrorInfo(rpcError);
125         errorPath = null;
126     }
127
128     private static String rpcErrorInfo(final RpcError rpcError) {
129         final var info = rpcError.getInfo();
130         if (info != null) {
131             return info;
132         }
133         final var cause = rpcError.getCause();
134         if (cause != null) {
135             return cause.getMessage();
136         }
137         final var severity = rpcError.getSeverity();
138         if (severity != null) {
139             return "<severity>" + severity.elementBody() + "</severity>";
140         }
141         return null;
142     }
143
144     public @NonNull ErrorType getErrorType() {
145         return errorType;
146     }
147
148     public @NonNull ErrorTag getErrorTag() {
149         return errorTag;
150     }
151
152     public String getErrorInfo() {
153         return errorInfo;
154     }
155
156     public String getErrorAppTag() {
157         return errorAppTag;
158     }
159
160     public String getErrorMessage() {
161         return errorMessage;
162     }
163
164     public YangInstanceIdentifier getErrorPath() {
165         return errorPath;
166     }
167
168     @Override
169     public String toString() {
170         return "RestconfError ["
171                 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
172                 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
173                 + (errorMessage != null ? ", error-message: " + errorMessage : "")
174                 + (errorInfo != null ? ", error-info: " + errorInfo : "")
175                 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")
176                 + "]";
177     }
178 }