aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-healthcheck-manager/src/main/java/org/openecomp/sdc/health/impl/HealthCheckManagerImpl.java
blob: dba724565f25418cc60b7f2de3a5fd6f4d88d7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.health.impl;

import com.amdocs.zusammen.commons.health.data.HealthInfo;
import com.amdocs.zusammen.commons.health.data.HealthStatus;
import com.amdocs.zusammen.datatypes.SessionContext;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
import org.openecomp.core.zusammen.api.ZusammenUtil;
import org.openecomp.sdc.health.HealthCheckDao;
import org.openecomp.sdc.health.HealthCheckDaoFactory;
import org.openecomp.sdc.health.HealthCheckManager;
import org.openecomp.sdc.health.data.HealthCheckStatus;
import org.openecomp.sdc.health.data.MonitoredModules;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class HealthCheckManagerImpl implements HealthCheckManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckManagerImpl.class);

    private HealthCheckDao healthCheckDao;

    public HealthCheckManagerImpl() {
        healthCheckDao = HealthCheckDaoFactory.getInstance().createInterface();
    }

    public String getBEVersion() {
        return this.getClass().getPackage().getImplementationVersion();
    }

    @Override
    public Collection<org.openecomp.sdc.health.data.HealthInfo> checkHealth() {
        org.openecomp.sdc.health.data.HealthInfo zeHealthInfo = null;
        org.openecomp.sdc.health.data.HealthInfo beHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                MonitoredModules.BE, HealthCheckStatus.UP, getBEVersion(), "OK");
        org.openecomp.sdc.health.data.HealthInfo cassandraHealthInfo = null;
        String zVersion = "Unknown";
        try {
            SessionContext context = ZusammenUtil.createSessionContext();
            ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory
                    .getInstance().createInterface();
            Collection<HealthInfo> zeHealthInfos = new ArrayList<>();
            try {
                zeHealthInfos = zusammenAdaptor.checkHealth(context);
            } catch (Exception ex) {
                LOGGER.error(ex.getMessage(), ex);
                zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                        MonitoredModules.ZU, HealthCheckStatus.DOWN,
                        zVersion, ex.getMessage());
            }
            boolean cassandraHealth = false;
            String description = "OK";
            try {
                cassandraHealth = healthCheckDao.checkHealth();
            } catch (Exception ex) {
                LOGGER.error(ex.getMessage(), ex);
                description = ex.getMessage();
                cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                        MonitoredModules.CAS, HealthCheckStatus.DOWN, zVersion, ex.getMessage());
            }
            zVersion = zusammenAdaptor.getVersion(context);
            if (cassandraHealthInfo == null) {
                HealthCheckStatus status = cassandraHealth ? HealthCheckStatus.UP : HealthCheckStatus.DOWN;
                if (!cassandraHealth){
                    description = "Cassandra is not available";
                }
                cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(MonitoredModules.CAS, status,
                        healthCheckDao.getVersion(), description);
            }
            if (zeHealthInfo == null) {
                List<HealthInfo> downHealth = zeHealthInfos.stream().
                        filter(h -> h.getHealthStatus().equals(HealthStatus.DOWN)).
                        collect(Collectors.toList());

                if (downHealth.isEmpty()) {
                    zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                            MonitoredModules.ZU, HealthCheckStatus.UP,
                            zVersion, "OK");
                } else {
                    String desc = downHealth.stream().map(healthInfo -> healthInfo.getDescription())
                            .collect(Collectors.joining(" , ", "[", "]"));
                    zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                            MonitoredModules.ZU, HealthCheckStatus.DOWN,
                            zVersion, desc);
                }

            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            zeHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                    MonitoredModules.ZU, HealthCheckStatus.DOWN, zVersion, e.getMessage()
            );
            cassandraHealthInfo = new org.openecomp.sdc.health.data.HealthInfo(
                    MonitoredModules.CAS, HealthCheckStatus.DOWN, zVersion, e.getMessage());
        }
        return Arrays.asList(zeHealthInfo, beHealthInfo, cassandraHealthInfo);
    }


}