diff options
author | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-02-12 10:01:26 +0100 |
---|---|---|
committer | Tomasz Wrobel <tomasz.wrobel@nokia.com> | 2020-02-17 08:41:25 +0100 |
commit | 1ecb545c877078b0f68979101ca46c6de0dd1d6a (patch) | |
tree | f08c31f3e0793bdb9242e4fed86c0c2092c327ad /asdc-controller/src/main/java | |
parent | 78eb0176ae83484534789cafb60f773c8f8d3f44 (diff) |
Sonar issue - refactor nested if
Issue-ID: SO-1841
Change-Id: I72d349501d6493367204396eb78e7b77a275df8f
Signed-off-by: Tomasz Wrobel <tomasz.wrobel@nokia.com>
Diffstat (limited to 'asdc-controller/src/main/java')
3 files changed, 160 insertions, 134 deletions
diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/activity/ActivitySpecsActions.java b/asdc-controller/src/main/java/org/onap/so/asdc/activity/ActivitySpecsActions.java index 06887f1253..a48e1770f0 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/activity/ActivitySpecsActions.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/activity/ActivitySpecsActions.java @@ -4,12 +4,15 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2020 Nokia + * ================================================================================ + * * 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. @@ -29,7 +32,6 @@ import org.onap.so.asdc.activity.beans.ActivitySpec; import org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse; import org.onap.so.client.HttpClient; import org.onap.so.client.HttpClientFactory; -import org.onap.so.logger.LoggingAnchor; import org.onap.logging.filter.base.ONAPComponents; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,8 +55,6 @@ public class ActivitySpecsActions { return null; } - String activitySpecId = null; - try { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); @@ -71,30 +71,34 @@ public class ActivitySpecsActions { int statusCode = response.getStatus(); if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) { logger.warn(LoggingAnchor.THREE, "ActivitySpec", activitySpec.getName(), "already exists in SDC"); - } else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) { + return null; + } + if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) { logger.warn(LoggingAnchor.THREE, "Error creating activity spec", activitySpec.getName(), statusCode); - } else { - if (response.getEntity() != null) { - ActivitySpecCreateResponse activitySpecCreateResponse = - response.readEntity(ActivitySpecCreateResponse.class); - if (activitySpecCreateResponse != null) { - activitySpecId = activitySpecCreateResponse.getId(); - } else { - logger.warn(LoggingAnchor.TWO, "Unable to read activity spec", activitySpec.getName()); - } - } else { - logger.warn(LoggingAnchor.TWO, "No activity spec response returned", activitySpec.getName()); - } + return null; + } + + if (response.getEntity() == null) { + logger.warn(LoggingAnchor.TWO, "No activity spec response returned", activitySpec.getName()); + return null; } + ActivitySpecCreateResponse activitySpecCreateResponse = + response.readEntity(ActivitySpecCreateResponse.class); + if (activitySpecCreateResponse == null) { + logger.warn(LoggingAnchor.TWO, "Unable to read activity spec", activitySpec.getName()); + return null; + } + return activitySpecCreateResponse.getId(); + + } catch (Exception e) { logger.warn(LoggingAnchor.TWO, "Exception creating activitySpec", e); } - return activitySpecId; + return null; } public boolean certifyActivitySpec(String hostname, String activitySpecId) { - boolean certificationResult = false; if (activitySpecId == null) { return false; } @@ -114,16 +118,19 @@ public class ActivitySpecsActions { if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) { logger.warn(LoggingAnchor.THREE, "ActivitySpec with id", activitySpecId, "is already certified in SDC"); - } else if (statusCode != HttpStatus.SC_OK) { + return false; + } + if (statusCode != HttpStatus.SC_OK) { logger.warn(LoggingAnchor.THREE, "Error certifying activity", activitySpecId, statusCode); - } else { - certificationResult = true; + return false; } + return true; + } catch (Exception e) { logger.warn(LoggingAnchor.TWO, "Exception certifying activitySpec", e); + return false; } - return certificationResult; } } diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/activity/DeployActivitySpecs.java b/asdc-controller/src/main/java/org/onap/so/asdc/activity/DeployActivitySpecs.java index e53b792a74..df8e828a16 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/activity/DeployActivitySpecs.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/activity/DeployActivitySpecs.java @@ -4,12 +4,14 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Modifications Copyright (c) 2020 Nokia + * ================================================================================ * 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. @@ -74,17 +76,19 @@ public class DeployActivitySpecs { logger.debug("{} {}", "Attempting to create activity ", activitySpecFromCatalog.getName()); ActivitySpec activitySpec = mapActivitySpecFromCatalogToSdc(activitySpecFromCatalog); String activitySpecId = activitySpecsActions.createActivitySpec(hostname, activitySpec); - if (activitySpecId != null) { - logger.info(LoggingAnchor.TWO, "Successfully created activitySpec", activitySpec.getName()); - boolean certificationResult = activitySpecsActions.certifyActivitySpec(hostname, activitySpecId); - if (certificationResult) { - logger.info(LoggingAnchor.TWO, "Successfully certified activitySpec", activitySpec.getName()); - } else { - logger.info(LoggingAnchor.TWO, "Failed to certify activitySpec", activitySpec.getName()); - } - } else { + if (activitySpecId == null) { logger.info(LoggingAnchor.TWO, "Failed to create activitySpec", activitySpec.getName()); + continue; + } + + logger.info(LoggingAnchor.TWO, "Successfully created activitySpec", activitySpec.getName()); + boolean certificationResult = activitySpecsActions.certifyActivitySpec(hostname, activitySpecId); + if (!certificationResult) { + logger.info(LoggingAnchor.TWO, "Failed to certify activitySpec", activitySpec.getName()); + continue; } + + logger.info(LoggingAnchor.TWO, "Successfully certified activitySpec", activitySpec.getName()); } } @@ -120,46 +124,41 @@ public class DeployActivitySpecs { List<Input> inputs = new ArrayList<>(); List<Output> outputs = new ArrayList<>(); for (ActivitySpecActivitySpecParameters activitySpecParam : activitySpecActivitySpecParameters) { - if (activitySpecParam != null) { - if (activitySpecParam.getActivitySpecParameters() != null) { - ActivitySpecParameters activitySpecParameters = activitySpecParam.getActivitySpecParameters(); - if (activitySpecParameters != null) { - if (activitySpecParameters.getDirection().equals(DIRECTION_INPUT)) { - Input input = new Input(); - input.setName(activitySpecParameters.getName()); - input.setType(activitySpecParameters.getType()); - inputs.add(input); - } else if (activitySpecParameters.getDirection().equals(DIRECTION_OUTPUT)) { - Output output = new Output(); - output.setName(activitySpecParameters.getName()); - output.setType(activitySpecParameters.getType()); - outputs.add(output); - } - } - } + if (activitySpecParam == null || activitySpecParam.getActivitySpecParameters() == null) { + continue; + } + ActivitySpecParameters activitySpecParameters = activitySpecParam.getActivitySpecParameters(); + + if (activitySpecParameters.getDirection().equals(DIRECTION_INPUT)) { + Input input = new Input(); + input.setName(activitySpecParameters.getName()); + input.setType(activitySpecParameters.getType()); + inputs.add(input); + continue; + } + if (activitySpecParameters.getDirection().equals(DIRECTION_OUTPUT)) { + Output output = new Output(); + output.setName(activitySpecParameters.getName()); + output.setType(activitySpecParameters.getType()); + outputs.add(output); } } activitySpec.setInputs(inputs); activitySpec.setOutputs(outputs); - return; } public boolean checkHttpServerUp(String host) { - URL url = null; - boolean isUp = false; - - int responseCode = 0; try { - url = new URL(host); + URL url = new URL(host); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); - responseCode = connection.getResponseCode(); + int responseCode = connection.getResponseCode(); + if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_NOT_FOUND) { + return true; + } } catch (Exception e) { logger.warn("Exception on connecting to SDC WFD endpoint: ", e); } - if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_NOT_FOUND) { - isUp = true; - } - return isUp; + return false; } } diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/client/ASDCController.java b/asdc-controller/src/main/java/org/onap/so/asdc/client/ASDCController.java index f64adfd760..c9d4f4d92f 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/client/ASDCController.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/client/ASDCController.java @@ -7,6 +7,8 @@ * ================================================================================ * Modifications Copyright (c) 2019 Samsung * ================================================================================ + * Modifications Copyright (c) 2020 Nokia + * ================================================================================ * 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 @@ -33,6 +35,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.List; import java.util.Optional; @@ -162,12 +165,7 @@ public class ASDCController { break; case IDLE: - if (this.nbOfNotificationsOngoing > 1) { - --this.nbOfNotificationsOngoing; - } else { - this.nbOfNotificationsOngoing = 0; - this.controllerStatus = newControllerStatus; - } + changeOnStatusIDLE(newControllerStatus); break; default: @@ -177,6 +175,15 @@ public class ASDCController { } } + private void changeOnStatusIDLE(ASDCControllerStatus newControllerStatus) { + if (this.nbOfNotificationsOngoing > 1) { + --this.nbOfNotificationsOngoing; + } else { + this.nbOfNotificationsOngoing = 0; + this.controllerStatus = newControllerStatus; + } + } + public ASDCControllerStatus getControllerStatus() { return this.controllerStatus; } @@ -296,13 +303,12 @@ public class ASDCController { protected void notifyErrorToAsdc(INotificationData iNotif, ToscaResourceStructure toscaResourceStructure, DistributionStatusEnum deployStatus, VfResourceStructure resourceStructure, String errorMessage) { // do csar lever first - this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus, errorMessage); + this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage); // at resource level for (IResourceInstance resource : iNotif.getResources()) { resourceStructure = new VfResourceStructure(iNotif, resource); errorMessage = String.format("Resource with UUID: %s already exists", resource.getResourceUUID()); - this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus, - errorMessage); + this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage); } } @@ -363,8 +369,7 @@ public class ASDCController { if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) { logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_SUC.toString(), - artifact.getArtifactURL(), artifact.getArtifactUUID(), - String.valueOf(downloadResult.getArtifactPayload().length)); + artifact.getArtifactURL(), artifact.getArtifactUUID(), downloadResult.getArtifactPayload().length); } else { logger.error(LoggingAnchor.SEVEN, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(), @@ -447,7 +452,7 @@ public class ASDCController { } } - protected void sendCsarDeployNotification(INotificationData iNotif, ResourceStructure resourceStructure, + protected void sendCsarDeployNotification(ResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure, DistributionStatusEnum statusEnum, String errorReason) { IArtifactInfo csarArtifact = toscaResourceStructure.getToscaArtifact(); @@ -471,8 +476,8 @@ public class ASDCController { } catch (ArtifactInstallerException e) { logger.info(LoggingAnchor.SIX, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(), resourceStructure.getResourceInstance().getResourceName(), - resourceStructure.getResourceInstance().getResourceUUID(), - String.valueOf(resourceStructure.getNumberOfResources()), "ASDC", "deployResourceStructure"); + resourceStructure.getResourceInstance().getResourceUUID(), resourceStructure.getNumberOfResources(), + "ASDC", "deployResourceStructure"); sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_ERROR, e.getMessage()); throw e; } @@ -480,8 +485,8 @@ public class ASDCController { if (resourceStructure.isDeployedSuccessfully() || toscaResourceStructure.isDeployedSuccessfully()) { logger.info(LoggingAnchor.SIX, MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC.toString(), resourceStructure.getResourceInstance().getResourceName(), - resourceStructure.getResourceInstance().getResourceUUID(), - String.valueOf(resourceStructure.getNumberOfResources()), "ASDC", "deployResourceStructure"); + resourceStructure.getResourceInstance().getResourceUUID(), resourceStructure.getNumberOfResources(), + "ASDC", "deployResourceStructure"); sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_OK, null); } @@ -509,26 +514,10 @@ public class ASDCController { try { IDistributionStatusMessage message = new DistributionStatusMessage(artifactURL, consumerID, distributionID, status, timestamp); - - switch (notificationType) { - case DOWNLOAD: - if (errorReason != null) { - this.distributionClient.sendDownloadStatus(message, errorReason); - } else { - this.distributionClient.sendDownloadStatus(message); - } - - break; - case DEPLOY: - if (errorReason != null) { - this.distributionClient.sendDeploymentStatus(message, errorReason); - } else { - this.distributionClient.sendDeploymentStatus(message); - } - - break; - default: - break; + if (errorReason != null) { + sendNotificationWithMessageAndErrorReason(notificationType, errorReason, message); + } else { + sendNotificationWithMessage(notificationType, message); } } catch (RuntimeException e) { logger.warn(LoggingAnchor.FIVE, MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC.toString(), "ASDC", @@ -537,6 +526,33 @@ public class ASDCController { } } + private void sendNotificationWithMessage(NotificationType notificationType, IDistributionStatusMessage message) { + switch (notificationType) { + case DOWNLOAD: + this.distributionClient.sendDownloadStatus(message); + break; + case DEPLOY: + this.distributionClient.sendDeploymentStatus(message); + break; + default: + break; + } + } + + private void sendNotificationWithMessageAndErrorReason(NotificationType notificationType, String errorReason, + IDistributionStatusMessage message) { + switch (notificationType) { + case DOWNLOAD: + this.distributionClient.sendDownloadStatus(message, errorReason); + break; + case DEPLOY: + this.distributionClient.sendDeploymentStatus(message, errorReason); + break; + default: + break; + } + } + protected void sendFinalDistributionStatus(String distributionID, DistributionStatusEnum status, String errorReason) { @@ -587,8 +603,8 @@ public class ASDCController { for (IResourceInstance resource : iNotif.getResources()) { noOfArtifacts += resource.getArtifacts().size(); } - logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF.toString(), - String.valueOf(noOfArtifacts), iNotif.getServiceUUID(), "ASDC"); + logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF.toString(), noOfArtifacts, + iNotif.getServiceUUID(), "ASDC"); try { if (iNotif.getDistributionID() != null && !iNotif.getDistributionID().isEmpty()) { @@ -721,7 +737,6 @@ public class ASDCController { protected void processResourceNotification(INotificationData iNotif) { // For each artifact, create a structure describing the VFModule in a ordered flat level - ResourceStructure resourceStructure = null; String msoConfigPath = getMsoConfigPath(); ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure(msoConfigPath); DistributionStatusEnum deployStatus = DistributionStatusEnum.DEPLOY_OK; @@ -734,6 +749,7 @@ public class ASDCController { return; } + ResourceStructure resourceStructure = null; for (IResourceInstance resource : iNotif.getResources()) { String resourceType = resource.getResourceType(); @@ -741,21 +757,11 @@ public class ASDCController { logger.info("Processing Resource Type: {}, Model UUID: {}", resourceType, resource.getResourceUUID()); - if ("VF".equals(resourceType)) { - resourceStructure = new VfResourceStructure(iNotif, resource); - } else if ("PNF".equals(resourceType)) { - resourceStructure = new PnfResourceStructure(iNotif, resource); - } else { - // There are cases where the Service has no VF resources, those are handled here - logger.info("No resources found for Service: {}", iNotif.getServiceUUID()); - resourceStructure = new VfResourceStructure(iNotif, new ResourceInstance()); - resourceStructure.setResourceType(ResourceType.OTHER); - } + resourceStructure = getResourceStructure(iNotif, resource, resourceType); try { if (!this.checkResourceAlreadyDeployed(resourceStructure, serviceDeployed)) { - logger.debug("Processing Resource Type: " + resourceType + " and Model UUID: " + resourceStructure.getResourceInstance().getResourceUUID()); @@ -765,25 +771,27 @@ public class ASDCController { for (IArtifactInfo artifact : resource.getArtifacts()) { IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact, iNotif.getDistributionID()); - if (resultArtifact != null) { - - if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) { - logger.debug("VF_MODULE_ARTIFACT: " - + new String(resultArtifact.getArtifactPayload(), "UTF-8")); - logger.debug(ASDCNotificationLogging - .dumpVfModuleMetaDataList(((VfResourceStructure) resourceStructure) - .decodeVfModuleArtifact(resultArtifact.getArtifactPayload()))); - } - if (!ASDCConfiguration.WORKFLOW.equals(artifact.getArtifactType())) { - resourceStructure.addArtifactToStructure(distributionClient, artifact, - resultArtifact); - } else { - writeArtifactToFile(artifact, resultArtifact); - logger.debug( - "Adding workflow artifact to structure: " + artifact.getArtifactName()); - resourceStructure.addWorkflowArtifactToStructure(artifact, resultArtifact); - } + if (resultArtifact == null) { + continue; } + + if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) { + logger.debug("VF_MODULE_ARTIFACT: " + + new String(resultArtifact.getArtifactPayload(), StandardCharsets.UTF_8)); + logger.debug(ASDCNotificationLogging + .dumpVfModuleMetaDataList(((VfResourceStructure) resourceStructure) + .decodeVfModuleArtifact(resultArtifact.getArtifactPayload()))); + } + if (!ASDCConfiguration.WORKFLOW.equals(artifact.getArtifactType())) { + resourceStructure.addArtifactToStructure(distributionClient, artifact, + resultArtifact); + } else { + writeArtifactToFile(artifact, resultArtifact); + logger.debug( + "Adding workflow artifact to structure: " + artifact.getArtifactName()); + resourceStructure.addWorkflowArtifactToStructure(artifact, resultArtifact); + } + } // Deploy VF resource and artifacts @@ -817,8 +825,7 @@ public class ASDCController { } } - this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus, - errorMessage); + this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage); } catch (ASDCDownloadException | UnsupportedEncodingException e) { logger.error(LoggingAnchor.SIX, MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(), @@ -827,6 +834,20 @@ public class ASDCController { } } + private ResourceStructure getResourceStructure(INotificationData iNotif, IResourceInstance resource, + String resourceType) { + if ("VF".equals(resourceType)) { + return new VfResourceStructure(iNotif, resource); + } + if ("PNF".equals(resourceType)) { + return new PnfResourceStructure(iNotif, resource); + } + logger.info("No resources found for Service: {}", iNotif.getServiceUUID()); + ResourceStructure resourceStructure = new VfResourceStructure(iNotif, new ResourceInstance()); + resourceStructure.setResourceType(ResourceType.OTHER); + return resourceStructure; + } + private String getMsoConfigPath() { String msoConfigPath = System.getProperty("mso.config.path"); if (msoConfigPath == null) { @@ -900,7 +921,6 @@ public class ASDCController { } - /** * @return the address of the ASDC we are connected to. */ |