From a542f19d421229508c4408583e552c4c0eba84f1 Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Tue, 9 Apr 2024 09:39:16 +0200 Subject: Listing distributions (/services/{distributionId}/distribution/) fails with ClassCastException - attempt to format AuditEvent timestamp when it's of long type - do not update gem package in docker builds since newer versions are incompatible with ruby 2.7.6 Issue-ID: SDC-4682 Signed-off-by: Fiete Ostkamp Change-Id: I6ff3ac6bfa3ce7985c69df3eb79f9711b753800f --- .../impl/DistributionMonitoringBusinessLogic.java | 42 +++++++++++++++------- .../be/info/DistributionStatusListResponse.java | 10 +++--- .../DistributionStatusOfServiceListResponce.java | 11 +++--- 3 files changed, 42 insertions(+), 21 deletions(-) (limited to 'catalog-be/src/main/java/org/openecomp') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DistributionMonitoringBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DistributionMonitoringBusinessLogic.java index f3334e6177..77bd9f3396 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DistributionMonitoringBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DistributionMonitoringBusinessLogic.java @@ -20,12 +20,16 @@ package org.openecomp.sdc.be.components.impl; import fj.data.Either; +import java.time.Instant; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.apache.http.HttpStatus; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao; @@ -44,6 +48,7 @@ import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent; import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent; import org.openecomp.sdc.common.datastructure.AuditingFieldsKey; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; @@ -52,11 +57,11 @@ import org.springframework.stereotype.Component; @Component("distributionMonitoringBusinessLogic") public class DistributionMonitoringBusinessLogic extends BaseBusinessLogic { + private static final Logger log = Logger.getLogger(ArtifactsBusinessLogic.class.getName()); private static final String DEPLOYED = "Deployed"; private static final String ERROR = "Error"; private static final String DISTRIBUTED = "Distributed"; private static final String IN_PROGRESS = "In Progress"; - private static final Logger log = Logger.getLogger(ArtifactsBusinessLogic.class.getName()); private final AuditCassandraDao cassandraDao; @Autowired @@ -77,13 +82,11 @@ public class DistributionMonitoringBusinessLogic extends BaseBusinessLogic { log.debug("not found distribution statuses for did {} status is {} ", did, distributionStatus.right().value()); return Either.right(componentsUtils.getResponseFormat(distributionStatus.right().value(), did)); } - List distribStatusInfoList = new ArrayList<>(); - List distributionStatusEventList = distributionStatus.left().value(); - if (distributionStatusEventList != null) { - for (AuditingGenericEvent distributionStatusEvent : distributionStatusEventList) { - distribStatusInfoList.add(new DistributionStatusInfo(distributionStatusEvent)); - } - } + List distribStatusInfoList = distributionStatus.left().value().stream() + .filter(Objects::nonNull) + .map(DistributionStatusInfo::new) + .collect(Collectors.toList()); + DistributionStatusListResponse distributionStatusListResponse = new DistributionStatusListResponse(); distributionStatusListResponse.setDistributionStatusList(distribStatusInfoList); log.trace("list statuses for did {} is {} ", did, distribStatusInfoList); @@ -98,11 +101,9 @@ public class DistributionMonitoringBusinessLogic extends BaseBusinessLogic { log.debug("failed to find service distribution statuses. error: {}", status); return Either.right(componentsUtils.getResponseFormat(status.right().value(), serviceUuid)); } - List distribStatusInfoList; List distributionStatusEventList = status.left().value(); - distribStatusInfoList = handleAuditingDaoResponse(distributionStatusEventList); - DistributionStatusOfServiceListResponce distributionStatusListResponse = new DistributionStatusOfServiceListResponce(); - distributionStatusListResponse.setDistributionStatusOfServiceList(distribStatusInfoList); + List distributionStatusInfoList = handleAuditingDaoResponse(distributionStatusEventList); + DistributionStatusOfServiceListResponce distributionStatusListResponse = new DistributionStatusOfServiceListResponce(distributionStatusInfoList); return Either.left(distributionStatusListResponse); } @@ -140,7 +141,7 @@ public class DistributionMonitoringBusinessLogic extends BaseBusinessLogic { Map fields = resAuditingGenericEvent.getFields(); if (fields != null) { Optional.ofNullable(fields.get(AuditingFieldsKey.AUDIT_TIMESTAMP.getDisplayName())) - .ifPresent(timestamp -> distributionStatusOfServiceInfo.setTimestamp((String) timestamp)); + .ifPresent(timestamp -> distributionStatusOfServiceInfo.setTimestamp(formatTimestamp(timestamp))); } } if (!isResult) { @@ -165,6 +166,21 @@ public class DistributionMonitoringBusinessLogic extends BaseBusinessLogic { return reslist; } + private String formatTimestamp(Object timestamp) { + if(timestamp instanceof String) { + return (String) timestamp; + } else if (timestamp instanceof Long) { + try { + return Instant.ofEpochMilli((Long) timestamp).atOffset(ZoneOffset.UTC).toString(); + } catch (Exception e) { + log.warn(EcompLoggerErrorCode.DATA_ERROR, "sdc-be", "Failed to format timestamp: {}. Returning without formatting", timestamp, e); + return String.valueOf(timestamp); + } + } else { + return String.valueOf(timestamp); + } + } + private String getStatusFromAuditEvent(AuditingGenericEvent auditingGenericEvent) { String status = ""; Object requestStatus = auditingGenericEvent.getFields().get(AuditingFieldsKey.AUDIT_STATUS.getDisplayName()); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusListResponse.java b/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusListResponse.java index acd0c47e38..6f716cbeae 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusListResponse.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusListResponse.java @@ -20,11 +20,13 @@ package org.openecomp.sdc.be.info; import java.util.List; -import lombok.Getter; -import lombok.Setter; -@Getter -@Setter +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor public class DistributionStatusListResponse { private List distributionStatusList; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusOfServiceListResponce.java b/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusOfServiceListResponce.java index b5e97585f0..81b56819e0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusOfServiceListResponce.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/info/DistributionStatusOfServiceListResponce.java @@ -20,11 +20,14 @@ package org.openecomp.sdc.be.info; import java.util.List; -import lombok.Getter; -import lombok.Setter; -@Getter -@Setter +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.AllArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor public class DistributionStatusOfServiceListResponce { private List distributionStatusOfServiceList; -- cgit 1.2.3-korg