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
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.ncmp.api.impl.events.cmsubscription.producer;
23 import io.cloudevents.CloudEvent;
24 import io.cloudevents.core.builder.CloudEventBuilder;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.TimeUnit;
33 import lombok.RequiredArgsConstructor;
34 import lombok.extern.slf4j.Slf4j;
35 import org.onap.cps.events.EventsPublisher;
36 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionMappersHandler;
37 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionNcmpOutEventPublishingTask;
38 import org.onap.cps.ncmp.api.impl.events.cmsubscription.DmiCmNotificationSubscriptionCacheHandler;
39 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
40 import org.onap.cps.utils.JsonObjectMapper;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
43 import org.springframework.stereotype.Component;
47 @RequiredArgsConstructor
48 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
49 public class CmNotificationSubscriptionNcmpOutEventProducer {
51 @Value("${app.ncmp.avc.cm-subscription-ncmp-out}")
52 private String cmNotificationSubscriptionNcmpOutEventTopic;
54 @Value("${ncmp.timers.subscription-forwarding.dmi-response-timeout-ms}")
55 private Integer cmNotificationSubscriptionDmiOutEventTimeoutInMs;
57 private final EventsPublisher<CloudEvent> eventsPublisher;
58 private final JsonObjectMapper jsonObjectMapper;
59 private final CmNotificationSubscriptionMappersHandler cmNotificationSubscriptionMappersHandler;
60 private final DmiCmNotificationSubscriptionCacheHandler dmiCmNotificationSubscriptionCacheHandler;
61 private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
62 private static final Map<String, ScheduledFuture<?>> scheduledTasksPerSubscriptionId = new ConcurrentHashMap<>();
65 * Publish the event to the client who requested the subscription with key as subscription id and event is Cloud
68 * @param subscriptionId Cm Subscription Id
69 * @param eventType Type of event
70 * @param cmNotificationSubscriptionNcmpOutEvent Cm Notification Subscription Event for the
72 * @param isScheduledEvent Determines if the event is to be scheduled
75 public void publishCmNotificationSubscriptionNcmpOutEvent(final String subscriptionId, final String eventType,
76 final CmNotificationSubscriptionNcmpOutEvent
77 cmNotificationSubscriptionNcmpOutEvent,
78 final boolean isScheduledEvent) {
80 if (isScheduledEvent && !scheduledTasksPerSubscriptionId.containsKey(subscriptionId)) {
81 final ScheduledFuture<?> scheduledFuture =
82 scheduleAndPublishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType);
83 scheduledTasksPerSubscriptionId.putIfAbsent(subscriptionId, scheduledFuture);
84 log.debug("Scheduled the CmNotificationSubscriptionEvent for subscriptionId : {}", subscriptionId);
86 cancelScheduledTaskForSubscriptionId(subscriptionId);
87 publishCmNotificationSubscriptionNcmpOutEventNow(subscriptionId, eventType,
88 cmNotificationSubscriptionNcmpOutEvent);
89 log.info("Published CmNotificationSubscriptionEvent on demand for subscriptionId : {}", subscriptionId);
93 private ScheduledFuture<?> scheduleAndPublishCmNotificationSubscriptionNcmpOutEvent(final String subscriptionId,
94 final String eventType) {
95 final CmNotificationSubscriptionNcmpOutEventPublishingTask
96 cmNotificationSubscriptionNcmpOutEventPublishingTask =
97 new CmNotificationSubscriptionNcmpOutEventPublishingTask(cmNotificationSubscriptionNcmpOutEventTopic,
98 subscriptionId, eventType, eventsPublisher, jsonObjectMapper,
99 cmNotificationSubscriptionMappersHandler, dmiCmNotificationSubscriptionCacheHandler);
100 return scheduledExecutorService.schedule(cmNotificationSubscriptionNcmpOutEventPublishingTask,
101 cmNotificationSubscriptionDmiOutEventTimeoutInMs, TimeUnit.MILLISECONDS);
104 private void cancelScheduledTaskForSubscriptionId(final String subscriptionId) {
106 final ScheduledFuture<?> scheduledFuture = scheduledTasksPerSubscriptionId.get(subscriptionId);
107 if (scheduledFuture != null) {
108 scheduledFuture.cancel(true);
109 scheduledTasksPerSubscriptionId.remove(subscriptionId);
115 private void publishCmNotificationSubscriptionNcmpOutEventNow(final String subscriptionId, final String eventType,
116 final CmNotificationSubscriptionNcmpOutEvent
117 cmNotificationSubscriptionNcmpOutEvent) {
118 final CloudEvent cmNotificationSubscriptionNcmpOutEventAsCloudEvent =
119 buildAndGetCmNotificationNcmpOutEventAsCloudEvent(jsonObjectMapper, subscriptionId, eventType,
120 cmNotificationSubscriptionNcmpOutEvent);
121 eventsPublisher.publishCloudEvent(cmNotificationSubscriptionNcmpOutEventTopic, subscriptionId,
122 cmNotificationSubscriptionNcmpOutEventAsCloudEvent);
123 dmiCmNotificationSubscriptionCacheHandler
124 .removeAcceptedAndRejectedDmiCmNotificationSubscriptionEntries(subscriptionId);
128 * Get an NCMP out event as cloud event.
130 * @param jsonObjectMapper JSON object mapper
131 * @param subscriptionId subscription id
132 * @param eventType event type
133 * @param cmNotificationSubscriptionNcmpOutEvent cm notification subscription NCMP out event
134 * @return cm notification subscription NCMP out event as cloud event
136 public static CloudEvent buildAndGetCmNotificationNcmpOutEventAsCloudEvent(
137 final JsonObjectMapper jsonObjectMapper, final String subscriptionId, final String eventType,
138 final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent) {
140 return CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withType(eventType)
141 .withSource(URI.create("NCMP")).withDataSchema(URI.create("org.onap.ncmp.cm.subscription:1.0.0"))
142 .withExtension("correlationid", subscriptionId)
143 .withData(jsonObjectMapper.asJsonBytes(cmNotificationSubscriptionNcmpOutEvent)).build();