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.services.AsyncInstantiationBusinessLogic;
30 import org.onap.vid.services.AuditService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
34 import java.time.ZonedDateTime;
35 import java.time.format.DateTimeParseException;
36 import java.util.UUID;
38 import static org.onap.vid.utils.TimeUtils.parseZonedDateTime;
41 public class InProgressStatusService {
43 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusService.class);
45 private final AsyncInstantiationBusinessLogic asyncInstantiationBL;
47 private final RestMsoImplementation restMso;
49 private final AuditService auditService;
52 public InProgressStatusService(AsyncInstantiationBusinessLogic asyncInstantiationBL, RestMsoImplementation restMso, AuditService auditService) {
53 this.asyncInstantiationBL = asyncInstantiationBL;
54 this.restMso = restMso;
55 this.auditService = auditService;
59 public Job.JobStatus call(ExpiryChecker expiryChecker, JobSharedData sharedData, String requestId) {
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;
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);
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());
85 public static class BadResponseFromMso extends RuntimeException {
86 private final RestObject<AsyncRequestStatus> msoResponse;
88 public BadResponseFromMso(RestObject<AsyncRequestStatus> msoResponse) {
89 this.msoResponse = msoResponse;
92 public RestObject<AsyncRequestStatus> getMsoResponse() {
97 private ZonedDateTime getZonedDateTime(RestObject<AsyncRequestStatus> asyncRequestStatusResponse, String requestId) {
98 ZonedDateTime jobStartTime;
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();