Merge "Query CM Handles RTD"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / event / lcm / LcmEventsCreator.java
index 783b299..aef1ed9 100644 (file)
 
 package org.onap.cps.ncmp.api.impl.event.lcm;
 
-import java.time.ZonedDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.List;
 import java.util.UUID;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
 import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter;
 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
-import org.onap.ncmp.cmhandle.lcm.event.Event;
-import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent;
+import org.onap.ncmp.cmhandle.event.lcm.Event;
+import org.onap.ncmp.cmhandle.event.lcm.LcmEvent;
+import org.onap.ncmp.cmhandle.event.lcm.Values;
 import org.springframework.stereotype.Component;
 
 
@@ -38,42 +40,61 @@ import org.springframework.stereotype.Component;
 @Component
 public class LcmEventsCreator {
 
-
     /**
-     * Populate NcmpEvent.
+     * Populate Lifecycle Management Event.
      *
-     * @param cmHandleId          Cm Handle Identifier
-     * @param ncmpServiceCmHandle Ncmp CmHandle Data
-     * @return Populated NcmpEvent
+     * @param cmHandleId                  cm handle identifier
+     * @param targetNcmpServiceCmHandle   target ncmp service cmhandle
+     * @param existingNcmpServiceCmHandle existing ncmp service cmhandle
+     * @return Populated LcmEvent
      */
-    public NcmpEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
-        return createLcmEvent(cmHandleId, ncmpServiceCmHandle);
+    public LcmEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
+            final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
+        return createLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
     }
 
-    private NcmpEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
-        final NcmpEvent ncmpEvent = lcmEventHeader(cmHandleId);
-        ncmpEvent.setEvent(lcmEventPayload(cmHandleId, ncmpServiceCmHandle));
-        return ncmpEvent;
+    private LcmEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
+            final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
+        final LcmEventType lcmEventType =
+                LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
+        final LcmEvent lcmEvent = lcmEventHeader(cmHandleId, lcmEventType);
+        lcmEvent.setEvent(
+                lcmEventPayload(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, lcmEventType));
+        return lcmEvent;
     }
 
-    private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
+    private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
+            final NcmpServiceCmHandle existingNcmpServiceCmHandle, final LcmEventType lcmEventType) {
         final Event event = new Event();
         event.setCmHandleId(eventCorrelationId);
-        event.setCmhandleState(
-                Event.CmhandleState.fromValue(ncmpServiceCmHandle.getCompositeState().getCmHandleState().toString()));
-        event.setCmhandleProperties(List.of(ncmpServiceCmHandle.getPublicProperties()));
+        final CmHandleValuesHolder cmHandleValuesHolder =
+                LcmEventsCreatorHelper.determineEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle,
+                        lcmEventType);
+        event.setOldValues(cmHandleValuesHolder.getOldValues());
+        event.setNewValues(cmHandleValuesHolder.getNewValues());
+
         return event;
     }
 
-    private NcmpEvent lcmEventHeader(final String eventCorrelationId) {
-        final NcmpEvent ncmpEvent = new NcmpEvent();
-        ncmpEvent.setEventId(UUID.randomUUID().toString());
-        ncmpEvent.setEventCorrelationId(eventCorrelationId);
-        ncmpEvent.setEventTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
-        ncmpEvent.setEventSource("org.onap.ncmp");
-        ncmpEvent.setEventType("org.onap.ncmp.cmhandle-lcm-event");
-        ncmpEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event:v1");
-        return ncmpEvent;
+    private LcmEvent lcmEventHeader(final String eventCorrelationId, final LcmEventType lcmEventType) {
+        final LcmEvent lcmEvent = new LcmEvent();
+        lcmEvent.setEventId(UUID.randomUUID().toString());
+        lcmEvent.setEventCorrelationId(eventCorrelationId);
+        lcmEvent.setEventTime(EventDateTimeFormatter.getCurrentDateTime());
+        lcmEvent.setEventSource("org.onap.ncmp");
+        lcmEvent.setEventType(lcmEventType.getEventType());
+        lcmEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event");
+        lcmEvent.setEventSchemaVersion("1.0");
+        return lcmEvent;
+    }
+
+    @NoArgsConstructor
+    @Getter
+    @Setter
+    static class CmHandleValuesHolder {
+
+        private Values oldValues;
+        private Values newValues;
     }
 
-}
+}
\ No newline at end of file