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