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
private static final ObjectMapper objectMapper = new ObjectMapper();
+ private static String randomId = UUID.randomUUID().toString();
+
/**
* Maps CloudEvent object to SubscriptionEvent.
*
*
* @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);
}
}
}