0d100f8f077edd94eba1532ce93354cad3253067
[multicloud/framework.git] / artifactbroker / plugins / reception-plugins / src / main / java / org / onap / policy / distribution / reception / handling / sdc / SdcReceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
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.DistributionClientFactory;
47 import org.onap.sdc.utils.DistributionActionResultEnum;
48 import org.onap.sdc.utils.DistributionStatusEnum;
49
50 /**
51  * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
52  */
53 public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback {
54
55     private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class);
56     private static final String SECONDS = "Seconds";
57
58     private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED;
59     private IDistributionClient distributionClient;
60     private SdcConfiguration sdcConfig;
61     private volatile int nbOfNotificationsOngoing = 0;
62     private int retryDelay;
63     private SdcClientHandler sdcClientHandler;
64
65     private enum DistributionStatusType {
66         DOWNLOAD, DEPLOY
67     }
68
69     @Override
70     protected void initializeReception(final String parameterGroupName) {
71         final SdcReceptionHandlerConfigurationParameterGroup handlerParameters =
72                 ParameterService.get(parameterGroupName);
73         retryDelay = handlerParameters.getRetryDelay() < 30 ? 30 : handlerParameters.getRetryDelay();
74         sdcConfig = new SdcConfiguration(handlerParameters);
75         distributionClient = createSdcDistributionClient();
76         sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.START, retryDelay);
77     }
78
79     @Override
80     public void destroy() {
81         if (distributionClient != null) {
82             sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.STOP, retryDelay);
83         }
84     }
85
86     @Override
87     public void activateCallback(final INotificationData notificationData) {
88         LOGGER.debug("Receieved the notification from SDC with ID: " + notificationData.getDistributionID());
89         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.BUSY);
90         processCsarServiceArtifacts(notificationData);
91         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
92         LOGGER.debug("Processed the notification from SDC with ID: " + notificationData.getDistributionID());
93     }
94
95     /**
96      * Method to change the status of this reception handler instance.
97      *
98      * @param newStatus the new status
99      */
100     private final synchronized void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
101         switch (newStatus) {
102             case INIT:
103             case STOPPED:
104                 sdcReceptionHandlerStatus = newStatus;
105                 break;
106             case IDLE:
107                 handleIdleStatusChange(newStatus);
108                 break;
109             case BUSY:
110                 ++nbOfNotificationsOngoing;
111                 sdcReceptionHandlerStatus = newStatus;
112                 break;
113             default:
114                 break;
115         }
116     }
117
118     /**
119      * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
120      *
121      * @return the {@link IDistributionClient} instance
122      */
123     protected IDistributionClient createSdcDistributionClient() {
124         return DistributionClientFactory.createDistributionClient();
125     }
126
127     /**
128      * Method to initialize the SDC client.
129      *
130      */
131     protected void initializeSdcClient() {
132
133         LOGGER.debug("Initializing the SDC Client...");
134         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
135             LOGGER.error("The SDC Client is already initialized");
136             return;
137         }
138         final IDistributionClientResult clientResult = distributionClient.init(sdcConfig, this);
139         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
140             LOGGER.error("SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult()
141                     + ". Initialization will be retried after " + retryDelay + SECONDS);
142             return;
143         }
144         LOGGER.debug("SDC Client is initialized successfully");
145         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
146     }
147
148     /**
149      * Method to start the SDC client.
150      *
151      */
152     protected void startSdcClient() {
153
154         LOGGER.debug("Going to start the SDC Client...");
155         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.INIT) {
156             LOGGER.error("The SDC Client is not initialized");
157             return;
158         }
159         final IDistributionClientResult clientResult = distributionClient.start();
160         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
161             LOGGER.error("SDC client start failed with reason:" + clientResult.getDistributionMessageResult()
162                     + ". Start will be retried after " + retryDelay + SECONDS);
163             return;
164         }
165         LOGGER.debug("SDC Client is started successfully");
166         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
167         sdcClientHandler.cancel();
168     }
169
170     /**
171      * Method to stop the SDC client.
172      *
173      */
174     protected void stopSdcClient() {
175         LOGGER.debug("Going to stop the SDC Client...");
176         final IDistributionClientResult clientResult = distributionClient.stop();
177         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
178             LOGGER.error("SDC client stop failed with reason:" + clientResult.getDistributionMessageResult()
179                     + ". Stop will be retried after " + retryDelay + SECONDS);
180             return;
181         }
182         LOGGER.debug("SDC Client is stopped successfully");
183         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
184         sdcClientHandler.cancel();
185     }
186
187     /**
188      * Method to process csar service artifacts from incoming SDC notification.
189      *
190      * @param notificationData the notification from SDC
191      */
192     public void processCsarServiceArtifacts(final INotificationData notificationData) {
193         boolean artifactsProcessedSuccessfully = true;
194         DistributionStatisticsManager.updateTotalDistributionCount();
195         for (final IArtifactInfo artifact : notificationData.getServiceArtifacts()) {
196             try {
197                 final IDistributionClientDownloadResult resultArtifact =
198                         downloadTheArtifact(artifact, notificationData);
199                 final Path filePath = writeArtifactToFile(artifact, resultArtifact);
200                 final Csar csarObject = new Csar(filePath.toString());
201                 inputReceived(csarObject);
202                 sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(),
203                         notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null);
204                 deleteArtifactFile(filePath);
205             } catch (final ArtifactDownloadException | PolicyDecodingException exp) {
206                 LOGGER.error("Failed to process csar service artifacts ", exp);
207                 artifactsProcessedSuccessfully = false;
208                 sendDistributionStatus(DistributionStatusType.DEPLOY, artifact.getArtifactURL(),
209                         notificationData.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,
210                         "Failed to deploy the artifact due to: " + exp.getMessage());
211             }
212         }
213         if (artifactsProcessedSuccessfully) {
214             DistributionStatisticsManager.updateDistributionSuccessCount();
215             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_OK,
216                     null);
217         } else {
218             DistributionStatisticsManager.updateDistributionFailureCount();
219             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_ERROR,
220                     "Failed to process the artifact");
221         }
222     }
223
224     /**
225      * Method to download the distribution artifact.
226      *
227      * @param artifact the artifact
228      * @return the download result
229      * @throws ArtifactDownloadException if download fails
230      */
231     private IDistributionClientDownloadResult downloadTheArtifact(final IArtifactInfo artifact,
232             final INotificationData notificationData) throws ArtifactDownloadException {
233
234         DistributionStatisticsManager.updateTotalDownloadCount();
235         final IDistributionClientDownloadResult downloadResult = distributionClient.download(artifact);
236         if (!downloadResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
237             DistributionStatisticsManager.updateDownloadFailureCount();
238             final String message = "Failed to download artifact with name: " + artifact.getArtifactName() + " due to: "
239                     + downloadResult.getDistributionMessageResult();
240             LOGGER.error(message);
241             sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
242                     notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, message);
243             throw new ArtifactDownloadException(message);
244         }
245         DistributionStatisticsManager.updateDownloadSuccessCount();
246         sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
247                 notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null);
248         return downloadResult;
249     }
250
251     /**
252      * Method to write the downloaded distribution artifact to local file system.
253      *
254      * @param artifact the notification artifact
255      * @param resultArtifact the download result artifact
256      * @return the local path of written file
257      * @throws ArtifactDownloadException if error occurs while writing the artifact
258      */
259     private Path writeArtifactToFile(final IArtifactInfo artifact,
260             final IDistributionClientDownloadResult resultArtifact) throws ArtifactDownloadException {
261         try {
262             final byte[] payloadBytes = resultArtifact.getArtifactPayload();
263             final File tempArtifactFile = File.createTempFile(artifact.getArtifactName(), ".csar");
264             try (FileOutputStream fileOutputStream = new FileOutputStream(tempArtifactFile)) {
265                 fileOutputStream.write(payloadBytes, 0, payloadBytes.length);
266                 return tempArtifactFile.toPath();
267             }
268         } catch (final Exception exp) {
269             final String message = "Failed to write artifact to local repository";
270             LOGGER.error(message, exp);
271             throw new ArtifactDownloadException(message, exp);
272         }
273     }
274
275     /**
276      * Method to delete the downloaded notification artifact from local file system.
277      *
278      * @param filePath the path of file
279      */
280     private void deleteArtifactFile(final Path filePath) {
281         try {
282             Files.deleteIfExists(filePath);
283         } catch (final IOException exp) {
284             LOGGER.error("Failed to delete the downloaded artifact file", exp);
285         }
286     }
287
288     /**
289      * Sends the distribution status to SDC using the input values.
290      *
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
296      */
297     private void sendDistributionStatus(final DistributionStatusType statusType, final String artifactUrl,
298             final String distributionId, final DistributionStatusEnum status, final String errorReason) {
299
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);
308             } else {
309                 clientResult = distributionClient.sendDownloadStatus(message);
310             }
311         } else {
312             if (errorReason != null) {
313                 clientResult = distributionClient.sendDeploymentStatus(message, errorReason);
314             } else {
315                 clientResult = distributionClient.sendDeploymentStatus(message);
316             }
317         }
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);
324         }
325         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
326             loggerMessage.insert(0, "Failed sending ");
327             LOGGER.debug(loggerMessage);
328         } else {
329             loggerMessage.insert(0, "Successfully Sent ");
330             LOGGER.debug(loggerMessage);
331         }
332     }
333
334     /**
335      * Sends the component done status to SDC using the input values.
336      *
337      * @param distributionId the distribution Id
338      * @param status the distribution status
339      * @param errorReason the error reason
340      */
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);
350         } else {
351             clientResult = distributionClient.sendComponentDoneStatus(message, errorReason);
352         }
353
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);
359         }
360         if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
361             loggerMessage.insert(0, "Failed sending ");
362             LOGGER.debug(loggerMessage);
363         } else {
364             loggerMessage.insert(0, "Successfully Sent ");
365             LOGGER.debug(loggerMessage);
366         }
367     }
368
369     /**
370      * Handle the status change of {@link SdcReceptionHandler} to Idle.
371      *
372      * @param newStatus the new status
373      */
374     private void handleIdleStatusChange(final SdcReceptionHandlerStatus newStatus) {
375         if (nbOfNotificationsOngoing > 1) {
376             --nbOfNotificationsOngoing;
377         } else {
378             nbOfNotificationsOngoing = 0;
379             sdcReceptionHandlerStatus = newStatus;
380         }
381     }
382 }