X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fsdc%2Fcontroller%2FSdcSingleController.java;h=c9bbd0679ab29b5f50a923cf08f744a69b079bc5;hb=refs%2Fchanges%2F51%2F44451%2F1;hp=3c877254ec8a827fae81358ba13a5296563ef45b;hpb=4c448e99a3d0dbf0b3220aa27188b741aefb6656;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java index 3c877254..c9bbd067 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java @@ -18,7 +18,7 @@ * limitations under the License. * ============LICENSE_END============================================ * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.clamp.clds.sdc.controller; @@ -27,6 +27,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.util.Date; +import java.util.concurrent.ThreadLocalRandom; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration; @@ -38,39 +39,42 @@ import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException; import org.onap.clamp.clds.sdc.controller.installer.CsarHandler; import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller; import org.onap.clamp.clds.util.LoggingUtils; -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; -import org.openecomp.sdc.api.consumer.INotificationCallback; -import org.openecomp.sdc.api.notification.IArtifactInfo; -import org.openecomp.sdc.api.notification.INotificationData; -import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; -import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.impl.DistributionClientFactory; -import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException; -import org.openecomp.sdc.utils.DistributionActionResultEnum; -import org.openecomp.sdc.utils.DistributionStatusEnum; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.consumer.IDistributionStatusMessage; +import org.onap.sdc.api.consumer.INotificationCallback; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.results.IDistributionClientDownloadResult; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.impl.DistributionClientFactory; +import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; +import org.onap.sdc.utils.DistributionActionResultEnum; +import org.onap.sdc.utils.DistributionStatusEnum; -@Component +/** + * This class handles one sdc controller defined in the config. + */ public class SdcSingleController { private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcSingleController.class); - protected boolean isAsdcClientAutoManaged = false; - protected CsarInstaller resourceInstaller; - @Autowired - protected ClampProperties refProp; + private boolean isSdcClientAutoManaged = false; + private CsarInstaller csarInstaller; + private ClampProperties refProp; public static final String CONFIG_SDC_FOLDER = "sdc.csarFolder"; + private int nbOfNotificationsOngoing = 0; + private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED; + private SdcSingleControllerConfiguration sdcConfig; + private IDistributionClient distributionClient; /** * Inner class for Notification callback */ - private final class ASDCNotificationCallBack implements INotificationCallback { + private final class SdcNotificationCallBack implements INotificationCallback { - private SdcSingleController asdcController; + private SdcSingleController sdcController; - ASDCNotificationCallBack(SdcSingleController controller) { - asdcController = controller; + SdcNotificationCallBack(SdcSingleController controller) { + sdcController = controller; } /** @@ -80,23 +84,26 @@ public class SdcSingleController { @Override public void activateCallback(INotificationData iNotif) { Date startTime = new Date(); - String event = "Receive a callback notification in ASDC, nb of resources: " + iNotif.getResources().size(); - logger.debug(event); - asdcController.treatNotification(iNotif); + logger.info("Receive a callback notification in SDC, nb of resources: " + iNotif.getResources().size()); + sdcController.treatNotification(iNotif); LoggingUtils.setTimeContext(startTime, new Date()); LoggingUtils.setResponseContext("0", "SDC Notification received and processed successfully", this.getClass().getName()); } } - // ***** Controller STATUS code - protected int nbOfNotificationsOngoing = 0; - public int getNbOfNotificationsOngoing() { return nbOfNotificationsOngoing; } - private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED; + private void changeControllerStatusIdle() { + if (this.nbOfNotificationsOngoing > 1) { + --this.nbOfNotificationsOngoing; + } else { + this.nbOfNotificationsOngoing = 0; + this.controllerStatus = SdcSingleControllerStatus.IDLE; + } + } protected final synchronized void changeControllerStatus(SdcSingleControllerStatus newControllerStatus) { switch (newControllerStatus) { @@ -105,12 +112,7 @@ public class SdcSingleController { this.controllerStatus = newControllerStatus; break; case IDLE: - if (this.nbOfNotificationsOngoing > 1) { - --this.nbOfNotificationsOngoing; - } else { - this.nbOfNotificationsOngoing = 0; - this.controllerStatus = newControllerStatus; - } + this.changeControllerStatusIdle(); break; default: this.controllerStatus = newControllerStatus; @@ -122,9 +124,13 @@ public class SdcSingleController { return this.controllerStatus; } - // ***** END of Controller STATUS code - protected SdcSingleControllerConfiguration sdcConfig; - private IDistributionClient distributionClient; + public SdcSingleController(ClampProperties clampProp, CsarInstaller csarInstaller, + SdcSingleControllerConfiguration sdcSingleConfig, boolean isClientAutoManaged) { + this.isSdcClientAutoManaged = isClientAutoManaged; + this.sdcConfig = sdcSingleConfig; + this.refProp = clampProp; + this.csarInstaller = csarInstaller; + } /** * This method initializes the SDC Controller and the SDC Client. @@ -137,23 +143,23 @@ public class SdcSingleController { * If there is an issue with the parameters provided */ public void initSdc() throws SdcControllerException { - logger.debug("Attempt to initialize the SDC Controller"); + logger.info("Attempt to initialize the SDC Controller"); if (this.getControllerStatus() != SdcSingleControllerStatus.STOPPED) { throw new SdcControllerException("The controller is already initialized, call the closeSDC method first"); } if (this.distributionClient == null) { distributionClient = DistributionClientFactory.createDistributionClient(); } - IDistributionClientResult result = this.distributionClient.init(sdcConfig, new ASDCNotificationCallBack(this)); + IDistributionClientResult result = this.distributionClient.init(sdcConfig, new SdcNotificationCallBack(this)); if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { - logger.error("ASDC distribution client init failed with reason:" + result.getDistributionMessageResult()); + logger.error("SDC distribution client init failed with reason:" + result.getDistributionMessageResult()); this.changeControllerStatus(SdcSingleControllerStatus.STOPPED); throw new SdcControllerException("Initialization of the SDC Controller failed with reason: " + result.getDistributionMessageResult()); } result = this.distributionClient.start(); if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { - logger.debug("SDC distribution client start failed with reason:" + result.getDistributionMessageResult()); + logger.error("SDC distribution client start failed with reason:" + result.getDistributionMessageResult()); this.changeControllerStatus(SdcSingleControllerStatus.STOPPED); throw new SdcControllerException( "Startup of the SDC Controller failed with reason: " + result.getDistributionMessageResult()); @@ -170,15 +176,15 @@ public class SdcSingleController { */ public void closeSdc() throws SdcControllerException { if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) { - throw new SdcControllerException("Cannot close the ASDC controller as it's currently in BUSY state"); + throw new SdcControllerException("Cannot close the SDC controller as it's currently in BUSY state"); } if (this.distributionClient != null) { this.distributionClient.stop(); // If auto managed we can set it to Null, SdcController controls it. // In the other case the client of this class has specified it, so // we can't reset it - if (isAsdcClientAutoManaged) { - // Next init will initialize it with a new Sdc Client + if (isSdcClientAutoManaged) { + // Next init will initialize it with a new SDC Client this.distributionClient = null; } } @@ -194,46 +200,54 @@ public class SdcSingleController { public void treatNotification(INotificationData iNotif) { CsarHandler csar = null; try { + // wait for a random time, so that 2 running Clamp will not treat + // the same Notification at the same time + long i = ThreadLocalRandom.current().nextInt(1, 5); + Thread.sleep(i * 1000L); logger.info("Notification received for service UUID:" + iNotif.getServiceUUID()); this.changeControllerStatus(SdcSingleControllerStatus.BUSY); csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(), refProp.getStringValue(CONFIG_SDC_FOLDER)); - if (resourceInstaller.isCsarAlreadyDeployed(csar)) { - csar.save(downloadTheArtifact(csar.getArtifactElement())); - this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), - sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null, - System.currentTimeMillis()); - resourceInstaller.installTheCsar(csar); - this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), - sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null, - System.currentTimeMillis()); - } else { - this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), + csar.save(downloadTheArtifact(csar.getArtifactElement())); + if (csarInstaller.isCsarAlreadyDeployed(csar)) { + this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.ALREADY_DOWNLOADED, null, System.currentTimeMillis()); - this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), + this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.ALREADY_DEPLOYED, null, System.currentTimeMillis()); + } else { + this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), + sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null, + System.currentTimeMillis()); + csarInstaller.installTheCsar(csar); + this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), + sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null, + System.currentTimeMillis()); } } catch (SdcArtifactInstallerException e) { logger.error("SdcArtifactInstallerException exception caught during the notification processing", e); - this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), + this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR, e.getMessage(), System.currentTimeMillis()); } catch (SdcDownloadException e) { logger.error("SdcDownloadException exception caught during the notification processing", e); - this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), + this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage(), System.currentTimeMillis()); } catch (CsarHandlerException e) { logger.error("CsarHandlerException exception caught during the notification processing", e); - this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(), - sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, - e.getMessage(), System.currentTimeMillis()); + this.sendSdcNotification(NotificationType.DOWNLOAD, null, sdcConfig.getConsumerID(), + iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage(), + System.currentTimeMillis()); } catch (SdcToscaParserException e) { - this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), + logger.error("SdcToscaParserException exception caught during the notification processing", e); + this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(), sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR, e.getMessage(), System.currentTimeMillis()); + } catch (InterruptedException e) { + logger.error("Interrupt exception caught during the notification processing", e); + Thread.currentThread().interrupt(); } catch (RuntimeException e) { logger.error("Unexpected exception caught during the notification processing", e); } finally { @@ -246,7 +260,7 @@ public class SdcSingleController { } private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException { - logger.debug("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: " + logger.info("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: " + artifact.getArtifactUUID()); IDistributionClientDownloadResult downloadResult; try { @@ -263,17 +277,17 @@ public class SdcSingleController { + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length); } else { throw new SdcDownloadException("Artifact " + artifact.getArtifactName() - + " could not be downloaded from ASDC URL " + artifact.getArtifactURL() + " UUID " + + " could not be downloaded from SDC URL " + artifact.getArtifactURL() + " UUID " + artifact.getArtifactUUID() + ")" + System.lineSeparator() + "Error message is " + downloadResult.getDistributionMessageResult() + System.lineSeparator()); } return downloadResult; } - private void sendASDCNotification(NotificationType notificationType, String artifactURL, String consumerID, + private void sendSdcNotification(NotificationType notificationType, String artifactURL, String consumerID, String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) { String event = "Sending " + notificationType.name() + "(" + status.name() + ")" - + " notification to ASDC for artifact:" + artifactURL; + + " notification to SDC for artifact:" + artifactURL; if (errorReason != null) { event = event + "(" + errorReason + ")"; } @@ -284,27 +298,35 @@ public class SdcSingleController { status, timestamp); switch (notificationType) { case DOWNLOAD: - if (errorReason != null) { - this.distributionClient.sendDownloadStatus(message, errorReason); - } else { - this.distributionClient.sendDownloadStatus(message); - } + this.sendDownloadStatus(message, errorReason); action = "sendDownloadStatus"; break; case DEPLOY: - if (errorReason != null) { - this.distributionClient.sendDeploymentStatus(message, errorReason); - } else { - this.distributionClient.sendDeploymentStatus(message); - } + this.sendDeploymentStatus(message, errorReason); action = "sendDeploymentdStatus"; break; default: break; } } catch (RuntimeException e) { - logger.warn("Unable to send the Sdc Notification (" + action + ") due to an exception", e); + logger.warn("Unable to send the SDC Notification (" + action + ") due to an exception", e); + } + logger.info("SDC Notification sent successfully(" + action + ")"); + } + + private void sendDownloadStatus(IDistributionStatusMessage message, String errorReason) { + if (errorReason != null) { + this.distributionClient.sendDownloadStatus(message, errorReason); + } else { + this.distributionClient.sendDownloadStatus(message); + } + } + + private void sendDeploymentStatus(IDistributionStatusMessage message, String errorReason) { + if (errorReason != null) { + this.distributionClient.sendDeploymentStatus(message, errorReason); + } else { + this.distributionClient.sendDeploymentStatus(message); } - logger.info("Sdc Notification sent successfully(" + action + ")"); } }