Test FTC100 fails since A1-SIM update
[nonrtric.git] / sample-services / ics-producer-consumer / producer / src / main / java / com / demo / producer / repository / Jobs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  *
5  * Copyright (C) 2024: OpenInfra Foundation Europe
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 com.demo.producer.repository;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Vector;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
30
31 import com.demo.producer.messages.PropertiesHelper;
32 import com.demo.producer.repository.Job.Parameters;
33 import com.google.gson.Gson;
34 import com.google.gson.GsonBuilder;
35
36 @Component
37 public class Jobs {
38     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
39
40     private Map<String, Job> allJobs = new HashMap<>();
41
42     public Jobs() {
43     }
44
45     public synchronized Job getJob(String id) throws Exception {
46         Job job = allJobs.get(id);
47         if (job == null) {
48             throw new Exception("Could not find job: " + id);
49         }
50         return job;
51     }
52
53     public synchronized Job get(String id) {
54         return allJobs.get(id);
55     }
56
57     public void addJob(String id, InfoType type, String owner, Parameters parameters) {
58         Job job = new Job(id, type, owner, parameters);
59         setKafkaServersEnvironment(job);
60         this.put(job);
61     }
62
63     private void setKafkaServersEnvironment(Job job) {
64         String kafkaServers = job.getParameters().getDeliveryInfo().getBootStrapServers();
65         if (kafkaServers != null && !kafkaServers.isEmpty()) {
66             PropertiesHelper.setKafkaServers(kafkaServers);
67             logger.info("Setting variable bootStrapServers: {}", kafkaServers);
68         } else {
69             logger.warn("bootStrapServers is not set for job: {}", job.getId());
70         }
71     }
72
73     private synchronized void put(Job job) {
74         logger.debug("Put job: {}", job.getId());
75         allJobs.put(job.getId(), job);
76     }
77
78     public synchronized Iterable<Job> getAll() {
79         return new Vector<>(allJobs.values());
80     }
81
82     public synchronized int size() {
83         return allJobs.size();
84     }
85
86     public synchronized Job delete(String id) {
87         return allJobs.remove(id);
88     }
89
90     @Override
91     public String toString() {
92         Gson gson = new GsonBuilder().create();
93         return gson.toJson(allJobs);
94     }
95 }