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;
24 import java.io.StringWriter;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerFactory;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamResult;
31 import javax.xml.xpath.XPath;
32 import javax.xml.xpath.XPathFactory;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.onap.so.bpmn.infrastructure.sdnc.exceptions.SDNCErrorResponseException;
35 import org.onap.so.client.exception.BadResponseException;
36 import org.onap.so.client.exception.ExceptionBuilder;
37 import org.onap.so.client.exception.MapperException;
38 import org.onap.so.client.sdnc.SDNCClient;
39 import org.onap.so.client.sdnc.beans.SDNCRequest;
40 import org.onap.logging.filter.base.ONAPComponents;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45 import org.springframework.web.client.HttpClientErrorException;
46 import org.w3c.dom.Document;
47 import org.xml.sax.InputSource;
48 import com.jayway.jsonpath.JsonPath;
49 import com.jayway.jsonpath.PathNotFoundException;
52 public class SDNCRequestTasks {
54 private static final Logger logger = LoggerFactory.getLogger(SDNCRequestTasks.class);
56 private static final String SDNC_REQUEST = "SDNCRequest";
57 private static final String MESSAGE = "_MESSAGE";
58 private static final String CORRELATOR = "_CORRELATOR";
59 protected static final String IS_CALLBACK_COMPLETED = "isCallbackCompleted";
60 protected static final String SDNC_SUCCESS = "200";
63 private ExceptionBuilder exceptionBuilder;
66 private SDNCClient sdncClient;
68 public void createCorrelationVariables(DelegateExecution execution) {
69 SDNCRequest request = (SDNCRequest) execution.getVariable(SDNC_REQUEST);
70 execution.setVariable(request.getCorrelationName() + CORRELATOR, request.getCorrelationValue());
71 execution.setVariable("sdncTimeout", request.getTimeOut());
74 public void callSDNC(DelegateExecution execution) {
75 SDNCRequest request = (SDNCRequest) execution.getVariable(SDNC_REQUEST);
77 String response = sdncClient.post(request.getSDNCPayload(), request.getTopology());
78 String finalMessageIndicator = JsonPath.read(response, "$.output.ack-final-indicator");
79 execution.setVariable("isSDNCCompleted", convertIndicatorToBoolean(finalMessageIndicator));
80 } catch (PathNotFoundException e) {
81 logger.error("Error Parsing SDNC Response. Could not find read final ack indicator from JSON.", e);
82 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
83 "Recieved invalid response from SDNC, unable to read message content.", ONAPComponents.SO);
84 } catch (MapperException e) {
85 logger.error("Failed to map SDNC object to JSON prior to POST.", e);
86 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
87 "Failed to map SDNC object to JSON prior to POST.", ONAPComponents.SO);
88 } catch (BadResponseException e) {
89 logger.error("Did not receive a successful response from SDNC.", e);
90 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e.getLocalizedMessage(),
92 } catch (HttpClientErrorException e) {
93 logger.error("HttpClientErrorException: 404 Not Found, Failed to contact SDNC", e);
94 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "SDNC cannot be contacted.",
99 public void processCallback(DelegateExecution execution) {
101 SDNCRequest request = (SDNCRequest) execution.getVariable(SDNC_REQUEST);
102 String asyncRequest = (String) execution.getVariable(request.getCorrelationName() + MESSAGE);
104 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
105 dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
106 dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
107 dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
108 DocumentBuilder db = dbf.newDocumentBuilder();
109 Document doc = db.parse(new InputSource(new StringReader(asyncRequest)));
111 String finalMessageIndicator = getXmlElement(doc, "/input/ack-final-indicator");
113 boolean isCallbackCompleted = convertIndicatorToBoolean(finalMessageIndicator);
114 execution.setVariable(IS_CALLBACK_COMPLETED, isCallbackCompleted);
115 if (isCallbackCompleted) {
116 String responseCode = getXmlElement(doc, "/input/response-code");
117 if (!SDNC_SUCCESS.equalsIgnoreCase(responseCode)) {
118 String responseMessage;
120 responseMessage = getXmlElement(doc, "/input/response-message");
121 } catch (Exception e) {
122 responseMessage = "Unknown Error in SDNC";
124 throw new SDNCErrorResponseException(responseMessage);
127 } catch (SDNCErrorResponseException e) {
128 logger.error("SDNC error response - " + e.getMessage());
129 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e.getMessage(), ONAPComponents.SDNC);
130 } catch (Exception e) {
131 logger.error("Error processing SDNC callback", e);
132 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error processing SDNC callback",
137 public void handleTimeOutException(DelegateExecution execution) {
138 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000,
139 "Error timed out waiting on SDNC Async-Response", ONAPComponents.SO);
142 protected boolean convertIndicatorToBoolean(String finalMessageIndicator) {
143 return "Y".equals(finalMessageIndicator);
146 protected String getXmlElement(Document doc, String exp) throws Exception {
147 TransformerFactory tf = TransformerFactory.newInstance();
148 Transformer transformer = tf.newTransformer();
149 StringWriter writer = new StringWriter();
150 transformer.transform(new DOMSource(doc), new StreamResult(writer));
151 logger.debug(writer.getBuffer().toString());
152 XPath xPath = XPathFactory.newInstance().newXPath();
153 String result = xPath.evaluate(exp, doc);
154 if (result == null || result.isEmpty()) {
155 throw new Exception("XPath Failed to find element expression: " + exp);