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 System.out.println("Creating table....");
60 boolean ksresult = createKeyspace();
63 result = getAdminKeySpace(consistency);
64 } catch (MusicServiceException e1) {
65 // TODO Auto-generated catch block
69 return "One or more nodes are down or not responding.";
75 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
80 private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
83 PreparedQueryObject pQuery = new PreparedQueryObject();
84 pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
85 pQuery.addValue(UUID.randomUUID());
86 ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
87 System.out.println(rs);
97 private boolean createKeyspace() {
98 PreparedQueryObject pQuery = new PreparedQueryObject();
99 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
100 ResultType rs = null ;
102 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
103 } catch (MusicServiceException e) {
104 // TODO Auto-generated catch block
107 if(rs.getResult().toLowerCase().contains("success"))
113 public String getZookeeperStatus() {
115 String host = MusicUtil.getMyZkHost();
116 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
118 MusicLockingService lockingService = MusicCore.getLockingServiceHandle();
119 // additionally need to call the ZK to create,aquire and delete lock
120 } catch (MusicLockingException e) {
121 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
122 ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
126 logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
129 // return "Zookeeper is not responding";
133 public String getCassandrHost() {
137 public void setCassandrHost(String cassandrHost) {
138 this.cassandrHost = cassandrHost;
141 public String getZookeeperHost() {
142 return zookeeperHost;
145 public void setZookeeperHost(String zookeeperHost) {
146 this.zookeeperHost = zookeeperHost;