079fc579f5348aeb64f322c1e7a7de987b84c0a4
[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.PreparedQueryObject;
30 import org.onap.music.eelf.logging.EELFLoggerDelegate;
31 import org.onap.music.eelf.logging.format.AppMessages;
32 import org.onap.music.eelf.logging.format.ErrorSeverity;
33 import org.onap.music.eelf.logging.format.ErrorTypes;
34 import org.onap.music.exceptions.MusicLockingException;
35 import org.onap.music.exceptions.MusicServiceException;
36 import org.onap.music.main.MusicUtil;
37 import org.onap.music.main.ResultType;
38 import org.onap.music.main.MusicCore;
39
40 import com.datastax.driver.core.ConsistencyLevel;
41
42 /**
43  * @author inam
44  *
45  */
46 public class MusicHealthCheck {
47
48     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
49
50     private String cassandrHost;
51
52     public String getCassandraStatus(String consistency) {
53         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
54         
55         boolean result = false;
56         try {
57             result = getAdminKeySpace(consistency);
58         } catch(Exception e) {
59             if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
60                 logger.error("Error", e);
61                 logger.debug("Creating table....");
62                 boolean ksresult = createKeyspace();
63                 if(ksresult)
64                     try {
65                         result = getAdminKeySpace(consistency);
66                     } catch (MusicServiceException e1) {
67                       logger.error(EELFLoggerDelegate.errorLogger, e1, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
68                     }
69             } else {
70                 logger.error("Error", e);
71                 return "One or more nodes are down or not responding.";
72             }
73         }
74         if (result) {
75             return "ACTIVE";
76         } else {
77             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
78             return "INACTIVE";
79         }
80     }
81
82     private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
83         PreparedQueryObject pQuery = new PreparedQueryObject();
84         pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
85         pQuery.addValue(UUID.randomUUID());
86         ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
87         logger.info(rs.toString());
88         return null != rs;
89     }
90     
91     private boolean createKeyspace() {
92         PreparedQueryObject pQuery = new PreparedQueryObject();
93         pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
94         ResultType rs = null ;
95         try {
96             rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
97         } catch (MusicServiceException e) {
98             logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
99         }
100         return rs != null && rs.getResult().toLowerCase().contains("success");
101     }
102
103     public String getCassandrHost() {
104         return cassandrHost;
105     }
106
107     public void setCassandrHost(String cassandrHost) {
108         this.cassandrHost = cassandrHost;
109     }
110
111 }