From 905324b15dd1a3e80397d514aa22873703d8f239 Mon Sep 17 00:00:00 2001 From: ramverma Date: Wed, 5 Sep 2018 17:19:10 +0100 Subject: Adding code for handling notifications from SDC Change-Id: I45f28cbce8defead5e1f43ed81fa5ea6a2b2208b Issue-ID: POLICY-974 Signed-off-by: ramverma --- .../handling/sdc/SdcNotificationCallBack.java | 45 -------- .../handling/sdc/SdcReceptionHandler.java | 119 ++++++++++++++++++- .../sdc/exceptions/ArtifactDownloadException.java | 54 +++++++++ .../sdc/exceptions/PssdDownloadException.java | 54 --------- .../reception/handling/sdc/DummyDecoder.java | 67 +++++++++++ .../reception/handling/sdc/DummyPolicy.java | 62 ++++++++++ .../handling/sdc/DummyPolicyForwarder.java | 77 +++++++++++++ .../handling/sdc/TestSdcReceptionHandler.java | 127 +++++++++++++++++++++ .../exceptions/ArtifactDownloadExceptionTest.java | 36 ++++++ .../sdc/exceptions/PssdDownloadExceptionTest.java | 36 ------ 10 files changed, 537 insertions(+), 140 deletions(-) delete mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/SdcNotificationCallBack.java create mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java delete mode 100644 plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadException.java create mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java create mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java create mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java create mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java delete mode 100644 plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java (limited to 'plugins') 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/ArtifactDownloadException.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java new file mode 100644 index 00000000..89f305fd --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadException.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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.exceptions; + + +/** + * Exception during download from Pssd. + */ +public class ArtifactDownloadException extends Exception { + + /** + * serialization id. + */ + private static final long serialVersionUID = -8507246953751956974L; + + /** + * Constructor for creating PssdDownloadException using message. + * + * @param message The message to dump + */ + public ArtifactDownloadException (final String message) { + super (message); + + } + + /** + * Constructor for creating PssdDownloadException using message and exception. + * + * @param message The message to dump + * @param e the exception that caused this exception to be thrown + */ + public ArtifactDownloadException (final String message, final Exception e) { + super (message, e); + + } +} 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/PssdDownloadException.java deleted file mode 100644 index 14c6b5ea..00000000 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadException.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Intel. 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.exceptions; - - -/** - * Exception during download from Pssd. - */ -public class PssdDownloadException extends Exception { - - /** - * serialization id. - */ - private static final long serialVersionUID = -8507246953751956974L; - - /** - * Constructor for creating PssdDownloadException using message. - * - * @param message The message to dump - */ - public PssdDownloadException (final String message) { - super (message); - - } - - /** - * Constructor for creating PssdDownloadException using message and 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) { - super (message, e); - - } -} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java new file mode 100644 index 00000000..6a33e787 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java @@ -0,0 +1,67 @@ +/*- + * ============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 java.util.Arrays; +import java.util.Collection; + +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * Class to create a dummy decoder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder { + + public static final String DUMMY_POLICY = "DummyPolicy"; + private DummyPolicy decodedPolicy; + + /** + * {@inheritDoc}. + */ + @Override + public boolean canHandle(final PolicyInput policyInput) { + return policyInput.getClass().isAssignableFrom(Csar.class); + } + + /** + * {@inheritDoc}. + */ + @Override + public Collection decode(final Csar input) throws PolicyDecodingException { + final DummyPolicy dummyPolicy = new DummyPolicy(input.getCsarPath(), DUMMY_POLICY); + decodedPolicy = dummyPolicy; + return Arrays.asList(dummyPolicy); + } + + /** + * Returns the policy decoded by this decoder. + * + * @return the policy + */ + public DummyPolicy getDecodedPolicy() { + return decodedPolicy; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java new file mode 100644 index 00000000..f4c56ab0 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicy.java @@ -0,0 +1,62 @@ +/*- + * ============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.distribution.model.Policy; + +/** + * Class to create a dummy policy for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicy implements Policy { + + private String policyName; + private String policyType; + + /** + * Constructor for instantiating {@link DummyPolicy} class. + * + * @param policyName the policy name + * @param policyType the policy type + */ + public DummyPolicy(final String policyName, final String policyType) { + super(); + this.policyName = policyName; + this.policyType = policyType; + } + + /** + * {@inheritDoc}. + */ + @Override + public String getPolicyName() { + return policyName; + } + + /** + * {@inheritDoc}. + */ + @Override + public String getPolicyType() { + return policyType; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java new file mode 100644 index 00000000..cd127e22 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyPolicyForwarder.java @@ -0,0 +1,77 @@ +/*- + * ============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 java.util.ArrayList; +import java.util.Collection; + +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.model.Policy; + +/** + * Class to create a dummy forwarder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicyForwarder implements PolicyForwarder { + private int numberOfPoliciesReceived = 0; + private Collection policiesReceived = new ArrayList<>(); + + /** + * {@inheritDoc}. + */ + @Override + public void forward(final Collection policies) throws PolicyForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + /** + * Returns the number of policies received by this forwarder. + * + * @return the integer value + */ + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + /** + * Checks if the forwarder has received a policy with given policy type. + * + * @param policyType the policy type + * @return the boolean result + */ + public boolean receivedPolicyWithGivenType(final String policyType) { + for (final Policy policy : policiesReceived) { + if (policy.getPolicyType().equals(policyType)) { + return true; + } + } + return false; + } + + /** + * {@inheritDoc}. + */ + @Override + public void configure(final String parameterGroupName) {} +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java index c876f99c..a648f807 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java @@ -20,6 +20,7 @@ package org.onap.policy.distribution.reception.handling.sdc; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; @@ -29,6 +30,12 @@ import com.google.gson.GsonBuilder; import java.io.FileReader; import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import org.junit.After; import org.junit.Before; @@ -40,8 +47,19 @@ import org.mockito.runners.MockitoJUnitRunner; 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.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; +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.PolicyDecoder; +import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; +import org.onap.policy.distribution.reception.handling.PluginHandler; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; +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.mock.DistributionClientStubImpl; import org.onap.sdc.utils.DistributionActionResultEnum; @@ -55,6 +73,7 @@ import org.onap.sdc.utils.DistributionActionResultEnum; public class TestSdcReceptionHandler { private static final Logger LOGGER = FlexLogger.getLogger(TestSdcReceptionHandler.class); + private static final String DUMMY_SERVICE_CSAR = "dummyService.csar"; @Mock private IDistributionClientResult successfulClientInitResult; @@ -62,10 +81,17 @@ public class TestSdcReceptionHandler { private IDistributionClientResult failureClientInitResult; @Mock private DistributionClientStubImpl distributionClient; + @Mock + private IDistributionClientDownloadResult successfulClientDownloadResult; + @Mock + private INotificationData notificationData; + @Mock + private IArtifactInfo artifactInfo; private SdcReceptionHandlerConfigurationParameterGroup pssdConfigParameters; private SdcReceptionHandler sypHandler; + /** * Setup for the test cases. * @@ -79,12 +105,20 @@ public class TestSdcReceptionHandler { ParameterService.register(pssdConfigParameters); final SdcReceptionHandler sdcHandler = new SdcReceptionHandler(); sypHandler = Mockito.spy(sdcHandler); + Mockito.when(sypHandler.createSdcDistributionClient()).thenReturn(distributionClient); Mockito.when(distributionClient.init(any(), any())).thenReturn(successfulClientInitResult); Mockito.when(distributionClient.start()).thenReturn(successfulClientInitResult); Mockito.when(distributionClient.stop()).thenReturn(successfulClientInitResult); + Mockito.when(distributionClient.download(any())).thenReturn(successfulClientDownloadResult); + Mockito.when(notificationData.getServiceArtifacts()).thenReturn(Arrays.asList(artifactInfo)); + Mockito.when(artifactInfo.getArtifactName()).thenReturn(DUMMY_SERVICE_CSAR); + Mockito.when(successfulClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]); Mockito.when(successfulClientInitResult.getDistributionActionResult()) .thenReturn(DistributionActionResultEnum.SUCCESS); + Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.SUCCESS); + } @After @@ -176,4 +210,97 @@ public class TestSdcReceptionHandler { assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason")); } } + + @Test + public void testNotificationCallBack() throws NoSuchFieldException, SecurityException, IllegalArgumentException, + IllegalAccessException, PluginInitializationException { + + final DummyDecoder policyDecoder = new DummyDecoder(); + final Collection> policyDecoders = new ArrayList<>(); + policyDecoders.add(policyDecoder); + + final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + final Collection policyForwarders = new ArrayList<>(); + policyForwarders.add(policyForwarder); + + setUpPlugins(sypHandler, policyDecoders, policyForwarders); + sypHandler.initializeReception(pssdConfigParameters.getName()); + sypHandler.activateCallback(notificationData); + + assertEquals(DummyDecoder.DUMMY_POLICY, policyDecoder.getDecodedPolicy().getPolicyType()); + assertTrue(policyDecoder.getDecodedPolicy().getPolicyName().contains(DUMMY_SERVICE_CSAR)); + assertEquals(1, policyForwarder.getNumberOfPoliciesReceived()); + assertTrue(policyForwarder.receivedPolicyWithGivenType(DummyDecoder.DUMMY_POLICY)); + } + + @Test + public void testDownloadArtifactFailure() throws NoSuchFieldException, SecurityException, IllegalArgumentException, + IllegalAccessException, PluginInitializationException { + + Mockito.when(successfulClientDownloadResult.getDistributionActionResult()) + .thenReturn(DistributionActionResultEnum.FAIL); + + final DummyDecoder policyDecoder = new DummyDecoder(); + final Collection> policyDecoders = new ArrayList<>(); + policyDecoders.add(policyDecoder); + + final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + final Collection policyForwarders = new ArrayList<>(); + policyForwarders.add(policyForwarder); + + setUpPlugins(sypHandler, policyDecoders, policyForwarders); + sypHandler.initializeReception(pssdConfigParameters.getName()); + sypHandler.activateCallback(notificationData); + + assertEquals(null, policyDecoder.getDecodedPolicy()); + assertEquals(0, policyForwarder.getNumberOfPoliciesReceived()); + } + + private void setUpPlugins(final AbstractReceptionHandler receptionHandler, + final Collection> decoders, final Collection forwarders) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, + PluginInitializationException { + final PluginHandlerParameters pluginParameters = getPluginHandlerParameters(); + pluginParameters.setName("DummyDistributionGroup"); + ParameterService.register(pluginParameters); + final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName()); + + final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders"); + decodersField.setAccessible(true); + decodersField.set(pluginHandler, decoders); + + final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders"); + forwardersField.setAccessible(true); + forwardersField.set(pluginHandler, forwarders); + + final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler"); + pluginHandlerField.setAccessible(true); + pluginHandlerField.set(receptionHandler, pluginHandler); + ParameterService.deregister(pluginParameters.getName()); + } + + private PluginHandlerParameters getPluginHandlerParameters() { + final Map policyDecoders = getPolicyDecoders(); + final Map policyForwarders = getPolicyForwarders(); + final PluginHandlerParameters pluginHandlerParameters = + new PluginHandlerParameters(policyDecoders, policyForwarders); + return pluginHandlerParameters; + } + + private Map getPolicyDecoders() { + final Map policyDecoders = new HashMap(); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("DummyDecoder", + "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder"); + policyDecoders.put("DummyDecoderKey", pDParameters); + return policyDecoders; + } + + private Map getPolicyForwarders() { + final Map policyForwarders = + new HashMap(); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters("DummyForwarder", + "org.onap.policy.distribution.reception.handling.sdc.DummyPolicyForwarder", "DummyConfiguration"); + policyForwarders.put("DummyForwarderKey", pFParameters); + return policyForwarders; + } } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java new file mode 100644 index 00000000..6d275f7a --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/ArtifactDownloadExceptionTest.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Intel. 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.exceptions; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.Test; + +public class ArtifactDownloadExceptionTest { + + @Test + public void test() { + assertNotNull(new ArtifactDownloadException("Message")); + assertNotNull(new ArtifactDownloadException("Message", new IOException())); + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java deleted file mode 100644 index 9f76b375..00000000 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/exceptions/PssdDownloadExceptionTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Intel. 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.exceptions; - -import static org.junit.Assert.assertNotNull; - -import java.io.IOException; - -import org.junit.Test; - -public class PssdDownloadExceptionTest { - - @Test - public void test() { - assertNotNull(new PssdDownloadException("Message")); - assertNotNull(new PssdDownloadException("Message", new IOException())); - } -} -- cgit 1.2.3-korg