2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
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 org.apache.commons.lang3.tuple.Pair;
34 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.user.UserBusinessLogic;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.common.api.HealthCheckInfo;
41 import org.openecomp.sdc.common.api.HealthCheckWrapper;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.stereotype.Controller;
45 import javax.inject.Inject;
46 import javax.servlet.ServletContext;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.ws.rs.Consumes;
49 import javax.ws.rs.GET;
50 import javax.ws.rs.Path;
51 import javax.ws.rs.Produces;
52 import javax.ws.rs.core.Context;
53 import javax.ws.rs.core.MediaType;
54 import javax.ws.rs.core.Response;
55 import java.util.List;
57 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
59 @Tag(name = "SDC Internal APIs")
60 @Server(url = "/sdc2/rest")
62 public class BeMonitoringServlet extends BeGenericServlet {
64 private final Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
66 private static final Logger log = Logger.getLogger(BeMonitoringServlet.class);
67 private final HealthCheckBusinessLogic healthCheckBusinessLogic;
70 public BeMonitoringServlet(UserBusinessLogic userBusinessLogic,
71 ComponentsUtils componentsUtils,
72 HealthCheckBusinessLogic healthCheckBusinessLogic){
73 super(userBusinessLogic, componentsUtils);
74 this.healthCheckBusinessLogic = healthCheckBusinessLogic;
79 @Consumes(MediaType.APPLICATION_JSON)
80 @Produces(MediaType.APPLICATION_JSON)
81 @Operation(description = "Return aggregate BE health check of SDC BE components",
82 summary = "return BE health check", responses = {
83 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))),
84 @ApiResponse(responseCode = "200", description = "SDC BE components are all up"),
85 @ApiResponse(responseCode = "500", description = "One or more SDC BE components are down")})
86 public Response getHealthCheck(@Context final HttpServletRequest request) {
88 Pair<Boolean, List<HealthCheckInfo>> beHealthCheckInfosStatus = healthCheckBusinessLogic.getBeHealthCheckInfosStatus();
89 Boolean aggregateStatus = beHealthCheckInfosStatus.getLeft();
90 ActionStatus status = aggregateStatus ? ActionStatus.OK : ActionStatus.GENERAL_ERROR;
91 String sdcVersion = getVersionFromContext(request);
92 if (sdcVersion == null || sdcVersion.isEmpty()) {
93 sdcVersion = "UNKNOWN";
95 String siteMode = healthCheckBusinessLogic.getSiteMode();
96 HealthCheckWrapper healthCheck = new HealthCheckWrapper(beHealthCheckInfosStatus.getRight(), sdcVersion, siteMode);
97 // The response can be either with 200 or 500 aggregate status - the
98 // body of individual statuses is returned either way
100 String healthCheckStr = prettyGson.toJson(healthCheck);
101 return buildOkResponse(getComponentsUtils().getResponseFormat(status), healthCheckStr);
103 } catch (Exception e) {
104 BeEcompErrorManager.getInstance().logBeHealthCheckError("BeHealthCheck");
105 log.debug("BE health check unexpected exception", e);
110 private String getVersionFromContext(HttpServletRequest request) {
111 ServletContext servletContext = request.getSession().getServletContext();
112 return (String) servletContext.getAttribute(Constants.ASDC_RELEASE_VERSION_ATTR);