Merge "Replace SO client"
[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.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;
37
38 import javax.inject.Inject;
39 import java.util.Map;
40 import java.util.UUID;
41
42
43 @Component
44 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
45 public class InProgressStatusCommand implements JobCommand {
46
47     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
48
49     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusCommand.class);
50
51     @Inject
52     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
53
54     @Inject
55     private MsoInterface restMso;
56
57     @Inject
58     private AuditService auditService;
59
60     private String requestId;
61
62     private UUID jobUuid;
63
64     public InProgressStatusCommand() {
65     }
66
67     InProgressStatusCommand(UUID jobUuid, String requestId) {
68         init(jobUuid, requestId);
69     }
70
71     @Override
72     public NextCommand call() {
73
74         try {
75             String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
76             HttpResponse<AsyncRequestStatus> msoResponse = restMso.get(path, AsyncRequestStatus.class);
77
78
79             JobStatus jobStatus;
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);
86             }
87             else {
88                 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.getBody());
89             }
90
91             asyncInstantiationBL.auditMsoStatus(jobUuid,msoResponse.getBody().request);
92
93
94             if (jobStatus == JobStatus.FAILED) {
95                 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
96             }
97             else {
98                 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
99             }
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);
103             }
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);
112         }
113     }
114
115     @Override
116     public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
117         return init(jobUuid, (String) data.get("requestId"));
118     }
119
120     private InProgressStatusCommand init(UUID jobUuid, String requestId) {
121         this.requestId = requestId;
122         this.jobUuid = jobUuid;
123         return this;
124     }
125
126     @Override
127     public Map<String, Object> getData() {
128         return ImmutableMap.of("requestId", requestId);
129     }
130
131
132 }