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.MusicLockingException;
36 import org.onap.music.exceptions.MusicQueryException;
37 import org.onap.music.exceptions.MusicServiceException;
38 import org.onap.music.main.MusicUtil;
39 import org.onap.music.main.ResultType;
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;
54 public String getCassandraStatus(String consistency) {
55 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
57 boolean result = false;
58 UUID randomUUID = UUID.randomUUID();
60 result = getAdminKeySpace(consistency, randomUUID);
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, randomUUID);
69 } catch (MusicServiceException e1) {
70 logger.error(EELFLoggerDelegate.errorLogger, e1, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
73 logger.error("Error", e);
74 return "One or more nodes are down or not responding.";
78 cleanHealthCheckId(randomUUID);
79 } catch (MusicServiceException | MusicQueryException e) {
80 // TODO Auto-generated catch block
81 logger.error("Error while cleaning healthcheck record id...", e);
86 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
91 private Boolean getAdminKeySpace(String consistency, UUID randomUUID) throws MusicServiceException {
92 PreparedQueryObject pQuery = new PreparedQueryObject();
93 pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
94 pQuery.addValue(randomUUID);
95 ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
96 logger.info(rs.toString());
101 private void cleanHealthCheckId(UUID randomUUID) throws MusicServiceException, MusicQueryException {
102 String cleanQuery = "delete from admin.healthcheck where id = ?";
103 PreparedQueryObject deleteQueryObject = new PreparedQueryObject();
104 deleteQueryObject.appendQueryString(cleanQuery);
105 deleteQueryObject.addValue(randomUUID);
106 MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "eventual");
107 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra healthcheck responded and cleaned up.");
112 private boolean createKeyspace() {
113 PreparedQueryObject pQuery = new PreparedQueryObject();
114 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
115 ResultType rs = null ;
117 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
118 } catch (MusicServiceException e) {
119 logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
121 return rs != null && rs.getResult().toLowerCase().contains("success");
124 public String getCassandrHost() {
128 public void setCassandrHost(String cassandrHost) {
129 this.cassandrHost = cassandrHost;