Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ResourceInstantiationCommand.java
1 package org.onap.vid.job.command;
2
3 import org.onap.vid.changeManagement.RequestDetailsWrapper;
4 import org.onap.vid.job.Job;
5 import org.onap.vid.job.JobAdapter;
6 import org.onap.vid.job.JobCommand;
7 import org.onap.vid.job.NextCommand;
8 import org.onap.vid.job.impl.JobSharedData;
9 import org.onap.vid.model.RequestReferencesContainer;
10 import org.onap.vid.mso.RestMsoImplementation;
11 import org.onap.vid.mso.RestObject;
12 import org.onap.vid.mso.model.BaseResourceInstantiationRequestDetails;
13 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
14 import org.onap.vid.services.AuditService;
15 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
16 import org.springframework.context.annotation.Scope;
17 import org.springframework.stereotype.Component;
18
19 import javax.inject.Inject;
20 import java.util.Map;
21
22
23 @Component
24 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
25 public abstract class ResourceInstantiationCommand extends BaseInstantiationCommand implements JobCommand {
26
27
28     @Inject
29     protected RestMsoImplementation restMso;
30
31     @Inject
32     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
33
34     @Inject
35     private AuditService auditService;
36
37     @Override
38     public ResourceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
39       super.init(sharedData, commandData);
40         return this;
41     }
42
43     @Override
44     public Map<String, Object> getData() {
45        return commandParentData.getParentData();
46     }
47
48     @Override
49     public NextCommand call() {
50         if (!shouldInstantiateMyself()) {
51             return new NextCommand(Job.JobStatus.COMPLETED_WITH_NO_ACTION);
52         }
53
54         RequestDetailsWrapper<? extends BaseResourceInstantiationRequestDetails> requestDetailsWrapper = generateMSORequest(
55                 getSharedData().getRequest(),
56                 getSharedData().getUserId()
57                 );
58         String instantiatePath = getRequestPath();
59
60         RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
61                 instantiatePath, RequestReferencesContainer.class);
62
63         if (msoResponse.getStatusCode() >= 200 && msoResponse.getStatusCode() < 400) {
64             String requestId = msoResponse.get().getRequestReferences().getRequestId();
65             String instanceId = msoResponse.get().getRequestReferences().getInstanceId();
66             asyncInstantiationBL.auditMsoStatus(getSharedData().getRootJobId(), getJobAuditMSOStatus(), requestId, null);
67             return getNextCommand(requestId, instanceId);
68         }
69         else {
70             auditService.setFailedAuditStatusFromMso(getSharedData().getRootJobId(), null, msoResponse.getStatusCode(), msoResponse.getRaw());
71             return new NextCommand(Job.JobStatus.FAILED);
72         }
73     }
74     protected NextCommand getNextCommand(String requestId, String instanceId){
75         return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, new ResourceInProgressStatusCommand(getSharedData(), requestId, instanceId));
76     }
77
78     protected boolean shouldInstantiateMyself() {
79         return true;
80     }
81
82     protected abstract String getRequestPath();
83     protected abstract RequestDetailsWrapper<? extends BaseResourceInstantiationRequestDetails> generateMSORequest(JobAdapter.AsyncJobRequest request, String userId);
84     protected abstract String getJobAuditMSOStatus();
85 }
86
87