diff options
author | halil.cakal <halil.cakal@est.tech> | 2023-05-03 13:22:33 +0100 |
---|---|---|
committer | Priyank Maheshwari <priyank.maheshwari@est.tech> | 2023-05-11 09:37:43 +0000 |
commit | f8aaf8b5f26573d0dd66fef2365e547b7ca6ee7a (patch) | |
tree | d6452788e425a938cae6bf3a6ea60e69c4e66804 /cps-ncmp-service/src | |
parent | e626c9661fd88a585b50dafab5f5542784690143 (diff) |
Subscription Create Event Outcome Database Part
- Add mapper to convert subscription response to Yang model
- Add update operation to update Yang model into database
- Change Subscription persistence to support both save and update operations
Issue-ID: CPS-1669
Change-Id: I40cab1052ada5846945c67cac8640c393358e988
Signed-off-by: halil.cakal <halil.cakal@est.tech>
Diffstat (limited to 'cps-ncmp-service/src')
9 files changed, 225 insertions, 19 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumer.java index b332ad1a0e..c173862476 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumer.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumer.java @@ -24,6 +24,9 @@ import com.hazelcast.map.IMap; import java.util.Set; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.events.avcsubscription.SubscriptionEventResponseMapper; +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; import org.onap.cps.ncmp.api.models.SubscriptionEventResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.kafka.annotation.KafkaListener; @@ -36,6 +39,10 @@ public class SubscriptionEventResponseConsumer { private final IMap<String, Set<String>> forwardedSubscriptionEventCache; + private final SubscriptionPersistence subscriptionPersistence; + + private final SubscriptionEventResponseMapper subscriptionEventResponseMapper; + @Value("${app.ncmp.avc.subscription-outcome-topic}") private String subscriptionOutcomeEventTopic; @@ -76,7 +83,9 @@ public class SubscriptionEventResponseConsumer { } private void updateSubscriptionEvent(final SubscriptionEventResponse subscriptionEventResponse) { - log.info("placeholder to update persisted subscription for subscriptionEventId: {}.", - subscriptionEventResponse.getClientId() + subscriptionEventResponse.getSubscriptionName()); + final YangModelSubscriptionEvent yangModelSubscriptionEvent = + subscriptionEventResponseMapper + .toYangModelSubscriptionEvent(subscriptionEventResponse); + subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent); } }
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventMapper.java index 3c238dda28..dcbdcf339d 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventMapper.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventMapper.java @@ -25,6 +25,7 @@ import java.util.stream.Collectors; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Named; +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; import org.onap.cps.ncmp.event.model.SubscriptionEvent; @@ -34,14 +35,21 @@ public interface SubscriptionEventMapper { @Mapping(source = "event.subscription.clientID", target = "clientId") @Mapping(source = "event.subscription.name", target = "subscriptionName") @Mapping(source = "event.subscription.isTagged", target = "tagged") - @Mapping(source = "event.predicates.targets", - target = "predicates.targetCmHandles", qualifiedByName = "mapTargetsToCmHandleTargets") + @Mapping(source = "event.predicates.targets", target = "predicates.targetCmHandles", + qualifiedByName = "mapTargetsToCmHandleTargets") @Mapping(source = "event.predicates.datastore", target = "predicates.datastore") YangModelSubscriptionEvent toYangModelSubscriptionEvent(SubscriptionEvent subscriptionEvent); + /** + * Maps list of Targets to list of TargetCmHandle. + * + * @param targets list of objects + * @return TargetCmHandle list + */ @Named("mapTargetsToCmHandleTargets") default List<YangModelSubscriptionEvent.TargetCmHandle> mapTargetsToCmHandleTargets(List<Object> targets) { - return targets.stream().map( - target -> new YangModelSubscriptionEvent.TargetCmHandle(target.toString())).collect(Collectors.toList()); + return targets.stream().map(target -> new YangModelSubscriptionEvent.TargetCmHandle(target.toString(), + SubscriptionStatus.PENDING)) + .collect(Collectors.toList()); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapper.java new file mode 100644 index 0000000000..44181c57c9 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapper.java @@ -0,0 +1,56 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.events.avcsubscription; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; +import org.onap.cps.ncmp.api.models.SubscriptionEventResponse; + +@Mapper(componentModel = "spring") +public interface SubscriptionEventResponseMapper { + + @Mapping(source = "clientId", target = "clientId") + @Mapping(source = "subscriptionName", target = "subscriptionName") + @Mapping(source = "cmHandleIdToStatus", target = "predicates.targetCmHandles", + qualifiedByName = "mapStatusToCmHandleTargets") + YangModelSubscriptionEvent toYangModelSubscriptionEvent( + SubscriptionEventResponse subscriptionEventResponse); + + /** + * Maps StatusToCMHandle to list of TargetCmHandle. + * + * @param targets as a map + * @return TargetCmHandle list + */ + @Named("mapStatusToCmHandleTargets") + default List<YangModelSubscriptionEvent.TargetCmHandle> mapStatusToCmHandleTargets( + Map<String, SubscriptionStatus> targets) { + return targets.entrySet().stream().map(target -> + new YangModelSubscriptionEvent.TargetCmHandle(target.getKey(), target.getValue())).collect( + Collectors.toList()); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java index 4895735afe..e8de083fde 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java @@ -22,10 +22,14 @@ package org.onap.cps.ncmp.api.impl.subscriptions; import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP; +import java.util.Collection; +import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsDataService; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent; +import org.onap.cps.spi.FetchDescendantsOption; +import org.onap.cps.spi.model.DataNode; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.stereotype.Component; @@ -44,9 +48,28 @@ public class SubscriptionPersistenceImpl implements SubscriptionPersistence { @Override public void saveSubscriptionEvent(final YangModelSubscriptionEvent yangModelSubscriptionEvent) { final String subscriptionEventJsonData = - createSubscriptionEventJsonData(jsonObjectMapper.asJsonString(yangModelSubscriptionEvent)); - cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, - SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP); + createSubscriptionEventJsonData(jsonObjectMapper.asJsonString(yangModelSubscriptionEvent)); + final Collection<DataNode> dataNodes = cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME, + SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + final Optional<DataNode> optional = dataNodes.stream().findFirst(); + if (optional.isPresent() && optional.get().getChildDataNodes().isEmpty()) { + saveOrUpdateSubscriptionEventYangModel(subscriptionEventJsonData, false); + } else { + saveOrUpdateSubscriptionEventYangModel(subscriptionEventJsonData, true); + } + } + + private void saveOrUpdateSubscriptionEventYangModel(final String subscriptionEventJsonData, + final boolean isDataNodeExist) { + if (isDataNodeExist) { + log.info("SubscriptionEventJsonData to be updated into DB {}", subscriptionEventJsonData); + cpsDataService.updateDataNodeAndDescendants(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, + SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP); + } else { + log.info("SubscriptionEventJsonData to be saved into DB {}", subscriptionEventJsonData); + cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, + SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP); + } } private static String createSubscriptionEventJsonData(final String yangModelSubscriptionAsJson) { diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/SubscriptionEventResponse.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/SubscriptionEventResponse.java index 95e773c8c9..05663a55d8 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/SubscriptionEventResponse.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/SubscriptionEventResponse.java @@ -21,6 +21,7 @@ package org.onap.cps.ncmp.api.models; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Map; import lombok.Getter; import lombok.Setter; @@ -30,8 +31,12 @@ import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus; @Getter @Setter public class SubscriptionEventResponse { + @JsonProperty("clientId") private String clientId; + @JsonProperty("subscriptionName") private String subscriptionName; + @JsonProperty("dmiName") private String dmiName; + @JsonProperty("cmHandleIdToStatus") private Map<String, SubscriptionStatus> cmHandleIdToStatus; } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumerSpec.groovy index a673462008..e9f66892cb 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumerSpec.groovy @@ -22,6 +22,8 @@ package org.onap.cps.ncmp.api.impl.event.avc import com.fasterxml.jackson.databind.ObjectMapper import com.hazelcast.map.IMap +import org.onap.cps.ncmp.api.impl.events.avcsubscription.SubscriptionEventResponseMapper +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.api.models.SubscriptionEventResponse import org.onap.cps.utils.JsonObjectMapper @@ -31,8 +33,11 @@ import org.springframework.boot.test.context.SpringBootTest class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec { IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>) + def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl) + def mockSubscriptionEventResponseMapper = Mock(SubscriptionEventResponseMapper) - def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache) + def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache, + mockSubscriptionPersistence, mockSubscriptionEventResponseMapper) def 'Consume Subscription Event Response where all DMIs have responded'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avc/SubscriptionEventResponseMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avc/SubscriptionEventResponseMapperSpec.groovy new file mode 100644 index 0000000000..7fb817bc9a --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avc/SubscriptionEventResponseMapperSpec.groovy @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.events.avc + +import com.fasterxml.jackson.databind.ObjectMapper +import org.mapstruct.factory.Mappers +import org.onap.cps.ncmp.api.impl.events.avcsubscription.SubscriptionEventResponseMapper +import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus +import org.onap.cps.ncmp.api.models.SubscriptionEventResponse +import org.onap.cps.ncmp.utils.TestUtils +import org.onap.cps.utils.JsonObjectMapper +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import spock.lang.Specification + + +@SpringBootTest(classes = [JsonObjectMapper, ObjectMapper]) +class SubscriptionEventResponseMapperSpec extends Specification { + + SubscriptionEventResponseMapper objectUnderTest = Mappers.getMapper(SubscriptionEventResponseMapper) + + @Autowired + JsonObjectMapper jsonObjectMapper + + def 'Map subscription response event to yang model subscription event'() { + given: 'a Subscription Response Event' + def jsonData = TestUtils.getResourceFileContent('avcSubscriptionEventResponse.json') + def testEventToMap = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventResponse.class) + when: 'the event is mapped to a yang model subscription' + def result = objectUnderTest.toYangModelSubscriptionEvent(testEventToMap) + then: 'the resulting yang model subscription event contains the correct clientId' + assert result.clientId == "SCO-9989752" + and: 'subscription name' + assert result.subscriptionName == "cm-subscription-001" + and: 'predicate targets ' + assert result.predicates.targetCmHandles.cmHandleId == ["CMHandle1", "CMHandle2"] + and: 'the status for these targets is set to expected values' + assert result.predicates.targetCmHandles.status == [SubscriptionStatus.ACCEPTED, SubscriptionStatus.REJECTED] + and: 'the topic is null' + assert result.topic == null + } + +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceSpec.groovy index dbc8104767..75760091d3 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceSpec.groovy @@ -23,6 +23,7 @@ package org.onap.cps.ncmp.api.impl.subscriptions import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.api.CpsDataService import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent +import org.onap.cps.spi.model.DataNodeBuilder import org.onap.cps.utils.JsonObjectMapper import spock.lang.Specification @@ -42,17 +43,19 @@ class SubscriptionPersistenceSpec extends Specification { def 'save a subscription event' () { given: 'a yang model subscription event' - def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(); - yangModelSubscriptionEvent.setClientId('some-client-id') - yangModelSubscriptionEvent.setSubscriptionName('some-subscription-name') - yangModelSubscriptionEvent.setTagged(true) - yangModelSubscriptionEvent.setTopic('some-topic') def predicates = new YangModelSubscriptionEvent.Predicates(datastore: 'some-datastore', - targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'), new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')]) - yangModelSubscriptionEvent.setPredicates(predicates) - when: 'the yangModelSubscriptionEvent is saved' + targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'), + new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')]) + def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(clientId: 'some-client-id', + subscriptionName: 'some-subscription-name', tagged: true, topic: 'some-topic', predicates: predicates) + and: 'a data node that does not exist in db' + def dataNodeNonExist = new DataNodeBuilder().withDataspace('NCMP-Admin') + .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry').build() + and: 'cps data service return non existing data node' + mockCpsDataService.getDataNodes(*_) >> [dataNodeNonExist] + when: 'the yangModelSubscriptionEvent is saved into db' objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent) - then: 'the cpsDataService is called with the correct data' + then: 'the cpsDataService save operation is called with the correct data' 1 * mockCpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT, '{"subscription":[{' + @@ -62,4 +65,31 @@ class SubscriptionPersistenceSpec extends Specification { NO_TIMESTAMP) } + def 'update a subscription event' () { + given: 'a yang model subscription event' + def predicates = new YangModelSubscriptionEvent.Predicates(datastore: 'some-datastore', + targetCmHandles: [new YangModelSubscriptionEvent.TargetCmHandle('cmhandle1'), + new YangModelSubscriptionEvent.TargetCmHandle('cmhandle2')]) + def yangModelSubscriptionEvent = new YangModelSubscriptionEvent(clientId: 'some-client-id', + subscriptionName: 'some-subscription-name', tagged: true, topic: 'some-topic', predicates: predicates) + and: 'a data node exist in db' + def childDataNode = new DataNodeBuilder().withDataspace('NCMP-Admin') + .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription').build() + def dataNodeExist = new DataNodeBuilder().withDataspace('NCMP-Admin') + .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry') + .withChildDataNodes([childDataNode]).build() + and: 'cps data service return existing data node' + mockCpsDataService.getDataNodes(*_) >> [dataNodeExist] + when: 'the yangModelSubscriptionEvent is saved into db' + objectUnderTest.saveSubscriptionEvent(yangModelSubscriptionEvent) + then: 'the cpsDataService update operation is called with the correct data' + 1 * mockCpsDataService.updateDataNodeAndDescendants(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, + SUBSCRIPTION_REGISTRY_PARENT, + '{"subscription":[{' + + '"topic":"some-topic",' + + '"predicates":{"datastore":"some-datastore","targetCmHandles":[{"cmHandleId":"cmhandle1","status":"PENDING"},{"cmHandleId":"cmhandle2","status":"PENDING"}]},' + + '"clientID":"some-client-id","subscriptionName":"some-subscription-name","isTagged":true}]}', + NO_TIMESTAMP) + } + } diff --git a/cps-ncmp-service/src/test/resources/avcSubscriptionEventResponse.json b/cps-ncmp-service/src/test/resources/avcSubscriptionEventResponse.json new file mode 100644 index 0000000000..b054362c93 --- /dev/null +++ b/cps-ncmp-service/src/test/resources/avcSubscriptionEventResponse.json @@ -0,0 +1,9 @@ +{ + "clientId": "SCO-9989752", + "subscriptionName": "cm-subscription-001", + "dmiName": "ncmp-dmi-plugin", + "cmHandleIdToStatus": { + "CMHandle1": "ACCEPTED", + "CMHandle2": "REJECTED" + } +}
\ No newline at end of file |