- 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
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
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 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();
}
public static JsonObject child(final JsonObject parent, final String name) {
return childElement(parent, name).getAsJsonObject();
}
}
private String getVnfNodeProperty(final String csarId, final String propertyName) {
}
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();
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");
final JsonObject root = new Gson().toJsonTree(new Yaml().load(onapVnfdContent)).getAsJsonObject();
final JsonObject topologyTemplates = child(root, "topology_template");
String propertyValue = null;
if ("tosca.nodes.nfv.VNF".equals(type)) {
final JsonObject properties = child(child, "properties");
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();
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) {
}
} catch (final Exception e) {
if ("tosca.nodes.nfv.VNF".equals(childElement(nodeType, "derived_from").getAsString())) {
final JsonObject properties = child(nodeType, "properties");
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);
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;
return childElement(property, "default").getAsJsonPrimitive().getAsString();
}
return null;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.google.common.base.Optional;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.google.common.base.Optional;
@Service
public class VnfmServiceProviderImpl implements VnfmServiceProvider {
private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
@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;
private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
private final VnfmUrlProvider urlProvider;
@Override
public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
final InstantiateVnfRequest instantiateVnfRequest) {
@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 =
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: "
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);
}
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);
}
return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
}
logger.info("Subscribing for notifications response {}", response);
} catch (final Exception exception) {
final String errorMessage =
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: "
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);
}
logger.error(errorMessage);
throw new VnfmRequestFailureException(errorMessage);
}
@Override
public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
final TerminateVnfRequest terminateVnfRequest) {
@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",
ResponseEntity<Void> response = null;
try {
response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
if (vnf.getInstantiationState().equals(InstantiationStateEnum.NOT_INSTANTIATED)) {
return JobManager.ALREADY_COMPLETED_OPERATION_ID;
} else {
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 =
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);
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) {
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(
final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
throw new VnfmRequestFailureException(
return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
} catch (final Exception exception) {
final String errorMessage =
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);
}
}
throw new VnfmRequestFailureException(errorMessage, exception);
}
}
private void checkIfResponseIsAcceptable(final ResponseEntity<Void> response, final String vnfSelfLink,
final TerminateVnfRequest terminateVnfRequest) {
if (response == null) {
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) {
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);
}
logger.error(errorMessage);
throw new VnfmRequestFailureException(errorMessage);
}
return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);
}
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();
+ }