aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/onap/so/utils/ExternalTaskServiceUtils.java
blob: 1a8307e8bcb9b0f27141d29d46263b85add284ac (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
package org.onap.so.utils;

import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.camunda.bpm.client.ExternalTaskClient;
import org.camunda.bpm.client.interceptor.ClientRequestInterceptor;
import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider;
import org.onap.logging.filter.base.ScheduledLogging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;



@Component
public class ExternalTaskServiceUtils {

    @Autowired
    public Environment env;


    private static final long DEFAULT_LOCK_DURATION_LONG = 2700000;
    private static final long DEFAULT_LOCK_DURATION_MEDIUM = 900000;
    private static final long DEFAULT_LOCK_DURATION_SHORT = 300000;

    private static final String LOCK_DURATION_LONG = "mso.workflow.topics.lockDurationLong";
    private static final String LOCK_DURATION_MEDIUM = "mso.workflow.topics.lockDurationMedium";
    private static final String LOCK_DURATION_SHORT = "mso.workflow.topics.lockDurationShort";

    protected Set<ExternalTaskClient> taskClients = ConcurrentHashMap.newKeySet();

    private static final Logger logger = LoggerFactory.getLogger(ExternalTaskServiceUtils.class);

    public ExternalTaskClient createExternalTaskClient() throws Exception {
        String auth = getAuth();
        ClientRequestInterceptor interceptor = createClientInterceptor(auth);
        ExternalTaskClient client =
                ExternalTaskClient.create().baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1)
                        .addInterceptor(interceptor).asyncResponseTimeout(120000).build();
        taskClients.add(client);
        return client;
    }

    protected ClientRequestInterceptor createClientInterceptor(String auth) {
        return new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth);
    }

    protected String getAuth() throws Exception {
        try {
            return CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey"));
        } catch (IllegalStateException | GeneralSecurityException e) {
            logger.error("Error Decrypting Password", e);
            throw new Exception("Cannot load password");
        }
    }

    public int getMaxClients() {
        return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "10"));
    }

    @ScheduledLogging
    @Scheduled(fixedDelay = 30000)
    public void checkAllClientsActive() {
        try {
            List<ExternalTaskClient> inactiveClients =
                    getClients().stream().filter(client -> !client.isActive()).collect(Collectors.toList());
            inactiveClients.forEach(c -> {
                c.start();
            });
        } catch (Exception e) {
            logger.error("Exception occured in checkAllClientsActive", e);
        }

    }

    protected Set<ExternalTaskClient> getClients() {
        return taskClients;
    }

    public long getLockDurationLong() {
        return env.getProperty(LOCK_DURATION_LONG, Long.class, new Long(DEFAULT_LOCK_DURATION_LONG));
    }

    public long getLockDurationMedium() {
        return env.getProperty(LOCK_DURATION_MEDIUM, Long.class, new Long(DEFAULT_LOCK_DURATION_MEDIUM));
    }

    public long getLockDurationShort() {
        return env.getProperty(LOCK_DURATION_SHORT, Long.class, new Long(DEFAULT_LOCK_DURATION_SHORT));
    }

}