2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.dmaapadapter.repository;
23 import java.util.Collection;
24 import java.util.HashMap;
26 import java.util.Vector;
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;
41 private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
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;
48 public Jobs(@Autowired KafkaTopicConsumers kafkaConsumers, @Autowired ApplicationConfig applicationConfig) {
49 this.kafkaConsumers = kafkaConsumers;
51 restclientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
54 public synchronized Job getJob(String id) throws ServiceException {
55 Job job = allJobs.get(id);
57 throw new ServiceException("Could not find job: " + id);
62 public synchronized Job get(String id) {
63 return allJobs.get(id);
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);
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);
82 public synchronized Iterable<Job> getAll() {
83 return new Vector<>(allJobs.values());
86 public synchronized Job remove(String id) {
87 Job job = allJobs.get(id);
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);
100 public synchronized int size() {
101 return allJobs.size();
104 public synchronized Collection<Job> getJobsForType(InfoType type) {
105 return jobsByType.get(type.getId());
108 public synchronized void clear() {