a729d3e19cfeff008e7300742719cae2c8200e0c
[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.data.api.YangInstanceIdentifier;
17
18 /**
19  * Encapsulates a single {@code error} within the
20  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.9">"errors" YANG Data Template</a>.
21  *
22  * @author Devin Avery
23  */
24 @Deprecated
25 public class RestconfError implements Serializable {
26     @java.io.Serial
27     private static final long serialVersionUID = 1L;
28
29     private final @NonNull ErrorType errorType;
30     private final @NonNull ErrorTag errorTag;
31     // FIXME: 'errorInfo' is defined as a formatted XML string. We need a better representation to enable reasonable
32     //        interop with JSON (and others)
33     private final String errorInfo;
34     private final String errorAppTag;
35     private final String errorMessage;
36     private final YangInstanceIdentifier errorPath;
37
38     /**
39      * Constructs a RestconfError.
40      *
41      * @param errorType The enumerated type indicating the layer where the error occurred.
42      * @param errorTag The enumerated tag representing a more specific error cause.
43      * @param errorMessage A string which provides a plain text string describing the error.
44      */
45     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
46         this(errorType, errorTag, errorMessage, null, null, null);
47     }
48
49     /**
50      * Constructs a RestconfError object.
51      *
52      * @param errorType The enumerated type indicating the layer where the error occurred.
53      * @param errorTag The enumerated tag representing a more specific error cause.
54      * @param errorMessage A string which provides a plain text string describing the error.
55      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
56      *                    cause.
57      */
58     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
59             final String errorAppTag) {
60         this(errorType, errorTag, errorMessage, errorAppTag, null, null);
61     }
62
63     /**
64      * Constructs a RestconfError object.
65      *
66      * @param errorType The enumerated type indicating the layer where the error occurred.
67      * @param errorTag The enumerated tag representing a more specific error cause.
68      * @param errorMessage A string which provides a plain text string describing the error.
69      * @param errorPath An instance identifier which contains error path
70      */
71     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
72             final YangInstanceIdentifier errorPath) {
73         this(errorType, errorTag, errorMessage, null, null, errorPath);
74     }
75
76     /**
77      * Constructs a RestconfError object.
78      *
79      * @param errorType The enumerated type indicating the layer where the error occurred.
80      * @param errorTag The enumerated tag representing a more specific error cause.
81      * @param errorMessage A string which provides a plain text string describing the error.
82      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
83      *                    cause.
84      * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
85      */
86     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
87             final String errorAppTag, final String errorInfo) {
88         this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
89     }
90
91     /**
92      * Constructs a RestConfError object.
93      *
94      * @param errorType The enumerated type indicating the layer where the error occurred.
95      * @param errorTag The enumerated tag representing a more specific error cause.
96      * @param errorMessage A string which provides a plain text string describing the error.
97      * @param errorAppTag A string which represents an application-specific error tag that further specifies the error
98      *                    cause.
99      * @param errorInfo A string, <b>formatted as XML</b>, which contains additional error information.
100      * @param errorPath An instance identifier which contains error path
101      */
102     public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
103             final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
104         this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
105         this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
106         this.errorMessage = errorMessage;
107         this.errorAppTag = errorAppTag;
108         this.errorInfo = errorInfo;
109         this.errorPath = errorPath;
110     }
111
112     public @NonNull ErrorType getErrorType() {
113         return errorType;
114     }
115
116     public @NonNull ErrorTag getErrorTag() {
117         return errorTag;
118     }
119
120     public String getErrorInfo() {
121         return errorInfo;
122     }
123
124     public String getErrorAppTag() {
125         return errorAppTag;
126     }
127
128     public String getErrorMessage() {
129         return errorMessage;
130     }
131
132     public YangInstanceIdentifier getErrorPath() {
133         return errorPath;
134     }
135
136     @Override
137     public String toString() {
138         return "RestconfError ["
139                 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
140                 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
141                 + (errorMessage != null ? ", error-message: " + errorMessage : "")
142                 + (errorInfo != null ? ", error-info: " + errorInfo : "")
143                 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")
144                 + "]";
145     }
146 }