24f642ae721f780b6d2bdbd0b028dd0292b73bc7
[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 java.io.StringReader;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.xpath.XPath;
28 import javax.xml.xpath.XPathFactory;
29
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.onap.so.bpmn.infrastructure.sdnc.exceptions.SDNCErrorResponseException;
32 import org.onap.so.client.exception.BadResponseException;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.client.exception.MapperException;
35 import org.onap.so.client.sdnc.SDNCClient;
36 import org.onap.so.client.sdnc.beans.SDNCRequest;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41 import org.springframework.web.client.HttpClientErrorException;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
44
45 import com.jayway.jsonpath.JsonPath;
46 import com.jayway.jsonpath.PathNotFoundException;
47  
48 @Component
49 public class SDNCRequestTasks {
50         
51         private static final Logger logger = LoggerFactory.getLogger(SDNCRequestTasks.class);
52         
53         private static final String SDNC_REQUEST = "SDNCRequest";
54         private static final String MESSAGE = "_MESSAGE";
55         private static final String CORRELATOR = "_CORRELATOR";
56         protected static final String IS_CALLBACK_COMPLETED = "isCallbackCompleted";
57         protected static final String SDNC_SUCCESS = "200";
58         
59         @Autowired
60         private ExceptionBuilder exceptionBuilder;
61         
62         @Autowired
63         private SDNCClient sdncClient;
64         
65         public void createCorrelationVariables (DelegateExecution execution) {
66                 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
67                 execution.setVariable(request.getCorrelationName()+CORRELATOR, request.getCorrelationValue());
68                 execution.setVariable("sdncTimeout", request.getTimeOut());
69         }
70         
71         public void callSDNC (DelegateExecution execution) {
72                 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
73                 try {
74                         String response = sdncClient.post(request.getSDNCPayload(),request.getTopology());
75                         String finalMessageIndicator = JsonPath.read(response, "$.output.ack-final-indicator");         
76                         execution.setVariable("isSDNCCompleted", convertIndicatorToBoolean(finalMessageIndicator));
77                 } catch(PathNotFoundException e) {
78                         logger.error("Error Parsing SDNC Response. Could not find read final ack indicator from JSON.", e);
79                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,"Recieved invalid response from SDNC, unable to read message content.");
80                 } catch (MapperException e) {
81                         logger.error("Failed to map SDNC object to JSON prior to POST.", e);
82                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,"Failed to map SDNC object to JSON prior to POST.");
83                 } catch (BadResponseException e) {
84                         logger.error("Did not receive a successful response from SDNC.", e);
85                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e.getLocalizedMessage());
86                 } catch (HttpClientErrorException e){
87                         logger.error("HttpClientErrorException: 404 Not Found, Failed to contact SDNC", e);
88                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,  "SDNC cannot be contacted.");
89                 }
90         }
91         
92         public void processCallback (DelegateExecution execution) {
93                 try {
94                         SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
95                         String asyncRequest = (String) execution.getVariable(request.getCorrelationName()+MESSAGE);
96                         
97                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
98                 DocumentBuilder db = dbf.newDocumentBuilder();
99                 Document doc = db.parse(new InputSource(new StringReader(asyncRequest)));
100
101                 String finalMessageIndicator = getXmlElement(doc, "/input/ack-final-indicator");
102                         boolean isCallbackCompleted = convertIndicatorToBoolean(finalMessageIndicator);
103                         execution.setVariable(IS_CALLBACK_COMPLETED, isCallbackCompleted);
104                         if(isCallbackCompleted) {
105                                 String responseCode = getXmlElement(doc, "/input/response-code");
106                                 String responseMessage = getXmlElement(doc, "/input/response-message"); 
107                                 if(!SDNC_SUCCESS.equalsIgnoreCase(responseCode)) {
108                                         throw new SDNCErrorResponseException(responseMessage);
109                                 }
110                         }
111                 } catch (SDNCErrorResponseException e) {
112                         logger.error("SDNC error response - " + e.getMessage());
113                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e.getMessage());
114                 } catch (Exception e) {
115                         logger.error("Error procesing SDNC callback", e);
116                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error procesing SDNC callback");
117                 }
118         }
119         
120         public void handleTimeOutException (DelegateExecution execution) {              
121                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error timed out waiting on SDNC Async-Response");
122         }
123
124         protected boolean convertIndicatorToBoolean(String finalMessageIndicator) {
125                 return "Y".equals(finalMessageIndicator);
126         }
127         
128         protected String getXmlElement(Document doc, String exp) throws Exception {
129                 XPath xPath = XPathFactory.newInstance().newXPath();
130                 return xPath.evaluate(exp, doc);
131         }
132         
133 }