2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  *  Copyright (C) 2019 Nordix Foundation.
 
   5  *  Copyright (C) 2020 AT&T Inc.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  19  * SPDX-License-Identifier: Apache-2.0
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.policy.distribution.reception.handling.sdc;
 
  26 import java.io.FileOutputStream;
 
  27 import java.io.IOException;
 
  28 import java.nio.file.Files;
 
  29 import java.nio.file.Path;
 
  30 import java.util.concurrent.atomic.AtomicInteger;
 
  31 import org.onap.policy.common.parameters.ParameterService;
 
  32 import org.onap.policy.distribution.model.Csar;
 
  33 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  34 import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
 
  35 import org.onap.policy.distribution.reception.handling.sdc.SdcClientHandler.SdcClientOperationType;
 
  36 import org.onap.policy.distribution.reception.handling.sdc.exceptions.ArtifactDownloadException;
 
  37 import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
 
  38 import org.onap.sdc.api.IDistributionClient;
 
  39 import org.onap.sdc.api.consumer.IComponentDoneStatusMessage;
 
  40 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
 
  41 import org.onap.sdc.api.consumer.INotificationCallback;
 
  42 import org.onap.sdc.api.notification.IArtifactInfo;
 
  43 import org.onap.sdc.api.notification.INotificationData;
 
  44 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
 
  45 import org.onap.sdc.api.results.IDistributionClientResult;
 
  46 import org.onap.sdc.impl.DistributionClientImpl;
 
  47 import org.onap.sdc.utils.DistributionActionResultEnum;
 
  48 import org.onap.sdc.utils.DistributionStatusEnum;
 
  49 import org.slf4j.Logger;
 
  50 import org.slf4j.LoggerFactory;
 
  53  * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
 
  55 public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback {
 
  57     private static final Logger LOGGER = LoggerFactory.getLogger(SdcReceptionHandler.class);
 
  58     private static final String SECONDS = "Seconds";
 
  60     private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED;
 
  61     private IDistributionClient distributionClient;
 
  62     private SdcConfiguration sdcConfig;
 
  63     private AtomicInteger nbOfNotificationsOngoing = new AtomicInteger();
 
  64     private int retryDelay;
 
  65     private SdcClientHandler sdcClientHandler;
 
  67     private enum DistributionStatusType {
 
  72     protected void initializeReception(final String parameterGroupName) {
 
  73         final SdcReceptionHandlerConfigurationParameterGroup handlerParameters =
 
  74                 ParameterService.get(parameterGroupName);
 
  75         retryDelay = handlerParameters.getRetryDelay() < 30 ? 30 : handlerParameters.getRetryDelay();
 
  76         sdcConfig = new SdcConfiguration(handlerParameters);
 
  77         distributionClient = createSdcDistributionClient();
 
  78         sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.START, retryDelay);
 
  82     public void destroy() {
 
  83         if (distributionClient != null) {
 
  84             sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.STOP, retryDelay);
 
  89     public void activateCallback(final INotificationData notificationData) {
 
  90         LOGGER.debug("Receieved the notification from SDC with ID: {}", notificationData.getDistributionID());
 
  91         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.BUSY);
 
  92         processCsarServiceArtifacts(notificationData);
 
  93         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
 
  94         LOGGER.debug("Processed the notification from SDC with ID: {}", notificationData.getDistributionID());
 
  98      * Method to change the status of this reception handler instance.
 
 100      * @param newStatus the new status
 
 102     private final synchronized void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
 
 106                 sdcReceptionHandlerStatus = newStatus;
 
 109                 handleIdleStatusChange(newStatus);
 
 112                 nbOfNotificationsOngoing.incrementAndGet();
 
 113                 sdcReceptionHandlerStatus = newStatus;
 
 121      * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
 
 123      * @return the {@link IDistributionClient} instance
 
 125     protected IDistributionClient createSdcDistributionClient() {
 
 126         return new DistributionClientImpl();
 
 130      * Method to initialize the SDC client.
 
 133     protected void initializeSdcClient() {
 
 135         LOGGER.debug("Initializing the SDC Client...");
 
 136         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
 
 137             LOGGER.error("The SDC Client is already initialized");
 
 140         final IDistributionClientResult clientResult = distributionClient.init(sdcConfig, this);
 
 141         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 142             LOGGER.error("SDC client initialization failed with reason: {}. Initialization will be retried after {} {}",
 
 143                     clientResult.getDistributionMessageResult(), retryDelay, SECONDS);
 
 146         LOGGER.debug("SDC Client is initialized successfully");
 
 147         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
 
 151      * Method to start the SDC client.
 
 154     protected void startSdcClient() {
 
 156         LOGGER.debug("Going to start the SDC Client...");
 
 157         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.INIT) {
 
 158             LOGGER.error("The SDC Client is not initialized");
 
 161         final IDistributionClientResult clientResult = distributionClient.start();
 
 162         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 163             LOGGER.error("SDC client start failed with reason: {}. Start will be retried after {} {}",
 
 164                     clientResult.getDistributionMessageResult(), retryDelay, SECONDS);
 
 167         LOGGER.debug("SDC Client is started successfully");
 
 168         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
 
 169         sdcClientHandler.cancel();
 
 173      * Method to stop the SDC client.
 
 176     protected void stopSdcClient() {
 
 177         LOGGER.debug("Going to stop the SDC Client...");
 
 178         final IDistributionClientResult clientResult = distributionClient.stop();
 
 179         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 180             LOGGER.error("SDC client stop failed with reason: {}. Stop will be retried after {} {}",
 
 181                     clientResult.getDistributionMessageResult(), retryDelay, SECONDS);
 
 184         LOGGER.debug("SDC Client is stopped successfully");
 
 185         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
 
 186         sdcClientHandler.cancel();
 
 190      * Method to process csar service artifacts from incoming SDC notification.
 
 192      * @param notificationData the notification from SDC
 
 194     public void processCsarServiceArtifacts(final INotificationData notificationData) {
 
 195         boolean artifactsProcessedSuccessfully = true;
 
 196         DistributionStatisticsManager.updateTotalDistributionCount();
 
 197         for (final IArtifactInfo artifact : notificationData.getServiceArtifacts()) {
 
 199                 final IDistributionClientDownloadResult resultArtifact =
 
 200                         downloadTheArtifact(artifact, notificationData);
 
 201                 final Path filePath = writeArtifactToFile(artifact, resultArtifact);
 
 202                 final Csar csarObject = new Csar(filePath.toString());
 
 203                 inputReceived(csarObject);
 
 204                 sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(),
 
 205                         notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null);
 
 206                 deleteArtifactFile(filePath);
 
 207             } catch (final ArtifactDownloadException | PolicyDecodingException exp) {
 
 208                 LOGGER.error("Failed to process csar service artifacts ", exp);
 
 209                 artifactsProcessedSuccessfully = false;
 
 210                 sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(),
 
 211                         notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,
 
 212                         "Failed to deploy the artifact due to: " + exp.getMessage());
 
 215         if (artifactsProcessedSuccessfully) {
 
 216             DistributionStatisticsManager.updateDistributionSuccessCount();
 
 217             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_OK,
 
 220             DistributionStatisticsManager.updateDistributionFailureCount();
 
 221             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_ERROR,
 
 222                     "Failed to process the artifact");
 
 227      * Method to download the distribution artifact.
 
 229      * @param artifact the artifact
 
 230      * @return the download result
 
 231      * @throws ArtifactDownloadException if download fails
 
 233     private IDistributionClientDownloadResult downloadTheArtifact(final IArtifactInfo artifact,
 
 234             final INotificationData notificationData) throws ArtifactDownloadException {
 
 236         DistributionStatisticsManager.updateTotalDownloadCount();
 
 237         final IDistributionClientDownloadResult downloadResult = distributionClient.download(artifact);
 
 238         if (!downloadResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 239             DistributionStatisticsManager.updateDownloadFailureCount();
 
 240             final String message = "Failed to download artifact with name: " + artifact.getArtifactName() + " due to: "
 
 241                     + downloadResult.getDistributionMessageResult();
 
 242             LOGGER.error(message);
 
 243             sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
 
 244                     notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, message);
 
 245             throw new ArtifactDownloadException(message);
 
 247         DistributionStatisticsManager.updateDownloadSuccessCount();
 
 248         sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
 
 249                 notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null);
 
 250         return downloadResult;
 
 254      * Method to write the downloaded distribution artifact to local file system.
 
 256      * @param artifact the notification artifact
 
 257      * @param resultArtifact the download result artifact
 
 258      * @return the local path of written file
 
 259      * @throws ArtifactDownloadException if error occurs while writing the artifact
 
 261     private Path writeArtifactToFile(final IArtifactInfo artifact,
 
 262             final IDistributionClientDownloadResult resultArtifact) throws ArtifactDownloadException {
 
 264             final byte[] payloadBytes = resultArtifact.getArtifactPayload();
 
 265             final File tempArtifactFile = File.createTempFile(artifact.getArtifactName(), ".csar");
 
 266             try (FileOutputStream fileOutputStream = new FileOutputStream(tempArtifactFile)) {
 
 267                 fileOutputStream.write(payloadBytes, 0, payloadBytes.length);
 
 268                 return tempArtifactFile.toPath();
 
 270         } catch (final Exception exp) {
 
 271             throw new ArtifactDownloadException("Failed to write artifact to local repository", exp);
 
 276      * Method to delete the downloaded notification artifact from local file system.
 
 278      * @param filePath the path of file
 
 280     private void deleteArtifactFile(final Path filePath) {
 
 282             Files.deleteIfExists(filePath);
 
 283         } catch (final IOException exp) {
 
 284             LOGGER.error("Failed to delete the downloaded artifact file", exp);
 
 289      * Sends the distribution status to SDC using the input values.
 
 291      * @param statusType the status type
 
 292      * @param artifactUrl the artifact url
 
 293      * @param distributionId the distribution id
 
 294      * @param status the status
 
 295      * @param errorReason the error reason
 
 297     private void sendDistributionStatus(final DistributionStatusType statusType, final String artifactUrl,
 
 298             final String distributionId, final DistributionStatusEnum status, final String errorReason) {
 
 300         IDistributionClientResult clientResult;
 
 301         final DistributionStatusMessageBuilder messageBuilder = new DistributionStatusMessageBuilder()
 
 302                 .setArtifactUrl(artifactUrl).setConsumerId(sdcConfig.getConsumerID()).setDistributionId(distributionId)
 
 303                 .setDistributionStatus(status).setTimestamp(System.currentTimeMillis());
 
 304         final IDistributionStatusMessage message = new DistributionStatusMessage(messageBuilder);
 
 305         if (DistributionStatusType.DOWNLOAD.equals(statusType)) {
 
 306             if (errorReason != null) {
 
 307                 clientResult = distributionClient.sendDownloadStatus(message, errorReason);
 
 309                 clientResult = distributionClient.sendDownloadStatus(message);
 
 312             if (errorReason != null) {
 
 313                 clientResult = distributionClient.sendDeploymentStatus(message, errorReason);
 
 315                 clientResult = distributionClient.sendDeploymentStatus(message);
 
 318         final StringBuilder loggerMessage = new StringBuilder();
 
 319         loggerMessage.append("distribution status to SDC with values - ").append("DistributionId")
 
 320                 .append(distributionId).append(" Artifact: ").append(artifactUrl).append(" StatusType: ")
 
 321                 .append(statusType.name()).append(" Status: ").append(status.name());
 
 322         if (errorReason != null) {
 
 323             loggerMessage.append(" ErrorReason: ").append(errorReason);
 
 325         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 326             loggerMessage.insert(0, "Failed sending ");
 
 327             LOGGER.debug("Failed sending {}", loggerMessage);
 
 329             loggerMessage.insert(0, "Successfully Sent ");
 
 330             LOGGER.debug("Successfully Sent {}", loggerMessage);
 
 335      * Sends the component done status to SDC using the input values.
 
 337      * @param distributionId the distribution Id
 
 338      * @param status the distribution status
 
 339      * @param errorReason the error reason
 
 341     private void sendComponentDoneStatus(final String distributionId, final DistributionStatusEnum status,
 
 342             final String errorReason) {
 
 343         IDistributionClientResult clientResult;
 
 344         final ComponentDoneStatusMessageBuilder messageBuilder = new ComponentDoneStatusMessageBuilder()
 
 345                 .setConsumerId(sdcConfig.getConsumerID()).setDistributionId(distributionId)
 
 346                 .setDistributionStatus(status).setTimestamp(System.currentTimeMillis());
 
 347         final IComponentDoneStatusMessage message = new ComponentDoneStatusMessage(messageBuilder);
 
 348         if (errorReason == null) {
 
 349             clientResult = distributionClient.sendComponentDoneStatus(message);
 
 351             clientResult = distributionClient.sendComponentDoneStatus(message, errorReason);
 
 354         final StringBuilder loggerMessage = new StringBuilder();
 
 355         loggerMessage.append("component done status to SDC with values - ").append("DistributionId")
 
 356                 .append(distributionId).append(" Status: ").append(status.name());
 
 357         if (errorReason != null) {
 
 358             loggerMessage.append(" ErrorReason: ").append(errorReason);
 
 360         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
 
 361             LOGGER.debug("Failed sending {}", loggerMessage);
 
 363             LOGGER.debug("Successfully Sent {}", loggerMessage);
 
 368      * Handle the status change of {@link SdcReceptionHandler} to Idle.
 
 370      * @param newStatus the new status
 
 372     private void handleIdleStatusChange(final SdcReceptionHandlerStatus newStatus) {
 
 373         if (nbOfNotificationsOngoing.get() > 1) {
 
 374             nbOfNotificationsOngoing.decrementAndGet();
 
 376             nbOfNotificationsOngoing.set(0);
 
 377             sdcReceptionHandlerStatus = newStatus;