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