sonar fix 03/116703/1
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Fri, 8 Jan 2021 16:49:33 +0000 (17:49 +0100)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Fri, 8 Jan 2021 16:49:33 +0000 (17:49 +0100)
- improving logging
- avoid null pointer exception
- sonar bug related to return statement

Issue-ID: SO-3428
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
Change-Id: I7f80fbbb1ea1db19d893599691a9b582ed3cf14d

adapters/etsi-sol003-adapter/etsi-sol003-lcm/etsi-sol003-lcm-adapter/src/main/java/org/onap/so/adapters/etsisol003adapter/lcm/NvfmAdapterUtils.java
adapters/etsi-sol003-adapter/etsi-sol003-lcm/etsi-sol003-lcm-adapter/src/main/java/org/onap/so/adapters/etsisol003adapter/lcm/extclients/EtsiPackageProvider.java
adapters/etsi-sol003-adapter/etsi-sol003-lcm/etsi-sol003-lcm-adapter/src/main/java/org/onap/so/adapters/etsisol003adapter/lcm/extclients/vnfm/VnfmServiceProviderImpl.java

index a46ad8f..367d8b2 100644 (file)
@@ -27,9 +27,13 @@ import org.slf4j.Logger;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 
-public class NvfmAdapterUtils {
+public final class NvfmAdapterUtils {
     private static Logger logger = getLogger(NvfmAdapterUtils.class);
 
+    private NvfmAdapterUtils() {
+        throw new IllegalStateException("Utility class");
+    }
+
     public static JsonObject child(final JsonObject parent, final String name) {
         return childElement(parent, name).getAsJsonObject();
     }
index c5164c1..ec46af4 100644 (file)
@@ -63,13 +63,13 @@ public class EtsiPackageProvider {
     }
 
     private String getVnfNodeProperty(final String csarId, final String propertyName) {
-        logger.debug("Getting " + propertyName + " from " + csarId);
+        logger.debug("Getting {} from {}", propertyName, csarId);
         final byte[] onapPackage = getPackage(csarId);
 
         try {
             final String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(onapPackage));
             final String onapVnfdContent = getFileInZip(new ByteArrayInputStream(onapPackage), vnfdLocation).toString();
-            logger.debug("VNFD CONTENTS: " + onapVnfdContent);
+            logger.debug("VNFD CONTENTS: {}", onapVnfdContent);
             final JsonObject root = new Gson().toJsonTree(new Yaml().load(onapVnfdContent)).getAsJsonObject();
 
             final JsonObject topologyTemplates = child(root, "topology_template");
@@ -79,14 +79,14 @@ public class EtsiPackageProvider {
                 String propertyValue = null;
                 if ("tosca.nodes.nfv.VNF".equals(type)) {
                     final JsonObject properties = child(child, "properties");
-                    logger.debug("properties: " + properties.toString());
-
+                    logger.debug("properties: {}", properties);
                     propertyValue = properties.get(propertyName).getAsJsonPrimitive().getAsString();
+                    if (propertyValue == null) {
+                        propertyValue = getValueFromNodeTypeDefinition(root, type, propertyName);
+                    }
+                    return propertyValue;
                 }
-                if (propertyValue == null) {
-                    propertyValue = getValueFromNodeTypeDefinition(root, type, propertyName);
-                }
-                return propertyValue;
+
             }
 
         } catch (final Exception e) {
@@ -102,10 +102,10 @@ public class EtsiPackageProvider {
 
         if ("tosca.nodes.nfv.VNF".equals(childElement(nodeType, "derived_from").getAsString())) {
             final JsonObject properties = child(nodeType, "properties");
-            logger.debug("properties: " + properties.toString());
+            logger.debug("properties: {}", properties);
             final JsonObject property = child(properties, propertyName);
-            logger.debug("property: " + property.toString());
-            logger.debug("property default: " + childElement(property, "default").toString());
+            logger.debug("property: {}", property);
+            logger.debug("property default: {}", childElement(property, "default"));
             return childElement(property, "default").getAsJsonPrimitive().getAsString();
         }
         return null;
index 6ad5c16..6e0978a 100644 (file)
@@ -40,10 +40,14 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import com.google.common.base.Optional;
+import java.util.List;
 
 @Service
 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
+    private static final String MESSAGE_RESULTED_IN_EXCEPTION = " resulted in exception";
+    private static final String MESSAGE_REQUEST = ", request: ";
+    private static final String MESSAGE_TERMINATE_REQUEST_TO = "Terminate request to ";
 
     private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
     private final VnfmUrlProvider urlProvider;
@@ -63,25 +67,24 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
     @Override
     public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
             final InstantiateVnfRequest instantiateVnfRequest) {
-        logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
-
+        logger.debug("Sending instantiate request {} to : {}", instantiateVnfRequest, vnfSelfLink);
         ResponseEntity<Void> response = null;
         try {
             response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
                     Void.class);
         } catch (final Exception exception) {
             final String errorMessage =
-                    "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
-            logger.error(errorMessage, exception);
+                    "Instantiate request to " + vnfSelfLink + MESSAGE_RESULTED_IN_EXCEPTION + instantiateVnfRequest;
+            logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage, exception);
         }
         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
             final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
-                    + response.getStatusCode() + ", request: " + instantiateVnfRequest;
+                    + response.getStatusCode() + MESSAGE_REQUEST + instantiateVnfRequest;
             logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage);
         }
-        final String locationHeader = response.getHeaders().get("Location").iterator().next();
+        String locationHeader = getLocationHeader(response);
         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
     }
 
@@ -96,13 +99,13 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
             logger.info("Subscribing for notifications response {}", response);
         } catch (final Exception exception) {
             final String errorMessage =
-                    "Subscription to VNFM " + vnfm.getVnfmId() + " resulted in exception" + subscriptionRequest;
-            logger.error(errorMessage, exception);
+                    "Subscription to VNFM " + vnfm.getVnfmId() + MESSAGE_RESULTED_IN_EXCEPTION + subscriptionRequest;
+            logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage, exception);
         }
         if (response.getStatusCode() != HttpStatus.CREATED) {
             final String errorMessage = "Subscription to VNFM " + vnfm.getVnfmId() + " returned status code: "
-                    + response.getStatusCode() + ", request: " + subscriptionRequest;
+                    + response.getStatusCode() + MESSAGE_REQUEST + subscriptionRequest;
             logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage);
         }
@@ -112,8 +115,7 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
     @Override
     public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
             final TerminateVnfRequest terminateVnfRequest) {
-        logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
-
+        logger.debug("Sending terminate request {} to : {}", terminateVnfRequest, vnfSelfLink);
         ResponseEntity<Void> response = null;
         try {
             response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
@@ -124,26 +126,26 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
                 if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) {
                     return JobManager.ALREADY_COMPLETED_OPERATION_ID;
                 } else {
-                    final String errorMessage =
-                            "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
+                    final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink
+                            + MESSAGE_RESULTED_IN_EXCEPTION + terminateVnfRequest;
                     logger.error(errorMessage, restProcessingException);
                     throw new VnfmRequestFailureException(errorMessage, restProcessingException);
                 }
             }
         } catch (final Exception exception) {
             final String errorMessage =
-                    "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
-            logger.error(errorMessage, exception);
+                    MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + MESSAGE_RESULTED_IN_EXCEPTION + terminateVnfRequest;
+            logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage, exception);
         }
         checkIfResponseIsAcceptable(response, vnfSelfLink, terminateVnfRequest);
-        final String locationHeader = response.getHeaders().get("Location").iterator().next();
+        String locationHeader = getLocationHeader(response);
         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
     }
 
     @Override
     public void deleteVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
-        logger.debug("Sending delete request to : " + vnfSelfLink);
+        logger.debug("Sending delete request to : {}", vnfSelfLink);
         final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
         if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
             throw new VnfmRequestFailureException(
@@ -165,8 +167,8 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
             return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
         } catch (final Exception exception) {
             final String errorMessage =
-                    "Create request to vnfm:" + vnfm.getVnfmId() + " resulted in exception" + createVnfRequest;
-            logger.error(errorMessage, exception);
+                    "Create request to vnfm:" + vnfm.getVnfmId() + MESSAGE_RESULTED_IN_EXCEPTION + createVnfRequest;
+            logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage, exception);
         }
     }
@@ -174,14 +176,14 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
     private void checkIfResponseIsAcceptable(final ResponseEntity<Void> response, final String vnfSelfLink,
             final TerminateVnfRequest terminateVnfRequest) {
         if (response == null) {
-            final String errorMessage =
-                    "Terminate request to " + vnfSelfLink + ", response is null, " + "request: " + terminateVnfRequest;
+            final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + ", response is null, "
+                    + "request: " + terminateVnfRequest;
             logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage);
         }
         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
-            final String errorMessage = "Terminate request to " + vnfSelfLink + ", returned status code: "
-                    + response.getStatusCode() + ", request: " + terminateVnfRequest;
+            final String errorMessage = MESSAGE_TERMINATE_REQUEST_TO + vnfSelfLink + ", returned status code: "
+                    + response.getStatusCode() + MESSAGE_REQUEST + terminateVnfRequest;
             logger.error(errorMessage);
             throw new VnfmRequestFailureException(errorMessage);
         }
@@ -191,4 +193,11 @@ public class VnfmServiceProviderImpl implements VnfmServiceProvider {
         return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);
     }
 
+    private String getLocationHeader(ResponseEntity<Void> response) {
+        List<String> headers = response.getHeaders().get("Location");
+        if ((headers == null) || (headers.isEmpty())) {
+            throw new VnfmRequestFailureException("No headers found in response");
+        }
+        return headers.iterator().next();
+    }
 }