diff options
author | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-05 17:19:10 +0100 |
---|---|---|
committer | ramverma <ram.krishna.verma@ericsson.com> | 2018-09-06 19:43:24 +0100 |
commit | 905324b15dd1a3e80397d514aa22873703d8f239 (patch) | |
tree | 0ff866aa2cd73b2505e699a44ee54234aa0fad7b /plugins/reception-plugins/src/main | |
parent | 9d3612f325ae52e41a0a6d9e794b42e98305d7ec (diff) |
Adding code for handling notifications from SDC
Change-Id: I45f28cbce8defead5e1f43ed81fa5ea6a2b2208b
Issue-ID: POLICY-974
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'plugins/reception-plugins/src/main')
-rw-r--r-- | plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java | 45 | ||||
-rw-r--r-- | plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java | 119 | ||||
-rw-r--r-- | plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java (renamed from plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadException.java) | 6 |
3 files changed, 117 insertions, 53 deletions
diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java deleted file mode 100644 index 0ea57799..00000000 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.distribution.reception.handling.sdc; - -import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; -import org.onap.sdc.api.consumer.INotificationCallback; -import org.onap.sdc.api.notification.INotificationData; - -/** - * Class to provide an implementation of INotificationCallback interface for receiving the incoming distribution - * notifications from SDC. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class SdcNotificationCallBack implements INotificationCallback { - - private static final Logger LOGGER = FlexLogger.getLogger(SdcNotificationCallBack.class); - - @Override - public void activateCallback(final INotificationData notificationData) { - - LOGGER.debug("Got the message from SDC:" + notificationData.getDistributionID()); - // Code for handling notification will come here - } - -} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java index f0f9e20d..f2d8aa4d 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcReceptionHandler.java @@ -20,13 +20,26 @@ package org.onap.policy.distribution.reception.handling.sdc; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.model.Csar; import org.onap.policy.distribution.reception.decoding.PluginInitializationException; import org.onap.policy.distribution.reception.decoding.PluginTerminationException; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; +import org.onap.policy.distribution.reception.handling.sdc.exceptions.ArtifactDownloadException; import org.onap.sdc.api.IDistributionClient; +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.utils.DistributionActionResultEnum; @@ -34,9 +47,10 @@ import org.onap.sdc.utils.DistributionActionResultEnum; /** * Handles reception of inputs from ONAP Service Design and Creation (SDC) from which policies may be decoded. */ -public class SdcReceptionHandler extends AbstractReceptionHandler { +public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback { private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class); + private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED; private SdcReceptionHandlerConfigurationParameterGroup handlerParameters; private IDistributionClient distributionClient; @@ -68,12 +82,21 @@ public class SdcReceptionHandler extends AbstractReceptionHandler { LOGGER.debug("SDC Client is stopped successfully"); } + @Override + public void activateCallback(final INotificationData notificationData) { + LOGGER.debug("Receieved the notification from SDC with ID: " + notificationData.getDistributionID()); + changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.BUSY); + processCsarServiceArtifacts(notificationData); + changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE); + LOGGER.debug("Processed the notification from SDC with ID: " + notificationData.getDistributionID()); + } + /** * Method to change the status of this reception handler instance. * * @param newStatus the new status */ - protected synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) { + private synchronized final void changeSdcReceptionHandlerStatus(final SdcReceptionHandlerStatus newStatus) { switch (newStatus) { case INIT: case STOPPED: @@ -110,7 +133,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler { */ private void initializeSdcClient() throws PluginInitializationException { - LOGGER.debug("Going to initialize the SDC Client..."); + LOGGER.debug("Initializing the SDC Client..."); if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) { final String message = "The SDC Client is already initialized"; LOGGER.error(message); @@ -118,8 +141,7 @@ public class SdcReceptionHandler extends AbstractReceptionHandler { } final SdcConfiguration sdcConfig = new SdcConfiguration(handlerParameters); distributionClient = createSdcDistributionClient(); - final IDistributionClientResult clientResult = - distributionClient.init(sdcConfig, new SdcNotificationCallBack()); + final IDistributionClientResult clientResult = distributionClient.init(sdcConfig, this); if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED); final String message = @@ -150,4 +172,91 @@ public class SdcReceptionHandler extends AbstractReceptionHandler { LOGGER.debug("SDC Client is started successfully"); this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE); } + + /** + * Method to process csar service artifacts from incoming SDC notification. + * + * @param iNotif the notification from SDC + */ + public void processCsarServiceArtifacts(final INotificationData iNotif) { + boolean artifactsProcessedSuccessfully = true; + + for (final IArtifactInfo artifact : iNotif.getServiceArtifacts()) { + try { + final IDistributionClientDownloadResult resultArtifact = downloadTheArtifact(artifact); + final Path filePath = writeArtifactToFile(artifact, resultArtifact); + final Csar csarObject = new Csar(filePath.toString()); + inputReceived(csarObject); + // send deploy success status to sdc + deleteArtifactFile(filePath); + } catch (final ArtifactDownloadException | PolicyDecodingException exp) { + LOGGER.error("Failed to process csar service artifacts ", exp); + artifactsProcessedSuccessfully = false; + // send deploy failed status to sdc + } + } + if (artifactsProcessedSuccessfully) { + // send final distribution success status to sdc + } else { + // send final distribution failed status to sdc + } + } + + /** + * Method to download the distribution artifact. + * + * @param artifact the artifact + * @return the download result + * @throws ArtifactDownloadException if download fails + */ + private IDistributionClientDownloadResult downloadTheArtifact(final IArtifactInfo artifact) + throws ArtifactDownloadException { + + final IDistributionClientDownloadResult downloadResult = distributionClient.download(artifact); + if (!downloadResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) { + final String message = "Failed to download artifact with name: " + artifact.getArtifactName(); + LOGGER.error(message); + // send failure download status to sdc + throw new ArtifactDownloadException(message); + } + // send success download status to sdc + return downloadResult; + } + + /** + * Method to write the downloaded distribution artifact to local file system. + * + * @param artifact the notification artifact + * @param resultArtifact the download result artifact + * @return the local path of written file + * @throws ArtifactDownloadException if error occurs while writing the artifact + */ + private Path writeArtifactToFile(final IArtifactInfo artifact, + final IDistributionClientDownloadResult resultArtifact) throws ArtifactDownloadException { + try { + final byte[] payloadBytes = resultArtifact.getArtifactPayload(); + final File tempArtifactFile = File.createTempFile(artifact.getArtifactName(), null); + try (FileOutputStream fileOutputStream = new FileOutputStream(tempArtifactFile)) { + fileOutputStream.write(payloadBytes, 0, payloadBytes.length); + return tempArtifactFile.toPath(); + } + } catch (final Exception exp) { + final String message = "Failed to write artifact to local repository"; + LOGGER.error(message, exp); + throw new ArtifactDownloadException(message, exp); + } + } + + /** + * Method to delete the downloaded notification artifact from local file system. + * + * @param filePath the path of file + */ + private void deleteArtifactFile(final Path filePath) { + try { + Files.deleteIfExists(filePath); + } catch (final IOException exp) { + LOGGER.error("Failed to delete the downloaded artifact file", exp); + } + } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadException.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java index 14c6b5ea..89f305fd 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadException.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java @@ -24,7 +24,7 @@ package org.onap.policy.distribution.reception.handling.sdc.exceptions; /** * Exception during download from Pssd. */ -public class PssdDownloadException extends Exception { +public class ArtifactDownloadException extends Exception { /** * serialization id. @@ -36,7 +36,7 @@ public class PssdDownloadException extends Exception { * * @param message The message to dump */ - public PssdDownloadException (final String message) { + public ArtifactDownloadException (final String message) { super (message); } @@ -47,7 +47,7 @@ public class PssdDownloadException extends Exception { * @param message The message to dump * @param e the exception that caused this exception to be thrown */ - public PssdDownloadException (final String message, final Exception e) { + public ArtifactDownloadException (final String message, final Exception e) { super (message, e); } |