MusicHealthCheck.java - Multiple Sonar Fixes
[music.git] / src / main / java / org / onap / music / eelf / healthcheck / MusicHealthCheck.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.eelf.healthcheck;
24
25 import java.util.UUID;
26
27 import org.onap.music.datastore.PreparedQueryObject;
28 import org.onap.music.eelf.logging.EELFLoggerDelegate;
29 import org.onap.music.eelf.logging.format.AppMessages;
30 import org.onap.music.eelf.logging.format.ErrorSeverity;
31 import org.onap.music.eelf.logging.format.ErrorTypes;
32 import org.onap.music.exceptions.MusicLockingException;
33 import org.onap.music.exceptions.MusicServiceException;
34 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
35 import org.onap.music.main.MusicUtil;
36 import org.onap.music.main.ResultType;
37 import org.onap.music.service.impl.MusicZKCore;
38 import org.onap.music.main.MusicCore;
39
40 import com.datastax.driver.core.ConsistencyLevel;
41
42 /**
43  * @author inam
44  *
45  */
46 public class MusicHealthCheck {
47
48     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
49
50     private String cassandrHost;
51     private String zookeeperHost;
52
53     public String getCassandraStatus(String consistency) {
54         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
55         
56         boolean result = false;
57         try {
58             result = getAdminKeySpace(consistency);
59         } catch(Exception e) {
60             if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
61                 logger.error("Error", e);
62                 logger.debug("Creating table....");
63                 boolean ksresult = createKeyspace();
64                 if(ksresult)
65                     try {
66                         result = getAdminKeySpace(consistency);
67                     } catch (MusicServiceException e1) {
68                         // TODO Auto-generated catch block
69                         logger.error("Error", e);
70                         e1.printStackTrace();
71                     }
72             } else {
73                 logger.error("Error", e);
74                 return "One or more nodes are down or not responding.";
75             }
76         }
77         if (result) {
78             return "ACTIVE";
79         } else {
80             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
81             return "INACTIVE";
82         }
83     }
84
85     private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
86         PreparedQueryObject pQuery = new PreparedQueryObject();
87         pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
88         pQuery.addValue(UUID.randomUUID());
89         ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
90         logger.info(rs.toString());
91         return null != rs;
92     }
93     
94     private boolean createKeyspace() {
95         PreparedQueryObject pQuery = new PreparedQueryObject();
96         pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
97         ResultType rs = null ;
98         try {
99             rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
100         } catch (MusicServiceException e) {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103             logger.error("Error", e);
104         }
105         return rs != null && rs.getResult().toLowerCase().contains("success");
106     }
107
108     public String getZookeeperStatus() {
109
110         String host = MusicUtil.getMyZkHost();
111         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
112         try {
113             MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle();
114             // additionally need to call the ZK to create,aquire and delete lock
115         } catch (MusicLockingException e) {
116             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
117                     ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
118             return "INACTIVE";
119         }
120
121         logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
122         return "ACTIVE";
123
124     }
125
126     public String getCassandrHost() {
127         return cassandrHost;
128     }
129
130     public void setCassandrHost(String cassandrHost) {
131         this.cassandrHost = cassandrHost;
132     }
133
134     public String getZookeeperHost() {
135         return zookeeperHost;
136     }
137
138     public void setZookeeperHost(String zookeeperHost) {
139         this.zookeeperHost = zookeeperHost;
140     }
141
142 }