Merge "Implant vid-app-common org.onap.vid.job (main and test)"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / impl / JobAdapterImpl.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.impl;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.onap.vid.job.Job;
25 import org.onap.vid.job.JobAdapter;
26 import org.onap.vid.job.JobType;
27 import org.onap.vid.model.JobModel;
28 import org.onap.vid.properties.Features;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31 import org.togglz.core.manager.FeatureManager;
32
33 import java.util.Map;
34 import java.util.UUID;
35
36 @Component
37 public class JobAdapterImpl implements JobAdapter {
38
39     private final FeatureManager featureManager;
40
41     @Autowired
42     public JobAdapterImpl(FeatureManager featureManager) {
43         this.featureManager = featureManager;
44     }
45
46
47     @Override
48     public JobModel toModel(Job job) {
49         JobModel jobModel = new JobModel();
50         jobModel.setUuid(job.getUuid());
51         jobModel.setStatus(job.getStatus());
52         jobModel.setTemplateId(job.getTemplateId());
53         return jobModel;
54     }
55
56     @Override
57     public Job createServiceInstantiationJob(JobType jobType, AsyncJobRequest request, UUID templateId, String userId, String testApi, String optimisticUniqueServiceInstanceName, Integer indexInBulk){
58         JobDaoImpl job = createJob(jobType, Job.JobStatus.PENDING , userId);
59         job.setSharedData(new JobSharedData(job.getUuid(), userId, request, testApi));
60         job.setTypeAndData(jobType, ImmutableMap.of(
61                 "optimisticUniqueServiceInstanceName", optimisticUniqueServiceInstanceName
62         ));
63         job.setTemplateId(templateId);
64         job.setIndexInBulk(indexInBulk);
65         return job;
66     }
67
68     @Override
69     public Job createChildJob(JobType jobType, AsyncJobRequest request, JobSharedData parentSharedData, Map<String, Object> jobData, int indexInBulk) {
70         Job.JobStatus jobStatus = featureManager.isActive(Features.FLAG_EXP_CREATE_RESOURCES_IN_PARALLEL) ?
71                 Job.JobStatus.CREATING : Job.JobStatus.PENDING_RESOURCE;
72         JobDaoImpl job = createJob(jobType, jobStatus , parentSharedData.getUserId());
73         job.setSharedData(new JobSharedData(job.getUuid(), request, parentSharedData));
74         job.setTypeAndData(jobType, jobData);
75         job.setTemplateId(parentSharedData.jobUuid);
76         job.setIndexInBulk(indexInBulk);
77         return job;
78     }
79
80     protected JobDaoImpl createJob(JobType jobType, Job.JobStatus jobStatus, String userId) {
81         JobDaoImpl job = new JobDaoImpl();
82         job.setTypeAndData(jobType, null);
83         job.setStatus(jobStatus);
84         job.setUuid(UUID.randomUUID());
85         job.setUserId(userId);
86         return job;
87     }
88
89 }