dba724565f25418cc60b7f2de3a5fd6f4d88d7a9
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.health.impl;
18
19 import com.amdocs.zusammen.commons.health.data.HealthInfo;
20 import com.amdocs.zusammen.commons.health.data.HealthStatus;
21 import com.amdocs.zusammen.datatypes.SessionContext;
22 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
23 import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
24 import org.openecomp.core.zusammen.api.ZusammenUtil;
25 import org.openecomp.sdc.health.HealthCheckDao;
26 import org.openecomp.sdc.health.HealthCheckDaoFactory;
27 import org.openecomp.sdc.health.HealthCheckManager;
28 import org.openecomp.sdc.health.data.HealthCheckStatus;
29 import org.openecomp.sdc.health.data.MonitoredModules;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.stream.Collectors;
38
39 public class HealthCheckManagerImpl implements HealthCheckManager {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckManagerImpl.class);
42
43     private HealthCheckDao healthCheckDao;
44
45     public HealthCheckManagerImpl() {
46         healthCheckDao = HealthCheckDaoFactory.getInstance().createInterface();
47     }
48
49     public String getBEVersion() {
50         return this.getClass().getPackage().getImplementationVersion();
51     }
52
53     @Override
54     public Collection<org.openecomp.sdc.health.data.HealthInfo> checkHealth() {
55         org.openecomp.sdc.health.data.HealthInfo zeHealthInfo = null;
56         org.openecomp.sdc.health.data.HealthInfo beHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
57                 MonitoredModules.BE, HealthCheckStatus.UP, getBEVersion(), "OK");
58         org.openecomp.sdc.health.data.HealthInfo cassandraHealthInfo = null;
59         String zVersion = "Unknown";
60         try {
61             SessionContext context = ZusammenUtil.createSessionContext();
62             ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory
63                     .getInstance().createInterface();
64             Collection<HealthInfo> zeHealthInfos = new ArrayList<>();
65             try {
66                 zeHealthInfos = zusammenAdaptor.checkHealth(context);
67             } catch (Exception ex) {
68                 LOGGER.error(ex.getMessage(), ex);
69                 zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
70                         MonitoredModules.ZU, HealthCheckStatus.DOWN,
71                         zVersion, ex.getMessage());
72             }
73             boolean cassandraHealth = false;
74             String description = "OK";
75             try {
76                 cassandraHealth = healthCheckDao.checkHealth();
77             } catch (Exception ex) {
78                 LOGGER.error(ex.getMessage(), ex);
79                 description = ex.getMessage();
80                 cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
81                         MonitoredModules.CAS, HealthCheckStatus.DOWN, zVersion, ex.getMessage());
82             }
83             zVersion = zusammenAdaptor.getVersion(context);
84             if (cassandraHealthInfo == null) {
85                 HealthCheckStatus status = cassandraHealth ? HealthCheckStatus.UP : HealthCheckStatus.DOWN;
86                 if (!cassandraHealth){
87                     description = "Cassandra is not available";
88                 }
89                 cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(MonitoredModules.CAS, status,
90                         healthCheckDao.getVersion(), description);
91             }
92             if (zeHealthInfo == null) {
93                 List<HealthInfo> downHealth = zeHealthInfos.stream().
94                         filter(h -> h.getHealthStatus().equals(HealthStatus.DOWN)).
95                         collect(Collectors.toList());
96
97                 if (downHealth.isEmpty()) {
98                     zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
99                             MonitoredModules.ZU, HealthCheckStatus.UP,
100                             zVersion, "OK");
101                 } else {
102                     String desc = downHealth.stream().map(healthInfo -> healthInfo.getDescription())
103                             .collect(Collectors.joining(" , ", "[", "]"));
104                     zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
105                             MonitoredModules.ZU, HealthCheckStatus.DOWN,
106                             zVersion, desc);
107                 }
108
109             }
110         } catch (Exception e) {
111             LOGGER.error(e.getMessage(), e);
112             zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
113                     MonitoredModules.ZU, HealthCheckStatus.DOWN, zVersion, e.getMessage()
114             );
115             cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
116                     MonitoredModules.CAS, HealthCheckStatus.DOWN, zVersion, e.getMessage());
117         }
118         return Arrays.asList(zeHealthInfo, beHealthInfo, cassandraHealthInfo);
119     }
120
121
122 }
123