Publish swagger files for SDC APIs
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / BeMonitoringServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.sdc.be.servlets;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.jcabi.aspects.Loggable;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.impl.ComponentsUtils;
39 import org.openecomp.sdc.be.user.UserBusinessLogic;
40 import org.openecomp.sdc.common.api.Constants;
41 import org.openecomp.sdc.common.api.HealthCheckInfo;
42 import org.openecomp.sdc.common.api.HealthCheckWrapper;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.springframework.stereotype.Controller;
45
46 import javax.inject.Inject;
47 import javax.servlet.ServletContext;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.ws.rs.Consumes;
50 import javax.ws.rs.GET;
51 import javax.ws.rs.Path;
52 import javax.ws.rs.Produces;
53 import javax.ws.rs.core.Context;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import java.util.List;
57
58 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
59 @Path("/")
60 @Tags({@Tag(name = "SDCE-2 APIs")})
61 @Server(url = "/sdc2/rest")
62 @Controller
63 public class BeMonitoringServlet extends BeGenericServlet {
64
65     private final Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
66
67     private static final Logger log = Logger.getLogger(BeMonitoringServlet.class);
68     private final HealthCheckBusinessLogic healthCheckBusinessLogic;
69
70     @Inject
71     public BeMonitoringServlet(UserBusinessLogic userBusinessLogic,
72         ComponentsUtils componentsUtils,
73         HealthCheckBusinessLogic healthCheckBusinessLogic){
74         super(userBusinessLogic, componentsUtils);
75         this.healthCheckBusinessLogic = healthCheckBusinessLogic;
76     }
77
78     @GET
79     @Path("/healthCheck")
80     @Consumes(MediaType.APPLICATION_JSON)
81     @Produces(MediaType.APPLICATION_JSON)
82     @Operation(description = "Return aggregate BE health check of SDC BE components",
83             summary = "return BE health check", responses = {
84             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))),
85             @ApiResponse(responseCode = "200", description = "SDC BE components are all up"),
86             @ApiResponse(responseCode = "500", description = "One or more SDC BE components are down")})
87     public Response getHealthCheck(@Context final HttpServletRequest request) {
88         try {
89             Pair<Boolean, List<HealthCheckInfo>> beHealthCheckInfosStatus = healthCheckBusinessLogic.getBeHealthCheckInfosStatus();
90             Boolean aggregateStatus = beHealthCheckInfosStatus.getLeft();
91             ActionStatus status = aggregateStatus ? ActionStatus.OK : ActionStatus.GENERAL_ERROR;
92             String sdcVersion = getVersionFromContext(request);
93             if (sdcVersion == null || sdcVersion.isEmpty()) {
94                 sdcVersion = "UNKNOWN";
95             }
96             String siteMode = healthCheckBusinessLogic.getSiteMode();
97             HealthCheckWrapper healthCheck = new HealthCheckWrapper(beHealthCheckInfosStatus.getRight(), sdcVersion, siteMode);
98             // The response can be either with 200 or 500 aggregate status - the
99             // body of individual statuses is returned either way
100
101             String healthCheckStr = prettyGson.toJson(healthCheck);
102             return buildOkResponse(getComponentsUtils().getResponseFormat(status), healthCheckStr);
103
104         } catch (Exception e) {
105             BeEcompErrorManager.getInstance().logBeHealthCheckError("BeHealthCheck");
106             log.debug("BE health check unexpected exception", e);
107             throw e;
108         }
109     }
110
111     private String getVersionFromContext(HttpServletRequest request) {
112         ServletContext servletContext = request.getSession().getServletContext();
113         return (String) servletContext.getAttribute(Constants.ASDC_RELEASE_VERSION_ATTR);
114     }
115
116 }