95896061a9798c4da83d037924e7eb12886b630a
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.impl.datajobs.subscription.cmavc;
22
23 import static org.onap.cps.ncmp.utils.events.CloudEventMapper.toTargetEvent;
24
25 import io.cloudevents.CloudEvent;
26 import io.cloudevents.kafka.impl.KafkaHeaders;
27 import io.micrometer.core.annotation.Timed;
28 import lombok.RequiredArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.apache.kafka.clients.consumer.ConsumerRecord;
31 import org.apache.kafka.common.header.Headers;
32 import org.onap.cps.events.EventsProducer;
33 import org.onap.cps.ncmp.events.avc1_0_0.AvcEvent;
34 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
37 import org.springframework.kafka.KafkaException;
38 import org.springframework.kafka.annotation.KafkaListener;
39 import org.springframework.stereotype.Component;
40 import org.springframework.transaction.annotation.Transactional;
41
42 /**
43  * Listener for AVC events based on CM Subscriptions.
44  */
45 @Component
46 @Slf4j
47 @RequiredArgsConstructor
48 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
49 public class CmAvcEventConsumer {
50
51
52     private static final String CLOUD_EVENT_SOURCE_SYSTEM_HEADER_KEY = "ce_source";
53
54     @Value("${app.ncmp.avc.cm-events-topic}")
55     private String cmEventsTopicName;
56
57     private final EventsProducer eventsProducer;
58     private final CmAvcEventService cmAvcEventService;
59     private final InventoryPersistence inventoryPersistence;
60
61     /**
62      * Incoming Cm AvcEvent in the form of Consumer Record, it will be forwarded as is to a target topic.
63      * The key from incoming record will be used as key for the target topic as well to preserve the message ordering.
64      * If event is coming from ONAP-DMI-PLUGIN then the event will also be processed by NCMP and the cps cache/database
65      * will be updated.
66      *
67      * @param cmAvcEventAsConsumerRecord Incoming raw consumer record
68      */
69     @Transactional
70     @KafkaListener(topics = "${app.dmi.cm-events.topic}",
71         containerFactory = "cloudEventConcurrentKafkaListenerContainerFactoryForEos")
72     @Timed(value = "cps.ncmp.cm.notifications.consume.and.forward", description = "Time taken to forward CM AVC events")
73     public void consumeAndForward(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
74         if (isEventFromOnapDmiPlugin(cmAvcEventAsConsumerRecord.headers())) {
75             processCmAvcEventChanges(cmAvcEventAsConsumerRecord);
76         }
77         final CloudEvent outgoingAvcEvent = cmAvcEventAsConsumerRecord.value();
78         final String outgoingAvcEventKey = cmAvcEventAsConsumerRecord.key();
79
80         // Only for testing/demo
81         if (outgoingAvcEventKey.equals("retry")) {
82             throw new KafkaException("test kafka exception for testing");
83         }
84
85         log.info("Consuming AVC event with key : {} and value : {}", outgoingAvcEventKey, outgoingAvcEvent);
86         eventsProducer.sendCloudEventUsingEos(cmEventsTopicName, outgoingAvcEventKey, outgoingAvcEvent);
87     }
88
89     private void processCmAvcEventChanges(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
90         final String cmHandleId = cmAvcEventAsConsumerRecord.key();
91         final Boolean dataSyncEnabled = inventoryPersistence.getCmHandleState(cmHandleId).getDataSyncEnabled();
92         if (Boolean.TRUE.equals(dataSyncEnabled)) {
93             final AvcEvent cmAvcEvent = toTargetEvent(cmAvcEventAsConsumerRecord.value(), AvcEvent.class);
94             log.debug("Event to be processed to update the cache with cmHandleId : {}", cmHandleId);
95             if (cmAvcEvent != null) {
96                 cmAvcEventService.processCmAvcEvent(cmHandleId, cmAvcEvent);
97             }
98         }
99     }
100
101     private boolean isEventFromOnapDmiPlugin(final Headers headers) {
102         final String sourceSystem = KafkaHeaders.getParsedKafkaHeader(headers, CLOUD_EVENT_SOURCE_SYSTEM_HEADER_KEY);
103         return sourceSystem != null && sourceSystem.equals("ONAP-DMI-PLUGIN");
104     }
105 }