Subscription Create Event Outcome Database Part 44/134444/6
authorhalil.cakal <halil.cakal@est.tech>
Wed, 3 May 2023 12:22:33 +0000 (13:22 +0100)
committerPriyank Maheshwari <priyank.maheshwari@est.tech>
Thu, 11 May 2023 09:37:43 +0000 (09:37 +0000)
- 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>
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumer.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventMapper.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapper.java [new file with mode: 0644]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/SubscriptionEventResponse.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/avc/SubscriptionEventResponseConsumerSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avc/SubscriptionEventResponseMapperSpec.groovy [new file with mode: 0644]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceSpec.groovy
cps-ncmp-service/src/test/resources/avcSubscriptionEventResponse.json [new file with mode: 0644]

index b332ad1..c173862 100644 (file)
@@ -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
index 3c238dd..dcbdcf3 100644 (file)
@@ -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 (file)
index 0000000..44181c5
--- /dev/null
@@ -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());
+    }
+}
index 4895735..e8de083 100644 (file)
@@ -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) {
index 95e773c..05663a5 100644 (file)
@@ -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;
 }
index a673462..e9f6689 100644 (file)
@@ -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 (file)
index 0000000..7fb817b
--- /dev/null
@@ -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
index dbc8104..7576009 100644 (file)
@@ -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 (file)
index 0000000..b054362
--- /dev/null
@@ -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