Merge "JsonInser.java : fixed sonar issue"
[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  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.eelf.healthcheck;
23
24 import java.util.UUID;
25
26 import org.onap.music.datastore.PreparedQueryObject;
27 import org.onap.music.eelf.logging.EELFLoggerDelegate;
28 import org.onap.music.eelf.logging.format.AppMessages;
29 import org.onap.music.eelf.logging.format.ErrorSeverity;
30 import org.onap.music.eelf.logging.format.ErrorTypes;
31 import org.onap.music.exceptions.MusicLockingException;
32 import org.onap.music.exceptions.MusicServiceException;
33 import org.onap.music.lockingservice.MusicLockingService;
34 import org.onap.music.main.MusicCore;
35 import org.onap.music.main.MusicUtil;
36 import org.onap.music.main.ResultType;
37
38 import com.datastax.driver.core.ConsistencyLevel;
39
40 /**
41  * @author inam
42  *
43  */
44 public class MusicHealthCheck {
45
46         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
47
48         private String cassandrHost;
49         private String zookeeperHost;
50
51         public String getCassandraStatus(String consistency) {
52                 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
53                 
54                 boolean result = false;
55                 try {
56                         result = getAdminKeySpace(consistency);
57                 } catch(Exception e) {
58                         if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
59                                 logger.error("Error", e);
60                                 logger.debug("Creating table....");
61                                 boolean ksresult = createKeyspace();
62                                 if(ksresult)
63                                         try {
64                                                 result = getAdminKeySpace(consistency);
65                                         } catch (MusicServiceException e1) {
66                                                 // TODO Auto-generated catch block
67                                                 logger.error("Error", e);
68                                                 e1.printStackTrace();
69                                         }
70                         } else {
71                                 logger.error("Error", e);
72                                 return "One or more nodes are down or not responding.";
73                         }
74                 }
75                 if (result) {
76                         return "ACTIVE";
77                 } else {
78                         logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
79                         return "INACTIVE";
80                 }
81         }
82
83         private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
84
85
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                         System.out.println(rs);
91                         if (rs != null) {
92                                 return Boolean.TRUE;
93                         } else {
94                                 return Boolean.FALSE;
95                         }
96
97
98         }
99         
100         private boolean createKeyspace() {
101                 PreparedQueryObject pQuery = new PreparedQueryObject();
102                 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
103                 ResultType rs = null ;
104                 try {
105                         rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
106                 } catch (MusicServiceException e) {
107                         // TODO Auto-generated catch block
108                         e.printStackTrace();
109                         logger.error("Error", e);
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 = MusicCore.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 }