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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.notification;
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;
33 public class NotificationPublisher {
35 private KafkaTemplate<String, CpsDataUpdatedEvent> kafkaTemplate;
36 private String topicName;
39 * Create an instance of Notification Publisher.
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
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;
54 * Send event to Kafka with correct message key.
56 * @param cpsDataUpdatedEvent event to be sent to kafka
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);