aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>2020-03-23 07:37:58 +0000
committerGerrit Code Review <gerrit@onap.org>2020-03-23 07:37:58 +0000
commita7f7a879f1ed9b89563495952761de4454a33875 (patch)
treee2d7335759c1cb0e908d3ea1b11bda7f6c4afb52
parent7e5054eb95756feb5aa770c751994c27368333ac (diff)
parentbd8667e2f9a930951c14b4c5ea07b60fdcd582a1 (diff)
Merge "Fix sonar issues"
-rw-r--r--adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnector.java147
-rw-r--r--adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnector.java50
-rw-r--r--adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java7
3 files changed, 107 insertions, 97 deletions
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnector.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnector.java
index 431e9db87b..ea24a0ce3e 100644
--- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnector.java
+++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnector.java
@@ -11,9 +11,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,6 +27,8 @@ package org.onap.so.adapters.sdnc.sdncrest;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
+import java.util.Arrays;
+import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -71,7 +73,6 @@ import org.springframework.core.env.Environment;
public abstract class SDNCConnector {
private static final Logger logger = LoggerFactory.getLogger(SDNCConnector.class);
- private static final String MSO_INTERNAL_ERROR = "MsoInternalError";
private static final String XPATH_EXCEPTION = "XPath Exception";
@Autowired
private Environment env;
@@ -84,32 +85,10 @@ public abstract class SDNCConnector {
HttpResponse httpResponse = null;
try {
- int timeout = Integer.parseInt(rt.getTimeout());
-
- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
- .setConnectionRequestTimeout(timeout).build();
HttpClient client = HttpClientBuilder.create().build();
- if ("POST".equals(rt.getReqMethod())) {
- HttpPost httpPost = new HttpPost(rt.getSdncUrl());
- httpPost.setConfig(requestConfig);
- httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_XML));
- method = httpPost;
- } else if ("PUT".equals(rt.getReqMethod())) {
- HttpPut httpPut = new HttpPut(rt.getSdncUrl());
- httpPut.setConfig(requestConfig);
- httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_XML));
- method = httpPut;
- } else if ("GET".equals(rt.getReqMethod())) {
- HttpGet httpGet = new HttpGet(rt.getSdncUrl());
- httpGet.setConfig(requestConfig);
- method = httpGet;
- } else if ("DELETE".equals(rt.getReqMethod())) {
- HttpDelete httpDelete = new HttpDelete(rt.getSdncUrl());
- httpDelete.setConfig(requestConfig);
- method = httpDelete;
- }
+ method = getHttpRequestMethod(content, rt);
String userCredentials = CryptoUtils.decrypt(env.getProperty(Constants.SDNC_AUTH_PROP),
@@ -122,8 +101,6 @@ public abstract class SDNCConnector {
logger.debug("method is NULL:");
}
-
-
httpResponse = client.execute(method);
String responseContent = null;
@@ -141,7 +118,7 @@ public abstract class SDNCConnector {
String errMsg = "SDNC returned " + statusCode + " " + statusMessage;
String errors = analyzeErrors(responseContent);
- if (errors != null) {
+ if (errors != null && !errors.isEmpty()) {
errMsg += " " + errors;
}
@@ -192,6 +169,36 @@ public abstract class SDNCConnector {
}
}
+ private HttpRequestBase getHttpRequestMethod(String content, TypedRequestTunables rt) {
+
+ int timeout = Integer.parseInt(rt.getTimeout());
+ HttpRequestBase method = null;
+
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
+ .setConnectionRequestTimeout(timeout).build();
+
+ if ("POST".equals(rt.getReqMethod())) {
+ HttpPost httpPost = new HttpPost(rt.getSdncUrl());
+ httpPost.setConfig(requestConfig);
+ httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_XML));
+ method = httpPost;
+ } else if ("PUT".equals(rt.getReqMethod())) {
+ HttpPut httpPut = new HttpPut(rt.getSdncUrl());
+ httpPut.setConfig(requestConfig);
+ httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_XML));
+ method = httpPut;
+ } else if ("GET".equals(rt.getReqMethod())) {
+ HttpGet httpGet = new HttpGet(rt.getSdncUrl());
+ httpGet.setConfig(requestConfig);
+ method = httpGet;
+ } else if ("DELETE".equals(rt.getReqMethod())) {
+ HttpDelete httpDelete = new HttpDelete(rt.getSdncUrl());
+ httpDelete.setConfig(requestConfig);
+ method = httpDelete;
+ }
+ return method;
+ }
+
protected void logError(String errMsg) {
logger.error(LoggingAnchor.FOUR, MessageEnum.RA_EXCEPTION_COMMUNICATE_SDNC.toString(), "SDNC",
ErrorCode.AvailabilityError.getValue(), errMsg);
@@ -205,7 +212,7 @@ public abstract class SDNCConnector {
/**
* Generates a response object from content received from SDNC. The response object may be a success response object
* or an error response object. This method must be overridden by the subclass to return the correct object type.
- *
+ *
* @param statusCode the response status code from SDNC (e.g. 200)
* @param statusMessage the response status message from SDNC (e.g. "OK")
* @param responseContent the body of the response from SDNC (possibly null)
@@ -218,7 +225,7 @@ public abstract class SDNCConnector {
/**
* Generates an error response object. This method must be overridden by the subclass to return the correct object
* type.
- *
+ *
* @param statusCode the response status code (from SDNC, or internally generated)
* @param errMsg the error message (normally a verbose explanation of the error)
* @param rt request tunables
@@ -229,17 +236,17 @@ public abstract class SDNCConnector {
/**
* Called by the send() method to analyze errors that may be encoded in the content of non-2XX responses. By
* default, this method tries to parse the content as a restconf error.
- *
+ *
* <pre>
* xmlns = "urn:ietf:params:xml:ns:yang:ietf-restconf"
* </pre>
- *
+ *
* If an error (or errors) can be obtained from the content, then the result is a string in this format:
- *
+ *
* <pre>
* [error-type:TYPE, error-tag:TAG, error-message:MESSAGE] ...
* </pre>
- *
+ *
* If no error could be obtained from the content, then the result is null.
* <p>
* The subclass can override this method to provide another implementation.
@@ -262,7 +269,7 @@ public abstract class SDNCConnector {
// </error>
// </errors>
- StringBuilder output = null;
+ String output = null;
try {
XPathFactory xpathFactory = XPathFactory.newInstance();
@@ -274,55 +281,47 @@ public abstract class SDNCConnector {
InputSource source = new InputSource(new StringReader(content));
Document doc = documentBuilderFactory.newDocumentBuilder().parse(source);
NodeList errors = (NodeList) xpath.evaluate("errors/error", doc, XPathConstants.NODESET);
+ StringBuilder errorDetails = getErrorDetails(xpath, errors);
+ if (errorDetails != null) {
+ output = errorDetails.toString();
+ }
+ } catch (Exception e) {
+ logger.error(LoggingAnchor.FOUR, MessageEnum.RA_ANALYZE_ERROR_EXC.toString(), "SDNC",
+ ErrorCode.DataError.getValue(), "Exception while analyzing errors", e);
+ }
- for (int i = 0; i < errors.getLength(); i++) {
- Element error = (Element) errors.item(i);
+ return output;
+ }
- String info = "";
+ private StringBuilder getErrorDetails(XPath xpath, NodeList errors) {
- try {
- String errorType = xpath.evaluate("error-type", error);
- info += "error-type:" + errorType;
- } catch (XPathExpressionException e) {
- logger.error(LoggingAnchor.SIX, MessageEnum.RA_EVALUATE_XPATH_ERROR.toString(), "error-type",
- error.toString(), "SDNC", ErrorCode.DataError.getValue(), XPATH_EXCEPTION, e);
- }
+ StringBuilder output = null;
- try {
- String errorTag = xpath.evaluate("error-tag", error);
- if (!info.isEmpty()) {
- info += ", ";
- }
- info += "error-tag:" + errorTag;
- } catch (XPathExpressionException e) {
- logger.error(LoggingAnchor.SIX, MessageEnum.RA_EVALUATE_XPATH_ERROR.toString(), "error-tag",
- error.toString(), "SDNC", ErrorCode.DataError.getValue(), XPATH_EXCEPTION, e);
- }
+ for (int i = 0; i < errors.getLength(); i++) {
+ Element error = (Element) errors.item(i);
+
+ StringBuilder info = new StringBuilder();
+ List<String> errorAttributesList = Arrays.asList("error-type", "error-tag", "error-message");
+
+ for (String errorAttrib : errorAttributesList) {
try {
- String errorMessage = xpath.evaluate("error-message", error);
- if (!info.isEmpty()) {
- info += ", ";
- }
- info += "error-message:" + errorMessage;
- } catch (Exception e) {
- logger.error(LoggingAnchor.SIX, MessageEnum.RA_EVALUATE_XPATH_ERROR.toString(), "error-message",
+ String errorAttribValue = xpath.evaluate(errorAttrib, error);
+ info.append(errorAttrib).append(":").append(errorAttribValue);
+ } catch (XPathExpressionException e) {
+ logger.error(LoggingAnchor.SIX, MessageEnum.RA_EVALUATE_XPATH_ERROR.toString(), errorAttrib,
error.toString(), "SDNC", ErrorCode.DataError.getValue(), XPATH_EXCEPTION, e);
}
+ }
- if (!info.isEmpty()) {
- if (output == null) {
- output = new StringBuilder("[" + info + "]");
- } else {
- output.append(" [").append(info).append("]");
- }
+ if (!info.toString().isEmpty()) {
+ if (output == null) {
+ output = new StringBuilder("[" + info + "]");
+ } else {
+ output.append(" [").append(info).append("]");
}
}
- } catch (Exception e) {
- logger.error(LoggingAnchor.FOUR, MessageEnum.RA_ANALYZE_ERROR_EXC.toString(), "SDNC",
- ErrorCode.DataError.getValue(), "Exception while analyzing errors", e);
}
-
- return output.toString();
+ return output;
}
}
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnector.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnector.java
index 6f76ea1eab..9dd95c6713 100644
--- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnector.java
+++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnector.java
@@ -11,9 +11,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -59,9 +59,6 @@ public class SDNCServiceRequestConnector extends SDNCConnector {
TypedRequestTunables rt) {
try {
return parseResponseContent(responseContent);
- } catch (ParseException e) {
- logger.error("Error occured:", e);
- return createErrorResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage(), rt);
} catch (Exception e) {
logger.error("Error occured:", e);
return createErrorResponse(HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage(), rt);
@@ -79,10 +76,10 @@ public class SDNCServiceRequestConnector extends SDNCConnector {
* response code contained in the content. For 2XX response codes, an SDNCServiceResponse is returned. Otherwise, an
* SDNCServiceError is returned. If the content cannot be parsed, or if the content does not contain all required
* elements, a parse exception is thrown. This method performs no logging or alarming.
- *
+ *
* @throws ParseException on error
*/
- public static SDNCResponseCommon parseResponseContent(String responseContent)
+ public SDNCResponseCommon parseResponseContent(String responseContent)
throws ParseException, ParserConfigurationException, SAXException, IOException {
// Note: this document builder is not namespace-aware, so namespaces are ignored.
@@ -119,16 +116,24 @@ public class SDNCServiceRequestConnector extends SDNCConnector {
List<Element> responseParameters = new ArrayList<>();
for (Element child : SDNCAdapterUtils.childElements(configurationResponseCommon)) {
- if ("response-code".equals(child.getNodeName())) {
- responseCode = child.getTextContent();
- } else if ("response-message".equals(child.getNodeName())) {
- responseMessage = child.getTextContent();
- } else if ("svc-request-id".equals(child.getNodeName())) {
- svcRequestId = child.getTextContent();
- } else if ("ack-final-indicator".equals(child.getNodeName())) {
- ackFinalIndicator = child.getTextContent();
- } else if ("response-parameters".equals(child.getNodeName())) {
- responseParameters.add(child);
+
+ switch (child.getNodeName()) {
+ case "response-code":
+ responseCode = child.getTextContent();
+ break;
+ case "response-message":
+ responseMessage = child.getTextContent();
+ break;
+ case "svc-request-id":
+ svcRequestId = child.getTextContent();
+ break;
+ case "ack-final-indicator":
+ ackFinalIndicator = child.getTextContent();
+ break;
+ case "response-parameters":
+ responseParameters.add(child);
+ break;
+ default:
}
}
@@ -167,8 +172,15 @@ public class SDNCServiceRequestConnector extends SDNCConnector {
return new SDNCServiceError(svcRequestId, responseCode, responseMessage, ackFinalIndicator);
}
- // Create a success response object.
+ return createSDNCServiceResponse(responseCode, responseMessage, svcRequestId, ackFinalIndicator,
+ responseParameters);
+
+ }
+
+ private SDNCServiceResponse createSDNCServiceResponse(String responseCode, String responseMessage,
+ String svcRequestId, String ackFinalIndicator, List<Element> responseParameters) throws ParseException {
+ // Create a success response object.
SDNCServiceResponse response =
new SDNCServiceResponse(svcRequestId, responseCode, responseMessage, ackFinalIndicator);
@@ -201,8 +213,6 @@ public class SDNCServiceRequestConnector extends SDNCConnector {
response.addParam(tagName, tagValue);
}
-
return response;
-
}
}
diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java
index 9911cae677..9adc6c52f5 100644
--- a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java
+++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -31,7 +31,8 @@ public class SDNCServiceRequestConnectorTest {
public void parseResponseContentTest() throws Exception {
String content = FileUtil.readResourceFile("SdncServiceResponse.xml");
- SDNCResponseCommon responseCommon = SDNCServiceRequestConnector.parseResponseContent(content);
+ SDNCServiceRequestConnector sdncServiceRequestConnector = new SDNCServiceRequestConnector();
+ SDNCResponseCommon responseCommon = sdncServiceRequestConnector.parseResponseContent(content);
assertNotNull(responseCommon);
}