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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.event.avc;
23 import com.hazelcast.map.IMap;
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;
40 @RequiredArgsConstructor
41 public class SubscriptionEventResponseConsumer {
43 private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
44 private final SubscriptionPersistence subscriptionPersistence;
45 private final SubscriptionEventResponseMapper subscriptionEventResponseMapper;
46 private final SubscriptionEventResponseOutcome subscriptionEventResponseOutcome;
48 @Value("${notification.enabled:true}")
49 private boolean notificationFeatureEnabled;
51 @Value("${ncmp.model-loader.subscription:false}")
52 private boolean subscriptionModelLoaderEnabled;
55 * Consume subscription response event.
57 * @param subscriptionEventResponseConsumerRecord the event to be consumed
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);
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();
77 if (isFullOutcomeResponse) {
78 forwardedSubscriptionEventCache.remove(subscriptionEventId);
81 if (subscriptionModelLoaderEnabled) {
82 updateSubscriptionEvent(subscriptionEventResponse);
84 if (isFullOutcomeResponse && notificationFeatureEnabled) {
85 subscriptionEventResponseOutcome.sendResponse(clientId, subscriptionName,
86 isFullOutcomeResponse);
90 private void updateSubscriptionEvent(final SubscriptionEventResponse subscriptionEventResponse) {
91 final YangModelSubscriptionEvent yangModelSubscriptionEvent =
92 subscriptionEventResponseMapper
93 .toYangModelSubscriptionEvent(subscriptionEventResponse);
94 subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);