2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.eelf.healthcheck;
24 import java.util.UUID;
26 import org.onap.music.datastore.PreparedQueryObject;
27 import org.onap.music.eelf.logging.EELFLoggerDelegate;
28 import org.onap.music.eelf.logging.format.AppMessages;
29 import org.onap.music.eelf.logging.format.ErrorSeverity;
30 import org.onap.music.eelf.logging.format.ErrorTypes;
31 import org.onap.music.exceptions.MusicLockingException;
32 import org.onap.music.exceptions.MusicServiceException;
33 import org.onap.music.lockingservice.MusicLockingService;
34 import org.onap.music.main.MusicCore;
35 import org.onap.music.main.MusicUtil;
36 import org.onap.music.main.ResultType;
38 import com.datastax.driver.core.ConsistencyLevel;
44 public class MusicHealthCheck {
46 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
48 private String cassandrHost;
49 private String zookeeperHost;
51 public String getCassandraStatus(String consistency) {
52 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
54 boolean result = false;
56 result = getAdminKeySpace(consistency);
57 } catch(Exception e) {
58 if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
59 logger.error("Error", e);
60 logger.debug("Creating table....");
61 boolean ksresult = createKeyspace();
64 result = getAdminKeySpace(consistency);
65 } catch (MusicServiceException e1) {
66 // TODO Auto-generated catch block
67 logger.error("Error", e);
71 logger.error("Error", e);
72 return "One or more nodes are down or not responding.";
78 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
83 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());
100 private boolean createKeyspace() {
101 PreparedQueryObject pQuery = new PreparedQueryObject();
102 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
103 ResultType rs = null ;
105 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
106 } catch (MusicServiceException e) {
107 // TODO Auto-generated catch block
109 logger.error("Error", e);
111 if(rs != null && rs.getResult().toLowerCase().contains("success"))
117 public String getZookeeperStatus() {
119 String host = MusicUtil.getMyZkHost();
120 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
122 MusicCore.getLockingServiceHandle();
123 // additionally need to call the ZK to create,aquire and delete lock
124 } catch (MusicLockingException e) {
125 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
126 ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
130 logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
135 public String getCassandrHost() {
139 public void setCassandrHost(String cassandrHost) {
140 this.cassandrHost = cassandrHost;
143 public String getZookeeperHost() {
144 return zookeeperHost;
147 public void setZookeeperHost(String zookeeperHost) {
148 this.zookeeperHost = zookeeperHost;