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.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;
38 import javax.inject.Inject;
40 import java.util.Objects;
41 import java.util.UUID;
45 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46 public class InProgressStatusCommand implements JobCommand {
48 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
50 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(InProgressStatusCommand.class);
53 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
56 private MsoInterface restMso;
59 private AuditService auditService;
61 private String requestId;
65 public InProgressStatusCommand() {
68 InProgressStatusCommand(UUID jobUuid, String requestId) {
69 init(jobUuid, requestId);
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;
80 public NextCommand call() {
83 String path = asyncInstantiationBL.getOrchestrationRequestsPath() + "/" + requestId;
84 HttpResponse<AsyncRequestStatus> msoResponse = restMso.get(path, AsyncRequestStatus.class);
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);
95 jobStatus = asyncInstantiationBL.calcStatus(msoResponse.getBody());
98 asyncInstantiationBL.auditMsoStatus(jobUuid, msoResponse.getBody().request);
101 if (jobStatus == JobStatus.FAILED) {
102 asyncInstantiationBL.handleFailedInstantiation(jobUuid);
104 asyncInstantiationBL.updateServiceInfoAndAuditStatus(jobUuid, jobStatus);
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);
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);
122 public InProgressStatusCommand init(UUID jobUuid, Map<String, Object> data) {
123 return init(jobUuid, (String) data.get("requestId"));
126 private InProgressStatusCommand init(UUID jobUuid, String requestId) {
127 this.requestId = requestId;
128 this.jobUuid = jobUuid;
133 public Map<String, Object> getData() {
134 return ImmutableMap.of("requestId", requestId);