33958a7850812225d943ac6d96a898a7f654d24e
[so.git] / common / src / main / java / org / onap / so / utils / ExternalTaskServiceUtils.java
1 package org.onap.so.utils;
2
3 import java.security.GeneralSecurityException;
4 import java.util.List;
5 import java.util.Set;
6 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.stream.Collectors;
8 import org.camunda.bpm.client.ExternalTaskClient;
9 import org.camunda.bpm.client.interceptor.ClientRequestInterceptor;
10 import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider;
11 import org.onap.logging.filter.base.ScheduledLogging;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.core.env.Environment;
16 import org.springframework.scheduling.annotation.Scheduled;
17 import org.springframework.stereotype.Component;
18
19
20
21 @Component
22 public class ExternalTaskServiceUtils {
23
24     @Autowired
25     public Environment env;
26
27
28     private static final long DEFAULT_LOCK_DURATION_LONG = 2700000;
29     private static final long DEFAULT_LOCK_DURATION_MEDIUM = 900000;
30     private static final long DEFAULT_LOCK_DURATION_SHORT = 300000;
31
32     private static final String LOCK_DURATION_LONG = "mso.workflow.topics.lockDurationLong";
33     private static final String LOCK_DURATION_MEDIUM = "mso.workflow.topics.lockDurationMedium";
34     private static final String LOCK_DURATION_SHORT = "mso.workflow.topics.lockDurationShort";
35
36     protected Set<ExternalTaskClient> taskClients = ConcurrentHashMap.newKeySet();
37
38     private static final Logger logger = LoggerFactory.getLogger(ExternalTaskServiceUtils.class);
39
40     public ExternalTaskClient createExternalTaskClient() throws Exception {
41         String auth = getAuth();
42         ClientRequestInterceptor interceptor = createClientInterceptor(auth);
43         ExternalTaskClient client =
44                 ExternalTaskClient.create().baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1)
45                         .addInterceptor(interceptor).asyncResponseTimeout(120000).build();
46         taskClients.add(client);
47         return client;
48     }
49
50     protected ClientRequestInterceptor createClientInterceptor(String auth) {
51         return new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth);
52     }
53
54     protected String getAuth() throws Exception {
55         try {
56             return CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey"));
57         } catch (IllegalStateException | GeneralSecurityException e) {
58             logger.error("Error Decrypting Password", e);
59             throw new Exception("Cannot load password");
60         }
61     }
62
63     public int getMaxClients() {
64         return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "10"));
65     }
66
67     public Long getLockDuration() {
68         Long lockDuration = Long.parseLong(env.getProperty("mso.audit.lock-time", "60000"));
69         return lockDuration;
70     }
71
72     public Long getLongLockDuration() {
73         Long lockDuration = Long.parseLong(env.getProperty("mso.long.lock-time", "600000"));
74         return lockDuration;
75     }
76
77     @ScheduledLogging
78     @Scheduled(fixedDelay = 30000)
79     public void checkAllClientsActive() {
80         try {
81             logger.debug("Executing scheduled task to check and restart external task clients"); // TODO remove
82                                                                                                  // eventually
83             List<ExternalTaskClient> inactiveClients =
84                     getClients().stream().filter(client -> !client.isActive()).collect(Collectors.toList());
85
86             inactiveClients.forEach(c -> {
87                 logger.debug("External Task Client found to be inactive. Restarting Client.");
88                 c.start();
89             });
90         } catch (Exception e) {
91             logger.error("Exception occured in checkAllClientsActive", e);
92         }
93
94     }
95
96     protected Set<ExternalTaskClient> getClients() {
97         return taskClients;
98     }
99
100     public long getLockDurationLong() {
101         return env.getProperty(LOCK_DURATION_LONG, Long.class, new Long(DEFAULT_LOCK_DURATION_LONG));
102     }
103
104     public long getLockDurationMedium() {
105         return env.getProperty(LOCK_DURATION_MEDIUM, Long.class, new Long(DEFAULT_LOCK_DURATION_MEDIUM));
106     }
107
108     public long getLockDurationShort() {
109         return env.getProperty(LOCK_DURATION_SHORT, Long.class, new Long(DEFAULT_LOCK_DURATION_SHORT));
110     }
111
112 }