blob: dc42136a441b7e9635270ad34d84544907c4380a [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20
21
22package org.onap.osam.job.command;
23
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.google.common.collect.ImmutableMap;
26import io.joshworks.restclient.http.HttpResponse;
27import org.onap.osam.job.Job.JobStatus;
28import org.onap.osam.job.JobCommand;
29import org.onap.osam.job.NextCommand;
30import org.onap.osam.mso.MsoInterface;
31import org.onap.osam.mso.rest.AsyncRequestStatus;
32import org.onap.osam.services.IAsyncInstantiationBusinessLogic;
33import org.onap.osam.services.IAuditService;
34import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
35import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36import org.springframework.context.annotation.Scope;
37import org.springframework.stereotype.Component;
38
39import javax.inject.Inject;
40import java.util.Map;
41import java.util.UUID;
42
43
44@Component
45@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46public 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 IAsyncInstantiationBusinessLogic asyncInstantiationBL;
54
55 @Inject
56 private MsoInterface restMso;
57
58 @Inject
59 private IAuditService 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 @Override
73 public NextCommand call() {
74
75 try {
76 String path = asyncInstantiationBL.getOrchestrationRequestsPath()+"/"+requestId;
77 HttpResponse<AsyncRequestStatus> msoResponse = restMso.get(path, AsyncRequestStatus.class);
78
79
80 JobStatus jobStatus;
81 if (msoResponse.getStatus() >= 400 || msoResponse.getBody() == null) {
82 auditService.setFailedAuditStatusFromMso(jobUuid, requestId, msoResponse.getStatus(), msoResponse.getBody().toString());
83 LOGGER.error(EELFLoggerDelegate.errorLogger,
84 "Failed to get orchestration status for {}. Status code: {}, Body: {}",
85 requestId, msoResponse.getStatus(), msoResponse.getRawBody().toString());
86 return new NextCommand(JobStatus.IN_PROGRESS, this);
87 }
88 else {
89 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.getBody());
90 }
91
92 asyncInstantiationBL.auditMsoStatus(jobUuid,msoResponse.getBody().request);
93
94
95 if (jobStatus == JobStatus.FAILED) {
96 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
97 }
98 else {
99 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
100 }
101 //in case of JobStatus.PAUSE we leave the job itself as IN_PROGRESS, for keep tracking job progress
102 if (jobStatus == JobStatus.PAUSE) {
103 return new NextCommand(JobStatus.IN_PROGRESS, this);
104 }
105 return new NextCommand(jobStatus, this);
106 } catch (javax.ws.rs.ProcessingException e) {
107 // Retry when we can't connect MSO during getStatus
108 LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
109 return new NextCommand(JobStatus.IN_PROGRESS, this);
110 } catch (RuntimeException e) {
111 LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
112 return new NextCommand(JobStatus.STOPPED, this);
113 }
114 }
115
116 @Override
117 public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
118 return init(jobUuid, (String) data.get("requestId"));
119 }
120
121 private InProgressStatusCommand init(UUID jobUuid, String requestId) {
122 this.requestId = requestId;
123 this.jobUuid = jobUuid;
124 return this;
125 }
126
127 @Override
128 public Map<String, Object> getData() {
129 return ImmutableMap.of("requestId", requestId);
130 }
131
132
133}