NONRTRIC - Implement DMaaP mediator producer service in Java
[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.clients.AsyncRestClient;
29 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
30 import org.oran.dmaapadapter.configuration.ApplicationConfig;
31 import org.oran.dmaapadapter.exceptions.ServiceException;
32 import org.oran.dmaapadapter.repository.Job.Parameters;
33 import org.oran.dmaapadapter.tasks.KafkaTopicConsumers;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class Jobs {
41     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
42
43     private Map<String, Job> allJobs = new HashMap<>();
44     private MultiMap<Job> jobsByType = new MultiMap<>();
45     private final KafkaTopicConsumers kafkaConsumers;
46     private final AsyncRestClientFactory restclientFactory;
47
48     public Jobs(@Autowired KafkaTopicConsumers kafkaConsumers, @Autowired ApplicationConfig applicationConfig) {
49         this.kafkaConsumers = kafkaConsumers;
50
51         restclientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
52     }
53
54     public synchronized Job getJob(String id) throws ServiceException {
55         Job job = allJobs.get(id);
56         if (job == null) {
57             throw new ServiceException("Could not find job: " + id);
58         }
59         return job;
60     }
61
62     public synchronized Job get(String id) {
63         return allJobs.get(id);
64     }
65
66     public void addJob(String id, String callbackUrl, InfoType type, String owner, String lastUpdated,
67             Parameters parameters) {
68         AsyncRestClient consumerRestClient = type.isUseHttpProxy() //
69                 ? restclientFactory.createRestClientUseHttpProxy(callbackUrl) //
70                 : restclientFactory.createRestClientNoHttpProxy(callbackUrl);
71         Job job = new Job(id, callbackUrl, type, owner, lastUpdated, parameters, consumerRestClient);
72         this.put(job);
73     }
74
75     private synchronized void put(Job job) {
76         logger.debug("Put job: {}", job.getId());
77         allJobs.put(job.getId(), job);
78         jobsByType.put(job.getType().getId(), job.getId(), job);
79         kafkaConsumers.addJob(job);
80     }
81
82     public synchronized Iterable<Job> getAll() {
83         return new Vector<>(allJobs.values());
84     }
85
86     public synchronized Job remove(String id) {
87         Job job = allJobs.get(id);
88         if (job != null) {
89             remove(job);
90         }
91         return job;
92     }
93
94     public synchronized void remove(Job job) {
95         this.allJobs.remove(job.getId());
96         jobsByType.remove(job.getType().getId(), job.getId());
97         kafkaConsumers.removeJob(job);
98     }
99
100     public synchronized int size() {
101         return allJobs.size();
102     }
103
104     public synchronized Collection<Job> getJobsForType(InfoType type) {
105         return jobsByType.get(type.getId());
106     }
107
108     public synchronized void clear() {
109         allJobs.clear();
110         jobsByType.clear();
111     }
112 }