98980a35dee145b266ab588679f6c917219019a4
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / ResourceInstantiationCommand.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 org.onap.vid.changeManagement.RequestDetailsWrapper;
24 import org.onap.vid.job.Job;
25 import org.onap.vid.job.JobAdapter;
26 import org.onap.vid.job.JobCommand;
27 import org.onap.vid.job.NextCommand;
28 import org.onap.vid.job.impl.JobSharedData;
29 import org.onap.vid.model.RequestReferencesContainer;
30 import org.onap.vid.mso.RestMsoImplementation;
31 import org.onap.vid.mso.RestObject;
32 import org.onap.vid.mso.model.BaseResourceInstantiationRequestDetails;
33 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
34 import org.onap.vid.services.AuditService;
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Component;
38
39 import javax.inject.Inject;
40 import java.util.Map;
41
42
43 @Component
44 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
45 public abstract class ResourceInstantiationCommand extends BaseInstantiationCommand implements JobCommand {
46
47
48     @Inject
49     protected RestMsoImplementation restMso;
50
51     @Inject
52     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
53
54     @Inject
55     private AuditService auditService;
56
57     @Override
58     public ResourceInstantiationCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
59       super.init(sharedData, commandData);
60         return this;
61     }
62
63     @Override
64     public Map<String, Object> getData() {
65        return commandParentData.getParentData();
66     }
67
68     @Override
69     public NextCommand call() {
70         if (!shouldInstantiateMyself()) {
71             return new NextCommand(Job.JobStatus.COMPLETED_WITH_NO_ACTION);
72         }
73
74         RequestDetailsWrapper<? extends BaseResourceInstantiationRequestDetails> requestDetailsWrapper = generateMSORequest(
75                 getSharedData().getRequest(),
76                 getSharedData().getUserId()
77                 );
78         String instantiatePath = getRequestPath();
79
80         RestObject<RequestReferencesContainer> msoResponse = restMso.PostForObject(requestDetailsWrapper,
81                 instantiatePath, RequestReferencesContainer.class);
82
83         if (msoResponse.getStatusCode() >= 200 && msoResponse.getStatusCode() < 400) {
84             String requestId = msoResponse.get().getRequestReferences().getRequestId();
85             String instanceId = msoResponse.get().getRequestReferences().getInstanceId();
86             asyncInstantiationBL.auditMsoStatus(getSharedData().getRootJobId(), getJobAuditMSOStatus(), requestId, null);
87             return getNextCommand(requestId, instanceId);
88         }
89         else {
90             auditService.setFailedAuditStatusFromMso(getSharedData().getRootJobId(), null, msoResponse.getStatusCode(), msoResponse.getRaw());
91             return new NextCommand(Job.JobStatus.FAILED);
92         }
93     }
94     protected NextCommand getNextCommand(String requestId, String instanceId){
95         return new NextCommand(Job.JobStatus.RESOURCE_IN_PROGRESS, new ResourceInProgressStatusCommand(getSharedData(), requestId, instanceId));
96     }
97
98     protected boolean shouldInstantiateMyself() {
99         return true;
100     }
101
102     protected abstract String getRequestPath();
103     protected abstract RequestDetailsWrapper<? extends BaseResourceInstantiationRequestDetails> generateMSORequest(JobAdapter.AsyncJobRequest request, String userId);
104     protected abstract String getJobAuditMSOStatus();
105 }
106
107