Fix test-deregistration script
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / avc / AvcEventConsumer.java
1 /*
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
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.api.impl.events.avc;
22
23 import java.util.UUID;
24 import lombok.RequiredArgsConstructor;
25 import lombok.extern.slf4j.Slf4j;
26 import org.apache.kafka.clients.consumer.ConsumerRecord;
27 import org.apache.kafka.common.header.Headers;
28 import org.apache.kafka.common.header.internals.RecordHeader;
29 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
30 import org.onap.cps.ncmp.events.avc.v1.AvcEvent;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
33 import org.springframework.kafka.annotation.KafkaListener;
34 import org.springframework.stereotype.Component;
35 import org.springframework.util.SerializationUtils;
36
37 /**
38  * Listener for AVC events.
39  */
40 @Component
41 @Slf4j
42 @RequiredArgsConstructor
43 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
44 public class AvcEventConsumer {
45
46
47     @Value("${app.ncmp.avc.cm-events-topic}")
48     private String cmEventsTopicName;
49
50     private final EventsPublisher<AvcEvent> eventsPublisher;
51     private final AvcEventMapper avcEventMapper;
52
53
54     /**
55      * Incoming AvcEvent in the form of Consumer Record.
56      *
57      * @param avcEventConsumerRecord Incoming raw consumer record
58      */
59     @KafkaListener(topics = "${app.dmi.cm-events.topic}",
60             properties = {"spring.json.value.default.type=org.onap.cps.ncmp.events.avc.v1.AvcEvent"})
61     public void consumeAndForward(final ConsumerRecord<String, AvcEvent> avcEventConsumerRecord) {
62         log.debug("Consuming AVC event {} ...", avcEventConsumerRecord.value());
63         final String mutatedEventId = UUID.randomUUID().toString();
64         mutateEventHeaderWithEventId(avcEventConsumerRecord.headers(), mutatedEventId);
65         final AvcEvent outgoingAvcEvent = avcEventMapper.toOutgoingAvcEvent(avcEventConsumerRecord.value());
66         eventsPublisher.publishEvent(cmEventsTopicName, mutatedEventId, avcEventConsumerRecord.headers(),
67                 outgoingAvcEvent);
68     }
69
70     private void mutateEventHeaderWithEventId(final Headers eventHeaders, final String mutatedEventId) {
71         final String eventId = "eventId";
72         final String existingEventId =
73                 (String) SerializationUtils.deserialize(eventHeaders.lastHeader(eventId).value());
74         eventHeaders.remove(eventId);
75         log.info("Removing existing eventId from header : {} and updating with id : {}", existingEventId,
76                 mutatedEventId);
77         eventHeaders.add(new RecordHeader(eventId, SerializationUtils.serialize(mutatedEventId)));
78
79     }
80 }