f3f57067dab170680e8d76c547e03317835e91bf
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 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 java.lang.invoke.MethodHandles;
30
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.HttpStatusCode;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
39 import reactor.core.publisher.Mono;
40
41 public class ErrorResponse {
42     private static Gson gson = new GsonBuilder() //
43             .create(); //
44     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
45
46     // Returned as body for all failed REST calls
47     @Schema(name = "error_information", description = "Problem as defined in https://tools.ietf.org/html/rfc7807")
48     public static class ErrorInfo {
49         @SerializedName("type")
50         private String type = "about:blank";
51
52         @SerializedName("title")
53         private String title = null;
54
55         @SerializedName("status")
56         private final Integer status;
57
58         @SerializedName("detail")
59         private String detail = null;
60
61         @SerializedName("instance")
62         private String instance = null;
63
64         public ErrorInfo(String detail, Integer status) {
65             this.detail = detail;
66             this.status = status;
67         }
68
69         @Schema(example = "404",
70                 description = "The HTTP status code generated by the origin server for this occurrence of the problem. ")
71         public Integer getStatus() {
72             return status;
73         }
74
75         @Schema(example = "Policy type not found",
76                 description = " A human-readable explanation specific to this occurrence of the problem.")
77         public String getDetail() {
78             return this.detail;
79         }
80
81     }
82
83     @Schema(name = "message", description = "message")
84     public final String message;
85
86     ErrorResponse(String message) {
87         this.message = message;
88     }
89
90     static Mono<ResponseEntity<Object>> createMono(String text, HttpStatusCode code) {
91         return Mono.just(create(text, code));
92     }
93
94     static Mono<ResponseEntity<Object>> createMono(Exception e, HttpStatusCode code) {
95         return createMono(e.toString(), code);
96     }
97
98     static ResponseEntity<Object> create(String text, HttpStatusCode code) {
99         logger.debug("Error response: {}, {}", code, text);
100         ErrorInfo p = new ErrorInfo(text, code.value());
101         String json = gson.toJson(p);
102         HttpHeaders headers = new HttpHeaders();
103         headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
104         return new ResponseEntity<>(json, headers, code);
105     }
106
107     public static ResponseEntity<Object> create(Throwable e, HttpStatusCode code) {
108         if (e instanceof RuntimeException) {
109             code = HttpStatus.INTERNAL_SERVER_ERROR;
110         } else if (e instanceof ServiceException) {
111             ServiceException se = (ServiceException) e;
112             if (se.getHttpStatus() != null) {
113                 code = se.getHttpStatus();
114             }
115         }
116         return create(e.toString(), code);
117     }
118
119 }