2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command;
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;
36 import java.time.ZonedDateTime;
37 import java.time.format.DateTimeParseException;
38 import java.util.UUID;
40 import static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
43 public class InProgressStatusService {
45 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
47 private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
49 private final RestMsoImplementation restMso;
51 private final AuditService auditService;
53 private final FeatureManager featureManager;
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;
64 public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
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());
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);
80 if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
81 throw new BadResponseFromMso(msoResponse);
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());
94 public static class BadResponseFromMso extends RuntimeException {
95 private final RestObject<AsyncRequestStatus> msoResponse;
97 public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
98 this.msoResponse = msoResponse;
101 public RestObject<AsyncRequestStatus> getMsoResponse() {
106 private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
107 ZonedDateTime jobStartTime;
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();