various Updates
[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                                 System.out.println("Creating table....");
60                                 boolean ksresult = createKeyspace();
61                                 if(ksresult)
62                                         try {
63                                                 result = getAdminKeySpace(consistency);
64                                         } catch (MusicServiceException e1) {
65                                                 // TODO Auto-generated catch block
66                                                 e1.printStackTrace();
67                                         }
68                         } else {
69                                 return "One or more nodes are down or not responding.";
70                         }
71                 }
72                 if (result) {
73                         return "ACTIVE";
74                 } else {
75                         logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
76                         return "INACTIVE";
77                 }
78         }
79
80         private Boolean getAdminKeySpace(String consistency) throws MusicServiceException {
81
82
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                         System.out.println(rs);
88                         if (rs != null) {
89                                 return Boolean.TRUE;
90                         } else {
91                                 return Boolean.FALSE;
92                         }
93
94
95         }
96         
97         private boolean createKeyspace() {
98                 PreparedQueryObject pQuery = new PreparedQueryObject();
99                 pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
100                 ResultType rs = null ;
101                 try {
102                         rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
103                 } catch (MusicServiceException e) {
104                         // TODO Auto-generated catch block
105                         e.printStackTrace();
106                 }
107                 if(rs.getResult().toLowerCase().contains("success"))
108                         return true;
109                 else
110                         return false;
111         }
112
113         public String getZookeeperStatus() {
114
115                 String host = MusicUtil.getMyZkHost();
116                 logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
117                 try {
118                         MusicLockingService lockingService = MusicCore.getLockingServiceHandle();
119                         // additionally need to call the ZK to create,aquire and delete lock
120                 } catch (MusicLockingException e) {
121                         logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
122                                         ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
123                         return "INACTIVE";
124                 }
125
126                 logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
127                 return "ACTIVE";
128
129                 // return "Zookeeper is not responding";
130
131         }
132
133         public String getCassandrHost() {
134                 return cassandrHost;
135         }
136
137         public void setCassandrHost(String cassandrHost) {
138                 this.cassandrHost = cassandrHost;
139         }
140
141         public String getZookeeperHost() {
142                 return zookeeperHost;
143         }
144
145         public void setZookeeperHost(String zookeeperHost) {
146                 this.zookeeperHost = zookeeperHost;
147         }
148
149 }