4d283f26b16e9e9d4a4da9b35c0a0571a68d7e78
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra;
24
25
26 import java.net.URI;
27 import java.util.Collections;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.MediaType;
31 import org.springframework.http.ResponseEntity;
32 import javax.annotation.PostConstruct;
33 import javax.transaction.Transactional;
34 import javax.ws.rs.DefaultValue;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.container.ContainerRequestContext;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import org.apache.http.HttpStatus;
44 import org.onap.so.logger.MessageEnum;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.core.env.Environment;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.stereotype.Component;
50 import org.springframework.web.client.RestTemplate;
51 import org.springframework.http.HttpMethod;
52 import io.swagger.annotations.Api;
53 import io.swagger.annotations.ApiOperation;
54
55
56 @Component
57 @Path("/globalhealthcheck")
58 @Api(value = "/globalhealthcheck", description = "APIH Infra Global Health Check")
59 public class GlobalHealthcheckHandler {
60     private static Logger logger = LoggerFactory.getLogger(GlobalHealthcheckHandler.class);
61     private static final String CONTEXTPATH_PROPERTY = "management.context-path";
62     private static final String PROPERTY_DOMAIN = "mso.health.endpoints";
63     private static final String CATALOGDB_PROPERTY = PROPERTY_DOMAIN + ".catalogdb";
64     private static final String REQUESTDB_PROPERTY = PROPERTY_DOMAIN + ".requestdb";
65     private static final String SDNC_PROPERTY = PROPERTY_DOMAIN + ".sdnc";
66     private static final String OPENSTACK_PROPERTY = PROPERTY_DOMAIN + ".openstack";
67     private static final String BPMN_PROPERTY = PROPERTY_DOMAIN + ".bpmn";
68     private static final String ASDC_PROPERTY = PROPERTY_DOMAIN + ".asdc";
69     private static final String REQUESTDBATTSVC_PROPERTY = PROPERTY_DOMAIN + ".requestdbattsvc";
70     private static final String DEFAULT_PROPERTY_VALUE = "";
71
72     // e.g. /manage
73     private String actuatorContextPath;
74     private String endpointCatalogdb;
75     private String endpointRequestdb;
76     private String endpointSdnc;
77     private String endpointOpenstack;
78     private String endpointBpmn;
79     private String endpointAsdc;
80     private String endpointRequestdbAttsvc;
81
82     @Autowired
83     private Environment env;
84
85     @Autowired
86     private RestTemplate restTemplate;
87     private final String health = "/health";
88
89
90     @PostConstruct
91     protected void init() {
92         actuatorContextPath = env.getProperty(CONTEXTPATH_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
93         endpointCatalogdb = env.getProperty(CATALOGDB_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
94         endpointRequestdb = env.getProperty(REQUESTDB_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
95         endpointSdnc = env.getProperty(SDNC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
96         endpointOpenstack = env.getProperty(OPENSTACK_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
97         endpointBpmn = env.getProperty(BPMN_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
98         endpointAsdc = env.getProperty(ASDC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
99         endpointRequestdbAttsvc = env.getProperty(REQUESTDBATTSVC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE);
100     }
101
102     @GET
103     @Produces("application/json")
104     @ApiOperation(value = "Performing global health check", response = Response.class)
105     @Transactional
106     public Response globalHealthcheck(@DefaultValue("true") @QueryParam("enableBpmn") boolean enableBpmn,
107             @Context ContainerRequestContext requestContext) {
108         Response HEALTH_CHECK_RESPONSE = null;
109         // Build internal response object
110         HealthcheckResponse rsp = new HealthcheckResponse();
111
112         try {
113             // Generated RequestId
114             String requestId = requestContext.getProperty("requestId").toString();
115             logger.info("{} {}", MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId);
116
117             // set APIH status, this is the main entry point
118             rsp.setApih(HealthcheckStatus.UP.toString());
119             // set BPMN
120             rsp.setBpmn(querySubsystemHealth(MsoSubsystems.BPMN));
121             // set SDNCAdapter
122             rsp.setSdncAdapter(querySubsystemHealth(MsoSubsystems.SDNC));
123             // set ASDCController
124             rsp.setAsdcController(querySubsystemHealth(MsoSubsystems.ASDC));
125             // set CatalogDbAdapter
126             rsp.setCatalogdbAdapter(querySubsystemHealth(MsoSubsystems.CATALOGDB));
127             // set RequestDbAdapter
128             rsp.setRequestdbAdapter(querySubsystemHealth(MsoSubsystems.REQUESTDB));
129             // set OpenStackAdapter
130             rsp.setOpenstackAdapter(querySubsystemHealth(MsoSubsystems.OPENSTACK));
131             // set RequestDbAdapterAttSvc
132             rsp.setRequestdbAdapterAttsvc(querySubsystemHealth(MsoSubsystems.REQUESTDBATT));
133             // set Message
134             rsp.setMessage(String.format("HttpStatus: %s", HttpStatus.SC_OK));
135             logger.info(rsp.toString());
136
137             HEALTH_CHECK_RESPONSE = Response.status(HttpStatus.SC_OK).entity(rsp).build();
138
139         } catch (Exception ex) {
140             logger.error("Exception occurred", ex);
141             rsp.setMessage(ex.getMessage());
142             HEALTH_CHECK_RESPONSE = Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(rsp).build();
143         }
144
145         return HEALTH_CHECK_RESPONSE;
146     }
147
148     protected HttpEntity<String> buildHttpEntityForRequest() {
149         HttpHeaders headers = new HttpHeaders();
150         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
151         headers.set("Content-Type", "application/json");
152         HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
153         return entity;
154     }
155
156     protected String querySubsystemHealth(MsoSubsystems subsystem) {
157         try {
158             // get port number for the subsystem
159             String ept = getEndpointUrlForSubsystemEnum(subsystem);
160
161             // build final endpoint url
162             UriBuilder builder = UriBuilder.fromPath(ept).path(actuatorContextPath).path(health);
163             URI uri = builder.build();
164             logger.info("Calculated URL: {}", uri.toString());
165
166             ResponseEntity<SubsystemHealthcheckResponse> result = restTemplate.exchange(uri, HttpMethod.GET,
167                     buildHttpEntityForRequest(), SubsystemHealthcheckResponse.class);
168
169             return processResponseFromSubsystem(result, subsystem);
170
171         } catch (Exception ex) {
172             logger.error("Exception occured in GlobalHealthcheckHandler.querySubsystemHealth() ", ex);
173             return HealthcheckStatus.DOWN.toString();
174         }
175     }
176
177     protected String processResponseFromSubsystem(ResponseEntity<SubsystemHealthcheckResponse> result,
178             MsoSubsystems subsystem) {
179         if (result == null || result.getStatusCodeValue() != HttpStatus.SC_OK) {
180             logger.error(String.format("Globalhealthcheck: checking subsystem: %s failed ! result object is: %s",
181                     subsystem, result == null ? "NULL" : result));
182             return HealthcheckStatus.DOWN.toString();
183         }
184
185         SubsystemHealthcheckResponse body = result.getBody();
186
187         String status = body.getStatus();
188         if ("UP".equalsIgnoreCase(status)) {
189             return HealthcheckStatus.UP.toString();
190         } else {
191             logger.error("{}, query health endpoint did not return UP status!", subsystem);
192             return HealthcheckStatus.DOWN.toString();
193         }
194     }
195
196
197     protected String getEndpointUrlForSubsystemEnum(MsoSubsystems subsystem) {
198         switch (subsystem) {
199             case SDNC:
200                 return this.endpointSdnc;
201             case ASDC:
202                 return this.endpointAsdc;
203             case BPMN:
204                 return this.endpointBpmn;
205             case CATALOGDB:
206                 return this.endpointCatalogdb;
207             case OPENSTACK:
208                 return this.endpointOpenstack;
209             case REQUESTDB:
210                 return this.endpointRequestdb;
211             case REQUESTDBATT:
212                 return this.endpointRequestdbAttsvc;
213             default:
214                 return "";
215         }
216     }
217 }