Merge "Normalize parent xpath when building datanodes in CpsDataService"
[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 java.util.Collection;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.concurrent.TimeUnit;
28 import lombok.RequiredArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.apache.kafka.clients.consumer.ConsumerRecord;
31 import org.onap.cps.ncmp.api.impl.config.embeddedcache.ForwardedSubscriptionEventCacheConfig;
32 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
33 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
34 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
35 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
36 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse;
37 import org.onap.cps.spi.model.DataNode;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.kafka.annotation.KafkaListener;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 @Slf4j
44 @RequiredArgsConstructor
45 public class SubscriptionEventResponseConsumer {
46
47     private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
48     private final SubscriptionPersistence subscriptionPersistence;
49     private final SubscriptionEventResponseMapper subscriptionEventResponseMapper;
50     private final SubscriptionEventResponseOutcome subscriptionEventResponseOutcome;
51
52     @Value("${notification.enabled:true}")
53     private boolean notificationFeatureEnabled;
54
55     @Value("${ncmp.model-loader.subscription:false}")
56     private boolean subscriptionModelLoaderEnabled;
57
58     /**
59      * Consume subscription response event.
60      *
61      * @param subscriptionEventResponseConsumerRecord the event to be consumed
62      */
63     @KafkaListener(topics = "${app.ncmp.avc.subscription-response-topic}",
64         properties = {"spring.json.value.default.type=org.onap.cps.ncmp.api.models.SubscriptionEventResponse"})
65     public void consumeSubscriptionEventResponse(
66             final ConsumerRecord<String, SubscriptionEventResponse> subscriptionEventResponseConsumerRecord) {
67         final SubscriptionEventResponse subscriptionEventResponse = subscriptionEventResponseConsumerRecord.value();
68         final String clientId = subscriptionEventResponse.getClientId();
69         log.info("subscription event response of clientId: {} is received.", clientId);
70         final String subscriptionName = subscriptionEventResponse.getSubscriptionName();
71         final String subscriptionEventId = clientId + subscriptionName;
72         boolean createOutcomeResponse = false;
73         if (forwardedSubscriptionEventCache.containsKey(subscriptionEventId)) {
74             final Set<String> dmiNames = forwardedSubscriptionEventCache.get(subscriptionEventId);
75
76             dmiNames.remove(subscriptionEventResponse.getDmiName());
77             forwardedSubscriptionEventCache.put(subscriptionEventId, dmiNames,
78                     ForwardedSubscriptionEventCacheConfig.SUBSCRIPTION_FORWARD_STARTED_TTL_SECS, TimeUnit.SECONDS);
79             createOutcomeResponse = forwardedSubscriptionEventCache.get(subscriptionEventId).isEmpty();
80         }
81         if (subscriptionModelLoaderEnabled) {
82             updateSubscriptionEvent(subscriptionEventResponse);
83         }
84         if (createOutcomeResponse
85                 && notificationFeatureEnabled
86                 && hasNoPendingCmHandles(clientId, subscriptionName)) {
87             subscriptionEventResponseOutcome.sendResponse(clientId, subscriptionName);
88             forwardedSubscriptionEventCache.remove(subscriptionEventId);
89         }
90     }
91
92     private boolean hasNoPendingCmHandles(final String clientId, final String subscriptionName) {
93         final Collection<DataNode> dataNodeSubscription = subscriptionPersistence.getCmHandlesForSubscriptionEvent(
94                 clientId, subscriptionName);
95         final Map<String, SubscriptionStatus> cmHandleIdToStatusMap =
96                 DataNodeHelper.getCmHandleIdToStatusMapFromDataNodes(
97                 dataNodeSubscription);
98         return !cmHandleIdToStatusMap.values().contains(SubscriptionStatus.PENDING);
99     }
100
101     private void updateSubscriptionEvent(final SubscriptionEventResponse subscriptionEventResponse) {
102         final YangModelSubscriptionEvent yangModelSubscriptionEvent =
103                 subscriptionEventResponseMapper
104                         .toYangModelSubscriptionEvent(subscriptionEventResponse);
105         subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);
106     }
107 }