blob: 280eb84fc6773f99bc8abb45e00f3c2aa827fe67 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.job.impl;
24
25import com.google.common.collect.ImmutableMap;
26import org.onap.osam.job.Job;
27import org.onap.osam.job.JobAdapter;
28import org.onap.osam.job.JobType;
29import org.onap.osam.model.JobBulk;
30import org.onap.osam.model.JobModel;
31import org.springframework.stereotype.Component;
32
33import javax.ws.rs.BadRequestException;
34import java.util.ArrayList;
35import java.util.List;
36import java.util.Map;
37import java.util.UUID;
38import java.util.stream.Collectors;
39
40@Component
41public class JobAdapterImpl implements JobAdapter {
42
43 @Override
44 public JobModel toModel(Job job) {
45 JobModel jobModel = new JobModel();
46 jobModel.setUuid(job.getUuid());
47 jobModel.setStatus(job.getStatus());
48 jobModel.setTemplateId(job.getTemplateId());
49 return jobModel;
50 }
51
52 @Override
53 public JobBulk toModelBulk(List<Job> jobList) {
54 return new JobBulk(jobList.stream().map(this::toModel).collect(Collectors.toList()));
55 }
56
57 @Override
58 public Job createJob(JobType jobType, AsyncJobRequest request, UUID templateId, String userId, Integer indexInBulk){
59 JobDaoImpl job = new JobDaoImpl();
60 job.setStatus(Job.JobStatus.PENDING);
61 job.setTypeAndData(jobType, ImmutableMap.of(
62 "request", request,
63 "userId", userId));
64 job.setTemplateId(templateId);
65 job.setIndexInBulk(indexInBulk);
66 job.setUserId(userId);
67 return job;
68 }
69
70 @Override
71 public List<Job> createBulkOfJobs(Map<String, Object> bulkRequest) {
72 int count;
73 JobType jobType;
74
75 try {
76 count = (Integer) bulkRequest.get("count");
77 jobType = JobType.valueOf((String) bulkRequest.get("type"));
78 } catch (Exception exception) {
79 throw new BadRequestException(exception);
80 }
81 List<Job> jobList = new ArrayList<>(count + 1);
82 UUID templateId = UUID.randomUUID();
83 for (int i = 0; i < count; i++) {
84 Job child = new JobDaoImpl();
85 child.setTypeAndData(jobType, bulkRequest);
86 child.setStatus(Job.JobStatus.PENDING);
87 child.setTemplateId(templateId);
88 child.setIndexInBulk(i);
89 jobList.add(child);
90 }
91 return jobList;
92 }
93
94}