Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / InProgressStatusService.java
1 package org.onap.vid.job.command;
2
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;
13
14 import java.time.ZonedDateTime;
15 import java.time.format.DateTimeParseException;
16 import java.util.UUID;
17
18 import static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
19
20 @Service
21 public class InProgressStatusService {
22
23     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
24
25     private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
26
27     private final RestMsoImplementation restMso;
28
29     private final AuditService auditService;
30
31     @Autowired
32     public InProgressStatusService(AsyncInstantiationBusinessLogic asyncInstantiationBL, RestMsoImplementation restMso, AuditService auditService) {
33         this.asyncInstantiationBL = asyncInstantiationBL;
34         this.restMso = restMso;
35         this.auditService = auditService;
36     }
37
38
39     public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
40
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;
46         return jobStatus;
47     }
48
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);
54         }
55         return msoResponse;
56     }
57
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());
63     }
64
65     public static class BadResponseFromMso extends RuntimeException {
66         private final RestObject<AsyncRequestStatus> msoResponse;
67
68         public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
69             this.msoResponse = msoResponse;
70         }
71
72         public RestObject<AsyncRequestStatus> getMsoResponse() {
73             return msoResponse;
74         }
75     }
76
77     private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
78         ZonedDateTime jobStartTime;
79         try {
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();
84         }
85         return jobStartTime;
86     }
87 }