Implementation of HealthCheckBB
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / CNFAdapterAsync.groovy
1 package org.onap.so.bpmn.common.scripts
2
3 import javax.ws.rs.core.Response
4
5 import org.camunda.bpm.engine.delegate.DelegateExecution
6 import org.slf4j.Logger
7 import org.slf4j.LoggerFactory
8
9 import com.fasterxml.jackson.databind.ObjectMapper
10
11 import org.onap.so.bpmn.core.UrnPropertiesReader
12 import org.onap.logging.filter.base.ONAPComponents
13 import org.onap.so.client.HttpClient
14 import org.onap.so.client.HttpClientFactory
15
16 import static org.onap.so.bpmn.common.scripts.GenericUtils.isBlank
17
18 public class CNFAdapterAsync extends AbstractServiceTaskProcessor {
19         private static final Logger logger = LoggerFactory.getLogger(CNFAdapterAsync.class)
20
21         ExceptionUtil exceptionUtil = new ExceptionUtil()
22         ObjectMapper mapper = new ObjectMapper();
23
24         @Override
25         public void preProcessRequest(DelegateExecution execution) {
26                 logger.debug("Start preProcessRequest");
27
28                 String apiPath = execution.getVariable("apiPath")
29                 if (isBlank(apiPath)) {
30                         String msg = "Cannot process CNF adapter call : API PATH is null"
31                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
32                 }
33
34                 //Object cnfRequestPayloadFromExecutionVariable = execution.getVariable("cnfRequestPayload")
35
36                 String cnfRequestPayload = execution.getVariable("cnfRequestPayload")
37                 if (isBlank(cnfRequestPayload)) {
38                         String msg = "Cannot process CNF adapter call : cnfRequestPayload is null"
39                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
40                 }
41
42                 String correlator = execution.getVariable("correlator")
43                 if (isBlank(correlator)) {
44                         String msg = "Cannot process CNF adapter call : correlator is null"
45                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
46                 }
47
48                 String messageType = execution.getVariable("messageType")
49                 if (isBlank(messageType)) {
50                         String msg = "Cannot process CNF adapter call : messageType is null"
51                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
52                 }
53
54                 String timeout = UrnPropertiesReader.getVariable("mso.adapters.cnf.timeout", execution);
55                 if (isBlank(timeout)) {
56                         logger.debug("Setting CNF Adapter timeout to default : PT30M")
57                         timeout = "PT30M"
58                 }
59
60                 execution.setVariable("timeout",timeout)
61
62                 logger.debug("Enter preProcessRequest: {}",execution.getVariable("messageType"));
63         }
64
65         void callCnfAdapter(DelegateExecution execution) {
66                 logger.debug("Start callCnfAdapter")
67                 String cnfAdapterEndpoint = execution.getVariable("apiPath")
68                 URL requestUrl = new URL(cnfAdapterEndpoint)
69                 String cnfRequest = execution.getVariable("cnfRequestPayload")
70                 logger.debug("cnfRequest : " + cnfRequest)
71                 HttpClient httpClient = new HttpClientFactory().newJsonClient(requestUrl, ONAPComponents.EXTERNAL)
72                 Response httpResponse = httpClient.post(cnfRequest)
73                 int responseCode = httpResponse.getStatus()
74                 logger.debug("CNF sync response code is: " + responseCode)
75                 if(responseCode < 200 || responseCode >= 300){
76                         exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from CNF.")
77                 }
78                 logger.debug("End callCnfAdapter")
79         }
80 }