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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.handling.sdc;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
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;
51 * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded.
53 public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback {
55 private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class);
56 private static final String SECONDS = "Seconds";
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;
65 private enum DistributionStatusType {
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);
80 public void destroy() {
81 if (distributionClient != null) {
82 sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.STOP, retryDelay);
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());
96 * Method to change the status of this reception handler instance.
98 * @param newStatus the new status
100 private final synchronized void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) {
104 sdcReceptionHandlerStatus = newStatus;
107 handleIdleStatusChange(newStatus);
110 ++nbOfNotificationsOngoing;
111 sdcReceptionHandlerStatus = newStatus;
119 * Creates an instance of {@link IDistributionClient} from {@link DistributionClientFactory}.
121 * @return the {@link IDistributionClient} instance
123 protected IDistributionClient createSdcDistributionClient() {
124 return DistributionClientFactory.createDistributionClient();
128 * Method to initialize the SDC client.
131 protected void initializeSdcClient() {
133 LOGGER.debug("Initializing the SDC Client...");
134 if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
135 LOGGER.error("The SDC Client is already initialized");
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);
144 LOGGER.debug("SDC Client is initialized successfully");
145 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
149 * Method to start the SDC client.
152 protected void startSdcClient() {
154 LOGGER.debug("Going to start the SDC Client...");
155 if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.INIT) {
156 LOGGER.error("The SDC Client is not initialized");
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);
165 LOGGER.debug("SDC Client is started successfully");
166 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
167 sdcClientHandler.cancel();
171 * Method to stop the SDC client.
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);
182 LOGGER.debug("SDC Client is stopped successfully");
183 changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
184 sdcClientHandler.cancel();
188 * Method to process csar service artifacts from incoming SDC notification.
190 * @param notificationData the notification from SDC
192 public void processCsarServiceArtifacts(final INotificationData notificationData) {
193 boolean artifactsProcessedSuccessfully = true;
194 DistributionStatisticsManager.updateTotalDistributionCount();
195 for (final IArtifactInfo artifact : notificationData.getServiceArtifacts()) {
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());
213 if (artifactsProcessedSuccessfully) {
214 DistributionStatisticsManager.updateDistributionSuccessCount();
215 sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_OK,
218 DistributionStatisticsManager.updateDistributionFailureCount();
219 sendComponentDoneStatus(notificationData.getDistributionID(), DistributionStatusEnum.COMPONENT_DONE_ERROR,
220 "Failed to process the artifact");
225 * Method to download the distribution artifact.
227 * @param artifact the artifact
228 * @return the download result
229 * @throws ArtifactDownloadException if download fails
231 private IDistributionClientDownloadResult downloadTheArtifact(final IArtifactInfo artifact,
232 final INotificationData notificationData) throws ArtifactDownloadException {
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);
245 DistributionStatisticsManager.updateDownloadSuccessCount();
246 sendDistributionStatus(DistributionStatusType.DOWNLOAD, artifact.getArtifactURL(),
247 notificationData.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null);
248 return downloadResult;
252 * Method to write the downloaded distribution artifact to local file system.
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
259 private Path writeArtifactToFile(final IArtifactInfo artifact,
260 final IDistributionClientDownloadResult resultArtifact) throws ArtifactDownloadException {
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();
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);
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(loggerMessage);
329 loggerMessage.insert(0, "Successfully Sent ");
330 LOGGER.debug(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 loggerMessage.insert(0, "Failed sending ");
362 LOGGER.debug(loggerMessage);
364 loggerMessage.insert(0, "Successfully Sent ");
365 LOGGER.debug(loggerMessage);
370 * Handle the status change of {@link SdcReceptionHandler} to Idle.
372 * @param newStatus the new status
374 private void handleIdleStatusChange(final SdcReceptionHandlerStatus newStatus) {
375 if (nbOfNotificationsOngoing > 1) {
376 --nbOfNotificationsOngoing;
378 nbOfNotificationsOngoing = 0;
379 sdcReceptionHandlerStatus = newStatus;