Merge "Introduced local variable"
[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
87
88         PreparedQueryObject pQuery = new PreparedQueryObject();
89         pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)");
90         pQuery.addValue(UUID.randomUUID());
91             ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency);
92             logger.info(rs.toString());
93             if (rs != null) {
94                 return Boolean.TRUE;
95             } else {
96                 return Boolean.FALSE;
97             }
98
99
100     }
101     
102     private boolean createKeyspace() {
103         PreparedQueryObject pQuery = new PreparedQueryObject();
104         pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
105         ResultType rs = null ;
106         try {
107             rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
108         } catch (MusicServiceException e) {
109             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
110         }
111         if(rs != null && rs.getResult().toLowerCase().contains("success"))
112             return true;
113         else
114             return false;
115     }
116
117     public String getZookeeperStatus() {
118
119         String host = MusicUtil.getMyZkHost();
120         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
121         try {
122             MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle();
123             // additionally need to call the ZK to create,aquire and delete lock
124         } catch (MusicLockingException e) {
125             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
126                     ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
127             return "INACTIVE";
128         }
129
130         logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
131         return "ACTIVE";
132
133     }
134
135     public String getCassandrHost() {
136         return cassandrHost;
137     }
138
139     public void setCassandrHost(String cassandrHost) {
140         this.cassandrHost = cassandrHost;
141     }
142
143     public String getZookeeperHost() {
144         return zookeeperHost;
145     }
146
147     public void setZookeeperHost(String zookeeperHost) {
148         this.zookeeperHost = zookeeperHost;
149     }
150
151 }