df6cc26591e7e47b268135397444b94cb270415a
[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                 boolean ksresult = createKeyspace();
66                 if(ksresult)
67                     try {
68                         result = getAdminKeySpace(consistency, randomUUID);
69                     } catch (MusicServiceException e1) {
70                         logger.error(EELFLoggerDelegate.errorLogger, e1, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
71                     }
72             } else {
73                 logger.error("Error", e);
74                 return "One or more nodes are down or not responding.";
75             }
76         }
77         try {
78                         cleanHealthCheckId(randomUUID);
79                 } catch (MusicServiceException | MusicQueryException e) {
80                         // TODO Auto-generated catch block
81                         logger.error("Error while cleaning healthcheck record id...", e);
82                 }
83         if (result) {
84             return "ACTIVE";
85         } else {
86             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
87             return "INACTIVE";
88         }
89     }
90
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());
97         return null != rs;
98         
99     }
100
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.");
108         }
109     
110     
111     
112     private boolean createKeyspace() {
113         PreparedQueryObject pQuery = new PreparedQueryObject();
114         pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
115         ResultType rs = null ;
116         try {
117             rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
118         } catch (MusicServiceException e) {
119             logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
120         }
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 }