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 static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
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;
42 public class InProgressStatusService {
44 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
46 private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
48 private final RestMsoImplementation restMso;
50 private final AuditService auditService;
52 private final FeatureManager featureManager;
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;
63 public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
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());
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);
79 if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
80 throw new BadResponseFromMso(msoResponse);
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());
92 public static class BadResponseFromMso extends RuntimeException {
93 private final RestObject<AsyncRequestStatus> msoResponse;
95 public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
96 this.msoResponse = msoResponse;
99 public RestObject<AsyncRequestStatus> getMsoResponse() {
104 private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
105 ZonedDateTime jobStartTime;
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();