23fbca5911adf916b2cff6475e740300b3be00c1
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.handling.sdc;
24
25 import java.io.File;
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;
51
52 /**
53  * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
54  */
55 public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(SdcReceptionHandler.class);
58     private static final String SECONDS = "Seconds";
59
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;
66
67     private enum DistributionStatusType {
68         DOWNLOAD, DEPLOY
69     }
70
71     @Override
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);
79     }
80
81     @Override
82     public void destroy() {
83         if (distributionClient != null) {
84             sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.STOP, retryDelay);
85         }
86     }
87
88     @Override
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());
95     }
96
97     /**
98      * Method to change the status of this reception handler instance.
99      *
100      * @param newStatus the new status
101      */
102     private final synchronized void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
103         switch (newStatus) {
104             case INIT:
105             case STOPPED:
106                 sdcReceptionHandlerStatus = newStatus;
107                 break;
108             case IDLE:
109                 handleIdleStatusChange(newStatus);
110                 break;
111             case BUSY:
112                 nbOfNotificationsOngoing.incrementAndGet();
113                 sdcReceptionHandlerStatus = newStatus;
114                 break;
115             default:
116                 break;
117         }
118     }
119
120     /**
121      * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
122      *
123      * @return the {@link IDistributionClient} instance
124      */
125     protected IDistributionClient createSdcDistributionClient() {
126         return new DistributionClientImpl();
127     }
128
129     /**
130      * Method to initialize the SDC client.
131      *
132      */
133     protected void initializeSdcClient() {
134
135         LOGGER.debug("Initializing the SDC Client...");
136         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
137             LOGGER.error("The SDC Client is already initialized");
138             return;
139         }
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);
144             return;
145         }
146         LOGGER.debug("SDC Client is initialized successfully");
147         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
148     }
149
150     /**
151      * Method to start the SDC client.
152      *
153      */
154     protected void startSdcClient() {
155
156         LOGGER.debug("Going to start the SDC Client...");
157         if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.INIT) {
158             LOGGER.error("The SDC Client is not initialized");
159             return;
160         }
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);
165             return;
166         }
167         LOGGER.debug("SDC Client is started successfully");
168         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
169         sdcClientHandler.cancel();
170     }
171
172     /**
173      * Method to stop the SDC client.
174      *
175      */
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);
182             return;
183         }
184         LOGGER.debug("SDC Client is stopped successfully");
185         changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
186         sdcClientHandler.cancel();
187     }
188
189     /**
190      * Method to process csar service artifacts from incoming SDC notification.
191      *
192      * @param notificationData the notification from SDC
193      */
194     public void processCsarServiceArtifacts(final INotificationData notificationData) {
195         var artifactsProcessedSuccessfully = true;
196         DistributionStatisticsManager.updateTotalDistributionCount();
197         for (final IArtifactInfo artifact : notificationData.getServiceArtifacts()) {
198             try {
199                 final IDistributionClientDownloadResult resultArtifact =
200                         downloadTheArtifact(artifact, notificationData);
201                 final var filePath = writeArtifactToFile(artifact, resultArtifact);
202                 final var 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());
213             }
214         }
215         if (artifactsProcessedSuccessfully) {
216             DistributionStatisticsManager.updateDistributionSuccessCount();
217             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_OK,
218                     null);
219         } else {
220             DistributionStatisticsManager.updateDistributionFailureCount();
221             sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_ERROR,
222                     "Failed to process the artifact");
223         }
224     }
225
226     /**
227      * Method to download the distribution artifact.
228      *
229      * @param artifact the artifact
230      * @return the download result
231      * @throws ArtifactDownloadException if download fails
232      */
233     private IDistributionClientDownloadResult downloadTheArtifact(final IArtifactInfo artifact,
234             final INotificationData notificationData) throws ArtifactDownloadException {
235
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);
246         }
247         DistributionStatisticsManager.updateDownloadSuccessCount();
248         sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
249                 notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null);
250         return downloadResult;
251     }
252
253     /**
254      * Method to write the downloaded distribution artifact to local file system.
255      *
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
260      */
261     private Path writeArtifactToFile(final IArtifactInfo artifact,
262             final IDistributionClientDownloadResult resultArtifact) throws ArtifactDownloadException {
263         try {
264             final byte[] payloadBytes = resultArtifact.getArtifactPayload();
265             final var tempArtifactFile = File.createTempFile(artifact.getArtifactName(), ".csar");
266             try (var fileOutputStream = new FileOutputStream(tempArtifactFile)) {
267                 fileOutputStream.write(payloadBytes, 0, payloadBytes.length);
268                 return tempArtifactFile.toPath();
269             }
270         } catch (final Exception exp) {
271             throw new ArtifactDownloadException("Failed to write artifact to local repository", 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 var 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("Failed sending {}", loggerMessage);
328         } else {
329             loggerMessage.insert(0, "Successfully Sent ");
330             LOGGER.debug("Successfully Sent {}", 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 var 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             LOGGER.debug("Failed sending {}", loggerMessage);
362         } else {
363             LOGGER.debug("Successfully Sent {}", loggerMessage);
364         }
365     }
366
367     /**
368      * Handle the status change of {@link SdcReceptionHandler} to Idle.
369      *
370      * @param newStatus the new status
371      */
372     private void handleIdleStatusChange(final SdcReceptionHandlerStatus newStatus) {
373         if (nbOfNotificationsOngoing.getAndUpdate(curval -> Math.max(0, curval - 1)) == 0) {
374             sdcReceptionHandlerStatus = newStatus;
375         }
376     }
377 }