3397df73f3acf54ad7833502c0286b014664ebcc
[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.annotation.KafkaListener;
38 import org.springframework.stereotype.Component;
39 import org.springframework.transaction.annotation.Transactional;
40
41 /**
42  * Listener for AVC events based on CM Subscriptions.
43  */
44 @Component
45 @Slf4j
46 @RequiredArgsConstructor
47 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
48 public class CmAvcEventConsumer {
49
50     private static final String CLOUD_EVENT_SOURCE_SYSTEM_HEADER_KEY = "ce_source";
51
52     @Value("${app.ncmp.avc.cm-events-topic}")
53     private String cmEventsTopicName;
54
55     private final EventsProducer eventsProducer;
56     private final CmAvcEventService cmAvcEventService;
57     private final InventoryPersistence inventoryPersistence;
58
59     /**
60      * Incoming Cm AvcEvent in the form of Consumer Record, it will be forwarded as is to a target topic.
61      * The key from incoming record will be used as key for the target topic as well to preserve the message ordering.
62      * If event is coming from ONAP-DMI-PLUGIN then the event will also be processed by NCMP and the cps cache/database
63      * will be updated.
64      *
65      * @param cmAvcEventAsConsumerRecord Incoming raw consumer record
66      */
67     @Transactional
68     @KafkaListener(topics = "${app.dmi.cm-events.topic}",
69         containerFactory = "cloudEventConcurrentKafkaListenerContainerFactoryForEos")
70     @Timed(value = "cps.ncmp.cm.notifications.consume.and.forward", description = "Time taken to forward CM AVC events")
71     public void consumeAndForward(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
72         if (isEventFromOnapDmiPlugin(cmAvcEventAsConsumerRecord.headers())) {
73             processCmAvcEventChanges(cmAvcEventAsConsumerRecord);
74         }
75         final CloudEvent outgoingAvcEvent = cmAvcEventAsConsumerRecord.value();
76         final String outgoingAvcEventKey = cmAvcEventAsConsumerRecord.key();
77
78         log.debug("Consuming AVC event with key : {} and value : {}", outgoingAvcEventKey, outgoingAvcEvent);
79         eventsProducer.sendCloudEventUsingEos(cmEventsTopicName, outgoingAvcEventKey, outgoingAvcEvent);
80     }
81
82     private void processCmAvcEventChanges(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
83         final String cmHandleId = cmAvcEventAsConsumerRecord.key();
84         final Boolean dataSyncEnabled = inventoryPersistence.getCmHandleState(cmHandleId).getDataSyncEnabled();
85         if (Boolean.TRUE.equals(dataSyncEnabled)) {
86             final AvcEvent cmAvcEvent = toTargetEvent(cmAvcEventAsConsumerRecord.value(), AvcEvent.class);
87             log.debug("Event to be processed to update the cache with cmHandleId : {}", cmHandleId);
88             if (cmAvcEvent != null) {
89                 cmAvcEventService.processCmAvcEvent(cmHandleId, cmAvcEvent);
90             }
91         }
92     }
93
94     private boolean isEventFromOnapDmiPlugin(final Headers headers) {
95         final String sourceSystem = KafkaHeaders.getParsedKafkaHeader(headers, CLOUD_EVENT_SOURCE_SYSTEM_HEADER_KEY);
96         return "ONAP-DMI-PLUGIN".equals(sourceSystem);
97     }
98 }