2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.common.errors;
10 import static java.util.Objects.requireNonNull;
12 import java.io.Serializable;
13 import java.util.Locale;
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;
20 * Encapsulates a restconf error as defined in the ietf restconf draft.
24 * <b>Note:</b> Enumerations defined within are provided by the ietf restconf draft.
27 * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
29 public class RestconfError implements Serializable {
30 private static final long serialVersionUID = 1L;
32 private final ErrorType errorType;
33 private final ErrorTag errorTag;
34 private final String errorInfo;
35 private final String errorAppTag;
36 private final String errorMessage;
37 private final YangInstanceIdentifier errorPath;
40 * Constructs a RestConfError.
43 * The enumerated type indicating the layer where the error occurred.
45 * The enumerated tag representing a more specific error cause.
47 * A string which provides a plain text string describing the error.
49 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage) {
50 this(errorType, errorTag, errorMessage, null, null, null);
54 * Constructs a RestConfError object.
57 * The enumerated type indicating the layer where the error occurred.
59 * The enumerated tag representing a more specific error cause.
61 * A string which provides a plain text string describing the error.
63 * A string which represents an application-specific error tag that further specifies the error cause.
65 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
66 final String errorAppTag) {
67 this(errorType, errorTag, errorMessage, errorAppTag, null, null);
71 * Constructs a RestConfError object.
74 * The enumerated type indicating the layer where the error occurred.
76 * The enumerated tag representing a more specific error cause.
78 * A string which provides a plain text string describing the error.
80 * An instance identifier which contains error path
82 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
83 final YangInstanceIdentifier errorPath) {
84 this(errorType, errorTag, errorMessage, null, null, errorPath);
88 * Constructs a RestConfError object.
91 * The enumerated type indicating the layer where the error occurred.
93 * The enumerated tag representing a more specific error cause.
95 * A string which provides a plain text string describing the error.
97 * A string which represents an application-specific error tag that further specifies the error cause.
99 * A string, <b>formatted as XML</b>, which contains additional error information.
101 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
102 final String errorAppTag, final String errorInfo) {
103 this(errorType, errorTag, errorMessage, errorAppTag, errorInfo, null);
107 * Constructs a RestConfError object.
110 * The enumerated type indicating the layer where the error occurred.
112 * The enumerated tag representing a more specific error cause.
113 * @param errorMessage
114 * A string which provides a plain text string describing the error.
116 * A string which represents an application-specific error tag that further specifies the error cause.
118 * A string, <b>formatted as XML</b>, which contains additional error information.
120 * An instance identifier which contains error path
122 public RestconfError(final ErrorType errorType, final ErrorTag errorTag, final String errorMessage,
123 final String errorAppTag, final String errorInfo, final YangInstanceIdentifier errorPath) {
124 this.errorType = requireNonNull(errorType, "Error type is required for RestConfError");
125 this.errorTag = requireNonNull(errorTag, "Error tag is required for RestConfError");
126 this.errorMessage = errorMessage;
127 this.errorAppTag = errorAppTag;
128 this.errorInfo = errorInfo;
129 this.errorPath = errorPath;
133 * Constructs a RestConfError object from an RpcError.
135 public RestconfError(final RpcError rpcError) {
137 errorType = rpcError.getErrorType();
139 final ErrorTag tag = rpcError.getTag();
140 errorTag = tag != null ? tag : ErrorTag.OPERATION_FAILED;
142 errorMessage = rpcError.getMessage();
143 errorAppTag = rpcError.getApplicationTag();
145 String localErrorInfo = null;
146 if (rpcError.getInfo() == null) {
147 if (rpcError.getCause() != null) {
148 localErrorInfo = rpcError.getCause().getMessage();
149 } else if (rpcError.getSeverity() != null) {
150 localErrorInfo = "<severity>" + rpcError.getSeverity().toString().toLowerCase(Locale.ROOT)
154 localErrorInfo = rpcError.getInfo();
157 errorInfo = localErrorInfo;
161 public ErrorType getErrorType() {
165 public ErrorTag getErrorTag() {
169 public String getErrorInfo() {
173 public String getErrorAppTag() {
177 public String getErrorMessage() {
181 public YangInstanceIdentifier getErrorPath() {
186 public String toString() {
187 return "RestconfError ["
188 + "error-type: " + errorType.elementBody() + ", error-tag: " + errorTag.elementBody()
189 + (errorAppTag != null ? ", error-app-tag: " + errorAppTag : "")
190 + (errorMessage != null ? ", error-message: " + errorMessage : "")
191 + (errorInfo != null ? ", error-info: " + errorInfo : "")
192 + (errorPath != null ? ", error-path: " + errorPath.toString() : "")