Merge "fixed sonar issue in MusicUtil.java"
[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 package org.onap.music.rest;
25
26 import java.util.HashMap;
27 /**
28  * @author inam
29  *
30  */
31 import java.util.Map;
32
33 import javax.servlet.http.HttpServletResponse;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42
43
44 import org.onap.music.eelf.healthcheck.MusicHealthCheck;
45 import org.onap.music.eelf.logging.EELFLoggerDelegate;
46 import org.onap.music.main.MusicUtil;
47
48 import com.datastax.driver.core.ConsistencyLevel;
49
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
53
54
55
56
57 @Path("/v{version: [0-9]+}/service")
58 @Api(value="Healthcheck Api")
59 public class RestMusicHealthCheckAPI {
60         
61         
62         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
63         private static final String activeStatus = "ACTIVE";
64         
65         @GET
66         @Path("/pingCassandra/{consistency}")
67         @ApiOperation(value = "Get Health Status", response = Map.class)
68         @Produces(MediaType.APPLICATION_JSON)
69         public Response cassandraStatus(@Context HttpServletResponse response, @ApiParam(value = "Consistency level",
70             required = true) @PathParam("consistency") String consistency) {
71                 logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra");
72                 
73                 Map<String, Object> resultMap = new HashMap<>();
74                 if(ConsistencyLevel.valueOf(consistency) == null) {
75                         resultMap.put("INVALID", "Consistency level is invalid...");
76                         return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
77                 }
78                 MusicHealthCheck cassHealthCheck = new MusicHealthCheck();
79                 String status = cassHealthCheck.getCassandraStatus(consistency);
80                 if(status.equals(activeStatus)) {
81                         resultMap.put(activeStatus, "Cassandra Running and Listening to requests");
82                         return Response.status(Status.OK).entity(resultMap).build();
83                 } else {
84                         resultMap.put("INACTIVE", "One or more nodes in the Cluster is/are down or not responding.");
85                         return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
86                 }
87                 
88                 
89                 
90         }
91         
92         @GET
93         @Path("/pingZookeeper")
94         @ApiOperation(value = "Get Health Status", response = Map.class)
95         @Produces(MediaType.APPLICATION_JSON)
96         public Response ZKStatus(@Context HttpServletResponse response) {
97                 logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Zookeeper");
98                 Map<String, Object> resultMap = new HashMap<>();
99                 MusicHealthCheck ZKHealthCheck = new MusicHealthCheck();
100                 String status = ZKHealthCheck.getZookeeperStatus();
101                 if(status.equals(activeStatus)) {
102                         resultMap.put(activeStatus, "Zookeeper is Active and Running");
103                         return Response.status(Status.OK).entity(resultMap).build();
104                 }else {
105                         resultMap.put("INACTIVE", "Zookeeper is not responding");
106                         return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
107                 }
108         }
109         
110         @GET
111         @Path("/musicHealthCheck")
112         @ApiOperation(value = "Get Health Status", response = Map.class)
113         @Produces(MediaType.APPLICATION_JSON)
114         public Response musicHealthCheck() {
115                 logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC");
116                 Map<String, Object> resultMap = new HashMap<>();
117                 MusicHealthCheck healthCheck = new MusicHealthCheck();
118                 String status = healthCheck.getZookeeperStatus();
119                 if(status.equals(activeStatus)) {
120                         resultMap.put("ZooKeeper", "Active");
121                 }else {
122                         resultMap.put("ZooKeeper", "Inactive");
123                 }
124                 status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString());
125                 if(status.equals(activeStatus)) {
126                         resultMap.put("Cassandra", "Active");
127                 } else {
128                         resultMap.put("Cassandra", "Inactive");
129                 }
130                 resultMap.put("MUSIC", "Active");
131                 return Response.status(Status.OK).entity(resultMap).build();
132         }
133
134 }