cf4b2bc9c6af6ae34a1486659c5356c6e7fd3f5c
[music.git] / src / main / java / org / onap / music / eelf / healthcheck / MusicHealthCheck.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *  
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
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.eelf.healthcheck;
26
27 import java.util.UUID;
28
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;
41
42 import com.datastax.driver.core.ConsistencyLevel;
43
44 /**
45  * @author inam
46  *
47  */
48 public class MusicHealthCheck {
49
50     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
51
52     private String cassandrHost;
53
54     public String getCassandraStatus(String consistency) {
55         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
56         
57         boolean result = false;
58         UUID randomUUID = UUID.randomUUID();
59         try {
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                 try {
66                     boolean ksresult = createKeyspace();
67                     if(ksresult) {
68                         result = getAdminKeySpace(consistency, randomUUID);
69                     }
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);
74                 }
75             } else {
76                 logger.error("Error", e);
77                 return "One or more nodes are down or not responding.";
78             }
79         }
80         try {
81                         cleanHealthCheckId(randomUUID);
82                 } catch (MusicServiceException | MusicQueryException e) {
83                         // TODO Auto-generated catch block
84                         logger.error("Error while cleaning healthcheck record id...", e);
85                 }
86         if (result) {
87             return "ACTIVE";
88         } else {
89             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
90             return "INACTIVE";
91         }
92     }
93
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);
98         ResultType rs = null;
99         rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
100         logger.info(rs.toString());
101         return null != rs;
102         
103     }
104
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.");
112         }
113     
114     
115     
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");
122     }
123
124     public String getCassandrHost() {
125         return cassandrHost;
126     }
127
128     public void setCassandrHost(String cassandrHost) {
129         this.cassandrHost = cassandrHost;
130     }
131
132 }