Modify DmiDataOperationRequest to make it identical as DataOperationRequest
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmSubscriptionDmiOutEventConsumer.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.cmsubscription;
22
23 import com.hazelcast.map.IMap;
24 import io.cloudevents.CloudEvent;
25 import java.util.Collection;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.concurrent.TimeUnit;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.apache.kafka.clients.consumer.ConsumerRecord;
32 import org.onap.cps.ncmp.api.impl.config.embeddedcache.ForwardedSubscriptionEventCacheConfig;
33 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
34 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
35 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
36 import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventResponseCloudMapper;
37 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
38 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent;
39 import org.onap.cps.spi.model.DataNode;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.kafka.annotation.KafkaListener;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 @Slf4j
46 @RequiredArgsConstructor
47 public class CmSubscriptionDmiOutEventConsumer {
48
49     private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
50     private final SubscriptionPersistence subscriptionPersistence;
51     private final CmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper
52             cmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper;
53     private final CmSubscriptionNcmpOutEventPublisher cmSubscriptionNcmpOutEventPublisher;
54     private final SubscriptionEventResponseCloudMapper subscriptionEventResponseCloudMapper;
55
56     @Value("${notification.enabled:true}")
57     private boolean notificationFeatureEnabled;
58
59     @Value("${ncmp.model-loader.subscription:false}")
60     private boolean subscriptionModelLoaderEnabled;
61
62     /**
63      * Consume subscription response event.
64      *
65      * @param cmSubscriptionDmiOutConsumerRecord the event to be consumed
66      */
67     @KafkaListener(topics = "${app.ncmp.avc.subscription-response-topic}",
68             containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory")
69     public void consumeSubscriptionEventResponse(
70             final ConsumerRecord<String, CloudEvent> cmSubscriptionDmiOutConsumerRecord) {
71         final CloudEvent cloudEvent = cmSubscriptionDmiOutConsumerRecord.value();
72         final String eventType = cmSubscriptionDmiOutConsumerRecord.value().getType();
73         final CmSubscriptionDmiOutEvent cmSubscriptionDmiOutEvent =
74                 subscriptionEventResponseCloudMapper.toCmSubscriptionDmiOutEvent(cloudEvent);
75         final String clientId = cmSubscriptionDmiOutEvent.getData().getClientId();
76         log.info("subscription event response of clientId: {} is received.", clientId);
77         final String subscriptionName = cmSubscriptionDmiOutEvent.getData().getSubscriptionName();
78         final String subscriptionEventId = clientId + subscriptionName;
79         boolean createOutcomeResponse = false;
80         if (forwardedSubscriptionEventCache.containsKey(subscriptionEventId)) {
81             final Set<String> dmiNames = forwardedSubscriptionEventCache.get(subscriptionEventId);
82             dmiNames.remove(cmSubscriptionDmiOutEvent.getData().getDmiName());
83             forwardedSubscriptionEventCache.put(subscriptionEventId, dmiNames,
84                     ForwardedSubscriptionEventCacheConfig.SUBSCRIPTION_FORWARD_STARTED_TTL_SECS, TimeUnit.SECONDS);
85             createOutcomeResponse = forwardedSubscriptionEventCache.get(subscriptionEventId).isEmpty();
86         }
87         if (subscriptionModelLoaderEnabled) {
88             updateSubscriptionEvent(cmSubscriptionDmiOutEvent);
89         }
90         if (createOutcomeResponse
91                 && notificationFeatureEnabled
92                 && hasNoPendingCmHandles(clientId, subscriptionName)) {
93             cmSubscriptionNcmpOutEventPublisher.sendResponse(cmSubscriptionDmiOutEvent, eventType);
94             forwardedSubscriptionEventCache.remove(subscriptionEventId);
95         }
96     }
97
98     private boolean hasNoPendingCmHandles(final String clientId, final String subscriptionName) {
99         final Collection<DataNode> dataNodeSubscription = subscriptionPersistence.getCmHandlesForSubscriptionEvent(
100                 clientId, subscriptionName);
101         final Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapOriginal =
102                 DataNodeHelper.cmHandleIdToStatusAndDetailsAsMapFromDataNode(dataNodeSubscription);
103         for (final Map<String, String> statusAndDetailsMap : cmHandleIdToStatusAndDetailsAsMapOriginal.values()) {
104             final String status = statusAndDetailsMap.get("status");
105             if (SubscriptionStatus.PENDING.toString().equals(status)) {
106                 return false;
107             }
108         }
109         return true;
110     }
111
112     private void updateSubscriptionEvent(final CmSubscriptionDmiOutEvent cmSubscriptionDmiOutEvent) {
113         final YangModelSubscriptionEvent yangModelSubscriptionEvent =
114                 cmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper
115                         .toYangModelSubscriptionEvent(cmSubscriptionDmiOutEvent);
116         subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);
117     }
118 }