Don't use EELFLoggerDelegate.errorLogger in Async jobs
[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 static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
24
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeParseException;
27 import java.util.UUID;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.vid.job.Job;
30 import org.onap.vid.job.impl.JobSharedData;
31 import org.onap.vid.mso.RestMsoImplementation;
32 import org.onap.vid.mso.RestObject;
33 import org.onap.vid.mso.rest.AsyncRequestStatus;
34 import org.onap.vid.properties.Features;
35 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
36 import org.onap.vid.services.AuditService;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39 import org.togglz.core.manager.FeatureManager;
40
41 @Service
42 public class InProgressStatusService {
43
44     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
45
46     private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
47
48     private final RestMsoImplementation restMso;
49
50     private final AuditService auditService;
51
52     private final FeatureManager featureManager;
53
54     @Autowired
55     public InProgressStatusService(AsyncInstantiationBusinessLogic asyncInstantiationBL, RestMsoImplementation restMso, AuditService auditService, FeatureManager featureManager) {
56         this.asyncInstantiationBL = asyncInstantiationBL;
57         this.restMso = restMso;
58         this.auditService = auditService;
59         this.featureManager = featureManager;
60     }
61
62
63     public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
64
65         RestObject<AsyncRequestStatus> asyncRequestStatus = getAsyncRequestStatus(requestId);
66         auditService.auditMsoStatus(sharedData.getRootJobId(), asyncRequestStatus.get().request);
67         Job.JobStatus jobStatus = asyncInstantiationBL.calcStatus(asyncRequestStatus.get());
68         ZonedDateTime jobStartTime = getZonedDateTime(asyncRequestStatus, requestId);
69         jobStatus = expiryChecker.isExpired(jobStartTime) ? Job.JobStatus.FAILED : jobStatus;
70         asyncInstantiationBL.updateResourceInfo(sharedData, jobStatus, asyncRequestStatus.get());
71         return jobStatus;
72     }
73
74     RestObject<AsyncRequestStatus> getAsyncRequestStatus(String requestId) {
75         String path = asyncInstantiationBL.getOrchestrationRequestsPath() + "/" + requestId +
76                 (featureManager.isActive(Features.FLAG_1908_RESUME_MACRO_SERVICE) ? "?format=detail" : "");
77         RestObject<AsyncRequestStatus> msoResponse = restMso.GetForObject(path, AsyncRequestStatus.class);
78
79         if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
80             throw new BadResponseFromMso(msoResponse);
81         }
82
83         return msoResponse;
84     }
85
86     public void handleFailedMsoResponse(UUID jobUUID, String requestId, RestObject<AsyncRequestStatus> msoResponse) {
87         auditService.setFailedAuditStatusFromMso(jobUUID, requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
88         LOGGER.error("Failed to get orchestration status for {}. Status code: {},  Body: {}",
89                 requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
90     }
91
92     public static class BadResponseFromMso extends RuntimeException {
93         private final RestObject<AsyncRequestStatus> msoResponse;
94
95         public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
96             this.msoResponse = msoResponse;
97         }
98
99         public RestObject<AsyncRequestStatus> getMsoResponse() {
100             return msoResponse;
101         }
102     }
103
104     private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
105         ZonedDateTime jobStartTime;
106         try {
107             jobStartTime = parseZonedDateTime(asyncRequestStatusResponse.get().request.startTime);
108         } catch (DateTimeParseException | NullPointerException e) {
109             LOGGER.error("Failed to parse start time for {}, body: {}. Current time will be used", requestId, asyncRequestStatusResponse.getRaw(), e);
110             jobStartTime = ZonedDateTime.now();
111         }
112         return jobStartTime;
113     }
114 }