Remove restricted notice from TOSCA file
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / BaseInProgressStatusCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
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 package org.onap.vid.job.command;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.vid.job.*;
26 import org.onap.vid.job.impl.JobSharedData;
27 import org.onap.vid.mso.RestMsoImplementation;
28 import org.onap.vid.mso.RestObject;
29 import org.onap.vid.mso.rest.AsyncRequestStatus;
30 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
31 import org.togglz.core.manager.FeatureManager;
32
33 import javax.inject.Inject;
34 import java.util.Map;
35
36 public abstract class BaseInProgressStatusCommand extends BaseInstantiationCommand implements JobCommand {
37     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(BaseInProgressStatusCommand.class);
38
39     @Inject
40     protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
41
42     @Inject
43     protected JobsBrokerService jobsBrokerService;
44
45     @Inject
46     protected JobAdapter jobAdapter;
47
48     @Inject
49     protected RestMsoImplementation restMso;
50
51     @Inject
52     protected FeatureManager featureManager;
53
54     @Inject
55     protected InProgressStatusService inProgressStatusService;
56
57
58     protected String requestId;
59
60     protected String instanceId;
61
62
63     @Override
64     public NextCommand call() {
65
66         try {
67             Job.JobStatus jobStatus =  inProgressStatusService.call(getExpiryChecker(), getSharedData(), requestId);
68             return processJobStatus(jobStatus);
69         } catch (javax.ws.rs.ProcessingException e) {
70             // Retry when we can't connect MSO during getStatus
71             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, will retry: {}", requestId, e, e);
72             return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
73         } catch (InProgressStatusService.BadResponseFromMso e) {
74             return handleFailedMsoResponse(e.getMsoResponse());
75         }
76         catch (RuntimeException e) {
77             LOGGER.error(EELFLoggerDelegate.errorLogger, "Cannot get orchestration status for {}, stopping: {}", requestId, e, e);
78             return new NextCommand(Job.JobStatus.STOPPED, this);
79         }
80     }
81
82     protected abstract ExpiryChecker getExpiryChecker();
83
84     abstract NextCommand processJobStatus(Job.JobStatus jobStatus);
85
86     private NextCommand handleFailedMsoResponse(RestObject<AsyncRequestStatus> msoResponse) {
87         inProgressStatusService.handleFailedMsoResponse(getSharedData().getJobUuid(), requestId, msoResponse);
88         return new NextCommand(Job.JobStatus.IN_PROGRESS, this);
89     }
90
91     @Override
92     public BaseInProgressStatusCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
93         return init(sharedData, (String) commandData.get("requestId"), (String) commandData.get("instanceId"));
94     }
95
96
97     protected BaseInProgressStatusCommand init(JobSharedData sharedData,
98                                                String requestId,
99                                                String instanceId) {
100         init(sharedData);
101         this.requestId = requestId;
102         this.instanceId = instanceId;
103         return this;
104     }
105
106     @Override
107     public Map<String, Object> getData() {
108         return ImmutableMap.of(
109             "requestId", requestId,
110             "instanceId", instanceId
111         );
112     }
113
114
115 }