Merge "Improve 32K limit tests"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / avcsubscription / SubscriptionEventResponseConsumer.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.avcsubscription;
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.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse;
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 SubscriptionEventResponseConsumer {
48
49     private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
50     private final SubscriptionPersistence subscriptionPersistence;
51     private final SubscriptionEventResponseMapper subscriptionEventResponseMapper;
52     private final SubscriptionEventResponseOutcome subscriptionEventResponseOutcome;
53
54     @Value("${notification.enabled:true}")
55     private boolean notificationFeatureEnabled;
56
57     @Value("${ncmp.model-loader.subscription:false}")
58     private boolean subscriptionModelLoaderEnabled;
59
60     /**
61      * Consume subscription response event.
62      *
63      * @param subscriptionEventResponseConsumerRecord the event to be consumed
64      */
65     @KafkaListener(topics = "${app.ncmp.avc.subscription-response-topic}",
66             containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory")
67     public void consumeSubscriptionEventResponse(
68             final ConsumerRecord<String, CloudEvent> subscriptionEventResponseConsumerRecord) {
69         final CloudEvent cloudEvent = subscriptionEventResponseConsumerRecord.value();
70         final String eventType = subscriptionEventResponseConsumerRecord.value().getType();
71         final SubscriptionEventResponse subscriptionEventResponse =
72                 SubscriptionEventResponseCloudMapper.toSubscriptionEventResponse(cloudEvent);
73         final String clientId = subscriptionEventResponse.getData().getClientId();
74         log.info("subscription event response of clientId: {} is received.", clientId);
75         final String subscriptionName = subscriptionEventResponse.getData().getSubscriptionName();
76         final String subscriptionEventId = clientId + subscriptionName;
77         boolean createOutcomeResponse = false;
78         if (forwardedSubscriptionEventCache.containsKey(subscriptionEventId)) {
79             final Set<String> dmiNames = forwardedSubscriptionEventCache.get(subscriptionEventId);
80             dmiNames.remove(subscriptionEventResponse.getData().getDmiName());
81             forwardedSubscriptionEventCache.put(subscriptionEventId, dmiNames,
82                     ForwardedSubscriptionEventCacheConfig.SUBSCRIPTION_FORWARD_STARTED_TTL_SECS, TimeUnit.SECONDS);
83             createOutcomeResponse = forwardedSubscriptionEventCache.get(subscriptionEventId).isEmpty();
84         }
85         if (subscriptionModelLoaderEnabled) {
86             updateSubscriptionEvent(subscriptionEventResponse);
87         }
88         if (createOutcomeResponse
89                 && notificationFeatureEnabled
90                 && hasNoPendingCmHandles(clientId, subscriptionName)) {
91             subscriptionEventResponseOutcome.sendResponse(subscriptionEventResponse, eventType);
92             forwardedSubscriptionEventCache.remove(subscriptionEventId);
93         }
94     }
95
96     private boolean hasNoPendingCmHandles(final String clientId, final String subscriptionName) {
97         final Collection<DataNode> dataNodeSubscription = subscriptionPersistence.getCmHandlesForSubscriptionEvent(
98                 clientId, subscriptionName);
99         final Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapOriginal =
100                 DataNodeHelper.cmHandleIdToStatusAndDetailsAsMapFromDataNode(dataNodeSubscription);
101         for (final Map<String, String> statusAndDetailsMap : cmHandleIdToStatusAndDetailsAsMapOriginal.values()) {
102             final String status = statusAndDetailsMap.get("status");
103             if (SubscriptionStatus.PENDING.toString().equals(status)) {
104                 return false;
105             }
106         }
107         return true;
108     }
109
110     private void updateSubscriptionEvent(final SubscriptionEventResponse subscriptionEventResponse) {
111         final YangModelSubscriptionEvent yangModelSubscriptionEvent =
112                 subscriptionEventResponseMapper
113                         .toYangModelSubscriptionEvent(subscriptionEventResponse);
114         subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);
115     }
116 }