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 * ====================================================================
23 package org.onap.music.eelf.healthcheck;
25 import java.util.UUID;
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;
40 import com.datastax.driver.core.ConsistencyLevel;
46 public class MusicHealthCheck {
48 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
50 private String cassandrHost;
51 private String zookeeperHost;
53 public String getCassandraStatus(String consistency) {
54 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
56 boolean result = false;
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();
66 result = getAdminKeySpace(consistency);
67 } catch (MusicServiceException e1) {
68 // TODO Auto-generated catch block
69 logger.error("Error", e);
73 logger.error("Error", e);
74 return "One or more nodes are down or not responding.";
80 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
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());
94 private boolean createKeyspace() {
95 PreparedQueryObject pQuery = new PreparedQueryObject();
96 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
97 ResultType rs = null ;
99 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
100 } catch (MusicServiceException e) {
101 // TODO Auto-generated catch block
103 logger.error("Error", e);
105 return rs != null && rs.getResult().toLowerCase().contains("success");
108 public String getZookeeperStatus() {
110 String host = MusicUtil.getMyZkHost();
111 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
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);
121 logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
126 public String getCassandrHost() {
130 public void setCassandrHost(String cassandrHost) {
131 this.cassandrHost = cassandrHost;
134 public String getZookeeperHost() {
135 return zookeeperHost;
138 public void setZookeeperHost(String zookeeperHost) {
139 this.zookeeperHost = zookeeperHost;