Merge "Use constants for magic numbers in perf tests"
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / CpsDataUpdatedEventFactory.java
index 1013c13..696fd60 100644 (file)
@@ -1,6 +1,8 @@
 /*
  * ============LICENSE_START=======================================================
  * Copyright (c) 2021-2022 Bell Canada.
+ * Modifications Copyright (c) 2022-2023 Nordix Foundation
+ * Modifications Copyright (C) 2023 TechMahindra Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,6 +28,7 @@ import java.time.OffsetDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.UUID;
 import lombok.AllArgsConstructor;
+import lombok.SneakyThrows;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.event.model.Content;
 import org.onap.cps.event.model.CpsDataUpdatedEvent;
@@ -34,6 +37,7 @@ import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.Anchor;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.utils.DataMapUtils;
+import org.onap.cps.utils.PrefixResolver;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Component;
 
@@ -41,25 +45,15 @@ import org.springframework.stereotype.Component;
 @AllArgsConstructor(onConstructor = @__(@Lazy))
 public class CpsDataUpdatedEventFactory {
 
-    private static final URI EVENT_SCHEMA;
-    private static final URI EVENT_SOURCE;
-    private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
     private static final DateTimeFormatter DATE_TIME_FORMATTER =
         DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
 
-    static {
-        try {
-            EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1");
-            EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
-        } catch (final URISyntaxException e) {
-            // As it is fixed string, I don't expect to see this error
-            throw new IllegalArgumentException(e);
-        }
-    }
-
     @Lazy
     private final CpsDataService cpsDataService;
 
+    @Lazy
+    private final PrefixResolver prefixResolver;
+
     /**
      * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
      *
@@ -71,25 +65,28 @@ public class CpsDataUpdatedEventFactory {
     public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor,
         final OffsetDateTime observedTimestamp, final Operation operation) {
         final var dataNode = (operation == Operation.DELETE) ? null :
-            cpsDataService.getDataNode(anchor.getDataspaceName(), anchor.getName(),
-                "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
+            cpsDataService.getDataNodes(anchor.getDataspaceName(), anchor.getName(),
+                "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS).iterator().next();
         return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation);
     }
 
-    private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
-        final OffsetDateTime observedTimestamp, final Operation operation) {
-        final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
+    @SneakyThrows(URISyntaxException.class)
+    private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor,
+                                                      final DataNode dataNode,
+                                                      final OffsetDateTime observedTimestamp,
+                                                      final Operation operation) {
+        final CpsDataUpdatedEvent cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
         cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation));
         cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
-        cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
-        cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
-        cpsDataUpdatedEvent.withType(EVENT_TYPE);
+        cpsDataUpdatedEvent.withSchema(new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1"));
+        cpsDataUpdatedEvent.withSource(new URI("urn:cps:org.onap.cps"));
+        cpsDataUpdatedEvent.withType("org.onap.cps.data-updated-event");
         return cpsDataUpdatedEvent;
     }
 
-    private Data createData(final DataNode dataNode) {
-        final var data = new Data();
-        DataMapUtils.toDataMapWithIdentifier(dataNode).forEach(data::setAdditionalProperty);
+    private Data createData(final DataNode dataNode, final String prefix) {
+        final Data data = new Data();
+        DataMapUtils.toDataMapWithIdentifier(dataNode, prefix).forEach(data::setAdditionalProperty);
         return data;
     }
 
@@ -103,7 +100,8 @@ public class CpsDataUpdatedEventFactory {
         content.withObservedTimestamp(
             DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
         if (dataNode != null) {
-            content.withData(createData(dataNode));
+            final String prefix = prefixResolver.getPrefix(anchor, dataNode.getXpath());
+            content.withData(createData(dataNode, prefix));
         }
         return content;
     }