Added logging and try/catch to external task.
[so.git] / common / src / main / java / org / onap / so / utils / ExternalTaskUtils.java
1 package org.onap.so.utils;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.core.env.Environment;
7 import org.springframework.stereotype.Component;
8
9 @Component
10 public abstract class ExternalTaskUtils {
11
12     private static final Logger logger = LoggerFactory.getLogger(ExternalTaskUtils.class);
13
14     @Autowired
15     Environment env;
16
17     private final RetrySequenceLevel retrySequenceLevel;
18
19     public ExternalTaskUtils() {
20         this.retrySequenceLevel = RetrySequenceLevel.MEDIUM;
21     }
22
23     public ExternalTaskUtils(RetrySequenceLevel retrySequenceLevel) {
24         this.retrySequenceLevel = retrySequenceLevel;
25     }
26
27     public long calculateRetryDelay(int currentRetries) {
28         int retrySequence = getRetrySequence().length - currentRetries;
29         return Integer.parseInt(getRetrySequence()[retrySequence]) * getRetryMutiplier();
30     }
31
32     protected Long getRetryMutiplier() {
33         return Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier", "6000"));
34     }
35
36     protected String[] getRetrySequence() {
37         switch (retrySequenceLevel) {
38             case SHORT:
39                 String[] seqShort = {"1", "1"};
40                 if (env.getProperty("mso.workflow.topics.retrySequence.short") != null) {
41                     seqShort = env.getProperty("mso.workflow.topics.retrySequence.short", String[].class);
42                 }
43                 return seqShort;
44             case MEDIUM:
45                 String[] seqInter = {"1", "1", "2", "3", "5"};
46                 if (env.getProperty("mso.workflow.topics.retrySequence.medium") != null) {
47                     seqInter = env.getProperty("mso.workflow.topics.retrySequence.medium", String[].class);
48                 }
49                 return seqInter;
50             case LONG:
51                 String[] seqLong = {"1", "1", "2", "3", "5", "8", "13", "20"};
52                 if (env.getProperty("mso.workflow.topics.retrySequence.long") != null) {
53                     seqLong = env.getProperty("mso.workflow.topics.retrySequence", String[].class);
54                 }
55                 return seqLong;
56             default:
57                 String[] seq = {"1"};
58                 return seq;
59         }
60
61     }
62
63 }