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.MusicDataStoreHandle;
30 import org.onap.music.datastore.PreparedQueryObject;
31 import org.onap.music.eelf.logging.EELFLoggerDelegate;
32 import org.onap.music.eelf.logging.format.AppMessages;
33 import org.onap.music.eelf.logging.format.ErrorSeverity;
34 import org.onap.music.eelf.logging.format.ErrorTypes;
35 import org.onap.music.exceptions.MusicQueryException;
36 import org.onap.music.exceptions.MusicServiceException;
37 import org.onap.music.main.MusicUtil;
38 import org.onap.music.main.ResultType;
39 import org.onap.music.main.MusicCore;
41 import com.datastax.driver.core.ConsistencyLevel;
47 public class MusicHealthCheck {
49 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
51 private String cassandrHost;
53 public String getCassandraStatus(String consistency) {
54 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
56 boolean result = false;
57 UUID randomUUID = UUID.randomUUID();
59 result = getAdminKeySpace(consistency, randomUUID);
60 } catch( Exception e) {
61 if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
62 logger.error("Error", e);
63 logger.debug("Creating table....");
65 boolean ksresult = createKeyspace();
67 result = getAdminKeySpace(consistency, randomUUID);
69 } catch (MusicServiceException e1) {
70 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN, e1);
71 } catch (MusicQueryException e1) {
72 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN,e1);
75 logger.error("Error", e);
76 return "One or more nodes are down or not responding.";
80 cleanHealthCheckId(randomUUID);
81 } catch (MusicServiceException | MusicQueryException e) {
82 logger.error("Error while cleaning healthcheck record id...", e);
87 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
92 private Boolean getAdminKeySpace(String consistency, UUID randomUUID) throws MusicServiceException,MusicQueryException {
93 PreparedQueryObject pQuery = new PreparedQueryObject();
94 pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
95 pQuery.addValue(randomUUID);
97 rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
98 logger.info(rs.toString());
103 private void cleanHealthCheckId(UUID randomUUID) throws MusicServiceException, MusicQueryException {
104 String cleanQuery = "delete from admin.healthcheck where id = ?";
105 PreparedQueryObject deleteQueryObject = new PreparedQueryObject();
106 deleteQueryObject.appendQueryString(cleanQuery);
107 deleteQueryObject.addValue(randomUUID);
108 MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "eventual");
109 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra healthcheck responded and cleaned up.");
114 private boolean createKeyspace() throws MusicServiceException,MusicQueryException {
115 PreparedQueryObject pQuery = new PreparedQueryObject();
116 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
117 ResultType rs = null ;
118 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
119 return rs != null && rs.getResult().toLowerCase().contains("success");
122 public String getCassandrHost() {
126 public void setCassandrHost(String cassandrHost) {
127 this.cassandrHost = cassandrHost;