diff options
24 files changed, 945 insertions, 696 deletions
diff --git a/sdc-distribution-client/pom.xml b/sdc-distribution-client/pom.xml index 2fd1ac7..3b4f2c3 100644 --- a/sdc-distribution-client/pom.xml +++ b/sdc-distribution-client/pom.xml @@ -243,5 +243,11 @@ </exclusion> </exclusions> </dependency> + <dependency> + <groupId>org.skyscreamer</groupId> + <artifactId>jsonassert</artifactId> + <version>1.5.3</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/IDistributionClient.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/IDistributionClient.java index 1115c91..7efeb4c 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/IDistributionClient.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/IDistributionClient.java @@ -32,6 +32,7 @@ import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.IVfModuleMetadata; +import org.onap.sdc.api.notification.StatusMessage; /** Client for sending/receiving notifications/status related to distributions from SDC. @@ -214,5 +215,6 @@ public interface IDistributionClient { @Deprecated List<IVfModuleMetadata> decodeVfModuleArtifact(byte[] artifactPayload); + IDistributionClientResult sendNotificationStatus(StatusMessage statusMessage); } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IArtifactInfo.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IArtifactInfo.java index c13f63b..383b24e 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IArtifactInfo.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IArtifactInfo.java @@ -22,7 +22,10 @@ package org.onap.sdc.api.notification; import java.util.List; +import org.onap.sdc.impl.ArtifactInfo; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +@JsonDeserialize(as = ArtifactInfo.class) public interface IArtifactInfo { /** diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/INotificationData.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/INotificationData.java index 8c24ed4..915b45f 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/INotificationData.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/INotificationData.java @@ -22,7 +22,11 @@ package org.onap.sdc.api.notification; import java.util.List; +import org.onap.sdc.impl.NotificationData; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(as = NotificationData.class) public interface INotificationData { /** * Global Distribution Identifier: UUID generated by SDC per each distribution activation.<br> diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IResourceInstance.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IResourceInstance.java index aece6d0..a701571 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IResourceInstance.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IResourceInstance.java @@ -22,6 +22,11 @@ package org.onap.sdc.api.notification; import java.util.List; +import org.onap.sdc.impl.ResourceInstance; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(as = ResourceInstance.class) public interface IResourceInstance { /** * Logical Resource Instance Name. <br> diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IStatusData.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IStatusData.java index 314c592..f4729f3 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IStatusData.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/IStatusData.java @@ -20,8 +20,12 @@ package org.onap.sdc.api.notification; +import org.onap.sdc.impl.StatusDataImpl; import org.onap.sdc.utils.DistributionStatusEnum; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(as = StatusDataImpl.class) public interface IStatusData { /** * Global Distribution Identifier: UUID generated by SDC per each diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/StatusMessage.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/StatusMessage.java new file mode 100644 index 0000000..9d77907 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/notification/StatusMessage.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2024 Deutsche Telekom Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.api.notification; + +import org.onap.sdc.utils.DistributionStatusEnum; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +@AllArgsConstructor +public class StatusMessage { + private final String distributionID; + private final String consumerID; + private final long timestamp; + private final String artifactURL; + private final DistributionStatusEnum status; + private final String errorReason; + + public StatusMessage(String distributionID, String consumerID, long timestamp, String artifactUrl, DistributionStatusEnum status) { + this.distributionID = distributionID; + this.consumerID = consumerID; + this.timestamp = timestamp; + this.artifactURL = artifactUrl; + this.status = status; + this.errorReason = null; + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ArtifactInfoImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ArtifactInfo.java index 72e6535..8ea586d 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ArtifactInfoImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ArtifactInfo.java @@ -24,8 +24,12 @@ import java.util.ArrayList; import java.util.List; import org.onap.sdc.api.notification.IArtifactInfo; +import lombok.Data; +import lombok.NoArgsConstructor; -class ArtifactInfoImpl implements IArtifactInfo { +@Data +@NoArgsConstructor +public class ArtifactInfo implements IArtifactInfo { private String artifactName; private String artifactType; @@ -40,10 +44,7 @@ class ArtifactInfoImpl implements IArtifactInfo { private List<String> relatedArtifacts; private List<IArtifactInfo> relatedArtifactsInfo; - ArtifactInfoImpl() { - } - - private ArtifactInfoImpl(IArtifactInfo iArtifactInfo) { + private ArtifactInfo(IArtifactInfo iArtifactInfo) { artifactName = iArtifactInfo.getArtifactName(); artifactType = iArtifactInfo.getArtifactType(); artifactURL = iArtifactInfo.getArtifactURL(); @@ -55,7 +56,6 @@ class ArtifactInfoImpl implements IArtifactInfo { generatedArtifact = iArtifactInfo.getGeneratedArtifact(); relatedArtifactsInfo = iArtifactInfo.getRelatedArtifacts(); relatedArtifacts = fillRelatedArtifactsUUID(relatedArtifactsInfo); - } @@ -70,62 +70,26 @@ class ArtifactInfoImpl implements IArtifactInfo { return relatedArtifactsUUID; } - public static List<ArtifactInfoImpl> convertToArtifactInfoImpl(List<IArtifactInfo> list) { - List<ArtifactInfoImpl> ret = new ArrayList<>(); + public static List<ArtifactInfo> convertToArtifactInfoImpl(List<IArtifactInfo> list) { + List<ArtifactInfo> ret = new ArrayList<>(); if (list != null) { for (IArtifactInfo artifactInfo : list) { - ret.add(new ArtifactInfoImpl(artifactInfo)); + ret.add(new ArtifactInfo(artifactInfo)); } } return ret; } - public String getArtifactName() { - return artifactName; - } - - public void setArtifactName(String artifactName) { - this.artifactName = artifactName; - } - - public String getArtifactType() { - return artifactType; - } - - public void setArtifactType(String artifactType) { - this.artifactType = artifactType; - } - - public String getArtifactURL() { - return artifactURL; - } - - public void setArtifactURL(String artifactURL) { - this.artifactURL = artifactURL; - } - - public String getArtifactChecksum() { - return artifactChecksum; - } - - public void setArtifactChecksum(String artifactChecksum) { - this.artifactChecksum = artifactChecksum; - } - - public String getArtifactDescription() { - return artifactDescription; - } - - public void setArtifactDescription(String artifactDescription) { - this.artifactDescription = artifactDescription; - } - - public Integer getArtifactTimeout() { - return artifactTimeout; + public List<IArtifactInfo> getRelatedArtifacts() { + List<IArtifactInfo> temp = new ArrayList<>(); + if (relatedArtifactsInfo != null) { + temp.addAll(relatedArtifactsInfo); + } + return temp; } - public void setArtifactTimeout(Integer artifactTimeout) { - this.artifactTimeout = artifactTimeout; + public List<String> getRelatedArtifactsUUID() { + return relatedArtifacts; } @Override @@ -138,57 +102,4 @@ class ArtifactInfoImpl implements IArtifactInfo { + ", artifactUUID=" + artifactUUID + ", artifactTimeout=" + artifactTimeout + "]"; } - - public String getArtifactVersion() { - return artifactVersion; - } - - public void setArtifactVersion(String artifactVersion) { - this.artifactVersion = artifactVersion; - } - - public String getArtifactUUID() { - return artifactUUID; - } - - public void setArtifactUUID(String artifactUUID) { - this.artifactUUID = artifactUUID; - } - - public String getGeneratedFromUUID() { - return generatedFromUUID; - } - - public void setGeneratedFromUUID(String generatedFromUUID) { - this.generatedFromUUID = generatedFromUUID; - } - - public IArtifactInfo getGeneratedArtifact() { - return generatedArtifact; - } - - public void setGeneratedArtifact(IArtifactInfo generatedArtifact) { - this.generatedArtifact = generatedArtifact; - } - - public List<IArtifactInfo> getRelatedArtifacts() { - List<IArtifactInfo> temp = new ArrayList<>(); - if (relatedArtifactsInfo != null) { - temp.addAll(relatedArtifactsInfo); - } - return temp; - } - - public void setRelatedArtifacts(List<String> relatedArtifacts) { - this.relatedArtifacts = relatedArtifacts; - } - - public void setRelatedArtifactsInfo(List<IArtifactInfo> relatedArtifactsInfo) { - this.relatedArtifactsInfo = relatedArtifactsInfo; - } - - public List<String> getRelatedArtifactsUUID() { - return relatedArtifacts; - } - } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java index 0c05b58..5be494c 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java @@ -49,6 +49,7 @@ import org.onap.sdc.api.consumer.INotificationCallback; import org.onap.sdc.api.consumer.IStatusCallback; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.IVfModuleMetadata; +import org.onap.sdc.api.notification.StatusMessage; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.http.HttpClientFactory; @@ -367,7 +368,8 @@ public class DistributionClientImpl implements IDistributionClient { return sendErrorStatus(DistributionStatusMessageJsonBuilderFactory.getSimpleBuilder(statusMessage)); } - IDistributionClientResult sendNotificationStatus(long currentTimeMillis, String distributionId, ArtifactInfoImpl artifactInfo, boolean isNotified) { + @Override + public IDistributionClientResult sendNotificationStatus(StatusMessage status) { log.info("DistributionClient - sendNotificationStatus"); Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); validateRunReady(errorWrapper); @@ -376,10 +378,10 @@ public class DistributionClientImpl implements IDistributionClient { } IDistributionStatusMessageJsonBuilder builder = DistributionStatusMessageJsonBuilderFactory.prepareBuilderForNotificationStatus( getConfiguration().getConsumerID(), - currentTimeMillis, - distributionId, - artifactInfo, - isNotified); + status.getTimestamp(), + status.getDistributionID(), + status.getArtifactURL(), + status.getStatus()); return sendStatus(builder); } @@ -577,7 +579,7 @@ public class DistributionClientImpl implements IDistributionClient { } - + private HttpHost getHttpProxyHost() { HttpHost proxyHost = null; if (Boolean.TRUE.equals(configuration.isUseSystemProxy() && System.getProperty("http.proxyHost") != null) && System.getProperty("http.proxyPort") != null) { diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java index 62be395..6182b7d 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java @@ -74,19 +74,20 @@ public class DistributionStatusMessageJsonBuilderFactory { return prepareBuilderFromImpl(message); } - static IDistributionStatusMessageJsonBuilder prepareBuilderForNotificationStatus(final String consumerId, final long currentTimeMillis, final String distributionId, - final ArtifactInfoImpl artifactInfo, boolean isNotified) { - - final DistributionStatusEnum distributionStatus = isNotified ? DistributionStatusEnum.NOTIFIED : DistributionStatusEnum.NOT_NOTIFIED; - final String jsonRequest = buildDistributionStatusJson(consumerId, currentTimeMillis, distributionId, artifactInfo, distributionStatus); + static IDistributionStatusMessageJsonBuilder prepareBuilderForNotificationStatus(String consumerId, + long currentTimeMillis, + String distributionId, + String artifactUrl, + DistributionStatusEnum distributionStatus) { + final String jsonRequest = buildDistributionStatusJson(consumerId, currentTimeMillis, distributionId, artifactUrl, distributionStatus); return () -> jsonRequest; } - private static String buildDistributionStatusJson(final String consumerId, - final long currentTimeMillis, final String distributionId, - final ArtifactInfoImpl artifactInfo, - final DistributionStatusEnum fakeStatusToBeReplaced) { + private static String buildDistributionStatusJson(String consumerId, + long currentTimeMillis, String distributionId, + String artifactUrl, + DistributionStatusEnum fakeStatusToBeReplaced) { IDistributionStatusMessage statusMessage = new IDistributionStatusMessage() { @Override public long getTimestamp() { @@ -111,7 +112,7 @@ public class DistributionStatusMessageJsonBuilderFactory { @Override public String getArtifactURL() { - return artifactInfo.getArtifactURL(); + return artifactUrl; } }; diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/JsonContainerResourceInstance.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/JsonContainerResourceInstance.java index eda14d4..149e21c 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/JsonContainerResourceInstance.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/JsonContainerResourceInstance.java @@ -26,9 +26,12 @@ import java.util.List; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.IResourceInstance; -class JsonContainerResourceInstance implements IResourceInstance { - JsonContainerResourceInstance() { - } +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class JsonContainerResourceInstance implements IResourceInstance { private String resourceInstanceName; private String resourceCustomizationUUID; @@ -39,7 +42,7 @@ class JsonContainerResourceInstance implements IResourceInstance { private String resourceInvariantUUID; private String category; private String subcategory; - private List<ArtifactInfoImpl> artifacts; + private List<ArtifactInfo> artifacts; private JsonContainerResourceInstance(IResourceInstance resourceInstance) { resourceInstanceName = resourceInstance.getResourceInstanceName(); @@ -51,7 +54,7 @@ class JsonContainerResourceInstance implements IResourceInstance { resourceInvariantUUID = resourceInstance.getResourceInvariantUUID(); category = resourceInstance.getCategory(); subcategory = resourceInstance.getSubcategory(); - artifacts = ArtifactInfoImpl.convertToArtifactInfoImpl(resourceInstance.getArtifacts()); + artifacts = ArtifactInfo.convertToArtifactInfoImpl(resourceInstance.getArtifacts()); } public static List<JsonContainerResourceInstance> convertToJsonContainer(List<IResourceInstance> resources) { @@ -65,33 +68,6 @@ class JsonContainerResourceInstance implements IResourceInstance { } @Override - public String getResourceInstanceName() { - return resourceInstanceName; - } - - public void setResourceInstanceName(String resourceInstanceName) { - this.resourceInstanceName = resourceInstanceName; - } - - @Override - public String getResourceName() { - return resourceName; - } - - public void setResourceName(String resourceName) { - this.resourceName = resourceName; - } - - @Override - public String getResourceVersion() { - return resourceVersion; - } - - public void setResourceVersion(String resourceVersion) { - this.resourceVersion = resourceVersion; - } - - @Override public String getResourceType() { return resoucreType; } @@ -101,61 +77,13 @@ class JsonContainerResourceInstance implements IResourceInstance { } @Override - public String getResourceUUID() { - return resourceUUID; - } - - public void setResourceUUID(String resourceUUID) { - this.resourceUUID = resourceUUID; - } - - @Override public List<IArtifactInfo> getArtifacts() { - List<IArtifactInfo> temp = new ArrayList<>(); - if (artifacts != null) { - temp.addAll(artifacts); - } - return temp; - } - - public void setArtifacts(List<ArtifactInfoImpl> artifacts) { - this.artifacts = artifacts; + return artifacts != null + ? new ArrayList<>(artifacts) + : new ArrayList<>(); } - public List<ArtifactInfoImpl> getArtifactsImpl() { + public List<ArtifactInfo> getArtifactsImpl() { return artifacts; } - - @Override - public String getResourceInvariantUUID() { - return resourceInvariantUUID; - } - - public void setResourceInvariantUUID(String resourceInvariantUUID) { - this.resourceInvariantUUID = resourceInvariantUUID; - } - - public String getResourceCustomizationUUID() { - return resourceCustomizationUUID; - } - - public void setResourceCustomizationUUID(String resourceCustomizationUUID) { - this.resourceCustomizationUUID = resourceCustomizationUUID; - } - - public String getCategory() { - return category; - } - - public void setCategory(String category) { - this.category = category; - } - - public String getSubcategory() { - return subcategory; - } - - public void setSubcategory(String subcategory) { - this.subcategory = subcategory; - } } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationCallbackBuilder.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationCallbackBuilder.java new file mode 100644 index 0000000..bf13260 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationCallbackBuilder.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2024 Deutsche Telekom Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.impl; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; +import org.onap.sdc.api.notification.StatusMessage; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.utils.ArtifactTypeEnum; +import org.onap.sdc.utils.DistributionActionResultEnum; +import org.onap.sdc.utils.DistributionStatusEnum; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RequiredArgsConstructor +public class NotificationCallbackBuilder { + + private final List<String> artifactsTypes; + private final IDistributionClient distributionClient; + + public NotificationDataImpl buildCallbackNotificationLogic(long currentTimeMillis, + final NotificationDataImpl notificationFromMessageBus) { + List<IResourceInstance> relevantResourceInstances = buildResourceInstancesLogic(notificationFromMessageBus, + currentTimeMillis); + List<ArtifactInfo> relevantServiceArtifacts = handleRelevantArtifacts(notificationFromMessageBus, + currentTimeMillis, notificationFromMessageBus.getServiceArtifactsImpl()); + notificationFromMessageBus.setResources(relevantResourceInstances); + notificationFromMessageBus.setServiceArtifacts(relevantServiceArtifacts); + return notificationFromMessageBus; + } + + private List<IResourceInstance> buildResourceInstancesLogic(NotificationDataImpl notificationFromMessageBus, + long currentTimeMillis) { + + List<IResourceInstance> relevantResourceInstances = new ArrayList<>(); + + for (JsonContainerResourceInstance resourceInstance : notificationFromMessageBus.getResourcesImpl()) { + final List<ArtifactInfo> artifactsImplList = resourceInstance.getArtifactsImpl(); + List<ArtifactInfo> foundRelevantArtifacts = handleRelevantArtifacts(notificationFromMessageBus, + currentTimeMillis, artifactsImplList); + if (!foundRelevantArtifacts.isEmpty() || distributionClient.getConfiguration().isFilterInEmptyResources()) { + resourceInstance.setArtifacts(foundRelevantArtifacts); + relevantResourceInstances.add(resourceInstance); + } + } + return relevantResourceInstances; + + } + + private List<ArtifactInfo> handleRelevantArtifacts(NotificationDataImpl notificationFromMessageBus, + long currentTimeMillis, final List<ArtifactInfo> artifactsImplList) { + List<ArtifactInfo> relevantArtifacts = new ArrayList<>(); + if (artifactsImplList != null) { + for (ArtifactInfo artifactInfo : artifactsImplList) { + handleRelevantArtifact(notificationFromMessageBus, currentTimeMillis, artifactsImplList, relevantArtifacts, + artifactInfo); + } + } + return relevantArtifacts; + } + + private void handleRelevantArtifact(NotificationDataImpl notificationFromMessageBus, long currentTimeMillis, + final List<ArtifactInfo> artifactsImplList, List<ArtifactInfo> relevantArtifacts, + ArtifactInfo artifactInfo) { + boolean isArtifactRelevant = artifactsTypes.contains(artifactInfo.getArtifactType()); + String artifactType = artifactInfo.getArtifactType(); + if (artifactInfo.getGeneratedFromUUID() != null && !artifactInfo.getGeneratedFromUUID().isEmpty()) { + IArtifactInfo generatedFromArtInfo = findGeneratedFromArtifact(artifactInfo.getGeneratedFromUUID(), + artifactsImplList); + if (generatedFromArtInfo != null) { + isArtifactRelevant = isArtifactRelevant && artifactsTypes.contains(generatedFromArtInfo.getArtifactType()); + } else { + isArtifactRelevant = false; + } + } + if (isArtifactRelevant) { + setRelatedArtifacts(artifactInfo, notificationFromMessageBus); + if (artifactType.equals(ArtifactTypeEnum.HEAT.name()) || artifactType.equals(ArtifactTypeEnum.HEAT_VOL.name()) + || artifactType.equals(ArtifactTypeEnum.HEAT_NET.name())) { + setGeneratedArtifact(artifactsImplList, artifactInfo); + } + relevantArtifacts.add(artifactInfo); + + } + DistributionStatusEnum distributionStatus = isArtifactRelevant + ? DistributionStatusEnum.NOTIFIED + : DistributionStatusEnum.NOT_NOTIFIED; + StatusMessage status = StatusMessage.builder() + .distributionID(notificationFromMessageBus.getDistributionID()) + .artifactURL(artifactInfo.getArtifactURL()) + .consumerID(distributionClient.getConfiguration().getConsumerID()) + .timestamp(currentTimeMillis) + .status(distributionStatus) + .build(); + IDistributionClientResult notificationStatus = distributionClient.sendNotificationStatus(status); + if (notificationStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + log.error("Error failed to send notification status to MessageBus failed status:{}, error message:{}", + notificationStatus.getDistributionActionResult().name(), notificationStatus.getDistributionMessageResult()); + } + } + + private void setRelatedArtifacts(ArtifactInfo artifact, INotificationData notificationData) { + if (artifact.getRelatedArtifactsUUID() != null) { + List<IArtifactInfo> relatedArtifacts = new ArrayList<>(); + for (String relatedArtifactUUID : artifact.getRelatedArtifactsUUID()) { + relatedArtifacts.add(notificationData.getArtifactMetadataByUUID(relatedArtifactUUID)); + } + artifact.setRelatedArtifactsInfo(relatedArtifacts); + } + + } + + private void setGeneratedArtifact(final List<ArtifactInfo> artifactsImplList, ArtifactInfo artifactInfo) { + IArtifactInfo found = null; + String artifactUUID = artifactInfo.getArtifactUUID(); + for (ArtifactInfo generatedArtifactInfo : artifactsImplList) { + if (generatedArtifactInfo.getArtifactType().equals(ArtifactTypeEnum.HEAT_ENV.name()) + && artifactUUID.equals(generatedArtifactInfo.getGeneratedFromUUID())) { + found = generatedArtifactInfo; + break; + } + } + + artifactInfo.setGeneratedArtifact(found); + } + + private IArtifactInfo findGeneratedFromArtifact(String getGeneratedFromUUID, List<ArtifactInfo> list) { + IArtifactInfo found = null; + for (ArtifactInfo artifactInfo : list) { + if (getGeneratedFromUUID.equals(artifactInfo.getArtifactUUID())) { + found = artifactInfo; + break; + } + } + return found; + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationConsumer.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationConsumer.java index c59612a..d3091c4 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationConsumer.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationConsumer.java @@ -3,6 +3,7 @@ * sdc-distribution-client * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications copyright (C) 2024 Deutsche Telekom. 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. @@ -22,15 +23,8 @@ package org.onap.sdc.impl; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import java.util.ArrayList; import java.util.List; 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.notification.IResourceInstance; -import org.onap.sdc.api.results.IDistributionClientResult; -import org.onap.sdc.utils.ArtifactTypeEnum; -import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.kafka.SdcKafkaConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,14 +35,12 @@ class NotificationConsumer implements Runnable { private final SdcKafkaConsumer kafkaConsumer; private final INotificationCallback clientCallback; - private final List<String> artifactsTypes; - private final DistributionClientImpl distributionClient; + private final NotificationCallbackBuilder callbackBuilder; NotificationConsumer(SdcKafkaConsumer kafkaConsumer, INotificationCallback clientCallback, List<String> artifactsTypes, DistributionClientImpl distributionClient) { this.kafkaConsumer = kafkaConsumer; this.clientCallback = clientCallback; - this.artifactsTypes = artifactsTypes; - this.distributionClient = distributionClient; + this.callbackBuilder = new NotificationCallbackBuilder(artifactsTypes, distributionClient); } @Override @@ -62,7 +54,7 @@ class NotificationConsumer implements Runnable { log.debug("received notification from broker: {}", notificationMsg); final NotificationDataImpl notificationFromMessageBus = gson.fromJson(notificationMsg, NotificationDataImpl.class); - NotificationDataImpl notificationForCallback = buildCallbackNotificationLogic(currentTimeMillis, notificationFromMessageBus); + NotificationDataImpl notificationForCallback = callbackBuilder.buildCallbackNotificationLogic(currentTimeMillis, notificationFromMessageBus); if (isActivateCallback(notificationForCallback)) { String stringNotificationForCallback = gson.toJson(notificationForCallback); log.debug("sending notification to client: {}", stringNotificationForCallback); @@ -82,99 +74,4 @@ class NotificationConsumer implements Runnable { return hasRelevantArtifactsInResourceInstance || hasRelevantArtifactsInService; } - - protected NotificationDataImpl buildCallbackNotificationLogic(long currentTimeMillis, final NotificationDataImpl notificationFromMessageBus) { - List<IResourceInstance> relevantResourceInstances = buildResourceInstancesLogic(notificationFromMessageBus, currentTimeMillis); - List<ArtifactInfoImpl> relevantServiceArtifacts = handleRelevantArtifacts(notificationFromMessageBus, currentTimeMillis, notificationFromMessageBus.getServiceArtifactsImpl()); - notificationFromMessageBus.setResources(relevantResourceInstances); - notificationFromMessageBus.setServiceArtifacts(relevantServiceArtifacts); - return notificationFromMessageBus; - } - - private List<IResourceInstance> buildResourceInstancesLogic(NotificationDataImpl notificationFromMessageBus, long currentTimeMillis) { - - List<IResourceInstance> relevantResourceInstances = new ArrayList<>(); - - for (JsonContainerResourceInstance resourceInstance : notificationFromMessageBus.getResourcesImpl()) { - final List<ArtifactInfoImpl> artifactsImplList = resourceInstance.getArtifactsImpl(); - List<ArtifactInfoImpl> foundRelevantArtifacts = handleRelevantArtifacts(notificationFromMessageBus, currentTimeMillis, artifactsImplList); - if (!foundRelevantArtifacts.isEmpty() || distributionClient.getConfiguration().isFilterInEmptyResources()) { - resourceInstance.setArtifacts(foundRelevantArtifacts); - relevantResourceInstances.add(resourceInstance); - } - } - return relevantResourceInstances; - - } - - private List<ArtifactInfoImpl> handleRelevantArtifacts(NotificationDataImpl notificationFromMessageBus, long currentTimeMillis, final List<ArtifactInfoImpl> artifactsImplList) { - List<ArtifactInfoImpl> relevantArtifacts = new ArrayList<>(); - if (artifactsImplList != null) { - for (ArtifactInfoImpl artifactInfo : artifactsImplList) { - handleRelevantArtifact(notificationFromMessageBus, currentTimeMillis, artifactsImplList, relevantArtifacts, artifactInfo); - } - } - return relevantArtifacts; - } - - private void handleRelevantArtifact(NotificationDataImpl notificationFromMessageBus, long currentTimeMillis, final List<ArtifactInfoImpl> artifactsImplList, List<ArtifactInfoImpl> relevantArtifacts, ArtifactInfoImpl artifactInfo) { - boolean isArtifactRelevant = artifactsTypes.contains(artifactInfo.getArtifactType()); - String artifactType = artifactInfo.getArtifactType(); - if (artifactInfo.getGeneratedFromUUID() != null && !artifactInfo.getGeneratedFromUUID().isEmpty()) { - IArtifactInfo generatedFromArtInfo = findGeneratedFromArtifact(artifactInfo.getGeneratedFromUUID(), artifactsImplList); - if (generatedFromArtInfo != null) { - isArtifactRelevant = isArtifactRelevant && artifactsTypes.contains(generatedFromArtInfo.getArtifactType()); - } else { - isArtifactRelevant = false; - } - } - if (isArtifactRelevant) { - setRelatedArtifacts(artifactInfo, notificationFromMessageBus); - if (artifactType.equals(ArtifactTypeEnum.HEAT.name()) || artifactType.equals(ArtifactTypeEnum.HEAT_VOL.name()) || artifactType.equals(ArtifactTypeEnum.HEAT_NET.name())) { - setGeneratedArtifact(artifactsImplList, artifactInfo); - } - relevantArtifacts.add(artifactInfo); - - } - IDistributionClientResult notificationStatus = distributionClient.sendNotificationStatus(currentTimeMillis, notificationFromMessageBus.getDistributionID(), artifactInfo, isArtifactRelevant); - if (notificationStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { - log.error("Error failed to send notification status to MessageBus failed status:{}, error message:{}", notificationStatus.getDistributionActionResult().name(), notificationStatus.getDistributionMessageResult()); - } - } - - private void setRelatedArtifacts(ArtifactInfoImpl artifact, INotificationData notificationData) { - if (artifact.getRelatedArtifactsUUID() != null) { - List<IArtifactInfo> relatedArtifacts = new ArrayList<>(); - for (String relatedArtifactUUID : artifact.getRelatedArtifactsUUID()) { - relatedArtifacts.add(notificationData.getArtifactMetadataByUUID(relatedArtifactUUID)); - } - artifact.setRelatedArtifactsInfo(relatedArtifacts); - } - - } - - private void setGeneratedArtifact(final List<ArtifactInfoImpl> artifactsImplList, ArtifactInfoImpl artifactInfo) { - IArtifactInfo found = null; - String artifactUUID = artifactInfo.getArtifactUUID(); - for (ArtifactInfoImpl generatedArtifactInfo : artifactsImplList) { - if (generatedArtifactInfo.getArtifactType().equals(ArtifactTypeEnum.HEAT_ENV.name()) && artifactUUID.equals(generatedArtifactInfo.getGeneratedFromUUID())) { - found = generatedArtifactInfo; - break; - } - } - - artifactInfo.setGeneratedArtifact(found); - } - - private IArtifactInfo findGeneratedFromArtifact(String getGeneratedFromUUID, List<ArtifactInfoImpl> list) { - IArtifactInfo found = null; - for (ArtifactInfoImpl artifactInfo : list) { - if (getGeneratedFromUUID.equals(artifactInfo.getArtifactUUID())) { - found = artifactInfo; - break; - } - } - return found; - } - } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationData.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationData.java new file mode 100644 index 0000000..7d5e837 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationData.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2024 Deutsche Telekom Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.impl; + +import java.util.List; +import java.util.function.Supplier; + +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IResourceInstance; + +import lombok.Data; + +@Data +public class NotificationData implements INotificationData { + String distributionID; + String serviceName; + String serviceVersion; + String serviceUUID; + String serviceDescription; + String serviceInvariantUUID; + String workloadContext; + String artifactMetadataByUUID; + List<IResourceInstance> resources; + List<IArtifactInfo> serviceArtifacts; + + @Override + public IArtifactInfo getArtifactMetadataByUUID(String artifactUUID) { + Supplier<IArtifactInfo> artifactInfoFromResources = () -> + resources.stream() + .flatMap(resourceInstance -> resourceInstance.getArtifacts().stream()) + .filter(artifactInfo -> artifactInfo.getArtifactUUID().equals(artifactUUID)) + .findAny() + .orElse(null); + + return serviceArtifacts.stream() + .filter(artifactInfo -> artifactInfo.getArtifactUUID().equals(artifactUUID)) + .findAny() + .orElseGet(artifactInfoFromResources); + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationDataImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationDataImpl.java index 1be0fa6..75a5bcc 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationDataImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/NotificationDataImpl.java @@ -24,11 +24,14 @@ import java.util.ArrayList; import java.util.List; import org.onap.sdc.api.notification.IResourceInstance; + +import lombok.Data; + import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.INotificationData; - -class NotificationDataImpl implements INotificationData { +@Data +public class NotificationDataImpl implements INotificationData { private String distributionID; private String serviceName; @@ -37,73 +40,10 @@ class NotificationDataImpl implements INotificationData { private String serviceDescription; private String serviceInvariantUUID; private List<JsonContainerResourceInstance> resources; - private List<ArtifactInfoImpl> serviceArtifacts; + private List<ArtifactInfo> serviceArtifacts; private String workloadContext; @Override - public String getDistributionID() { - return distributionID; - } - - @Override - public String getServiceName() { - return serviceName; - } - - @Override - public String getServiceVersion() { - return serviceVersion; - } - - @Override - public String getServiceUUID() { - return serviceUUID; - } - - public void setDistributionID(String distributionID) { - this.distributionID = distributionID; - } - - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - public void setServiceVersion(String serviceVersion) { - this.serviceVersion = serviceVersion; - } - - public void setServiceUUID(String serviceUUID) { - this.serviceUUID = serviceUUID; - } - - - public String getServiceDescription() { - return serviceDescription; - } - - public void setServiceDescription(String serviceDescription) { - this.serviceDescription = serviceDescription; - } - - @Override - public String getWorkloadContext() { - return workloadContext; - } - - @Override - public void setWorkloadContext(String workloadContext) { - this.workloadContext = workloadContext; - } - - @Override - public String toString() { - return "NotificationDataImpl [distributionID=" + distributionID + ", serviceName=" + serviceName - + ", serviceVersion=" + serviceVersion + ", serviceUUID=" + serviceUUID + ", serviceDescription=" - + serviceDescription + ", serviceInvariantUUID=" + serviceInvariantUUID + ", resources=" + resources - + ", serviceArtifacts=" + serviceArtifacts + ", workloadContext=" + workloadContext + "]"; - } - - @Override public List<IResourceInstance> getResources() { List<IResourceInstance> ret = new ArrayList<>(); if (resources != null) { @@ -120,7 +60,7 @@ class NotificationDataImpl implements INotificationData { return resources; } - List<ArtifactInfoImpl> getServiceArtifactsImpl() { + List<ArtifactInfo> getServiceArtifactsImpl() { return serviceArtifacts; } @@ -134,21 +74,6 @@ class NotificationDataImpl implements INotificationData { return temp; } - void setServiceArtifacts(List<ArtifactInfoImpl> relevantServiceArtifacts) { - serviceArtifacts = relevantServiceArtifacts; - - } - - @Override - public String getServiceInvariantUUID() { - return serviceInvariantUUID; - } - - - public void setServiceInvariantUUID(String serviceInvariantUUID) { - this.serviceInvariantUUID = serviceInvariantUUID; - } - @Override public IArtifactInfo getArtifactMetadataByUUID(String artifactUUID) { IArtifactInfo ret = findArtifactInfoByUUID(artifactUUID, serviceArtifacts); @@ -164,7 +89,7 @@ class NotificationDataImpl implements INotificationData { } - private IArtifactInfo findArtifactInfoByUUID(String artifactUUID, List<ArtifactInfoImpl> listToCheck) { + private IArtifactInfo findArtifactInfoByUUID(String artifactUUID, List<ArtifactInfo> listToCheck) { IArtifactInfo ret = null; if (listToCheck != null) { for (IArtifactInfo curr : listToCheck) { diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ResourceInstance.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ResourceInstance.java new file mode 100644 index 0000000..a28a9b5 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ResourceInstance.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2024 Deutsche Telekom Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.impl; + +import java.util.List; + +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.IResourceInstance; + +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +public class ResourceInstance implements IResourceInstance { + String resourceInstanceName; + String resourceName; + String resourceVersion; + @JsonAlias("resoucreType") // also recognize this spelling when deserializing to class + @JsonProperty("resourceType") + String resourceType; + String resourceUUID; + String resourceInvariantUUID; + String resourceCustomizationUUID; + String category; + String subcategory; + List<IArtifactInfo> artifacts; +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java index a8aaa33..0dd57b0 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/StatusDataImpl.java @@ -21,15 +21,11 @@ package org.onap.sdc.impl; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.Data; import org.onap.sdc.api.notification.IStatusData; import org.onap.sdc.utils.DistributionStatusEnum; -@Getter -@Setter -@NoArgsConstructor +@Data public class StatusDataImpl implements IStatusData { private String distributionID; diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java index 9e9f7e1..fd42677 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/ArtifactTypeEnum.java @@ -37,6 +37,12 @@ public enum ArtifactTypeEnum { HEAT_NESTED, HEAT_ARTIFACT, VF_MODULES_METADATA, + ETSI_PACKAGE, + YANG_MODULE, + PM_DICTIONARY, + VES_EVENTS, + TOSCA_TEMPLATE, + TOSCA_CSAR, //DCAE Artifacts DCAE_TOSCA, DCAE_JSON, DCAE_POLICY, DCAE_DOC, DCAE_EVENT, DCAE_INVENTORY_TOSCA, DCAE_INVENTORY_JSON, diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationCallbackBuilderTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationCallbackBuilderTest.java new file mode 100644 index 0000000..790346a --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationCallbackBuilderTest.java @@ -0,0 +1,125 @@ +/** + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright © 2024 Deutsche Telekom AG Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.sdc.utils.ArtifactTypeEnum; +import org.onap.sdc.utils.DistributionActionResultEnum; +import org.onap.sdc.utils.TestConfiguration; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +@ExtendWith(MockitoExtension.class) +public class NotificationCallbackBuilderTest { + + private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + @Mock + DistributionClientImpl distributionClient; + + NotificationCallbackBuilder notificationCallbackBuilder; + + @BeforeEach + void setup() { + DistributionClientResultImpl distributionClientResult = new DistributionClientResultImpl(DistributionActionResultEnum.SUCCESS, "success"); + when(distributionClient.sendNotificationStatus(any())).thenReturn(distributionClientResult); + notificationCallbackBuilder = new NotificationCallbackBuilder(List.of(ArtifactTypeEnum.HEAT.name()), distributionClient); + } + + @Test + final void testBuildCallbackNotificationLogicFlagIsTrue() { + TestConfiguration testConfiguration = new TestConfiguration(); + testConfiguration.setFilterInEmptyResources(true); + when(distributionClient.getConfiguration()).thenReturn(testConfiguration); + NotificationDataImpl notification = gson.fromJson(getNotificationWithMultipleResources(), NotificationDataImpl.class); + + NotificationDataImpl notificationBuiltInClient = notificationCallbackBuilder.buildCallbackNotificationLogic(0, notification); + assertEquals(2, notificationBuiltInClient.getResources().size()); + } + + @Test + final void testBuildCallbackNotificationLogicFlagIsFalse() { + TestConfiguration testConfiguration = new TestConfiguration(); + testConfiguration.setFilterInEmptyResources(false); + when(distributionClient.getConfiguration()).thenReturn(testConfiguration); + NotificationDataImpl notification = gson.fromJson(getNotificationWithMultipleResources(), NotificationDataImpl.class); + + NotificationDataImpl notificationBuiltInClient = notificationCallbackBuilder.buildCallbackNotificationLogic(0, notification); + assertEquals(1, notificationBuiltInClient.getResources().size()); + } + + private String getNotificationWithMultipleResources(){ + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + + " \"serviceName\" : \"Testnotificationser1\",\r\n" + + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + + " \"resources\" : [{\r\n" + + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + + " \"resourceVersion\" : \"1.0\",\r\n" + + " \"resoucreType\" : \"VF\",\r\n" + + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + + " \"artifactDescription\" : \"MyYang\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + + " \"artifactVersion\" : \"1\"\r\n" + + " }" + + " ]\r\n" + + " },\r\n" + + " {\r\n" + + " \"resourceInstanceName\" : \"testnotificationvf12\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + + " \"resourceVersion\" : \"1.0\",\r\n" + + " \"resoucreType\" : \"VF\",\r\n" + + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2e\",\r\n" + + " \"artifacts\" : [{\r\n" + + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + + " \"artifactDescription\" : \"heat\",\r\n" + + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + + " \"artifactVersion\" : \"1\"\r\n" + + " }" + + " ]\r\n" + + " }\r\n" + + " ]}"; + } + +} diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationConsumerTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationConsumerTest.java index a70a537..64f7284 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationConsumerTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/NotificationConsumerTest.java @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * Modifications copyright (C) 2019 Nokia. All rights reserved. + * Modifications copyright (C) 2024 Deutsche Telekom. 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. @@ -26,8 +27,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -39,304 +38,241 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.mockito.stubbing.Answer; +import org.onap.sdc.api.consumer.IConfiguration; import org.onap.sdc.api.consumer.INotificationCallback; import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.StatusMessage; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.utils.ArtifactTypeEnum; import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.DistributionClientConstants; +import org.onap.sdc.utils.DistributionStatusEnum; import org.onap.sdc.utils.TestConfiguration; import org.onap.sdc.utils.kafka.SdcKafkaConsumer; class NotificationConsumerTest { - private final SdcKafkaConsumer consumer = mock(SdcKafkaConsumer.class); - private final INotificationCallback clientCallback = spy(INotificationCallback.class); - private final Queue<Iterable<String>> notificationsQueue = new LinkedList<>(); - private final DistributionClientImpl distributionClient = Mockito.spy(DistributionClientImpl.class); - private List<String> artifactsTypes = List.of(ArtifactTypeEnum.HEAT.name()); - private final List<Boolean> notificationStatusResults = new ArrayList<>(); - final static IDistributionClientResult DISTRIBUTION_SUCCESS_RESULT = buildSuccessResult(); - - private NotificationConsumer createNotificationConsumer() { - return new NotificationConsumer(consumer, clientCallback, artifactsTypes, distributionClient); - } - - @BeforeEach - public void beforeTest() { - Mockito.reset(clientCallback, distributionClient); - when(consumer.poll()).then((Answer<Iterable<String>>) invocation -> { - if (!notificationsQueue.isEmpty()) { - return notificationsQueue.remove(); - } else { - return new ArrayList<>(); - } - }); - when(distributionClient.sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean())).then( - (Answer<IDistributionClientResult>) invocation -> { - boolean isNotified = (boolean) invocation.getArguments()[3]; - notificationStatusResults.add(isNotified); - return DISTRIBUTION_SUCCESS_RESULT; - }); - - } - - private static IDistributionClientResult buildSuccessResult() { - return new IDistributionClientResult() { - - @Override - public String getDistributionMessageResult() { - return ""; - } - - @Override - public DistributionActionResultEnum getDistributionActionResult() { - return DistributionActionResultEnum.SUCCESS; - } - }; - } - - @Test - void testNoNotifiactionsSent() throws InterruptedException { - - ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); - executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); - - Thread.sleep(1000); - executorPool.shutdown(); - - Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); - - } - - @Test - void testNonRelevantNotificationSent() throws InterruptedException { - - simulateNotificationFromMessageBus(getSdcServiceNotificationWithoutHeatArtifact()); - Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); - - } - - @Test - void testRelevantNotificationSent() throws InterruptedException { - simulateNotificationFromMessageBus(getSdcServiceNotificationWithHeatArtifact()); - Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); - - } - - @Test - void testNonExistingArtifactsNotificationSent() throws InterruptedException { - simulateNotificationFromMessageBus(getSdcNotificationWithNonExistentArtifact()); - Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); - - } - - @Test - void testNotificationStatusSent() throws InterruptedException { - simulateNotificationFromMessageBus(getSdcServiceNotificationWithHeatArtifact()); - - Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); - assertEquals(1, countInstances(notificationStatusResults, Boolean.TRUE)); - assertEquals(2, countInstances(notificationStatusResults, Boolean.FALSE)); - } - - @Test - void testNotificationRelatedArtifacts() throws InterruptedException { - List<String> artifactTypesTmp = new ArrayList<>(); - for (ArtifactTypeEnum artifactTypeEnum : ArtifactTypeEnum.values()) { - artifactTypesTmp.add(artifactTypeEnum.name()); - } - artifactsTypes = artifactTypesTmp; - simulateNotificationFromMessageBus(getSdcServiceNotificationWithRelatedArtifacts()); - - Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); - assertEquals(3, countInstances(notificationStatusResults, Boolean.TRUE)); - assertEquals(0, countInstances(notificationStatusResults, Boolean.FALSE)); - } - - @Test - void testNotificationStatusWithServiceArtifatcs() throws InterruptedException { - simulateNotificationFromMessageBus(getNotificationWithServiceArtifatcs()); - Mockito.verify(distributionClient, Mockito.times(6)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); - assertEquals(2, countInstances(notificationStatusResults, Boolean.TRUE)); - assertEquals(4, countInstances(notificationStatusResults, Boolean.FALSE)); - - } - - @Test - final void testBuildCallbackNotificationLogicFlagIsFalse() { - NotificationConsumer consumer = createNotificationConsumer(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - TestConfiguration testConfiguration = new TestConfiguration(); - testConfiguration.setFilterInEmptyResources(false); - when(distributionClient.getConfiguration()).thenReturn(testConfiguration); - NotificationDataImpl notification = gson.fromJson(getNotificationWithMultipleResources(), NotificationDataImpl.class); - NotificationDataImpl notificationBuiltInClient = consumer.buildCallbackNotificationLogic(0, notification); - assertEquals(1, notificationBuiltInClient.getResources().size()); - } - - @Test - final void testBuildCallbackNotificationLogicFlagIsTrue() { - NotificationConsumer consumer = createNotificationConsumer(); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - TestConfiguration testConfiguration = new TestConfiguration(); - testConfiguration.setFilterInEmptyResources(true); - when(distributionClient.getConfiguration()).thenReturn(testConfiguration); - NotificationDataImpl notification = gson.fromJson(getNotificationWithMultipleResources(), NotificationDataImpl.class); - NotificationDataImpl notificationBuiltInClient = consumer.buildCallbackNotificationLogic(0, notification); - assertEquals(2, notificationBuiltInClient.getResources().size()); - } - - private void simulateNotificationFromMessageBus(final String notificationFromMessageBus) throws InterruptedException { - ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); - executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); - - Thread.sleep(200); - - List<String> nonHeatNotification = List.of(notificationFromMessageBus); - notificationsQueue.add(nonHeatNotification); - Thread.sleep(800); - executorPool.shutdown(); - } - - private String getSdcServiceNotificationWithHeatArtifact() { - return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" - + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" - + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" - + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" - + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" - + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" - + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" - + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" - + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" - + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" - + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" - + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" - + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; - } - - private String getNotificationWithMultipleResources(){ - return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + - " \"serviceName\" : \"Testnotificationser1\",\r\n" + - " \"serviceVersion\" : \"1.0\",\r\n" + - " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + - " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + - " \"resources\" : [{\r\n" + - " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + - " \"resourceName\" : \"TestNotificationVF1\",\r\n" + - " \"resourceVersion\" : \"1.0\",\r\n" + - " \"resoucreType\" : \"VF\",\r\n" + - " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + - " \"artifacts\" : [{\r\n" + - " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + - " \"artifactType\" : \"YANG_XML\",\r\n" + - " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + - " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + - " \"artifactDescription\" : \"MyYang\",\r\n" + - " \"artifactTimeout\" : 0,\r\n" + - " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + - " \"artifactVersion\" : \"1\"\r\n" + - " }" + - " ]\r\n" + - " },\r\n" + - " {\r\n" + - " \"resourceInstanceName\" : \"testnotificationvf12\",\r\n" + - " \"resourceName\" : \"TestNotificationVF1\",\r\n" + - " \"resourceVersion\" : \"1.0\",\r\n" + - " \"resoucreType\" : \"VF\",\r\n" + - " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2e\",\r\n" + - " \"artifacts\" : [{\r\n" + - " \"artifactName\" : \"heat.yaml\",\r\n" + - " \"artifactType\" : \"HEAT\",\r\n" + - " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + - " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + - " \"artifactDescription\" : \"heat\",\r\n" + - " \"artifactTimeout\" : 60,\r\n" + - " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + - " \"artifactVersion\" : \"1\"\r\n" + - " }" + - " ]\r\n" + - " }\r\n" + - " ]}"; - } - - - private String getSdcNotificationWithNonExistentArtifact() { - return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" - + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"bugabuga\" : \"xyz\",\r\n" + " \"resources\" : [{\r\n" - + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" - + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + " \"artifactType\" : \"HEAT\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" - + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" - + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactBuga\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" - + " }, {\r\n" + " \"artifactName\" : \"buga.bug\",\r\n" + " \"artifactType\" : \"BUGA_BUGA\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" - + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" - + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" - + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; - } - - private String getSdcServiceNotificationWithRelatedArtifacts() { - return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" - + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" - + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" - + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" - + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" - + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + " \"relatedArtifacts\" : [\r\n" - + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" - + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" - + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" - + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\", \r\n" + " \"relatedArtifacts\" : [\r\n" - + " \"0005bc4a-2c19-452e-be6d-d574a56be4d0\", \r\n" + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" - + " \"artifactName\" : \"heat.env\",\r\n" + " \"artifactType\" : \"HEAT_ENV\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" - + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" - + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" - + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; - } - - private String getSdcServiceNotificationWithoutHeatArtifact() { - return "{" + " \"distributionID\" : \"5v1234d8-5b6d-42c4-7t54-47v95n58qb7\"," + " \"serviceName\" : \"srv1\"," + " \"serviceVersion\": \"2.0\"," + " \"serviceUUID\" : \"4e0697d8-5b6d-42c4-8c74-46c33d46624c\"," - + " \"serviceArtifacts\":[" + " {" + " \"artifactName\" : \"ddd.yml\"," + " \"artifactType\" : \"DG_XML\"," + " \"artifactTimeout\" : \"65\"," - + " \"artifactDescription\" : \"description\"," + " \"artifactURL\" :" + " \"/sdc/v1/catalog/services/srv1/2.0/resources/ddd/3.0/artifacts/ddd.xml\" ," - + " \"resourceUUID\" : \"4e5874d8-5b6d-42c4-8c74-46c33d90drw\" ," + " \"checksum\" : \"15e389rnrp58hsw==\"" + " }" + " ]" + "}"; - } - - private String getNotificationWithServiceArtifatcs() { - return "{\r\n" + " \"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" - + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"serviceArtifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" - + " \"artifactType\" : \"YANG_XML\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" - + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" - + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" - + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" - + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" - + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" - + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" - + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" - + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" - + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ],\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" - + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" - + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" - + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" - + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" - + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" - + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" - + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" - + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" - + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" - + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" - + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" - + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]\r\n" + "}"; - } - - private <T> int countInstances(List<T> list, T element) { - int count = 0; - for (T curr : list) { - if (curr.equals(element)) { - count++; - } - } - return count; - } -}
\ No newline at end of file + private final SdcKafkaConsumer consumer = mock(SdcKafkaConsumer.class); + private final INotificationCallback clientCallback = spy(INotificationCallback.class); + private final Queue<Iterable<String>> notificationsQueue = new LinkedList<>(); + private final DistributionClientImpl distributionClient = Mockito.spy(DistributionClientImpl.class); + private List<String> artifactsTypes = List.of(ArtifactTypeEnum.HEAT.name()); + private final List<Boolean> notificationStatusResults = new ArrayList<>(); + final static IDistributionClientResult DISTRIBUTION_SUCCESS_RESULT = buildSuccessResult(); + + private NotificationConsumer createNotificationConsumer() { + return new NotificationConsumer(consumer, clientCallback, artifactsTypes, distributionClient); + } + + @BeforeEach + public void beforeTest() { + Mockito.reset(clientCallback, distributionClient); + when(consumer.poll()).then((Answer<Iterable<String>>) invocation -> { + if (!notificationsQueue.isEmpty()) { + return notificationsQueue.remove(); + } else { + return new ArrayList<>(); + } + }); + when(distributionClient.sendNotificationStatus(Mockito.any(StatusMessage.class))).then( + (Answer<IDistributionClientResult>) invocation -> { + StatusMessage statusMessage = (StatusMessage) invocation.getArguments()[0]; + boolean isNotified = statusMessage.getStatus().equals(DistributionStatusEnum.NOTIFIED); + notificationStatusResults.add(isNotified); + return DISTRIBUTION_SUCCESS_RESULT; + }); + IConfiguration configuration = Mockito.mock(TestConfiguration.class); + when(distributionClient.getConfiguration()).thenReturn(configuration); + when(configuration.getRelevantArtifactTypes()).thenReturn(artifactsTypes); + + } + + private static IDistributionClientResult buildSuccessResult() { + return new IDistributionClientResult() { + + @Override + public String getDistributionMessageResult() { + return ""; + } + + @Override + public DistributionActionResultEnum getDistributionActionResult() { + return DistributionActionResultEnum.SUCCESS; + } + }; + } + + @Test + void testNoNotifiactionsSent() throws InterruptedException { + + ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); + executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); + + Thread.sleep(1000); + executorPool.shutdown(); + + Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + void testNonRelevantNotificationSent() throws InterruptedException { + + simulateNotificationFromMessageBus(getSdcServiceNotificationWithoutHeatArtifact()); + Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + void testRelevantNotificationSent() throws InterruptedException { + simulateNotificationFromMessageBus(getSdcServiceNotificationWithHeatArtifact()); + Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + void testNonExistingArtifactsNotificationSent() throws InterruptedException { + simulateNotificationFromMessageBus(getSdcNotificationWithNonExistentArtifact()); + Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + void testNotificationStatusSent() throws InterruptedException { + simulateNotificationFromMessageBus(getSdcServiceNotificationWithHeatArtifact()); + + Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.any(StatusMessage.class)); + assertEquals(1, countInstances(notificationStatusResults, Boolean.TRUE)); + assertEquals(2, countInstances(notificationStatusResults, Boolean.FALSE)); + } + + @Test + void testNotificationRelatedArtifacts() throws InterruptedException { + List<String> artifactTypesTmp = new ArrayList<>(); + for (ArtifactTypeEnum artifactTypeEnum : ArtifactTypeEnum.values()) { + artifactTypesTmp.add(artifactTypeEnum.name()); + } + artifactsTypes = artifactTypesTmp; + simulateNotificationFromMessageBus(getSdcServiceNotificationWithRelatedArtifacts()); + + Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.any(StatusMessage.class)); + assertEquals(3, countInstances(notificationStatusResults, Boolean.TRUE)); + assertEquals(0, countInstances(notificationStatusResults, Boolean.FALSE)); + } + + @Test + void testNotificationStatusWithServiceArtifatcs() throws InterruptedException { + simulateNotificationFromMessageBus(getNotificationWithServiceArtifatcs()); + Mockito.verify(distributionClient, Mockito.times(6)).sendNotificationStatus(Mockito.any(StatusMessage.class)); + assertEquals(2, countInstances(notificationStatusResults, Boolean.TRUE)); + assertEquals(4, countInstances(notificationStatusResults, Boolean.FALSE)); + + } + + private void simulateNotificationFromMessageBus(final String notificationFromMessageBus) throws InterruptedException { + ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); + executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); + + Thread.sleep(200); + + List<String> nonHeatNotification = List.of(notificationFromMessageBus); + notificationsQueue.add(nonHeatNotification); + Thread.sleep(800); + executorPool.shutdown(); + } + + private String getSdcServiceNotificationWithHeatArtifact() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getSdcNotificationWithNonExistentArtifact() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"bugabuga\" : \"xyz\",\r\n" + " \"resources\" : [{\r\n" + + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + " \"artifactType\" : \"HEAT\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactBuga\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + + " }, {\r\n" + " \"artifactName\" : \"buga.bug\",\r\n" + " \"artifactType\" : \"BUGA_BUGA\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getSdcServiceNotificationWithRelatedArtifacts() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + " \"relatedArtifacts\" : [\r\n" + + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\", \r\n" + " \"relatedArtifacts\" : [\r\n" + + " \"0005bc4a-2c19-452e-be6d-d574a56be4d0\", \r\n" + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" + + " \"artifactName\" : \"heat.env\",\r\n" + " \"artifactType\" : \"HEAT_ENV\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getSdcServiceNotificationWithoutHeatArtifact() { + return "{" + " \"distributionID\" : \"5v1234d8-5b6d-42c4-7t54-47v95n58qb7\"," + " \"serviceName\" : \"srv1\"," + " \"serviceVersion\": \"2.0\"," + " \"serviceUUID\" : \"4e0697d8-5b6d-42c4-8c74-46c33d46624c\"," + + " \"serviceArtifacts\":[" + " {" + " \"artifactName\" : \"ddd.yml\"," + " \"artifactType\" : \"DG_XML\"," + " \"artifactTimeout\" : \"65\"," + + " \"artifactDescription\" : \"description\"," + " \"artifactURL\" :" + " \"/sdc/v1/catalog/services/srv1/2.0/resources/ddd/3.0/artifacts/ddd.xml\" ," + + " \"resourceUUID\" : \"4e5874d8-5b6d-42c4-8c74-46c33d90drw\" ," + " \"checksum\" : \"15e389rnrp58hsw==\"" + " }" + " ]" + "}"; + } + + private String getNotificationWithServiceArtifatcs() { + return "{\r\n" + " \"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"serviceArtifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactType\" : \"YANG_XML\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ],\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/sdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]\r\n" + "}"; + } + + private <T> int countInstances(List<T> list, T element) { + int count = 0; + for (T curr : list) { + if (curr.equals(element)) { + count++; + } + } + return count; + } +} diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/SerializationTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/SerializationTest.java new file mode 100644 index 0000000..df718d1 --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/SerializationTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2024 Deutsche Telekom Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.sdc.impl; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; +import org.onap.sdc.api.notification.INotificationData; +import org.onap.sdc.api.notification.IStatusData; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.SneakyThrows; + +public class SerializationTest { + + private static final ObjectMapper mapper = new ObjectMapper(); + + @Test + @SneakyThrows + void thatINotificationDataDefaultImplementationCanBeDeserialized() { + String json = "src/test/resources/messages/distribution.json"; + String notification = Files.readString(Paths.get(json)); + + INotificationData notificationData = mapper.readValue(notification, INotificationData.class); + assertNotNull(notificationData); + String expectedNotification = notification.replace("resoucreType", "resourceType"); // The resourceType attribute has a @TypeAlias for resoucreType + JSONAssert.assertEquals(expectedNotification, mapper.writeValueAsString(notificationData), JSONCompareMode.LENIENT); + } + + @Test + @SneakyThrows + void thatIStatusDataDefaultImplementationCanBeDeserialized() { + String json = "src/test/resources/messages/distributionStatus.json"; + String distributionStatus = Files.readString(Paths.get(json)); + + IStatusData statusData = mapper.readValue(distributionStatus, IStatusData.class); + assertNotNull(statusData); + JSONAssert.assertEquals(distributionStatus, mapper.writeValueAsString(statusData), JSONCompareMode.LENIENT); + } +} diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java index 40e1869..7420817 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/mock/DistributionClientStubImpl.java @@ -31,6 +31,7 @@ import org.onap.sdc.api.consumer.INotificationCallback; import org.onap.sdc.api.consumer.IStatusCallback; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.notification.IVfModuleMetadata; +import org.onap.sdc.api.notification.StatusMessage; import org.onap.sdc.api.results.IDistributionClientDownloadResult; import org.onap.sdc.api.results.IDistributionClientResult; @@ -125,5 +126,8 @@ public class DistributionClientStubImpl implements IDistributionClient { return null; } - + @Override + public IDistributionClientResult sendNotificationStatus(StatusMessage statusMessage) { + return new DistributionClientResultStubImpl(); + } } diff --git a/sdc-distribution-client/src/test/resources/messages/distribution.json b/sdc-distribution-client/src/test/resources/messages/distribution.json new file mode 100644 index 0000000..6fb88eb --- /dev/null +++ b/sdc-distribution-client/src/test/resources/messages/distribution.json @@ -0,0 +1,106 @@ +{ + "distributionID": "ac6f29fc-d6b3-428b-8ee2-b2e3ea636fe9", + "serviceName": "pnf_macro", + "serviceVersion": "1.0", + "serviceUUID": "1b8b0466-f7f2-4616-8198-055228f8d1eb", + "serviceDescription": "ONAP SDK Service", + "serviceInvariantUUID": "8939eac6-07f9-4395-b7ab-fe5c1a3e7cfe", + "resources": [ + { + "resourceInstanceName": "pnf_macro 0", + "resourceName": "pnf_macro", + "resourceVersion": "1.0", + "resoucreType": "PNF", + "resourceUUID": "ef5c823c-c048-4d0c-94d3-4569ee66947c", + "resourceInvariantUUID": "27dd7ddf-babc-402e-b04a-bb76a0f58a4a", + "resourceCustomizationUUID": "33e9569f-0e58-4bb4-9825-43925ddeb213", + "category": "Generic", + "subcategory": "Network Service", + "artifacts": [ + { + "artifactName": "vf-license-model.xml", + "artifactType": "VF_LICENSE", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/vf-license-model.xml", + "artifactChecksum": "ZmJlMzZkN2NkZWRjNDUyNDUwZTY2YjE0NmNkMGFiOWI=", + "artifactDescription": "VF license file", + "artifactTimeout": 0, + "artifactUUID": "f8c6e818-aa35-43d2-bc23-fb2c498fb675", + "artifactVersion": "1" + }, + { + "artifactName": "pNF.csar", + "artifactType": "ETSI_PACKAGE", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/pNF.csar", + "artifactChecksum": "ZGVhZmM4YTM3NDgwYjQxNDFmZjYzYzQ5N2E4NDFkNDE=", + "artifactDescription": "Artifact created from csar", + "artifactTimeout": 0, + "artifactUUID": "27a07164-8d63-4d7e-a8b3-8422e938b37c", + "artifactVersion": "1" + }, + { + "artifactName": "mynetconf.yang", + "artifactType": "YANG_MODULE", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/mynetconf.yang", + "artifactChecksum": "N2Q1MDkxNjUwYzU1MWI2OTUzNmY4YTc3YmQ2YTlkZTc=", + "artifactDescription": "Artifact created from csar", + "artifactTimeout": 0, + "artifactUUID": "b5ce5c4e-4501-45e6-826b-6cbfbe8dbfee", + "artifactVersion": "1" + }, + { + "artifactName": "vendor-license-model.xml", + "artifactType": "VENDOR_LICENSE", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/vendor-license-model.xml", + "artifactChecksum": "MTZiYmJhZmJiOTBjZmVkYzNjM2UwNjFkZmZmYzNkNjc=", + "artifactDescription": " Vendor license file", + "artifactTimeout": 0, + "artifactUUID": "8c68e938-4988-4107-834a-eab4c4e65392", + "artifactVersion": "1" + }, + { + "artifactName": "PM_Dictionary.yaml", + "artifactType": "PM_DICTIONARY", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/PM_Dictionary.yaml", + "artifactChecksum": "ZDJlYTVlNjA2ZGRhOTc3MDBkODVjNmEyMjA2OTA5YTE=", + "artifactDescription": "Artifact created from csar", + "artifactTimeout": 0, + "artifactUUID": "e014ef41-35a6-4222-9c52-907c3a42a0b3", + "artifactVersion": "1" + }, + { + "artifactName": "MyPnf_Pnf_v1.yaml", + "artifactType": "VES_EVENTS", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/resourceInstances/pnf_macro0/artifacts/MyPnf_Pnf_v1.yaml", + "artifactChecksum": "ZTllOGUwNjdiYWEyMWJhZDc4NjNlOGNmNGJkZWVlMWY=", + "artifactDescription": "Artifact created from csar", + "artifactTimeout": 0, + "artifactUUID": "e01c8cab-cb60-493b-895f-b1998bbaed84", + "artifactVersion": "1" + } + ] + } + ], + "serviceArtifacts": [ + { + "artifactName": "service-PnfMacro-template.yml", + "artifactType": "TOSCA_TEMPLATE", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/artifacts/service-PnfMacro-template.yml", + "artifactChecksum": "YmEzMTIzYTU5NDQ4MGIzNThmM2Q3N2Q5OTNiZjhlYjA=", + "artifactDescription": "TOSCA representation of the asset", + "artifactTimeout": 0, + "artifactUUID": "9c5c224c-7c6e-41c6-abd9-0bc5169e566c", + "artifactVersion": "1" + }, + { + "artifactName": "service-PnfMacro-csar.csar", + "artifactType": "TOSCA_CSAR", + "artifactURL": "/sdc/v1/catalog/services/PnfMacro/1.0/artifacts/service-PnfMacro-csar.csar", + "artifactChecksum": "MTQ5YmNkMjVkOWI2NmJhYzg5NTNiNzQ3YTA2NGIzYTA=", + "artifactDescription": "TOSCA definition package of the asset", + "artifactTimeout": 0, + "artifactUUID": "61d45180-7c5f-4ae0-9044-6b005de76b3f", + "artifactVersion": "1" + } + ], + "workloadContext": "Production" +} diff --git a/sdc-distribution-client/src/test/resources/messages/distributionStatus.json b/sdc-distribution-client/src/test/resources/messages/distributionStatus.json new file mode 100644 index 0000000..4348eeb --- /dev/null +++ b/sdc-distribution-client/src/test/resources/messages/distributionStatus.json @@ -0,0 +1,8 @@ +{ + "distributionID": "someId", + "consumerID": "someId", + "timestamp": 123456, + "artifactURL": "example.com", + "status":"NOTIFIED", + "componentName": "someName" +} |