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