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

import java.security.GeneralSecurityException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
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;

    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"));
    }

    public Long getLockDuration() {
        Long lockDuration = Long.parseLong(env.getProperty("mso.audit.lock-time", "60000"));
        return lockDuration;
    }

    @ScheduledLogging
    @Scheduled(fixedDelay = 30000)
    public void checkAllClientsActive() {
        getClients().stream().filter(client -> !client.isActive()).forEach(ExternalTaskClient::start);
    }

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

}