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....");
66 boolean ksresult = createKeyspace();
68 result = getAdminKeySpace(consistency, randomUUID);
70 } catch (MusicServiceException e1) {
71 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
72 } catch (MusicQueryException e1) {
73 logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN,e1);
76 logger.error("Error", e);
77 return "One or more nodes are down or not responding.";
81 cleanHealthCheckId(randomUUID);
82 } catch (MusicServiceException | MusicQueryException e) {
83 // TODO Auto-generated catch block
84 logger.error("Error while cleaning healthcheck record id...", e);
89 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
94 private Boolean getAdminKeySpace(String consistency, UUID randomUUID) throws MusicServiceException,MusicQueryException {
95 PreparedQueryObject pQuery = new PreparedQueryObject();
96 pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
97 pQuery.addValue(randomUUID);
99 rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
100 logger.info(rs.toString());
105 private void cleanHealthCheckId(UUID randomUUID) throws MusicServiceException, MusicQueryException {
106 String cleanQuery = "delete from admin.healthcheck where id = ?";
107 PreparedQueryObject deleteQueryObject = new PreparedQueryObject();
108 deleteQueryObject.appendQueryString(cleanQuery);
109 deleteQueryObject.addValue(randomUUID);
110 MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "eventual");
111 logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra healthcheck responded and cleaned up.");
116 private boolean createKeyspace() throws MusicServiceException,MusicQueryException {
117 PreparedQueryObject pQuery = new PreparedQueryObject();
118 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
119 ResultType rs = null ;
120 rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
121 return rs != null && rs.getResult().toLowerCase().contains("success");
124 public String getCassandrHost() {
128 public void setCassandrHost(String cassandrHost) {
129 this.cassandrHost = cassandrHost;