Subscription Creation: NCMP to Client CloudEvent transformation
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / SubscriptionEventCloudMapper.java
index a7de479..df3998f 100644 (file)
@@ -27,10 +27,12 @@ import io.cloudevents.core.builder.CloudEventBuilder;
 import io.cloudevents.core.data.PojoCloudEventData;
 import io.cloudevents.jackson.PojoCloudEventDataMapper;
 import java.net.URI;
+import java.util.UUID;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent;
+import org.onap.cps.spi.exceptions.CloudEventConstructionException;
 
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 @Slf4j
@@ -38,6 +40,8 @@ public class SubscriptionEventCloudMapper {
 
     private static final ObjectMapper objectMapper = new ObjectMapper();
 
+    private static String randomId = UUID.randomUUID().toString();
+
     /**
      * Maps CloudEvent object to SubscriptionEvent.
      *
@@ -62,18 +66,24 @@ public class SubscriptionEventCloudMapper {
      *
      * @param ncmpSubscriptionEvent object.
      * @param eventKey as String.
-     * @return CloudEvent builded.
+     * @return CloudEvent built.
      */
     public static CloudEvent toCloudEvent(
             final org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent ncmpSubscriptionEvent,
-            final String eventKey) {
+            final String eventKey, final String eventType) {
         try {
             return CloudEventBuilder.v1()
-                    .withData(objectMapper.writeValueAsBytes(ncmpSubscriptionEvent))
-                    .withId(eventKey).withType("CREATE").withSource(
-                            URI.create(ncmpSubscriptionEvent.getData().getSubscription().getClientID())).build();
+                    .withId(randomId)
+                    .withSource(URI.create(ncmpSubscriptionEvent.getData().getSubscription().getClientID()))
+                    .withType(eventType)
+                    .withExtension("correlationid", eventKey)
+                    .withDataSchema(URI.create("urn:cps:"
+                            + org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi
+                                    .SubscriptionEvent.class.getName() + ":1.0.0"))
+                    .withData(objectMapper.writeValueAsBytes(ncmpSubscriptionEvent)).build();
         } catch (final Exception ex) {
-            throw new RuntimeException("The Cloud Event could not be constructed.", ex);
+            throw new CloudEventConstructionException("The Cloud Event could not be constructed", "Invalid object to "
+                    + "serialize or required headers is missing", ex);
         }
     }
 }