diff options
author | k.kazak <k.kazak@samsung.com> | 2019-02-22 09:00:16 +0100 |
---|---|---|
committer | k.kazak <k.kazak@samsung.com> | 2019-02-22 09:00:16 +0100 |
commit | edc2b5329e4dde7b5dcb01b8a8bccf51a8064131 (patch) | |
tree | 8b32df089e0976af75c73e3d02af99fedfa1584e /adapters/mso-adapter-utils/src/main | |
parent | f8842dbd4065e0b90d4ed0d4af9e4cc72ca218dd (diff) |
fix potential nullpointer from sonar
MsoMulticloudUtils: multicloudClient may be null
throw MsoException if it is null (instead of NullPointer)
removed unnecessary StackInfo object creation
using format specifiers for a logger instead of String concatenation
removed else block to make the code easier to read
Added tests for the method
Change-Id: I80d2e0ba2ef2d20c488cdfcf103d9cfa5784ab65
Issue-ID: SO-1516
Signed-off-by: k.kazak <k.kazak@samsung.com>
Diffstat (limited to 'adapters/mso-adapter-utils/src/main')
-rw-r--r-- | adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java index 5c0110b680..49758a1240 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2018 Intel Corp. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ * 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 @@ -20,6 +22,9 @@ package org.onap.so.openstack.utils; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.woorea.openstack.heat.model.CreateStackParam; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; @@ -27,10 +32,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; - import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilderException; - import org.onap.so.adapters.vdu.CloudInfo; import org.onap.so.adapters.vdu.PluginAction; import org.onap.so.adapters.vdu.VduArtifact; @@ -59,10 +62,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.woorea.openstack.heat.model.CreateStackParam; - @Component public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ @@ -194,30 +193,35 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, null); RestClient multicloudClient = getMulticloudClient(multicloudEndpoint); - Response response = multicloudClient.post(multicloudRequest); + if (multicloudClient == null) { + MsoOpenstackException me = new MsoOpenstackException(0, "", "Multicloud client could not be initialized"); + me.addContext(CREATE_STACK); + throw me; + } - StackInfo createInfo = new StackInfo(); - createInfo.setName(stackName); + Response response = multicloudClient.post(multicloudRequest); MulticloudCreateResponse multicloudResponseBody = null; if (response.hasEntity()) { - multicloudResponseBody = getCreateBody((java.io.InputStream)response.getEntity()); + multicloudResponseBody = getCreateBody((java.io.InputStream) response.getEntity()); } if (response.getStatus() == Response.Status.CREATED.getStatusCode() && response.hasEntity()) { - createInfo.setCanonicalName(stackName + "/" + multicloudResponseBody.getWorkloadId()); - if (logger.isDebugEnabled()) { - logger.debug("Multicloud Create Response Body: " + multicloudResponseBody); + String canonicalName = stackName + "/"; + if (multicloudResponseBody != null) { + canonicalName = canonicalName + multicloudResponseBody.getWorkloadId(); } - return getStackStatus(cloudSiteId, tenantId, createInfo.getCanonicalName(), pollForCompletion, timeoutMinutes, backout); - } else { - StringBuilder stackErrorStatusReason = new StringBuilder(response.getStatusInfo().getReasonPhrase()); - if (null != multicloudResponseBody) { - stackErrorStatusReason.append(multicloudResponseBody.toString()); + if (logger.isDebugEnabled()) { + logger.debug("Multicloud Create Response Body: {}", multicloudResponseBody); } - MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString()); - me.addContext(CREATE_STACK); - throw me; + return getStackStatus(cloudSiteId, tenantId, canonicalName, pollForCompletion, timeoutMinutes, backout); + } + StringBuilder stackErrorStatusReason = new StringBuilder(response.getStatusInfo().getReasonPhrase()); + if (null != multicloudResponseBody) { + stackErrorStatusReason.append(multicloudResponseBody.toString()); } + MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString()); + me.addContext(CREATE_STACK); + throw me; } @Override |