2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.adapters.sdnc.sdncrest;
25 import java.io.StringWriter;
27 import javax.xml.XMLConstants;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.transform.OutputKeys;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
36 import org.apache.http.HttpStatus;
37 import org.onap.so.adapters.sdnc.exception.SDNCAdapterException;
38 import org.onap.so.adapters.sdncrest.RequestInformation;
39 import org.onap.so.adapters.sdncrest.SDNCResponseCommon;
40 import org.onap.so.adapters.sdncrest.SDNCServiceError;
41 import org.onap.so.adapters.sdncrest.SDNCServiceRequest;
42 import org.onap.so.logger.ErrorCode;
43 import org.onap.so.logger.MessageEnum;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.scheduling.annotation.Async;
48 import org.springframework.stereotype.Component;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.Element;
53 public class SDNCServiceRequestTask {
54 private static final Logger logger = LoggerFactory.getLogger(SDNCServiceRequestTask.class);
57 private SDNCServiceRequestConnector connector;
60 MapTypedRequestTunablesData mapTunables;
63 private BPRestCallback bpRestCallback;
66 public void runRequest(SDNCServiceRequest request,String msoRequestId,String msoServiceInstanceId,String myUrlSuffix)
69 String sdncRequestId = request.getSdncRequestId();
70 String sdncService = request.getSdncService();
71 String sdncOperation = request.getSdncOperation();
73 TypedRequestTunables rt = new TypedRequestTunables(sdncRequestId, myUrlSuffix);
74 rt.setServiceKey(sdncService, sdncOperation);
75 TypedRequestTunables mappedTunables;
77 mappedTunables = mapTunables.setTunables(rt);
78 } catch(SDNCAdapterException e) {
79 bpRestCallback.send(request.getBPNotificationUrl(), e.getMessage());
82 if (!mappedTunables.getError().isEmpty()) {
83 // Note that the error was logged and alarmed by setTunables()
84 SDNCServiceError error = new SDNCServiceError(request.getSdncRequestId(),
85 String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR), mappedTunables.getError(), "Y");
86 bpRestCallback.send(request.getBPNotificationUrl(), error.toJson());
90 String xml = genSdncReq(request, mappedTunables);
92 long sdncStartTime = System.currentTimeMillis();
93 SDNCResponseCommon response = connector.send(xml, mappedTunables);
95 long bpStartTime = System.currentTimeMillis();
96 boolean callbackSuccess = bpRestCallback.send(request.getBPNotificationUrl(), response.toJson());
99 private Element addChild(Element parent, String tag) {
100 Element child = parent.getOwnerDocument().createElement(tag);
101 parent.appendChild(child);
105 private void addTextChild(Element parent, String tag, String text) {
107 Element child = parent.getOwnerDocument().createElement(tag);
108 child.setTextContent(text);
109 parent.appendChild(child);
113 private String genSdncReq(SDNCServiceRequest request, TypedRequestTunables rt) {
117 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
118 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
120 doc = documentBuilder.newDocument();
121 Element root = doc.createElementNS(rt.getNamespace(), "input");
122 doc.appendChild(root);
124 Element hdr = addChild(root, rt.getHeaderName());
125 addTextChild(hdr, "svc-request-id", rt.getReqId());
126 addTextChild(hdr, "svc-notification-url", rt.getMyUrl());
128 RequestInformation requestInfo = request.getRequestInformation();
129 Element requestInformation = addChild(root, "request-information");
130 addTextChild(requestInformation, "request-id", requestInfo.getRequestId());
133 if(requestInfo.getRequestAction()!= null) {
134 addTextChild(requestInformation, "request-action",
135 requestInfo.getRequestAction());
137 if(requestInfo.getRequestSubAction()!= null) {
138 addTextChild(requestInformation, "request-sub-action",
139 requestInfo.getRequestSubAction());
141 if(requestInfo.getOrderNumber() != null &&
142 !requestInfo.getOrderNumber().isEmpty() ) {
143 addTextChild(requestInformation, "order-number",
144 requestInfo.getOrderNumber());
147 if(requestInfo.getOrderVersion() != null &&
148 !requestInfo.getOrderVersion().isEmpty() ) {
149 addTextChild(requestInformation, "order-version",
150 requestInfo.getOrderVersion());
154 addTextChild(requestInformation, "source", requestInfo.getSource());
155 addTextChild(requestInformation, "notification-url", requestInfo.getNotificationUrl());
157 Element serviceInformation = addChild(root, "service-information");
158 addTextChild(serviceInformation, "service-type", request.getServiceInformation().getServiceType());
159 addTextChild(serviceInformation, "service-instance-id", request.getServiceInformation().getServiceInstanceId());
160 addTextChild(serviceInformation, "subscriber-name", request.getServiceInformation().getSubscriberName());
161 addTextChild(serviceInformation, "subscriber-global-id", request.getServiceInformation().getSubscriberGlobalId());
163 Element agnosticServiceInformation = addChild(root, "agnostic-service-information");
164 addTextChild(agnosticServiceInformation, "operation", request.getSdncOperation());
165 addTextChild(agnosticServiceInformation, "service", request.getSdncService());
167 // anydata is mandatory in the SDNC schema, so if the data we got is null,
168 // set use an empty string instead to ensure we generate an empty element.
170 String anydata = request.getSdncServiceData();
171 if (anydata == null) {
175 // content-type is also mandatory.
177 String contentType = request.getSdncServiceDataType();
179 if (contentType == null || contentType.isEmpty()) {
180 if (anydata.isEmpty()) {
183 if (anydata.startsWith("<")) {
186 contentType = "JSON";
191 addTextChild(agnosticServiceInformation, "content-type", contentType);
192 addTextChild(agnosticServiceInformation, "anydata", anydata);
193 } catch (Exception e) {
194 logger.error("{} {} {} {}", MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST.toString(), "SDNC",
195 ErrorCode.BusinessProcesssError.getValue(), "Exception in genSdncReq", e);
202 StringWriter writer = new StringWriter();
203 TransformerFactory factory = TransformerFactory.newInstance();
204 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD,"");
205 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET,"");
206 Transformer transformer = factory.newTransformer();
207 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
208 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
209 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
210 transformer.transform(new DOMSource(doc), new StreamResult(writer));
211 xml = writer.toString();
212 } catch (Exception e) {
213 logger.error("{} {} {}", MessageEnum.RA_ERROR_CONVERT_XML2STR.toString(), ErrorCode.DataError.getValue(),
214 "Exception - domToStr", e);
218 logger.trace("Formatted SDNC service request XML:\n {}", xml);