2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
7 * Modifications Copyright (C) 2019 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.eelf.healthcheck;
27 import java.util.UUID;
29 import org.onap.music.datastore.PreparedQueryObject;
30 import org.onap.music.eelf.logging.EELFLoggerDelegate;
31 import org.onap.music.eelf.logging.format.AppMessages;
32 import org.onap.music.eelf.logging.format.ErrorSeverity;
33 import org.onap.music.eelf.logging.format.ErrorTypes;
34 import org.onap.music.exceptions.MusicLockingException;
35 import org.onap.music.exceptions.MusicServiceException;
36 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
37 import org.onap.music.main.MusicUtil;
38 import org.onap.music.main.ResultType;
39 import org.onap.music.service.impl.MusicZKCore;
40 import org.onap.music.main.MusicCore;
42 import com.datastax.driver.core.ConsistencyLevel;
48 public class MusicHealthCheck {
50 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
52 private String cassandrHost;
53 private String zookeeperHost;
55 public String getCassandraStatus(String consistency) {
56 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
58 boolean result = false;
60 result = getAdminKeySpace(consistency);
61 } catch(Exception e) {
62 if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
63 logger.error("Error", e);
64 logger.debug("Creating table....");
65 boolean ksresult = createKeyspace();
68 result = getAdminKeySpace(consistency);
69 } catch (MusicServiceException e1) {
70 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
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 {
88 PreparedQueryObject pQuery = new PreparedQueryObject();
89 pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
90 pQuery.addValue(UUID.randomUUID());
91 ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
92 logger.info(rs.toString());
102 private boolean createKeyspace() {
103 PreparedQueryObject pQuery = new PreparedQueryObject();
104 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
105 ResultType rs = null ;
107 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
108 } catch (MusicServiceException e) {
109 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
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 MusicLockingService lockingService = MusicZKCore.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;