AT&T 1712 and 1802 release code
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / sdncrest / SDNCServiceRequestConnector.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.adapters.sdnc.sdncrest;
22
23 import org.openecomp.mso.adapters.sdncrest.SDNCErrorCommon;
24 import org.openecomp.mso.adapters.sdncrest.SDNCResponseCommon;
25 import org.openecomp.mso.adapters.sdncrest.SDNCServiceError;
26 import org.openecomp.mso.adapters.sdncrest.SDNCServiceResponse;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.xml.sax.InputSource;
30
31 import javax.xml.XMLConstants;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import java.io.StringReader;
34 import java.net.HttpURLConnection;
35 import java.text.ParseException;
36 import java.util.ArrayList;
37 import java.util.List;
38 import org.openecomp.mso.logger.MsoLogger;
39
40 /**
41  * SDNCConnector for "agnostic" API services.
42  */
43 public class SDNCServiceRequestConnector extends SDNCConnector {
44
45     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
46         @Override
47         protected SDNCResponseCommon createResponseFromContent(int statusCode, String statusMessage,
48                         String responseContent, TypedRequestTunables rt) {
49                 try {
50                         return parseResponseContent(responseContent);
51                 } catch (ParseException e) {
52                         logError(e.getMessage());
53                         return createErrorResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage(), rt);
54                 }
55         }
56
57         @Override
58         protected SDNCErrorCommon createErrorResponse(int statusCode, String errMsg,
59                         TypedRequestTunables rt) {
60                 return new SDNCServiceError(rt.getReqId(), String.valueOf(statusCode), errMsg, "Y");
61         }
62
63         /**
64          * Parses SDNC synchronous service response content or service notification content.
65          * If the content can be parsed and contains all required elements, then an object
66          * is returned.  The type of the returned object depends on the response code
67          * contained in the content.  For 2XX response codes, an SDNCServiceResponse is
68          * returned.  Otherwise, an SDNCServiceError is returned.  If the content cannot
69          * be parsed, or if the content does not contain all required elements, a parse
70          * exception is thrown.  This method performs no logging or alarming.
71          * @throws ParseException on error
72          */
73         public static SDNCResponseCommon parseResponseContent(String responseContent)
74                         throws ParseException {
75                 try {
76                         // Note: this document builder is not namespace-aware, so namespaces are ignored.
77                         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
78                         documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
79                         InputSource source = new InputSource(new StringReader(responseContent));
80                         Document doc = documentBuilderFactory.newDocumentBuilder().parse(source);
81
82                         // Find the configuration-response-common child under the root element.
83                         // The root element is expected to be an "output" element, but we don't really care.
84
85                         Element root = doc.getDocumentElement();
86                         Element configurationResponseCommon = null;
87
88                         for (Element child : SDNCAdapterUtils.childElements(root)) {
89                                 if ("configuration-response-common".equals(child.getNodeName())) {
90                                         configurationResponseCommon = child;
91                                         break;
92                                 }
93                         }
94
95                         if (configurationResponseCommon == null) {
96                                 throw new ParseException("No configuration-response-common element in SDNC response", 0);
97                         }
98
99                         // Process the children of configuration-response-common.
100
101                         String responseCode = null;
102                         String responseMessage = null;
103                         String svcRequestId = null;
104                         String ackFinalIndicator = null;
105                         List<Element> responseParameters = new ArrayList<>();
106
107                         for (Element child : SDNCAdapterUtils.childElements(configurationResponseCommon)) {
108                                 if ("response-code".equals(child.getNodeName())) {
109                                         responseCode = child.getTextContent();
110                                 } else if ("response-message".equals(child.getNodeName())) {
111                                         responseMessage = child.getTextContent();
112                                 } else if ("svc-request-id".equals(child.getNodeName())) {
113                                         svcRequestId = child.getTextContent();
114                                 } else if ("ack-final-indicator".equals(child.getNodeName())) {
115                                         ackFinalIndicator = child.getTextContent();
116                                 } else if ("response-parameters".equals(child.getNodeName())) {
117                     responseParameters.add(child);
118                                 }
119                         }
120
121                         // svc-request-id is mandatory.
122
123                         if (svcRequestId == null || svcRequestId.isEmpty()) {
124                                 throw new ParseException("No svc-request-id in SDNC response", 0);
125                         }
126
127                         // response-code is mandatory.
128
129                         if (responseCode == null || responseCode.isEmpty()) {
130                                 throw new ParseException("No response-code in SDNC response", 0);
131                         }
132
133                         // ack-final-indicator is optional: default to "Y".
134
135                         if (ackFinalIndicator == null || ackFinalIndicator.trim().isEmpty()) {
136                                 ackFinalIndicator = "Y";
137                         }
138
139                         if (!ackFinalIndicator.equals("Y") && !"N".equals(ackFinalIndicator)) {
140                                 throw new ParseException("Invalid ack-final-indicator in SDNC response: '" + ackFinalIndicator + "'", 0);
141                         }
142
143                         // response-message is optional.  If the value is empty, omit it from the response object.
144
145                         if (responseMessage != null && responseMessage.isEmpty()) {
146                                 responseMessage = null;
147                         }
148
149                         // If the response code in the message from SDNC was not 2XX, return SDNCServiceError.
150
151                         if (!responseCode.matches("2[0-9][0-9]") && !responseCode.equals("0")) {
152                                 // Not a 2XX response.  Return SDNCServiceError.
153                                 return new SDNCServiceError(svcRequestId, responseCode, responseMessage, ackFinalIndicator);
154                         }
155
156                         // Create a success response object.
157
158                         SDNCServiceResponse response = new SDNCServiceResponse(svcRequestId,
159                                 responseCode, responseMessage, ackFinalIndicator);
160
161             // Process any response-parameters that might be present.
162
163             for (Element element : responseParameters) {
164                 String tagName = null;
165                 String tagValue = null;
166
167                 for (Element child : SDNCAdapterUtils.childElements(element)) {
168                     if ("tag-name".equals(child.getNodeName())) {
169                         tagName = child.getTextContent();
170                     } else if ("tag-value".equals(child.getNodeName())) {
171                         tagValue = child.getTextContent();
172                     }
173                 }
174
175                 // tag-name is mandatory
176
177                 if (tagName == null) {
178                     throw new ParseException("Missing tag-name in SDNC response parameter", 0);
179                 }
180
181                 // tag-value is optional.  If absent, make it an empty string so we don't
182                 // end up with null values in the parameter map.
183
184                 if (tagValue == null) {
185                     tagValue = "";
186                 }
187
188                 response.addParam(tagName, tagValue);
189             }
190
191                         return response;
192                 } catch (ParseException e) {
193                         throw e;
194                 } catch (Exception e) {
195                     LOGGER.debug("Exception:", e);
196                         throw new ParseException("Failed to parse SDNC response", 0);
197                 }
198         }
199 }