8a388248a9073c2ab0b127634357c2f7423c59c6
[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.oran.dmaapadapter.tasks.KafkaTopicConsumers;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public class Jobs {
37     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
38
39     private Map<String, Job> allJobs = new HashMap<>();
40     private MultiMap<Job> jobsByType = new MultiMap<>();
41     private final KafkaTopicConsumers kafkaConsumers;
42
43     public Jobs(@Autowired KafkaTopicConsumers kafkaConsumers) {
44         this.kafkaConsumers = kafkaConsumers;
45     }
46
47     public synchronized Job getJob(String id) throws ServiceException {
48         Job job = allJobs.get(id);
49         if (job == null) {
50             throw new ServiceException("Could not find job: " + id);
51         }
52         return job;
53     }
54
55     public synchronized Job get(String id) {
56         return allJobs.get(id);
57     }
58
59     public synchronized void put(Job job) {
60         logger.debug("Put job: {}", job.getId());
61         allJobs.put(job.getId(), job);
62         jobsByType.put(job.getType().getId(), job.getId(), job);
63         kafkaConsumers.addJob(job);
64     }
65
66     public synchronized Iterable<Job> getAll() {
67         return new Vector<>(allJobs.values());
68     }
69
70     public synchronized Job remove(String id) {
71         Job job = allJobs.get(id);
72         if (job != null) {
73             remove(job);
74         }
75         return job;
76     }
77
78     public synchronized void remove(Job job) {
79         this.allJobs.remove(job.getId());
80         jobsByType.remove(job.getType().getId(), job.getId());
81         kafkaConsumers.removeJob(job);
82     }
83
84     public synchronized int size() {
85         return allJobs.size();
86     }
87
88     public synchronized Collection<Job> getJobsForType(InfoType type) {
89         return jobsByType.get(type.getId());
90     }
91
92     public synchronized void clear() {
93         allJobs.clear();
94         jobsByType.clear();
95     }
96 }