1 package org.onap.vid.job.command;
3 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
4 import org.onap.vid.job.Job;
5 import org.onap.vid.job.impl.JobSharedData;
6 import org.onap.vid.mso.RestMsoImplementation;
7 import org.onap.vid.mso.RestObject;
8 import org.onap.vid.mso.rest.AsyncRequestStatus;
9 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
10 import org.onap.vid.services.AuditService;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Service;
14 import java.time.ZonedDateTime;
15 import java.time.format.DateTimeParseException;
16 import java.util.UUID;
18 import static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
21 public class InProgressStatusService {
23 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
25 private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
27 private final RestMsoImplementation restMso;
29 private final AuditService auditService;
32 public InProgressStatusService(AsyncInstantiationBusinessLogic asyncInstantiationBL, RestMsoImplementation restMso, AuditService auditService) {
33 this.asyncInstantiationBL = asyncInstantiationBL;
34 this.restMso = restMso;
35 this.auditService = auditService;
39 public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
41 RestObject<AsyncRequestStatus> asyncRequestStatus = getAsyncRequestStatus(requestId);
42 asyncInstantiationBL.auditMsoStatus(sharedData.getRootJobId(), asyncRequestStatus.get().request);
43 Job.JobStatus jobStatus = asyncInstantiationBL.calcStatus(asyncRequestStatus.get());
44 ZonedDateTime jobStartTime = getZonedDateTime(asyncRequestStatus, requestId);
45 jobStatus = expiryChecker.isExpired(jobStartTime) ? Job.JobStatus.FAILED : jobStatus;
49 private RestObject<AsyncRequestStatus> getAsyncRequestStatus(String requestId) {
50 String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
51 RestObject<AsyncRequestStatus> msoResponse = restMso.GetForObject(path, AsyncRequestStatus.class);
52 if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
53 throw new BadResponseFromMso(msoResponse);
58 public void handleFailedMsoResponse(UUID jobUUID, String requestId, RestObject<AsyncRequestStatus> msoResponse) {
59 auditService.setFailedAuditStatusFromMso(jobUUID, requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
60 LOGGER.error(EELFLoggerDelegate.errorLogger,
61 "Failed to get orchestration status for {}. Status code: {}, Body: {}",
62 requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
65 public static class BadResponseFromMso extends RuntimeException {
66 private final RestObject<AsyncRequestStatus> msoResponse;
68 public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
69 this.msoResponse = msoResponse;
72 public RestObject<AsyncRequestStatus> getMsoResponse() {
77 private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
78 ZonedDateTime jobStartTime;
80 jobStartTime = parseZonedDateTime(asyncRequestStatusResponse.get().request.startTime);
81 } catch (DateTimeParseException | NullPointerException e) {
82 LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to parse start time for {}, body: {}. Current time will be used", requestId, asyncRequestStatusResponse.getRaw(), e);
83 jobStartTime = ZonedDateTime.now();