2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
7 * Modifications Copyright (C) 2018 IBM.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.rest;
27 import java.util.HashMap;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
45 import org.onap.music.eelf.healthcheck.MusicHealthCheck;
46 import org.onap.music.eelf.logging.EELFLoggerDelegate;
47 import org.onap.music.main.MusicUtil;
49 import com.datastax.driver.core.ConsistencyLevel;
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiParam;
59 @Api(value="Healthcheck Api")
60 public class RestMusicHealthCheckAPI {
63 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
64 private static final String ACTIVE_STATUS = "ACTIVE";
65 private static final String INVALID_STATUS = "INVALID";
66 private static final String INACTIVE_STATUS = "INACTIVE";
67 private static final String INVALID_MESSAGE = "Consistency level is invalid...";
68 private static final String INACTIVE_MESSAGE = "One or more nodes in the Cluster is/are down or not responding.";
69 private static final String ACTIVE_MESSAGE = "Cassandra Running and Listening to requests";
70 private static final String STATUS_KEY = "status";
71 private static final String MESSAGE_KEY = "message";
74 @Path("/pingCassandra/{consistency}")
75 @ApiOperation(value = "Get Health Status", response = Map.class)
76 @Produces(MediaType.APPLICATION_JSON)
77 public Response cassandraStatus(
78 @Context HttpServletResponse response,
79 @ApiParam(value = "Consistency level",required = true)
80 @PathParam("consistency") String consistency) {
81 logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra");
83 Map<String, Object> resultMap = new HashMap<>();
84 if(ConsistencyLevel.valueOf(consistency) == null) {
85 resultMap.put(STATUS_KEY,INVALID_STATUS);
86 resultMap.put(MESSAGE_KEY, INVALID_MESSAGE);
87 resultMap.put(INVALID_STATUS, INVALID_STATUS);
88 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
90 MusicHealthCheck cassHealthCheck = new MusicHealthCheck();
91 String status = cassHealthCheck.getCassandraStatus(consistency);
92 if(status.equals(ACTIVE_STATUS)) {
93 resultMap.put(STATUS_KEY,ACTIVE_STATUS);
94 resultMap.put(MESSAGE_KEY, ACTIVE_MESSAGE);
95 resultMap.put(ACTIVE_STATUS, ACTIVE_MESSAGE);
96 return Response.status(Status.OK).entity(resultMap).build();
98 resultMap.put(STATUS_KEY,INACTIVE_STATUS);
99 resultMap.put(MESSAGE_KEY, INACTIVE_MESSAGE);
100 resultMap.put(INACTIVE_STATUS, INACTIVE_MESSAGE);
101 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
106 @Path("/musicHealthCheck")
107 @ApiOperation(value = "Get Health Status", response = Map.class)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response musicHealthCheck() {
110 logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC");
111 Map<String, Object> resultMap = new HashMap<>();
112 MusicHealthCheck healthCheck = new MusicHealthCheck();
114 String status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString());
115 if(status.equals(ACTIVE_STATUS)) {
116 resultMap.put("Cassandra", "Active");
118 resultMap.put("Cassandra", "Inactive");
120 resultMap.put("MUSIC", "Active");
121 return Response.status(Status.OK).entity(resultMap).build();