cb8965eebaa0dd8aa4cf9fc2a66a1fe3cbb3cc9c
[music.git] / src / main / java / org / onap / music / rest / RestMusicHealthCheckAPI.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *
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
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  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.rest;
26
27 import java.util.HashMap;
28 /**
29  * @author inam
30  *
31  */
32 import java.util.Map;
33
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;
43
44
45 import org.onap.music.eelf.healthcheck.MusicHealthCheck;
46 import org.onap.music.eelf.logging.EELFLoggerDelegate;
47 import org.onap.music.main.MusicUtil;
48
49 import com.datastax.driver.core.ConsistencyLevel;
50
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiParam;
54
55
56
57
58 @Path("/v2/service")
59 @Api(value="Healthcheck Api")
60 public class RestMusicHealthCheckAPI {
61     
62     
63     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
64     private static final String ACTIVE_STATUS = "ACTIVE";
65     
66     @GET
67     @Path("/pingCassandra/{consistency}")
68     @ApiOperation(value = "Get Health Status", response = Map.class)
69     @Produces(MediaType.APPLICATION_JSON)
70     public Response cassandraStatus(@Context HttpServletResponse response, @ApiParam(value = "Consistency level",
71             required = true) @PathParam("consistency") String consistency) {
72         logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra");
73         
74         Map<String, Object> resultMap = new HashMap<>();
75         if(ConsistencyLevel.valueOf(consistency) == null) {
76             resultMap.put("INVALID", "Consistency level is invalid...");
77             return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
78         }
79         MusicHealthCheck cassHealthCheck = new MusicHealthCheck();
80         String status = cassHealthCheck.getCassandraStatus(consistency);
81         if(status.equals(ACTIVE_STATUS)) {
82             resultMap.put(ACTIVE_STATUS, "Cassandra Running and Listening to requests");
83             return Response.status(Status.OK).entity(resultMap).build();
84         } else {
85             resultMap.put("INACTIVE", "One or more nodes in the Cluster is/are down or not responding.");
86             return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
87         }
88         
89         
90         
91     }
92     
93     @GET
94     @Path("/musicHealthCheck")
95     @ApiOperation(value = "Get Health Status", response = Map.class)
96     @Produces(MediaType.APPLICATION_JSON)
97     public Response musicHealthCheck() {
98         logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC");
99         Map<String, Object> resultMap = new HashMap<>();
100         MusicHealthCheck healthCheck = new MusicHealthCheck();
101
102         String status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString());
103         if(status.equals(ACTIVE_STATUS)) {
104             resultMap.put("Cassandra", "Active");
105         } else {
106             resultMap.put("Cassandra", "Inactive");
107         }
108         resultMap.put("MUSIC", "Active");
109         return Response.status(Status.OK).entity(resultMap).build();
110     }
111
112 }