From f8aaf8b5f26573d0dd66fef2365e547b7ca6ee7a Mon Sep 17 00:00:00 2001 From: "halil.cakal" Date: Wed, 3 May 2023 13:22:33 +0100 Subject: 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 --- .../avc/SubscriptionEventResponseConsumer.java | 13 ++++- .../avcsubscription/SubscriptionEventMapper.java | 16 +++++-- .../SubscriptionEventResponseMapper.java | 56 ++++++++++++++++++++++ .../subscriptions/SubscriptionPersistenceImpl.java | 29 +++++++++-- .../ncmp/api/models/SubscriptionEventResponse.java | 5 ++ 5 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapper.java (limited to 'cps-ncmp-service/src/main/java/org') 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> 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 mapTargetsToCmHandleTargets(List 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 mapStatusToCmHandleTargets( + Map 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 dataNodes = cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME, + SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + final Optional 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 cmHandleIdToStatus; } -- cgit 1.2.3-korg