[SDC] rebase 1710 code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / CassandraHealthCheck.java
1 package org.openecomp.sdc.be.components.impl;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Properties;
13 import java.util.Set;
14
15 import javax.annotation.PostConstruct;
16
17 import org.openecomp.sdc.be.config.ConfigurationManager;
18 import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils;
19 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
20 import org.openecomp.sdc.common.util.GeneralUtility;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.stereotype.Component;
24
25 import com.datastax.driver.core.Cluster;
26 import com.datastax.driver.core.KeyspaceMetadata;
27 import com.datastax.driver.core.Metadata;
28 import com.datastax.driver.core.Session;
29
30 @Component("cassandra-health-check")
31 public class CassandraHealthCheck {
32         
33         
34         private static Logger log = LoggerFactory.getLogger(CassandraHealthCheck.class.getName());
35         
36         private String localDataCenterName = null;
37         
38         private Set<String> sdcKeyspaces = new HashSet<String>();
39         
40         private int HC_FormulaNumber;
41         
42         @PostConstruct
43         private void init() {
44                 
45                 //Initialize local data center name - this field must be filled by DevOps
46                 localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
47                 
48                 if (GeneralUtility.isEmptyString(localDataCenterName))  {
49                         log.error("localDataCenter Name in configuration.yaml is missing.");
50                         return;
51                 }
52                 
53                 //Collect all SDC keyspaces
54                 for (Table table : Table.values()) {
55                         sdcKeyspaces.add(table.getTableDescription().getKeyspace());    
56                 }
57                 
58                 String titanCfgFile = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanCfgFile();
59                 Properties prop = new Properties();
60                 InputStream titanProp = null;
61                 try {
62                         //load a properties file
63                         titanProp = new FileInputStream(titanCfgFile);
64                         prop.load(titanProp);
65                         //Add titan keyspace
66                         String titanKeyspace = prop.getProperty("storage.cassandra.keyspace");
67                         if (!GeneralUtility.isEmptyString(titanKeyspace))  {
68                                 sdcKeyspaces.add(titanKeyspace);
69                         }
70                 } catch (Exception e) {
71                         log.error("Failed to open titen.properties file , url is : {}", titanCfgFile);
72                 }
73         
74                 log.info("All sdc keyspaces are : {}", sdcKeyspaces);
75                 
76                 //Calculate the Formula of Health Check
77                 Cluster cluster = null;
78                 try {
79                         
80                         log.info("creating cluster for Cassandra Health Check.");
81                         //Create cluster from nodes in cassandra configuration
82                         cluster = SdcSchemaUtils.createCluster();
83                         if (cluster == null) {
84                                 log.error("Failure create cassandra cluster.");
85                                 return;
86                         }
87                         
88                         Metadata metadata = cluster.getMetadata();
89                         
90                         if (metadata == null) {
91                                 log.error("Failure get cassandra metadata.");
92                                 return;
93                         }
94                         
95                         log.info("Cluster Metadata: {}", metadata.toString());
96                         List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
97                         List<Integer> replactionFactorList = new ArrayList<Integer>();
98                         
99                         //Collect the keyspaces Replication Factor of current localDataCenter
100                         for (KeyspaceMetadata keyspace : keyspaces) {
101                                 
102                                 if (sdcKeyspaces.contains(keyspace.getName()))  {
103                                         
104                                         log.info("keyspace : {} , replication: {}",  keyspace.getName(), keyspace.getReplication());
105                                         Map<String, String> replicationOptions = keyspace.getReplication();
106                                         
107                                         //In 1 site with one data center
108                                         if (replicationOptions.containsKey("replication_factor")) {
109                                                 replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
110                                         }
111                                         //In multiple sites with some data center
112                                         else if (replicationOptions.containsKey(localDataCenterName)) {
113                                                 replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
114                                         }
115                                 }
116                         }
117                         
118                         if (replactionFactorList.size() == 0)  {
119                                 log.error("Replication factor NOT found in all keyspaces");
120                                 return;
121                         }
122                         
123                         int maxReplicationFactor = Collections.max(replactionFactorList);
124                         log.info("maxReplication Factor is: {}", maxReplicationFactor);
125                         
126                         int localQuorum = maxReplicationFactor/2 + 1;
127                         log.info("localQuorum is: {}", localQuorum);
128                         
129                         HC_FormulaNumber = maxReplicationFactor - localQuorum;
130                         
131                         log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
132                         
133                 
134                 } catch (Exception e) {
135                         log.error("create cassandra cluster failed with exception.", e);
136                 } finally {
137                         if (cluster != null) {
138                                 cluster.close();
139                         }
140                 }
141                 
142         }
143         
144         public boolean getCassandraStatus()  {
145                 
146                 if (GeneralUtility.isEmptyString(localDataCenterName)) {
147                         log.error("localDataCenter Name in configuration.yaml is missing.");
148                         return false;
149                 }
150                 
151                 Cluster cluster = null;
152                 Session session = null;
153                 try {
154                         log.info("creating cluster for Cassandra for monitoring.");
155                         cluster = SdcSchemaUtils.createCluster();
156                         if (cluster == null) {
157                                 log.error("Failure create cassandra cluster.");
158                                 return false;
159                         }
160                         session = cluster.connect();
161                         Metadata metadata = cluster.getMetadata();
162                         
163                         if (metadata == null) {
164                                 log.error("Failure get cassandra metadata.");
165                                 return false;
166                         }
167                         
168                         log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
169                         
170                         //Count the number of data center nodes that are down
171                         Long downHostsNumber = metadata.getAllHosts().stream()
172                                         .filter(x -> x.getDatacenter().equals(localDataCenterName) && !x.isUp()).count();
173                         
174                         log.info("The cassandra down nodes number is {}", downHostsNumber.toString());
175                         return (HC_FormulaNumber >= downHostsNumber);
176                         
177                 } catch (Exception e) {
178                         log.error("create cassandra cluster failed with exception.", e);
179                         return false;
180                 } finally {
181                         if (session != null) {
182                 session.close();
183             }
184                         if (cluster != null) {
185                                 cluster.close();
186                         }
187                 }
188         }
189 }