1 package org.onap.so.utils;
3 import java.security.GeneralSecurityException;
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;
22 public class ExternalTaskServiceUtils {
25 public Environment env;
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;
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";
36 protected Set<ExternalTaskClient> taskClients = ConcurrentHashMap.newKeySet();
38 private static final Logger logger = LoggerFactory.getLogger(ExternalTaskServiceUtils.class);
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);
50 protected ClientRequestInterceptor createClientInterceptor(String auth) {
51 return new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth);
54 protected String getAuth() throws Exception {
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");
63 public int getMaxClients() {
64 return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "10"));
67 public Long getLockDuration() {
68 Long lockDuration = Long.parseLong(env.getProperty("mso.audit.lock-time", "60000"));
72 public Long getLongLockDuration() {
73 Long lockDuration = Long.parseLong(env.getProperty("mso.long.lock-time", "600000"));
78 @Scheduled(fixedDelay = 30000)
79 public void checkAllClientsActive() {
81 logger.debug("Executing scheduled task to check and restart external task clients"); // TODO remove
83 List<ExternalTaskClient> inactiveClients =
84 getClients().stream().filter(client -> !client.isActive()).collect(Collectors.toList());
86 inactiveClients.forEach(c -> {
87 logger.debug("External Task Client found to be inactive. Restarting Client.");
90 } catch (Exception e) {
91 logger.error("Exception occured in checkAllClientsActive", e);
96 protected Set<ExternalTaskClient> getClients() {
100 public long getLockDurationLong() {
101 return env.getProperty(LOCK_DURATION_LONG, Long.class, new Long(DEFAULT_LOCK_DURATION_LONG));
104 public long getLockDurationMedium() {
105 return env.getProperty(LOCK_DURATION_MEDIUM, Long.class, new Long(DEFAULT_LOCK_DURATION_MEDIUM));
108 public long getLockDurationShort() {
109 return env.getProperty(LOCK_DURATION_SHORT, Long.class, new Long(DEFAULT_LOCK_DURATION_SHORT));