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 fj.data.Either;
27 import io.swagger.annotations.Api;
28 import io.swagger.annotations.ApiOperation;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.MonitoringBusinessLogic;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.api.HealthCheckInfo;
39 import org.openecomp.sdc.common.api.HealthCheckWrapper;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.common.monitoring.MonitoringEvent;
42 import org.openecomp.sdc.exception.ResponseFormat;
43 import org.springframework.web.context.WebApplicationContext;
45 import javax.inject.Singleton;
46 import javax.servlet.ServletContext;
47 import javax.servlet.http.HttpServletRequest;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.MediaType;
51 import javax.ws.rs.core.Response;
52 import java.util.List;
54 @Loggable(prepend = true, value = Loggable.TRACE, trim = false)
56 @Api(value = "BE Monitoring", description = "BE Monitoring")
58 public class BeMonitoringServlet extends BeGenericServlet {
60 Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
62 private static final Logger log = Logger.getLogger(ConfigServlet.class);
66 @Consumes(MediaType.APPLICATION_JSON)
67 @Produces(MediaType.APPLICATION_JSON)
68 @ApiOperation(value = "Return aggregate BE health check of SDC BE components", notes = "return BE health check", response = String.class)
69 @ApiResponses(value = { @ApiResponse(code = 200, message = "SDC BE components are all up"), @ApiResponse(code = 500, message = "One or more SDC BE components are down") })
70 public Response getHealthCheck(@Context final HttpServletRequest request) {
72 HealthCheckBusinessLogic healthCheckBusinessLogic = getHealthCheckBL(request.getSession().getServletContext());
73 Pair<Boolean, List<HealthCheckInfo>> beHealthCheckInfosStatus = healthCheckBusinessLogic.getBeHealthCheckInfosStatus();
74 Boolean aggregateStatus = beHealthCheckInfosStatus.getLeft();
75 ActionStatus status = aggregateStatus ? ActionStatus.OK : ActionStatus.GENERAL_ERROR;
76 String sdcVersion = getVersionFromContext(request);
77 if (sdcVersion == null || sdcVersion.isEmpty()) {
78 sdcVersion = "UNKNOWN";
80 String siteMode = healthCheckBusinessLogic.getSiteMode();
81 HealthCheckWrapper healthCheck = new HealthCheckWrapper(beHealthCheckInfosStatus.getRight(), sdcVersion, siteMode);
82 // The response can be either with 200 or 500 aggregate status - the
83 // body of individual statuses is returned either way
85 String healthCheckStr = prettyGson.toJson(healthCheck);
86 return buildOkResponse(getComponentsUtils().getResponseFormat(status), healthCheckStr);
88 } catch (Exception e) {
89 BeEcompErrorManager.getInstance().logBeHealthCheckError("BeHealthCheck");
90 log.debug("BE health check unexpected exception", e);
91 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
97 @Consumes(MediaType.APPLICATION_JSON)
98 @Produces(MediaType.APPLICATION_JSON)
99 public Response processMonitoringMetrics(@Context final HttpServletRequest request, String json) {
101 MonitoringEvent monitoringEvent = convertContentToJson(json, MonitoringEvent.class);
102 if (monitoringEvent == null) {
103 return buildErrorResponse(getComponentsUtils().getResponseFormatAdditionalProperty(ActionStatus.GENERAL_ERROR));
105 log.trace("Received monitoring metrics: {}", monitoringEvent);
106 ServletContext context = request.getSession().getServletContext();
107 MonitoringBusinessLogic bl = getMonitoringBL(context);
108 Either<Boolean, ResponseFormat> result = bl.logMonitoringEvent(monitoringEvent);
109 if (result.isRight()) {
110 return buildErrorResponse(result.right().value());
112 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), null);
114 } catch (Exception e) {
115 log.debug("BE system metrics unexpected exception", e);
116 return buildErrorResponse(getComponentsUtils().getResponseFormatAdditionalProperty(ActionStatus.GENERAL_ERROR));
122 @Consumes(MediaType.APPLICATION_JSON)
123 @Produces(MediaType.APPLICATION_JSON)
124 @ApiOperation(value = "return the ASDC application version", notes = "return the ASDC application version", response = String.class)
125 @ApiResponses(value = { @ApiResponse(code = 200, message = "return ASDC version"), @ApiResponse(code = 500, message = "Internal Error") })
126 public Response getSdcVersion(@Context final HttpServletRequest request) {
128 String url = request.getMethod() + " " + request.getRequestURI();
129 log.debug("Start handle request of {}", url);
131 String version = getVersionFromContext(request);
132 log.debug("asdc version from manifest is: {}", version);
133 if (version == null || version.isEmpty()) {
134 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.ASDC_VERSION_NOT_FOUND));
137 HealthCheckInfo versionInfo = new HealthCheckInfo();
138 versionInfo.setVersion(version);
140 // The response can be either with 200 or 500 aggregate status - the
141 // body of individual statuses is returned either way
142 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), versionInfo);
144 } catch (Exception e) {
145 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getSDCVersion");
146 log.debug("BE get ASDC version unexpected exception", e);
147 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
151 private String getVersionFromContext(HttpServletRequest request) {
152 ServletContext servletContext = request.getSession().getServletContext();
153 return (String) servletContext.getAttribute(Constants.ASDC_RELEASE_VERSION_ATTR);
156 protected MonitoringEvent convertContentToJson(String content, Class<MonitoringEvent> clazz) {
158 MonitoringEvent object = null;
160 object = gson.fromJson(content, clazz);
161 object.setFields(null);
162 } catch (Exception e) {
163 log.debug("Failed to convert the content {} to object.", content.substring(0, Math.min(50, content.length())), e);
169 private HealthCheckBusinessLogic getHealthCheckBL(ServletContext context) {
170 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
171 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
172 return webApplicationContext.getBean(HealthCheckBusinessLogic.class);