2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.vid.job.command;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.collect.ImmutableMap;
25 import io.joshworks.restclient.http.HttpResponse;
26 import org.onap.vid.job.Job.JobStatus;
27 import org.onap.vid.job.JobCommand;
28 import org.onap.vid.job.NextCommand;
29 import org.onap.vid.mso.MsoInterface;
30 import org.onap.vid.mso.rest.AsyncRequestStatus;
31 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
32 import org.onap.vid.services.AuditService;
33 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
35 import org.springframework.context.annotation.Scope;
36 import org.springframework.stereotype.Component;
38 import javax.inject.Inject;
40 import java.util.UUID;
44 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
45 public class InProgressStatusCommand implements JobCommand {
47 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
49 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusCommand.class);
52 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
55 private MsoInterface restMso;
58 private AuditService auditService;
60 private String requestId;
64 public InProgressStatusCommand() {
67 InProgressStatusCommand(UUID jobUuid, String requestId) {
68 init(jobUuid, requestId);
72 public NextCommand call() {
75 String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
76 HttpResponse<AsyncRequestStatus> msoResponse = restMso.get(path, AsyncRequestStatus.class);
80 if (msoResponse.getStatus() >= 400 || msoResponse.getBody() == null) {
81 auditService.setFailedAuditStatusFromMso(jobUuid, requestId, msoResponse.getStatus(), msoResponse.getBody().toString());
82 LOGGER.error(EELFLoggerDelegate.errorLogger,
83 "Failed to get orchestration status for {}. Status code: {}, Body: {}",
84 requestId, msoResponse.getStatus(), msoResponse.getRawBody().toString());
85 return new NextCommand(JobStatus.IN_PROGRESS, this);
88 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.getBody());
91 asyncInstantiationBL.auditMsoStatus(jobUuid,msoResponse.getBody().request);
94 if (jobStatus == JobStatus.FAILED) {
95 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
98 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
100 //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
101 if (jobStatus == JobStatus.PAUSE) {
102 return new NextCommand(JobStatus.IN_PROGRESS, this);
104 return new NextCommand(jobStatus, this);
105 } catch (javax.ws.rs.ProcessingException e) {
106 // Retry when we can't connect MSO during getStatus
107 LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
108 return new NextCommand(JobStatus.IN_PROGRESS, this);
109 } catch (RuntimeException e) {
110 LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
111 return new NextCommand(JobStatus.STOPPED, this);
116 public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
117 return init(jobUuid, (String) data.get("requestId"));
120 private InProgressStatusCommand init(UUID jobUuid, String requestId) {
121 this.requestId = requestId;
122 this.jobUuid = jobUuid;
127 public Map<String, Object> getData() {
128 return ImmutableMap.of("requestId", requestId);