2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.sdnc.tasks;
23 import java.io.StringReader;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.xpath.XPath;
28 import javax.xml.xpath.XPathFactory;
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;
45 import com.jayway.jsonpath.JsonPath;
46 import com.jayway.jsonpath.PathNotFoundException;
49 public class SDNCRequestTasks {
51 private static final Logger logger = LoggerFactory.getLogger(SDNCRequestTasks.class);
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";
60 private ExceptionBuilder exceptionBuilder;
63 private SDNCClient sdncClient;
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());
71 public void callSDNC (DelegateExecution execution) {
72 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
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.");
92 public void processCallback (DelegateExecution execution) {
94 SDNCRequest request = (SDNCRequest)execution.getVariable(SDNC_REQUEST);
95 String asyncRequest = (String) execution.getVariable(request.getCorrelationName()+MESSAGE);
97 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
98 DocumentBuilder db = dbf.newDocumentBuilder();
99 Document doc = db.parse(new InputSource(new StringReader(asyncRequest)));
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);
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");
120 public void handleTimeOutException (DelegateExecution execution) {
121 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error timed out waiting on SDNC Async-Response");
124 protected boolean convertIndicatorToBoolean(String finalMessageIndicator) {
125 return "Y".equals(finalMessageIndicator);
128 protected String getXmlElement(Document doc, String exp) throws Exception {
129 XPath xPath = XPathFactory.newInstance().newXPath();
130 return xPath.evaluate(exp, doc);