6e2b3265f4488804132cc18db0cd0a4a7e4b9a72
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / repository / Jobs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
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.oran.dmaapadapter.repository;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.oran.dmaapadapter.exceptions.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class Jobs {
35     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
36
37     private Map<String, Job> allJobs = new HashMap<>();
38     private MultiMap<Job> jobsByType = new MultiMap<>();
39
40     public Jobs() {}
41
42     public synchronized Job getJob(String id) throws ServiceException {
43         Job job = allJobs.get(id);
44         if (job == null) {
45             throw new ServiceException("Could not find job: " + id);
46         }
47         return job;
48     }
49
50     public synchronized Job get(String id) {
51         return allJobs.get(id);
52     }
53
54     public synchronized void put(Job job) {
55         logger.debug("Put service: {}", job.getId());
56         allJobs.put(job.getId(), job);
57         jobsByType.put(job.getType().getId(), job.getId(), job);
58     }
59
60     public synchronized Iterable<Job> getAll() {
61         return new Vector<>(allJobs.values());
62     }
63
64     public synchronized Job remove(String id) {
65         Job job = allJobs.get(id);
66         if (job != null) {
67             remove(job);
68         }
69         return job;
70     }
71
72     public synchronized void remove(Job job) {
73         this.allJobs.remove(job.getId());
74         jobsByType.remove(job.getType().getId(), job.getId());
75     }
76
77     public synchronized int size() {
78         return allJobs.size();
79     }
80
81     public synchronized Collection<Job> getJobsForType(InfoType type) {
82         return jobsByType.get(type.getId());
83     }
84
85     public synchronized void clear() {
86         allJobs.clear();
87         jobsByType.clear();
88     }
89 }