Removed MsoLogger class
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / onap / so / adapters / sdnc / sdncrest / SDNCServiceRequestTask.java
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
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;
35
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;
51
52 @Component
53 public class SDNCServiceRequestTask {
54         private static final Logger logger =  LoggerFactory.getLogger(SDNCServiceRequestTask.class);
55
56         @Autowired
57         private SDNCServiceRequestConnector connector;
58         
59         @Autowired
60         MapTypedRequestTunablesData mapTunables;
61         
62         @Autowired
63         private BPRestCallback bpRestCallback;
64
65         @Async
66         public void runRequest(SDNCServiceRequest request,String msoRequestId,String msoServiceInstanceId,String myUrlSuffix)
67         {
68
69                 String sdncRequestId = request.getSdncRequestId();
70                 String sdncService = request.getSdncService();
71                 String sdncOperation = request.getSdncOperation();
72
73                 TypedRequestTunables rt = new TypedRequestTunables(sdncRequestId, myUrlSuffix);
74                 rt.setServiceKey(sdncService, sdncOperation);
75                 TypedRequestTunables mappedTunables;
76                 try {
77                         mappedTunables = mapTunables.setTunables(rt);
78                 } catch(SDNCAdapterException e) {
79                         bpRestCallback.send(request.getBPNotificationUrl(), e.getMessage());
80                         return;
81                 }
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());
87                         return;
88                 }
89
90                 String xml = genSdncReq(request, mappedTunables);
91
92                 long sdncStartTime = System.currentTimeMillis();                
93                 SDNCResponseCommon response = connector.send(xml, mappedTunables);
94
95                 long bpStartTime = System.currentTimeMillis();
96                 boolean callbackSuccess = bpRestCallback.send(request.getBPNotificationUrl(), response.toJson());
97         }
98
99         private Element addChild(Element parent, String tag) {
100                 Element child = parent.getOwnerDocument().createElement(tag);
101                 parent.appendChild(child);
102                 return child;
103         }
104
105         private void addTextChild(Element parent, String tag, String text) {
106                 if (text != null) {
107                         Element child = parent.getOwnerDocument().createElement(tag);
108                         child.setTextContent(text);
109                         parent.appendChild(child);
110                 }
111         }
112
113         private String genSdncReq(SDNCServiceRequest request, TypedRequestTunables rt) {
114                 Document doc;
115
116                 try {
117                         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
118                         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
119
120                         doc = documentBuilder.newDocument();
121                         Element root = doc.createElementNS(rt.getNamespace(), "input");
122                         doc.appendChild(root);
123
124                         Element hdr = addChild(root, rt.getHeaderName());
125                         addTextChild(hdr, "svc-request-id", rt.getReqId());
126                         addTextChild(hdr, "svc-notification-url", rt.getMyUrl());
127
128                         RequestInformation requestInfo = request.getRequestInformation();
129                         Element requestInformation = addChild(root, "request-information");
130                         addTextChild(requestInformation, "request-id", requestInfo.getRequestId());
131                 
132                         
133                         if(requestInfo.getRequestAction()!= null) {
134                                 addTextChild(requestInformation, "request-action",
135                                                 requestInfo.getRequestAction());
136                         }
137                         if(requestInfo.getRequestSubAction()!= null) {
138                                 addTextChild(requestInformation, "request-sub-action",
139                                                 requestInfo.getRequestSubAction());
140                         }
141                         if(requestInfo.getOrderNumber() != null &&
142                                         !requestInfo.getOrderNumber().isEmpty() ) {
143                                 addTextChild(requestInformation, "order-number",
144                                                 requestInfo.getOrderNumber());
145                         }
146                         
147                         if(requestInfo.getOrderVersion() != null &&
148                                         !requestInfo.getOrderVersion().isEmpty() ) {
149                                 addTextChild(requestInformation, "order-version",
150                                                 requestInfo.getOrderVersion());
151                         }
152                         
153                         
154                         addTextChild(requestInformation, "source", requestInfo.getSource());
155                         addTextChild(requestInformation, "notification-url", requestInfo.getNotificationUrl());
156
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());
162
163                         Element agnosticServiceInformation = addChild(root, "agnostic-service-information");
164                         addTextChild(agnosticServiceInformation, "operation", request.getSdncOperation());
165                         addTextChild(agnosticServiceInformation, "service", request.getSdncService());
166
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.
169
170                         String anydata = request.getSdncServiceData();
171                         if (anydata == null) {
172                                 anydata = "";
173                         }
174
175                         // content-type is also mandatory.
176
177                         String contentType = request.getSdncServiceDataType();
178
179                         if (contentType == null || contentType.isEmpty()) {
180                                 if (anydata.isEmpty()) {
181                                         contentType = "XML";
182                                 } else {
183                                         if (anydata.startsWith("<")) {
184                                                 contentType = "XML";
185                                         } else {
186                                                 contentType = "JSON";
187                                         }
188                                 }
189                         }
190
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);
196                         return null;
197                 }
198
199                 String xml;
200
201                 try {
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);
215                         return null;
216                 }
217
218                 logger.trace("Formatted SDNC service request XML:\n {}", xml);
219                 return xml;
220         }
221 }