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.events.avcsubscription;
23 import com.hazelcast.map.IMap;
24 import io.cloudevents.CloudEvent;
25 import java.util.Collection;
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;
46 @RequiredArgsConstructor
47 public class SubscriptionEventResponseConsumer {
49 private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
50 private final SubscriptionPersistence subscriptionPersistence;
51 private final SubscriptionEventResponseMapper subscriptionEventResponseMapper;
52 private final SubscriptionEventResponseOutcome subscriptionEventResponseOutcome;
53 private final SubscriptionEventResponseCloudMapper subscriptionEventResponseCloudMapper;
55 @Value("${notification.enabled:true}")
56 private boolean notificationFeatureEnabled;
58 @Value("${ncmp.model-loader.subscription:false}")
59 private boolean subscriptionModelLoaderEnabled;
62 * Consume subscription response event.
64 * @param subscriptionEventResponseConsumerRecord the event to be consumed
66 @KafkaListener(topics = "${app.ncmp.avc.subscription-response-topic}",
67 containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory")
68 public void consumeSubscriptionEventResponse(
69 final ConsumerRecord<String, CloudEvent> subscriptionEventResponseConsumerRecord) {
70 final CloudEvent cloudEvent = subscriptionEventResponseConsumerRecord.value();
71 final String eventType = subscriptionEventResponseConsumerRecord.value().getType();
72 final SubscriptionEventResponse subscriptionEventResponse =
73 subscriptionEventResponseCloudMapper.toSubscriptionEventResponse(cloudEvent);
74 final String clientId = subscriptionEventResponse.getData().getClientId();
75 log.info("subscription event response of clientId: {} is received.", clientId);
76 final String subscriptionName = subscriptionEventResponse.getData().getSubscriptionName();
77 final String subscriptionEventId = clientId + subscriptionName;
78 boolean createOutcomeResponse = false;
79 if (forwardedSubscriptionEventCache.containsKey(subscriptionEventId)) {
80 final Set<String> dmiNames = forwardedSubscriptionEventCache.get(subscriptionEventId);
81 dmiNames.remove(subscriptionEventResponse.getData().getDmiName());
82 forwardedSubscriptionEventCache.put(subscriptionEventId, dmiNames,
83 ForwardedSubscriptionEventCacheConfig.SUBSCRIPTION_FORWARD_STARTED_TTL_SECS, TimeUnit.SECONDS);
84 createOutcomeResponse = forwardedSubscriptionEventCache.get(subscriptionEventId).isEmpty();
86 if (subscriptionModelLoaderEnabled) {
87 updateSubscriptionEvent(subscriptionEventResponse);
89 if (createOutcomeResponse
90 && notificationFeatureEnabled
91 && hasNoPendingCmHandles(clientId, subscriptionName)) {
92 subscriptionEventResponseOutcome.sendResponse(subscriptionEventResponse, eventType);
93 forwardedSubscriptionEventCache.remove(subscriptionEventId);
97 private boolean hasNoPendingCmHandles(final String clientId, final String subscriptionName) {
98 final Collection<DataNode> dataNodeSubscription = subscriptionPersistence.getCmHandlesForSubscriptionEvent(
99 clientId, subscriptionName);
100 final Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapOriginal =
101 DataNodeHelper.cmHandleIdToStatusAndDetailsAsMapFromDataNode(dataNodeSubscription);
102 for (final Map<String, String> statusAndDetailsMap : cmHandleIdToStatusAndDetailsAsMapOriginal.values()) {
103 final String status = statusAndDetailsMap.get("status");
104 if (SubscriptionStatus.PENDING.toString().equals(status)) {
111 private void updateSubscriptionEvent(final SubscriptionEventResponse subscriptionEventResponse) {
112 final YangModelSubscriptionEvent yangModelSubscriptionEvent =
113 subscriptionEventResponseMapper
114 .toYangModelSubscriptionEvent(subscriptionEventResponse);
115 subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);