Merge "Cm Subscription: Predicates optional now"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / producer / CmNotificationSubscriptionDmiInEventProducer.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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.cmsubscription.producer;
22
23 import io.cloudevents.CloudEvent;
24 import io.cloudevents.core.builder.CloudEventBuilder;
25 import java.net.URI;
26 import java.util.UUID;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.events.EventsPublisher;
29 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.CmNotificationSubscriptionDmiInEvent;
30 import org.onap.cps.utils.JsonObjectMapper;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 @RequiredArgsConstructor
37 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
38 public class CmNotificationSubscriptionDmiInEventProducer {
39
40     private final EventsPublisher<CloudEvent> eventsPublisher;
41     private final JsonObjectMapper jsonObjectMapper;
42
43     @Value("${app.ncmp.avc.cm-subscription-dmi-in}")
44     private String cmNotificationSubscriptionDmiInEventTopic;
45
46     /**
47      * Publish the event to the provided dmi plugin with key as subscription id and the event is in Cloud Event format.
48      *
49      * @param subscriptionId                       Cm Subscription Id
50      * @param dmiPluginName                        Dmi Plugin Name
51      * @param eventType                            Type of event
52      * @param cmNotificationSubscriptionDmiInEvent Cm Notification Subscription event for Dmi
53      */
54     public void publishCmNotificationSubscriptionDmiInEvent(final String subscriptionId, final String dmiPluginName,
55             final String eventType, final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent) {
56         eventsPublisher.publishCloudEvent(cmNotificationSubscriptionDmiInEventTopic, subscriptionId,
57                 buildAndGetCmNotificationDmiInEventAsCloudEvent(subscriptionId, dmiPluginName, eventType,
58                         cmNotificationSubscriptionDmiInEvent));
59
60     }
61
62     private CloudEvent buildAndGetCmNotificationDmiInEventAsCloudEvent(final String subscriptionId,
63             final String dmiPluginName, final String eventType,
64             final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent) {
65         return CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withType(eventType)
66                        .withSource(URI.create("NCMP"))
67                        .withDataSchema(URI.create("org.onap.ncmp.dmi.cm.subscription:1.0.0"))
68                        .withExtension("correlationid", subscriptionId.concat("#").concat(dmiPluginName))
69                        .withData(jsonObjectMapper.asJsonBytes(cmNotificationSubscriptionDmiInEvent)).build();
70     }
71
72
73 }