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;
26 import javax.xml.XMLConstants;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.onap.so.logger.LoggingAnchor;
35 import org.apache.http.HttpStatus;
36 import org.onap.so.adapters.sdnc.exception.SDNCAdapterException;
37 import org.onap.so.adapters.sdncrest.RequestInformation;
38 import org.onap.so.adapters.sdncrest.SDNCResponseCommon;
39 import org.onap.so.adapters.sdncrest.SDNCServiceError;
40 import org.onap.so.adapters.sdncrest.SDNCServiceRequest;
41 import org.onap.so.logger.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.scheduling.annotation.Async;
47 import org.springframework.stereotype.Component;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Element;
52 public class SDNCServiceRequestTask {
53 private static final Logger logger = LoggerFactory.getLogger(SDNCServiceRequestTask.class);
56 private SDNCServiceRequestConnector connector;
59 MapTypedRequestTunablesData mapTunables;
62 private BPRestCallback bpRestCallback;
65 public void runRequest(SDNCServiceRequest request, String msoRequestId, String msoServiceInstanceId,
68 String sdncRequestId = request.getSdncRequestId();
69 String sdncService = request.getSdncService();
70 String sdncOperation = request.getSdncOperation();
72 TypedRequestTunables rt = new TypedRequestTunables(sdncRequestId, myUrlSuffix);
73 rt.setServiceKey(sdncService, sdncOperation);
74 TypedRequestTunables mappedTunables;
76 mappedTunables = mapTunables.setTunables(rt);
77 } catch (SDNCAdapterException e) {
78 bpRestCallback.send(request.getBPNotificationUrl(), e.getMessage());
81 if (!mappedTunables.getError().isEmpty()) {
82 // Note that the error was logged and alarmed by setTunables()
83 SDNCServiceError error = new SDNCServiceError(request.getSdncRequestId(),
84 String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR), mappedTunables.getError(), "Y");
85 bpRestCallback.send(request.getBPNotificationUrl(), error.toJson());
89 String xml = genSdncReq(request, mappedTunables);
91 SDNCResponseCommon response = connector.send(xml, mappedTunables);
93 bpRestCallback.send(request.getBPNotificationUrl(), response.toJson());
96 private Element addChild(Element parent, String tag) {
97 Element child = parent.getOwnerDocument().createElement(tag);
98 parent.appendChild(child);
102 private void addTextChild(Element parent, String tag, String text) {
104 Element child = parent.getOwnerDocument().createElement(tag);
105 child.setTextContent(text);
106 parent.appendChild(child);
110 private String genSdncReq(SDNCServiceRequest request, TypedRequestTunables rt) {
114 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
115 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
117 doc = documentBuilder.newDocument();
118 Element root = doc.createElementNS(rt.getNamespace(), "input");
119 doc.appendChild(root);
121 Element hdr = addChild(root, rt.getHeaderName());
122 addTextChild(hdr, "svc-request-id", rt.getReqId());
123 addTextChild(hdr, "svc-notification-url", rt.getMyUrl());
125 RequestInformation requestInfo = request.getRequestInformation();
126 Element requestInformation = addChild(root, "request-information");
127 addTextChild(requestInformation, "request-id", requestInfo.getRequestId());
130 if (requestInfo.getRequestAction() != null) {
131 addTextChild(requestInformation, "request-action", requestInfo.getRequestAction());
133 if (requestInfo.getRequestSubAction() != null) {
134 addTextChild(requestInformation, "request-sub-action", requestInfo.getRequestSubAction());
136 if (requestInfo.getOrderNumber() != null && !requestInfo.getOrderNumber().isEmpty()) {
137 addTextChild(requestInformation, "order-number", requestInfo.getOrderNumber());
140 if (requestInfo.getOrderVersion() != null && !requestInfo.getOrderVersion().isEmpty()) {
141 addTextChild(requestInformation, "order-version", requestInfo.getOrderVersion());
145 addTextChild(requestInformation, "source", requestInfo.getSource());
146 addTextChild(requestInformation, "notification-url", requestInfo.getNotificationUrl());
148 Element serviceInformation = addChild(root, "service-information");
149 addTextChild(serviceInformation, "service-type", request.getServiceInformation().getServiceType());
150 addTextChild(serviceInformation, "service-instance-id",
151 request.getServiceInformation().getServiceInstanceId());
152 addTextChild(serviceInformation, "subscriber-name", request.getServiceInformation().getSubscriberName());
153 addTextChild(serviceInformation, "subscriber-global-id",
154 request.getServiceInformation().getSubscriberGlobalId());
156 Element agnosticServiceInformation = addChild(root, "agnostic-service-information");
157 addTextChild(agnosticServiceInformation, "operation", request.getSdncOperation());
158 addTextChild(agnosticServiceInformation, "service", request.getSdncService());
160 // anydata is mandatory in the SDNC schema, so if the data we got is null,
161 // set use an empty string instead to ensure we generate an empty element.
163 String anydata = request.getSdncServiceData();
164 if (anydata == null) {
168 // content-type is also mandatory.
170 String contentType = request.getSdncServiceDataType();
172 if (contentType == null || contentType.isEmpty()) {
173 if (anydata.isEmpty()) {
176 if (anydata.startsWith("<")) {
179 contentType = "JSON";
184 addTextChild(agnosticServiceInformation, "content-type", contentType);
185 addTextChild(agnosticServiceInformation, "anydata", anydata);
186 } catch (Exception e) {
187 logger.error(LoggingAnchor.FOUR, MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST.toString(), "SDNC",
188 ErrorCode.BusinessProcesssError.getValue(), "Exception in genSdncReq", e);
195 StringWriter writer = new StringWriter();
196 TransformerFactory factory = TransformerFactory.newInstance();
197 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
198 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
199 Transformer transformer = factory.newTransformer();
200 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
201 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
202 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
203 transformer.transform(new DOMSource(doc), new StreamResult(writer));
204 xml = writer.toString();
205 } catch (Exception e) {
206 logger.error(LoggingAnchor.THREE, MessageEnum.RA_ERROR_CONVERT_XML2STR.toString(),
207 ErrorCode.DataError.getValue(), "Exception - domToStr", e);
211 logger.trace("Formatted SDNC service request XML:\n {}", xml);