Sonar Fixes - CadiAuthFilter.java
[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
23 package org.onap.music.eelf.healthcheck;
24
25 import java.util.UUID;
26
27 import org.onap.music.datastore.PreparedQueryObject;
28 import org.onap.music.eelf.logging.EELFLoggerDelegate;
29 import org.onap.music.eelf.logging.format.AppMessages;
30 import org.onap.music.eelf.logging.format.ErrorSeverity;
31 import org.onap.music.eelf.logging.format.ErrorTypes;
32 import org.onap.music.exceptions.MusicLockingException;
33 import org.onap.music.exceptions.MusicServiceException;
34 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
35 import org.onap.music.main.MusicUtil;
36 import org.onap.music.main.ResultType;
37 import org.onap.music.service.impl.MusicZKCore;
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     private String zookeeperHost;
52
53     public String getCassandraStatus(String consistency) {
54         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra");
55         
56         boolean result = false;
57         try {
58             result = getAdminKeySpace(consistency);
59         } catch(Exception e) {
60             if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
61                 logger.error("Error", e);
62                 logger.debug("Creating table....");
63                 boolean ksresult = createKeyspace();
64                 if(ksresult)
65                     try {
66                         result = getAdminKeySpace(consistency);
67                     } catch (MusicServiceException e1) {
68                         // TODO Auto-generated catch block
69                         logger.error("Error", e);
70                         e1.printStackTrace();
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             // TODO Auto-generated catch block
110             e.printStackTrace();
111             logger.error("Error", e);
112         }
113         if(rs != null && rs.getResult().toLowerCase().contains("success"))
114             return true;
115         else
116             return false;
117     }
118
119     public String getZookeeperStatus() {
120
121         String host = MusicUtil.getMyZkHost();
122         logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host);
123         try {
124             MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle();
125             // additionally need to call the ZK to create,aquire and delete lock
126         } catch (MusicLockingException e) {
127             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
128                     ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
129             return "INACTIVE";
130         }
131
132         logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
133         return "ACTIVE";
134
135     }
136
137     public String getCassandrHost() {
138         return cassandrHost;
139     }
140
141     public void setCassandrHost(String cassandrHost) {
142         this.cassandrHost = cassandrHost;
143     }
144
145     public String getZookeeperHost() {
146         return zookeeperHost;
147     }
148
149     public void setZookeeperHost(String zookeeperHost) {
150         this.zookeeperHost = zookeeperHost;
151     }
152
153 }