f504207e3f2c210c5881e916e36cd935c02772ee
[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.cmnotificationsubscription.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
40 /**
41  * Listener for AVC events based on CM Subscriptions.
42  */
43 @Component
44 @Slf4j
45 @RequiredArgsConstructor
46 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
47 public class CmAvcEventConsumer {
48
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<CloudEvent> 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     @KafkaListener(topics = "${app.dmi.cm-events.topic}",
68             containerFactory = "cloudEventConcurrentKafkaListenerContainerFactory")
69     @Timed(value = "cps.ncmp.cmnotifications.consumeandforward", description = "Time taken to forward CM AVC events")
70     public void consumeAndForward(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
71         if (isEventFromOnapDmiPlugin(cmAvcEventAsConsumerRecord.headers())) {
72             processCmAvcEventChanges(cmAvcEventAsConsumerRecord);
73         }
74         final CloudEvent outgoingAvcEvent = cmAvcEventAsConsumerRecord.value();
75         final String outgoingAvcEventKey = cmAvcEventAsConsumerRecord.key();
76         log.debug("Consuming AVC event with key : {} and value : {}", outgoingAvcEventKey, outgoingAvcEvent);
77         eventsProducer.sendCloudEvent(cmEventsTopicName, outgoingAvcEventKey, outgoingAvcEvent);
78     }
79
80     private void processCmAvcEventChanges(final ConsumerRecord<String, CloudEvent> cmAvcEventAsConsumerRecord) {
81         final String cmHandleId = cmAvcEventAsConsumerRecord.key();
82         final Boolean dataSyncEnabled = inventoryPersistence.getCmHandleState(cmHandleId).getDataSyncEnabled();
83         if (Boolean.TRUE.equals(dataSyncEnabled)) {
84             final AvcEvent cmAvcEvent = toTargetEvent(cmAvcEventAsConsumerRecord.value(), AvcEvent.class);
85             log.debug("Event to be processed to update the cache with cmHandleId : {}", cmHandleId);
86             cmAvcEventService.processCmAvcEvent(cmHandleId, cmAvcEvent);
87         }
88     }
89
90     private boolean isEventFromOnapDmiPlugin(final Headers headers) {
91         final String sourceSystem = KafkaHeaders.getParsedKafkaHeader(headers, CLOUD_EVENT_SOURCE_SYSTEM_HEADER_KEY);
92         return sourceSystem != null && sourceSystem.equals("ONAP-DMI-PLUGIN");
93     }
94 }