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;
 
  23 import io.cloudevents.CloudEvent;
 
  24 import io.cloudevents.core.builder.CloudEventBuilder;
 
  26 import java.util.UUID;
 
  27 import lombok.RequiredArgsConstructor;
 
  28 import lombok.extern.slf4j.Slf4j;
 
  29 import org.onap.cps.events.EventsPublisher;
 
  30 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
 
  31 import org.onap.cps.utils.JsonObjectMapper;
 
  32 import org.springframework.beans.factory.annotation.Value;
 
  33 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 
  34 import org.springframework.stereotype.Component;
 
  38 @RequiredArgsConstructor
 
  39 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
 
  40 public class CmNotificationSubscriptionNcmpOutEventProducer {
 
  42     private final EventsPublisher<CloudEvent> eventsPublisher;
 
  43     private final JsonObjectMapper jsonObjectMapper;
 
  45     @Value("${app.ncmp.avc.subscription-outcome-topic}")
 
  46     private String cmNotificationSubscriptionNcmpOutEventTopic;
 
  49      * Publish the event to the client who requested the subscription with key as subscription id and event is Cloud
 
  52      * @param subscriptionId                         Cm Subscription Id
 
  53      * @param eventType                              Type of event
 
  54      * @param cmNotificationSubscriptionNcmpOutEvent Cm Notification Subscription Event for the client
 
  56     public void publishCmNotificationSubscriptionNcmpOutEvent(final String subscriptionId, final String eventType,
 
  57             final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent) {
 
  59         eventsPublisher.publishCloudEvent(cmNotificationSubscriptionNcmpOutEventTopic, subscriptionId,
 
  60                 buildAndGetCmNotificationNcmpOutEventAsCloudEvent(subscriptionId, eventType,
 
  61                         cmNotificationSubscriptionNcmpOutEvent));
 
  65     private CloudEvent buildAndGetCmNotificationNcmpOutEventAsCloudEvent(final String subscriptionId,
 
  66             final String eventType,
 
  67             final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent) {
 
  69         return CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withType(eventType)
 
  70                 .withSource(URI.create("NCMP")).withDataSchema(URI.create("org.onap.ncmp.cm.subscription:1.0.0"))
 
  71                 .withExtension("correlationid", subscriptionId)
 
  72                 .withData(jsonObjectMapper.asJsonBytes(cmNotificationSubscriptionNcmpOutEvent)).build();