Process data-updated event asynchronously
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / NotificationPublisher.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (c) 2021 Bell Canada.
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.notification;
22
23 import lombok.extern.slf4j.Slf4j;
24 import org.checkerframework.checker.nullness.qual.NonNull;
25 import org.onap.cps.event.model.CpsDataUpdatedEvent;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.kafka.core.KafkaTemplate;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 @Slf4j
33 public class NotificationPublisher {
34
35     private KafkaTemplate<String, CpsDataUpdatedEvent> kafkaTemplate;
36     private String topicName;
37
38     /**
39      * Create an instance of Notification Publisher.
40      *
41      * @param kafkaTemplate kafkaTemplate is send event using kafka
42      * @param topicName     topic, to which cpsDataUpdatedEvent is sent, is provided by setting
43      *                      'notification.data-updated.topic' in the application properties
44      */
45     @Autowired
46     public NotificationPublisher(
47         final KafkaTemplate<String, CpsDataUpdatedEvent> kafkaTemplate,
48         final @Value("${notification.data-updated.topic}") String topicName) {
49         this.kafkaTemplate = kafkaTemplate;
50         this.topicName = topicName;
51     }
52
53     /**
54      * Send event to Kafka with correct message key.
55      *
56      * @param cpsDataUpdatedEvent event to be sent to kafka
57      */
58     public void sendNotification(@NonNull final CpsDataUpdatedEvent cpsDataUpdatedEvent) {
59         final var messageKey = cpsDataUpdatedEvent.getContent().getDataspaceName() + ","
60             + cpsDataUpdatedEvent.getContent().getAnchorName();
61         log.debug("Data Updated event is being sent with messageKey: '{}' & body : {} ",
62             messageKey, cpsDataUpdatedEvent);
63         kafkaTemplate.send(topicName, messageKey, cpsDataUpdatedEvent);
64     }
65
66 }