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