Catalog alignment
[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.OpenAPIDefinition;
27 import io.swagger.v3.oas.annotations.Operation;
28 import io.swagger.v3.oas.annotations.info.Info;
29 import io.swagger.v3.oas.annotations.media.ArraySchema;
30 import io.swagger.v3.oas.annotations.media.Content;
31 import io.swagger.v3.oas.annotations.media.Schema;
32 import io.swagger.v3.oas.annotations.responses.ApiResponse;
33 import io.swagger.v3.oas.annotations.responses.ApiResponses;
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.impl.WebAppContextWrapper;
40 import org.openecomp.sdc.be.user.UserBusinessLogic;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.openecomp.sdc.common.api.HealthCheckInfo;
43 import org.openecomp.sdc.common.api.HealthCheckWrapper;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.context.WebApplicationContext;
47
48 import javax.inject.Inject;
49 import javax.servlet.ServletContext;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.ws.rs.Consumes;
52 import javax.ws.rs.GET;
53 import javax.ws.rs.Path;
54 import javax.ws.rs.Produces;
55 import javax.ws.rs.core.Context;
56 import javax.ws.rs.core.MediaType;
57 import javax.ws.rs.core.Response;
58 import java.util.List;
59
60 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
61 @Path("/")
62 @OpenAPIDefinition(info = @Info(title = "BE Monitoring", description = "BE Monitoring"))
63 @Controller
64 public class BeMonitoringServlet extends BeGenericServlet {
65
66     Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
67
68     private static final Logger log = Logger.getLogger(ConfigServlet.class);
69     private final HealthCheckBusinessLogic healthCheckBusinessLogic;
70
71     @Inject
72     public BeMonitoringServlet(UserBusinessLogic userBusinessLogic,
73         ComponentsUtils componentsUtils,
74         HealthCheckBusinessLogic healthCheckBusinessLogic){
75         super(userBusinessLogic, componentsUtils);
76         this.healthCheckBusinessLogic = healthCheckBusinessLogic;
77     }
78
79     @GET
80     @Path("/healthCheck")
81     @Consumes(MediaType.APPLICATION_JSON)
82     @Produces(MediaType.APPLICATION_JSON)
83     @Operation(description = "Return aggregate BE health check of SDC BE components", summary = "return BE health check",
84             responses = @ApiResponse(
85                     content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))))
86     @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "SDC BE components are all up"),
87             @ApiResponse(responseCode = "500", description = "One or more SDC BE components are down")})
88     public Response getHealthCheck(@Context final HttpServletRequest request) {
89         try {
90             Pair<Boolean, List<HealthCheckInfo>> beHealthCheckInfosStatus = healthCheckBusinessLogic.getBeHealthCheckInfosStatus();
91             Boolean aggregateStatus = beHealthCheckInfosStatus.getLeft();
92             ActionStatus status = aggregateStatus ? ActionStatus.OK : ActionStatus.GENERAL_ERROR;
93             String sdcVersion = getVersionFromContext(request);
94             if (sdcVersion == null || sdcVersion.isEmpty()) {
95                 sdcVersion = "UNKNOWN";
96             }
97             String siteMode = healthCheckBusinessLogic.getSiteMode();
98             HealthCheckWrapper healthCheck = new HealthCheckWrapper(beHealthCheckInfosStatus.getRight(), sdcVersion, siteMode);
99             // The response can be either with 200 or 500 aggregate status - the
100             // body of individual statuses is returned either way
101
102             String healthCheckStr = prettyGson.toJson(healthCheck);
103             return buildOkResponse(getComponentsUtils().getResponseFormat(status), healthCheckStr);
104
105         } catch (Exception e) {
106             BeEcompErrorManager.getInstance().logBeHealthCheckError("BeHealthCheck");
107             log.debug("BE health check unexpected exception", e);
108             throw e;
109         }
110     }
111
112
113     //TODO remove after UI alignment and tests after API consolidation ASDC-191
114     /*@GET
115     @Path("/version")
116     @Consumes(MediaType.APPLICATION_JSON)
117     @Produces(MediaType.APPLICATION_JSON)
118     @ApiOperation(value = "return the ASDC application version", notes = "return the ASDC application version", response = String.class)
119     @ApiResponses(value = { @ApiResponse(code = 200, message = "return ASDC version"), @ApiResponse(code = 500, message = "Internal Error") })
120     public Response getSdcVersion(@Context final HttpServletRequest request) {
121         try {
122             String url = request.getMethod() + " " + request.getRequestURI();
123             log.debug("Start handle request of {}", url);
124
125             String version = getVersionFromContext(request);
126             log.debug("asdc version from manifest is: {}", version);
127             if (version == null || version.isEmpty()) {
128                 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.ASDC_VERSION_NOT_FOUND));
129             }
130
131             HealthCheckInfo versionInfo = new HealthCheckInfo();
132             versionInfo.setVersion(version);
133
134             // The response can be either with 200 or 500 aggregate status - the
135             // body of individual statuses is returned either way
136             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), versionInfo);
137
138         } catch (Exception e) {
139             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getSDCVersion");
140             log.debug("BE get ASDC version unexpected exception", e);
141             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
142         }
143     }*/
144
145     private String getVersionFromContext(HttpServletRequest request) {
146         ServletContext servletContext = request.getSession().getServletContext();
147         return (String) servletContext.getAttribute(Constants.ASDC_RELEASE_VERSION_ATTR);
148     }
149
150     private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) {
151         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
152         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
153         return webApplicationContext.getBean(HealthCheckBusinessLogic.class);
154     }
155
156 }