aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/scheduledtasks/RecoveryThreadManager.java
blob: 594e0b9418e8240e5f59f6a6ffddc29eb18c8812 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.components.scheduledtasks;

import static org.apache.commons.collections.CollectionUtils.isEmpty;
import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.convertToFunction;

import com.google.common.annotations.VisibleForTesting;
import fj.data.Either;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.openecomp.sdc.be.components.distribution.engine.EnvironmentsEngine;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
import org.openecomp.sdc.common.datastructure.Wrapper;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("recoveryThreadManager")
public class RecoveryThreadManager extends AbstractScheduleTaskRunner {

    private static final Logger log = Logger.getLogger(RecoveryThreadManager.class);
    @VisibleForTesting
    FixEnvironmentTask task = new FixEnvironmentTask();
    @VisibleForTesting
    Integer allowedTimeBeforeStaleSec;
    @Resource
    private OperationalEnvironmentDao operationalEnvironmentDao;
    @Autowired
    private EnvironmentsEngine environmentsEngine;
    private ScheduledExecutorService scheduledService = Executors
        .newScheduledThreadPool(NumberUtils.INTEGER_ONE, new BasicThreadFactory.Builder().namingPattern("EnvironmentCleanThread-%d").build());

    @PostConstruct
    public void init() {
        log.debug("Enter init method of RecoveryThreadManager");
        final DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager()
            .getDistributionEngineConfiguration();
        Integer opEnvRecoveryIntervalSec = distributionEngineConfiguration.getOpEnvRecoveryIntervalSec();
        scheduledService.scheduleAtFixedRate(task, NumberUtils.INTEGER_ZERO, opEnvRecoveryIntervalSec, TimeUnit.SECONDS);
        this.allowedTimeBeforeStaleSec = distributionEngineConfiguration.getAllowedTimeBeforeStaleSec();
        log.debug("End init method of AsdcComponentsCleaner");
    }

    @PreDestroy
    public void destroy() {
        shutdownExecutor();
    }

    @Override
    public ExecutorService getExecutorService() {
        return scheduledService;
    }

    protected class FixEnvironmentTask implements Runnable {

        @Override
        public void run() {
            try {
                // Failed Envs
                Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> eitherFailedEnv = operationalEnvironmentDao
                    .getByEnvironmentsStatus(EnvironmentStatusEnum.FAILED);
                eitherFailedEnv.bimap(convertToFunction(this::handleFailedeEnvironmentsRecords),
                    convertToFunction(cassandraError -> logFailedRetrieveRecord(EnvironmentStatusEnum.FAILED, cassandraError)));
                // In-Progress Envs
                Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> eitherInProgressEnv = operationalEnvironmentDao
                    .getByEnvironmentsStatus(EnvironmentStatusEnum.IN_PROGRESS);
                eitherInProgressEnv.bimap(convertToFunction(this::handleInProgressEnvironmentsRecords),
                    convertToFunction(cassandraError -> logFailedRetrieveRecord(EnvironmentStatusEnum.IN_PROGRESS, cassandraError)));
                // Envs To Connect to UEB topics
                Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> eitherCompleteEnv = operationalEnvironmentDao
                    .getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED);
                eitherCompleteEnv.bimap(convertToFunction(this::handleCompleteEnvironmentsRecords),
                    convertToFunction(cassandraError -> logFailedRetrieveRecord(EnvironmentStatusEnum.COMPLETED, cassandraError)));
            } catch (Exception e) {
                log.debug("error while handling operational environments to be fixed :{}", e.getMessage(), e);
            }
        }

        private void handleCompleteEnvironmentsRecords(List<OperationalEnvironmentEntry> completeEnvironmentsRecords) {
            if (!isEmpty(completeEnvironmentsRecords)) {
                completeEnvironmentsRecords.stream().filter(env -> !environmentsEngine.isInMap(env)).forEach(opEnvEntry -> {
                    environmentsEngine.createUebTopicsForEnvironment(opEnvEntry);
                    environmentsEngine.addToMap(opEnvEntry);
                });
            }
        }

        private void handleFailedeEnvironmentsRecords(List<OperationalEnvironmentEntry> failedEnvironmentsRecords) {
            if (!isEmpty(failedEnvironmentsRecords)) {
                failedEnvironmentsRecords.parallelStream().forEach(env -> environmentsEngine.buildOpEnv(new Wrapper<>(), env));
            }
        }

        private void handleInProgressEnvironmentsRecords(List<OperationalEnvironmentEntry> inProgressEnvList) {
            if (!isEmpty(inProgressEnvList)) {
                long currentTimeMillis = System.currentTimeMillis();
                if (!isEmpty(inProgressEnvList)) {
                    List<OperationalEnvironmentEntry> staleInProgressEnvList = inProgressEnvList.stream()
                        .filter(record -> (record.getLastModified().getTime() + (allowedTimeBeforeStaleSec * 1000)) < currentTimeMillis)
                        .collect(Collectors.toList());
                    staleInProgressEnvList.parallelStream().forEach(env -> environmentsEngine.buildOpEnv(new Wrapper<>(), env));
                }
            }
        }

        private void logFailedRetrieveRecord(EnvironmentStatusEnum recordStatus, CassandraOperationStatus error) {
            log.debug("error: {} while retrieving operational environments with status: {}", error, recordStatus);
        }
    }
}