c3ee195281830edd45be2d96e88abb14d7e48a89
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
26
27 import io.swagger.v3.oas.annotations.media.Schema;
28
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.ResponseEntity;
33 import reactor.core.publisher.Mono;
34
35 public class ErrorResponse {
36     private static Gson gson = new GsonBuilder() //
37             .create(); //
38
39     // Returned as body for all failed REST calls
40     @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
41     public static class ErrorInfo {
42         @SerializedName("type")
43         private String type = "about:blank";
44
45         @SerializedName("title")
46         private String title = null;
47
48         @SerializedName("status")
49         private final Integer status;
50
51         @SerializedName("detail")
52         private String detail = null;
53
54         @SerializedName("instance")
55         private String instance = null;
56
57         public ErrorInfo(String detail, Integer status) {
58             this.detail = detail;
59             this.status = status;
60         }
61
62         @Schema(example = "503",
63                 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
64         public Integer getStatus() {
65             return status;
66         }
67
68         @Schema(example = "Policy type not found",
69                 description = " A human-readable explanation specific to this occurrence of the problem.")
70         public String getDetail() {
71             return this.detail;
72         }
73
74     }
75
76     @Schema(name = "message", description = "message")
77     public final String message;
78
79     ErrorResponse(String message) {
80         this.message = message;
81     }
82
83     static Mono<ResponseEntity<Object>> createMono(String text, HttpStatus code) {
84         return Mono.just(create(text, code));
85     }
86
87     static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatus code) {
88         return createMono(e.toString(), code);
89     }
90
91     static ResponseEntity<Object> create(String text, HttpStatus code) {
92         ErrorInfo p = new ErrorInfo(text, code.value());
93         String json = gson.toJson(p);
94         HttpHeaders headers = new HttpHeaders();
95         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
96         return new ResponseEntity<>(json, headers, code);
97     }
98
99     public static ResponseEntity<Object> create(Exception e, HttpStatus code) {
100         return create(e.toString(), code);
101     }
102
103 }