58c6db109cbe7b8fe2b37764144286a9b24995ac
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCRequestTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.sdnc.tasks;
22
23 import org.camunda.bpm.engine.delegate.DelegateExecution;
24 import org.onap.so.client.exception.BadResponseException;
25 import org.onap.so.client.exception.ExceptionBuilder;
26 import org.onap.so.client.exception.MapperException;
27 import org.onap.so.client.sdnc.SDNCClient;
28 import org.onap.so.client.sdnc.beans.SDNCRequest;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 import com.jayway.jsonpath.JsonPath;
35 import com.jayway.jsonpath.PathNotFoundException;
36  
37 @Component
38 public class SDNCRequestTasks {
39         
40         private static final Logger logger = LoggerFactory.getLogger(SDNCRequestTasks.class);
41         
42         private static final String SDNC_REQUEST = "SDNCRequest";
43         private static final String MESSAGE = "_MESSAGE";
44         private static final String CORRELATOR = "_CORRELATOR";
45         protected static final String IS_CALLBACK_COMPLETED = "isCallbackCompleted";
46         
47         @Autowired
48         private ExceptionBuilder exceptionBuilder;
49         
50         @Autowired
51         private SDNCClient sdncClient;
52         
53         public void createCorrelationVariables (DelegateExecution execution) {
54                 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
55                 execution.setVariable(request.getCorrelationName()+CORRELATOR, request.getCorrelationValue());
56                 execution.setVariable("sdncTimeout", request.getTimeOut());
57         }
58         
59         public void callSDNC (DelegateExecution execution) {
60                 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
61                 try {
62                         String response = sdncClient.post(request.getSDNCPayload(),request.getTopology());
63                         String finalMessageIndicator = JsonPath.read(response, "$.output.ack-final-indicator");         
64                         execution.setVariable("isSDNCCompleted", convertIndicatorToBoolean(finalMessageIndicator));
65                 } catch(PathNotFoundException e) {
66                         logger.error("Error Parsing SDNC Response", e);
67                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,"Error Parsing SDNC Response");
68                 } catch (MapperException e) {
69                         logger.error("Error Parsing SDNC Response", e);
70                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,"Error Parsing SDNC Response");
71                 } catch (BadResponseException e) {
72                         logger.error("Error Reading SDNC Response", e);
73                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error Reading SDNC Response");
74                 }               
75         }
76         
77         public void processCallback (DelegateExecution execution) {
78                 try {
79                         SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
80                         String asyncRequest = (String) execution.getVariable(request.getCorrelationName()+MESSAGE);
81                         String finalMessageIndicator = JsonPath.read(asyncRequest, "$.input.ack-final-indicator");              
82                         boolean isCallbackCompleted = convertIndicatorToBoolean(finalMessageIndicator);
83                         execution.setVariable(IS_CALLBACK_COMPLETED, isCallbackCompleted);
84                 } catch (Exception e) {
85                         logger.error("Error procesing SDNC callback", e);
86                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error procesing SDNC callback");
87                 }
88         }
89         
90         public void handleTimeOutException (DelegateExecution execution) {              
91                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error timed out waiting on SDNC Async-Response");
92         }
93         
94         public void handleSyncError (DelegateExecution execution) {
95                 String msg = (String) execution.getVariable("SDNCSyncError");           
96                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
97         }
98
99         protected boolean convertIndicatorToBoolean(String finalMessageIndicator) {
100                 return "Y".equals(finalMessageIndicator);
101         }
102         
103 }