842a1bd1775a49a32bc905e9712fbd101e769376
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / InProgressStatusService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.job.command;
22
23 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
24 import org.onap.vid.job.Job;
25 import org.onap.vid.job.impl.JobSharedData;
26 import org.onap.vid.mso.RestMsoImplementation;
27 import org.onap.vid.mso.RestObject;
28 import org.onap.vid.mso.rest.AsyncRequestStatus;
29 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
30 import org.onap.vid.services.AuditService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 import java.time.ZonedDateTime;
35 import java.time.format.DateTimeParseException;
36 import java.util.UUID;
37
38 import static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
39
40 @Service
41 public class InProgressStatusService {
42
43     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
44
45     private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
46
47     private final RestMsoImplementation restMso;
48
49     private final AuditService auditService;
50
51     @Autowired
52     public InProgressStatusService(AsyncInstantiationBusinessLogic asyncInstantiationBL, RestMsoImplementation restMso, AuditService auditService) {
53         this.asyncInstantiationBL = asyncInstantiationBL;
54         this.restMso = restMso;
55         this.auditService = auditService;
56     }
57
58
59     public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
60
61         RestObject<AsyncRequestStatus> asyncRequestStatus = getAsyncRequestStatus(requestId);
62         asyncInstantiationBL.auditMsoStatus(sharedData.getRootJobId(), asyncRequestStatus.get().request);
63         Job.JobStatus jobStatus = asyncInstantiationBL.calcStatus(asyncRequestStatus.get());
64         ZonedDateTime jobStartTime = getZonedDateTime(asyncRequestStatus, requestId);
65         jobStatus = expiryChecker.isExpired(jobStartTime) ? Job.JobStatus.FAILED : jobStatus;
66         return jobStatus;
67     }
68
69     private RestObject<AsyncRequestStatus> getAsyncRequestStatus(String requestId) {
70         String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
71         RestObject<AsyncRequestStatus> msoResponse = restMso.GetForObject(path, AsyncRequestStatus.class);
72         if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
73             throw new BadResponseFromMso(msoResponse);
74         }
75         return msoResponse;
76     }
77
78     public void handleFailedMsoResponse(UUID jobUUID, String requestId, RestObject<AsyncRequestStatus> msoResponse) {
79         auditService.setFailedAuditStatusFromMso(jobUUID, requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
80         LOGGER.error(EELFLoggerDelegate.errorLogger,
81                 "Failed to get orchestration status for {}. Status code: {},  Body: {}",
82                 requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
83     }
84
85     public static class BadResponseFromMso extends RuntimeException {
86         private final RestObject<AsyncRequestStatus> msoResponse;
87
88         public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
89             this.msoResponse = msoResponse;
90         }
91
92         public RestObject<AsyncRequestStatus> getMsoResponse() {
93             return msoResponse;
94         }
95     }
96
97     private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
98         ZonedDateTime jobStartTime;
99         try {
100             jobStartTime = parseZonedDateTime(asyncRequestStatusResponse.get().request.startTime);
101         } catch (DateTimeParseException | NullPointerException e) {
102             LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to parse start time for {}, body: {}. Current time will be used", requestId, asyncRequestStatusResponse.getRaw(), e);
103             jobStartTime = ZonedDateTime.now();
104         }
105         return jobStartTime;
106     }
107 }