6685a63d61f655e6f997ef187e6dd9da5c2efdd9
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / InProgressStatusCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.vid.job.command;
22
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.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.onap.vid.job.Job.JobStatus;
28 import org.onap.vid.job.JobCommand;
29 import org.onap.vid.job.NextCommand;
30 import org.onap.vid.mso.MsoInterface;
31 import org.onap.vid.mso.rest.AsyncRequestStatus;
32 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
33 import org.onap.vid.services.AuditService;
34 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
35 import org.springframework.context.annotation.Scope;
36 import org.springframework.stereotype.Component;
37
38 import javax.inject.Inject;
39 import java.util.Map;
40 import java.util.Objects;
41 import java.util.UUID;
42
43
44 @Component
45 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46 public class InProgressStatusCommand implements JobCommand {
47
48     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
49
50     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusCommand.class);
51
52     @Inject
53     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
54
55     @Inject
56     private MsoInterface restMso;
57
58     @Inject
59     private AuditService auditService;
60
61     private String requestId;
62
63     private UUID jobUuid;
64
65     public InProgressStatusCommand() {
66     }
67
68     InProgressStatusCommand(UUID jobUuid, String requestId) {
69         init(jobUuid, requestId);
70     }
71
72     InProgressStatusCommand(AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic, MsoInterface msoInterface, AuditService auditService, UUID jobUuid, String requestId) {
73         this(jobUuid, requestId);
74         this.asyncInstantiationBL = asyncInstantiationBusinessLogic;
75         this.restMso = msoInterface;
76         this.auditService = auditService;
77     }
78
79     @Override
80     public NextCommand call() {
81
82         try {
83             String path = asyncInstantiationBL.getOrchestrationRequestsPath() + "/" + requestId;
84             HttpResponse<AsyncRequestStatus> msoResponse = restMso.get(path, AsyncRequestStatus.class);
85
86
87             JobStatus jobStatus;
88             if (msoResponse.getStatus() >= 400 || msoResponse.getBody() == null) {
89                 auditService.setFailedAuditStatusFromMso(jobUuid, requestId, msoResponse.getStatus(), Objects.toString(msoResponse.getBody()));
90                 LOGGER.error(EELFLoggerDelegate.errorLogger,
91                         "Failed to get orchestration status for {}. Status code: {},  Body: {}",
92                         requestId, msoResponse.getStatus(), Objects.toString(msoResponse.getRawBody()));
93                 return new NextCommand(JobStatus.IN_PROGRESS, this);
94             } else {
95                 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.getBody());
96             }
97
98             asyncInstantiationBL.auditMsoStatus(jobUuid, msoResponse.getBody().request);
99
100
101             if (jobStatus == JobStatus.FAILED) {
102                 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
103             } else {
104                 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
105             }
106             //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
107             if (jobStatus == JobStatus.PAUSE) {
108                 return new NextCommand(JobStatus.IN_PROGRESS, this);
109             }
110             return new NextCommand(jobStatus, this);
111         } catch (javax.ws.rs.ProcessingException e) {
112             // Retry when we can't connect MSO during getStatus
113             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
114             return new NextCommand(JobStatus.IN_PROGRESS, this);
115         } catch (RuntimeException e) {
116             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
117             return new NextCommand(JobStatus.STOPPED, this);
118         }
119     }
120
121     @Override
122     public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
123         return init(jobUuid, (String) data.get("requestId"));
124     }
125
126     private InProgressStatusCommand init(UUID jobUuid, String requestId) {
127         this.requestId = requestId;
128         this.jobUuid = jobUuid;
129         return this;
130     }
131
132     @Override
133     public Map<String, Object> getData() {
134         return ImmutableMap.of("requestId", requestId);
135     }
136
137 }