1f9fe5bad4b6c7787c5a9cee311e33f22b668921
[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.lockingservice.zookeeper.MusicLockingService;
37 import org.onap.music.main.MusicUtil;
38 import org.onap.music.main.ResultType;
39 import org.onap.music.service.impl.MusicZKCore;
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     private String zookeeperHost;
54
55     public String getCassandraStatus(String consistency) {
56         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
57         
58         boolean result = false;
59         try {
60             result = getAdminKeySpace(consistency);
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);
69                     } catch (MusicServiceException e1) {
70                       logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),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         if (result) {
78             return "ACTIVE";
79         } else {
80             logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
81             return "INACTIVE";
82         }
83     }
84
85     private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
86         PreparedQueryObject pQuery = new PreparedQueryObject();
87         pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
88         pQuery.addValue(UUID.randomUUID());
89         ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
90         logger.info(rs.toString());
91         return null != rs;
92     }
93     
94     private boolean createKeyspace() {
95         PreparedQueryObject pQuery = new PreparedQueryObject();
96         pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
97         ResultType rs = null ;
98         try {
99             rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
100         } catch (MusicServiceException e) {
101             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
102         }
103         return rs != null && rs.getResult().toLowerCase().contains("success");
104     }
105
106     public String getZookeeperStatus() {
107
108         String host = MusicUtil.getMyZkHost();
109         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
110         try {
111             MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle();
112             // additionally need to call the ZK to create,aquire and delete lock
113         } catch (MusicLockingException e) {
114             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
115                     ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
116             return "INACTIVE";
117         }
118
119         logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
120         return "ACTIVE";
121
122     }
123
124     public String getCassandrHost() {
125         return cassandrHost;
126     }
127
128     public void setCassandrHost(String cassandrHost) {
129         this.cassandrHost = cassandrHost;
130     }
131
132     public String getZookeeperHost() {
133         return zookeeperHost;
134     }
135
136     public void setZookeeperHost(String zookeeperHost) {
137         this.zookeeperHost = zookeeperHost;
138     }
139
140 }