5697ed56a546331a9e84f126497e25cc470c06f1
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.adapters.sdnc.sdncrest;
24
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;
50
51 @Component
52 public class SDNCServiceRequestTask {
53     private static final Logger logger = LoggerFactory.getLogger(SDNCServiceRequestTask.class);
54
55     @Autowired
56     private SDNCServiceRequestConnector connector;
57
58     @Autowired
59     MapTypedRequestTunablesData mapTunables;
60
61     @Autowired
62     private BPRestCallback bpRestCallback;
63
64     @Async
65     public void runRequest(SDNCServiceRequest request, String msoRequestId, String msoServiceInstanceId,
66             String myUrlSuffix) {
67
68         String sdncRequestId = request.getSdncRequestId();
69         String sdncService = request.getSdncService();
70         String sdncOperation = request.getSdncOperation();
71
72         TypedRequestTunables rt = new TypedRequestTunables(sdncRequestId, myUrlSuffix);
73         rt.setServiceKey(sdncService, sdncOperation);
74         TypedRequestTunables mappedTunables;
75         try {
76             mappedTunables = mapTunables.setTunables(rt);
77         } catch (SDNCAdapterException e) {
78             bpRestCallback.send(request.getBPNotificationUrl(), e.getMessage());
79             return;
80         }
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());
86             return;
87         }
88
89         String xml = genSdncReq(request, mappedTunables);
90
91         SDNCResponseCommon response = connector.send(xml, mappedTunables);
92
93         bpRestCallback.send(request.getBPNotificationUrl(), response.toJson());
94     }
95
96     private Element addChild(Element parent, String tag) {
97         Element child = parent.getOwnerDocument().createElement(tag);
98         parent.appendChild(child);
99         return child;
100     }
101
102     private void addTextChild(Element parent, String tag, String text) {
103         if (text != null) {
104             Element child = parent.getOwnerDocument().createElement(tag);
105             child.setTextContent(text);
106             parent.appendChild(child);
107         }
108     }
109
110     private String genSdncReq(SDNCServiceRequest request, TypedRequestTunables rt) {
111         Document doc;
112
113         try {
114             DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
115             DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
116
117             doc = documentBuilder.newDocument();
118             Element root = doc.createElementNS(rt.getNamespace(), "input");
119             doc.appendChild(root);
120
121             Element hdr = addChild(root, rt.getHeaderName());
122             addTextChild(hdr, "svc-request-id", rt.getReqId());
123             addTextChild(hdr, "svc-notification-url", rt.getMyUrl());
124
125             RequestInformation requestInfo = request.getRequestInformation();
126             Element requestInformation = addChild(root, "request-information");
127             addTextChild(requestInformation, "request-id", requestInfo.getRequestId());
128
129
130             if (requestInfo.getRequestAction() != null) {
131                 addTextChild(requestInformation, "request-action", requestInfo.getRequestAction());
132             }
133             if (requestInfo.getRequestSubAction() != null) {
134                 addTextChild(requestInformation, "request-sub-action", requestInfo.getRequestSubAction());
135             }
136             if (requestInfo.getOrderNumber() != null && !requestInfo.getOrderNumber().isEmpty()) {
137                 addTextChild(requestInformation, "order-number", requestInfo.getOrderNumber());
138             }
139
140             if (requestInfo.getOrderVersion() != null && !requestInfo.getOrderVersion().isEmpty()) {
141                 addTextChild(requestInformation, "order-version", requestInfo.getOrderVersion());
142             }
143
144
145             addTextChild(requestInformation, "source", requestInfo.getSource());
146             addTextChild(requestInformation, "notification-url", requestInfo.getNotificationUrl());
147
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());
155
156             Element agnosticServiceInformation = addChild(root, "agnostic-service-information");
157             addTextChild(agnosticServiceInformation, "operation", request.getSdncOperation());
158             addTextChild(agnosticServiceInformation, "service", request.getSdncService());
159
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.
162
163             String anydata = request.getSdncServiceData();
164             if (anydata == null) {
165                 anydata = "";
166             }
167
168             // content-type is also mandatory.
169
170             String contentType = request.getSdncServiceDataType();
171
172             if (contentType == null || contentType.isEmpty()) {
173                 if (anydata.isEmpty()) {
174                     contentType = "XML";
175                 } else {
176                     if (anydata.startsWith("<")) {
177                         contentType = "XML";
178                     } else {
179                         contentType = "JSON";
180                     }
181                 }
182             }
183
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);
189             return null;
190         }
191
192         String xml;
193
194         try {
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);
208             return null;
209         }
210
211         logger.trace("Formatted SDNC service request XML:\n {}", xml);
212         return xml;
213     }
214 }