Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / InProgressStatusCommand.java
1 package org.onap.vid.job.command;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.google.common.collect.ImmutableMap;
5 import org.onap.vid.job.Job.JobStatus;
6 import org.onap.vid.job.JobCommand;
7 import org.onap.vid.job.NextCommand;
8 import org.onap.vid.mso.RestMsoImplementation;
9 import org.onap.vid.mso.RestObject;
10 import org.onap.vid.mso.rest.AsyncRequestStatus;
11 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
12 import org.onap.vid.services.AuditService;
13 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
14 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
15 import org.springframework.context.annotation.Scope;
16 import org.springframework.stereotype.Component;
17
18 import javax.inject.Inject;
19 import java.util.Map;
20 import java.util.UUID;
21
22
23 @Component
24 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
25 public class InProgressStatusCommand implements JobCommand {
26
27     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
28
29     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusCommand.class);
30
31     @Inject
32     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
33
34     @Inject
35     private RestMsoImplementation restMso;
36
37     @Inject
38     private AuditService auditService;
39
40     private String requestId;
41
42     private UUID jobUuid;
43
44     public InProgressStatusCommand() {
45     }
46
47     InProgressStatusCommand(UUID jobUuid, String requestId) {
48         init(jobUuid, requestId);
49     }
50
51     @Override
52     public NextCommand call() {
53
54         try {
55             String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
56             RestObject<AsyncRequestStatus> msoResponse = restMso.GetForObject("", path, AsyncRequestStatus.class);
57             JobStatus jobStatus;
58             if (msoResponse.getStatusCode() >= 400 || msoResponse.get() == null) {
59                 auditService.setFailedAuditStatusFromMso(jobUuid, requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
60                 LOGGER.error(EELFLoggerDelegate.errorLogger,
61                         "Failed to get orchestration status for {}. Status code: {},  Body: {}",
62                         requestId, msoResponse.getStatusCode(), msoResponse.getRaw());
63                 return new NextCommand(JobStatus.IN_PROGRESS, this);
64             }
65             else {
66                 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.get());
67             }
68
69             asyncInstantiationBL.auditMsoStatus(jobUuid,msoResponse.get().request);
70
71
72             if (jobStatus == JobStatus.FAILED) {
73                 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
74             }
75             else {
76                 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
77             }
78             //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
79             if (jobStatus == JobStatus.PAUSE) {
80                 return new NextCommand(JobStatus.IN_PROGRESS, this);
81             }
82             return new NextCommand(jobStatus, this);
83         } catch (javax.ws.rs.ProcessingException e) {
84             // Retry when we can't connect MSO during getStatus
85             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
86             return new NextCommand(JobStatus.IN_PROGRESS, this);
87         } catch (RuntimeException e) {
88             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
89             return new NextCommand(JobStatus.STOPPED, this);
90         }
91     }
92
93     @Override
94     public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
95         return init(jobUuid, (String) data.get("requestId"));
96     }
97
98     private InProgressStatusCommand init(UUID jobUuid, String requestId) {
99         this.requestId = requestId;
100         this.jobUuid = jobUuid;
101         return this;
102     }
103
104     @Override
105     public Map<String, Object> getData() {
106         return ImmutableMap.of("requestId", requestId);
107     }
108
109
110 }