fbfc0de671c6be47de1f063474fa4abfa72acf0c
[music.git] / music-rest / 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.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;
40
41 import com.datastax.driver.core.ConsistencyLevel;
42
43 /**
44  * @author inam
45  *
46  */
47 public class MusicHealthCheck {
48
49     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
50
51     private String cassandrHost;
52
53     public String getCassandraStatus(String consistency) {
54         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
55         
56         boolean result = false;
57         UUID randomUUID = UUID.randomUUID();
58         try {
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....");
64                 try {
65                     boolean ksresult = createKeyspace();
66                     if(ksresult) {
67                         result = getAdminKeySpace(consistency, randomUUID);
68                     }
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);
73                 }
74             } else {
75                 logger.error("Error", e);
76                 return "One or more nodes are down or not responding.";
77             }
78         }
79         try {
80                         cleanHealthCheckId(randomUUID);
81                 } catch (MusicServiceException | MusicQueryException e) {
82                         logger.error("Error while cleaning healthcheck record id...", e);
83                 }
84         if (result) {
85             return "ACTIVE";
86         } else {
87             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
88             return "INACTIVE";
89         }
90     }
91
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);
96         ResultType rs = null;
97         rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
98         logger.info(rs.toString());
99         return null != rs;
100         
101     }
102
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.");
110         }
111     
112     
113     
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");
120     }
121
122     public String getCassandrHost() {
123         return cassandrHost;
124     }
125
126     public void setCassandrHost(String cassandrHost) {
127         this.cassandrHost = cassandrHost;
128     }
129
130 }