Migrate restconf to MD-SAL APIs
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / errors / RestconfDocumentedException.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
9 package org.opendaylight.restconf.common.errors;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.Response.Status;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.OperationFailedException;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23
24 /**
25  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
26  * client.
27  *
28  * <p>
29  * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
30  *
31  * @author Devin Avery
32  * @author Thomas Pantelis
33  */
34 public class RestconfDocumentedException extends WebApplicationException {
35
36     private static final long serialVersionUID = 1L;
37
38     private final ImmutableList<RestconfError> errors;
39     private final Status status;
40
41     /**
42      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
43      * to OPERATION_FAILED.
44      *
45      * @param message
46      *            A string which provides a plain text string describing the error.
47      */
48     public RestconfDocumentedException(final String message) {
49         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
50     }
51
52     /**
53      * Constructs an instance with an error message, error type, error tag and exception cause.
54      *
55      * @param message
56      *            A string which provides a plain text string describing the error.
57      * @param errorType
58      *            The enumerated type indicating the layer where the error occurred.
59      * @param errorTag
60      *            The enumerated tag representing a more specific error cause.
61      * @param cause
62      *            The underlying exception cause.
63      */
64     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
65                                        final Throwable cause) {
66         this(cause, new RestconfError(errorType, errorTag, message, null,
67                 cause.getMessage(), null));
68     }
69
70     /**
71      * Constructs an instance with an error message, error type, and error tag.
72      *
73      * @param message
74      *            A string which provides a plain text string describing the error.
75      * @param errorType
76      *            The enumerated type indicating the layer where the error occurred.
77      * @param errorTag
78      *            The enumerated tag representing a more specific error cause.
79      */
80     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag) {
81         this(null, new RestconfError(errorType, errorTag, message));
82     }
83
84     /**
85      * Constructs an instance with an error message, error type, error tag and error path.
86      *
87      * @param message
88      *            A string which provides a plain text string describing the error.
89      * @param errorType
90      *            The enumerated type indicating the layer where the error occurred.
91      * @param errorTag
92      *            The enumerated tag representing a more specific error cause.
93      * @param errorPath
94      *            The instance identifier representing error path
95      */
96     public RestconfDocumentedException(final String message, final ErrorType errorType, final ErrorTag errorTag,
97                                        final YangInstanceIdentifier errorPath) {
98         this(null, new RestconfError(errorType, errorTag, message, errorPath));
99     }
100
101     /**
102      * Constructs an instance with an error message and exception cause.
103      * The underlying exception is included in the error-info.
104      *
105      * @param message
106      *            A string which provides a plain text string describing the error.
107      * @param cause
108      *            The underlying exception cause.
109      */
110     public RestconfDocumentedException(final String message, final Throwable cause) {
111         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
112                 message, null, cause.getMessage(), null));
113     }
114
115     /**
116      * Constructs an instance with the given error.
117      */
118     public RestconfDocumentedException(final RestconfError error) {
119         this(null, error);
120     }
121
122     /**
123      * Constructs an instance with the given errors.
124      */
125     public RestconfDocumentedException(final String message, final Throwable cause, final List<RestconfError> errors) {
126         // FIXME: We override getMessage so supplied message is lost for any public access
127         // this was lost also in original code.
128         super(cause);
129         if (!errors.isEmpty()) {
130             this.errors = ImmutableList.copyOf(errors);
131         } else {
132             this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
133                     RestconfError.ErrorTag.OPERATION_FAILED, message));
134         }
135
136         status = null;
137     }
138
139     /**
140      * Constructs an instance with the given RpcErrors.
141      */
142     public RestconfDocumentedException(final String message, final Throwable cause,
143                                        final Collection<? extends RpcError> rpcErrors) {
144         this(message, cause, convertToRestconfErrors(rpcErrors));
145     }
146
147     /**
148      * Constructs an instance with an HTTP status and no error information.
149      *
150      * @param status
151      *            the HTTP status.
152      */
153     public RestconfDocumentedException(final Status status) {
154         Preconditions.checkNotNull(status, "Status can't be null");
155         errors = ImmutableList.of();
156         this.status = status;
157     }
158
159     public RestconfDocumentedException(final Throwable cause, final RestconfError error) {
160         super(cause);
161         Preconditions.checkNotNull(error, "RestconfError can't be null");
162         errors = ImmutableList.of(error);
163         status = null;
164     }
165
166     public static RestconfDocumentedException decodeAndThrow(final String message,
167             final OperationFailedException cause) {
168         for (final RpcError error : cause.getErrorList()) {
169             if (error.getErrorType() == RpcError.ErrorType.TRANSPORT
170                     && error.getTag().equals(ErrorTag.RESOURCE_DENIED.getTagValue())) {
171                 throw new RestconfDocumentedException(error.getMessage(), ErrorType.TRANSPORT,
172                     ErrorTag.RESOURCE_DENIED_TRANSPORT, cause);
173             }
174         }
175         throw new RestconfDocumentedException(message, cause, cause.getErrorList());
176     }
177
178     private static List<RestconfError> convertToRestconfErrors(final Collection<? extends RpcError> rpcErrors) {
179         final List<RestconfError> errorList = new ArrayList<>();
180         if (rpcErrors != null) {
181             for (RpcError rpcError : rpcErrors) {
182                 errorList.add(new RestconfError(rpcError));
183             }
184         }
185
186         return errorList;
187     }
188
189     public List<RestconfError> getErrors() {
190         return errors;
191     }
192
193     public Status getStatus() {
194         return status;
195     }
196
197     @Override
198     public String getMessage() {
199         return "errors: " + errors + (status != null ? ", status: " + status : "");
200     }
201 }