Delete netconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / 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.controller.sal.restconf.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Lists;
14
15 import java.util.Collection;
16 import java.util.List;
17
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.Response.Status;
20
21 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
22 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24
25 /**
26  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
27  * client.
28  *
29  * @author Devin Avery
30  * @author Thomas Pantelis
31  * @see {@link https://tools.ietf.org/html/draft-bierman-netconf-restconf-02}
32  */
33 public class RestconfDocumentedException extends WebApplicationException {
34
35     private static final long serialVersionUID = 1L;
36
37     private final List<RestconfError> errors;
38     private final Status status;
39
40     /**
41      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
42      * to OPERATION_FAILED.
43      *
44      * @param message
45      *            A string which provides a plain text string describing the error.
46      */
47     public RestconfDocumentedException(String message) {
48         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
49     }
50
51     /**
52      * Constructs an instance with an error message, error type, and error tag.
53      *
54      * @param message
55      *            A string which provides a plain text string describing the error.
56      * @param errorType
57      *            The enumerated type indicating the layer where the error occurred.
58      * @param errorTag
59      *            The enumerated tag representing a more specific error cause.
60      */
61     public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
62         this(null, new RestconfError(errorType, errorTag, message));
63     }
64
65     /**
66      * Constructs an instance with an error message and exception cause.
67      * The stack trace of the exception is included in the error info.
68      *
69      * @param message
70      *            A string which provides a plain text string describing the error.
71      * @param cause
72      *            The underlying exception cause.
73      */
74     public RestconfDocumentedException(String message, Throwable cause) {
75         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
76                 message, null, RestconfError.toErrorInfo(cause)));
77     }
78
79     /**
80      * Constructs an instance with the given error.
81      */
82     public RestconfDocumentedException(RestconfError error) {
83         this(null, error);
84     }
85
86     /**
87      * Constructs an instance with the given errors.
88      */
89     public RestconfDocumentedException(String message, Throwable cause, List<RestconfError> errors) {
90         // FIXME: We override getMessage so supplied message is lost for any public access
91         // this was lost also in original code.
92         super(cause);
93         if(!errors.isEmpty()) {
94             this.errors = ImmutableList.copyOf(errors);
95         } else {
96             this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
97                     RestconfError.ErrorTag.OPERATION_FAILED, message));
98         }
99
100         status = null;
101     }
102
103     /**
104      * Constructs an instance with the given RpcErrors.
105      */
106     public RestconfDocumentedException(String message, Throwable cause, Collection<RpcError> rpcErrors) {
107         this(message, cause, convertToRestconfErrors(rpcErrors));
108     }
109
110     /**
111      * Constructs an instance with an HTTP status and no error information.
112      *
113      * @param status
114      *            the HTTP status.
115      */
116     public RestconfDocumentedException(Status status) {
117         Preconditions.checkNotNull(status, "Status can't be null");
118         errors = ImmutableList.of();
119         this.status = status;
120     }
121
122     private RestconfDocumentedException(Throwable cause, RestconfError error) {
123         super(cause);
124         Preconditions.checkNotNull(error, "RestconfError can't be null");
125         errors = ImmutableList.of(error);
126         status = null;
127     }
128
129     private static List<RestconfError> convertToRestconfErrors(Collection<RpcError> rpcErrors) {
130         List<RestconfError> errorList = Lists.newArrayList();
131         if(rpcErrors != null) {
132             for (RpcError rpcError : rpcErrors) {
133                 errorList.add(new RestconfError(rpcError));
134             }
135         }
136
137         return errorList;
138     }
139
140
141     public List<RestconfError> getErrors() {
142         return errors;
143     }
144
145     public Status getStatus() {
146         return status;
147     }
148
149     @Override
150     public String getMessage() {
151         return "errors: " + errors + (status != null ? ", status: " + status : "");
152     }
153 }