Added support for using oauth token for Kafka
[nonrtric/plt/ranpm.git] / pmproducer / src / main / java / org / oran / pmproducer / repository / Jobs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 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.pmproducer.repository;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import lombok.Getter;
31
32 import org.oran.pmproducer.configuration.ApplicationConfig;
33 import org.oran.pmproducer.exceptions.ServiceException;
34 import org.oran.pmproducer.filter.FilterFactory;
35 import org.oran.pmproducer.filter.FilteredData;
36 import org.oran.pmproducer.filter.PmReportFilter;
37 import org.oran.pmproducer.oauth2.SecurityContext;
38 import org.oran.pmproducer.repository.Job.Parameters;
39 import org.oran.pmproducer.repository.Job.Parameters.KafkaDeliveryInfo;
40 import org.oran.pmproducer.tasks.TopicListener.DataFromTopic;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.stereotype.Component;
46
47 @Component
48 public class Jobs {
49     public interface Observer {
50         void onJobbGroupAdded(JobGroup jobGroup);
51
52         void onJobGroupRemoved(JobGroup jobGroup);
53     }
54
55     public static class JobGroup {
56         @Getter
57         private final KafkaDeliveryInfo deliveryInfo;
58
59         private Map<String, Job> jobs = new HashMap<>();
60
61         @Getter
62         private PmReportFilter filter;
63
64         @Getter
65         private final InfoType type;
66
67         public JobGroup(InfoType type, KafkaDeliveryInfo deliveryInfo) {
68             this.deliveryInfo = deliveryInfo;
69             this.type = type;
70         }
71
72         public synchronized void add(Job job) {
73             this.jobs.put(job.getId(), job);
74             this.filter = createFilter();
75         }
76
77         public synchronized void remove(Job job) {
78             this.jobs.remove(job.getId());
79             if (!this.jobs.isEmpty()) {
80                 this.filter = createFilter();
81             }
82         }
83
84         public boolean isEmpty() {
85             return jobs.isEmpty();
86         }
87
88         public FilteredData filter(DataFromTopic data) {
89             return filter.filter(data);
90         }
91
92         public Job getAJob() {
93             if (this.jobs.isEmpty()) {
94                 return null;
95             }
96             return this.jobs.values().iterator().next();
97         }
98
99         private PmReportFilter createFilter() {
100             Collection<PmReportFilter> filterData = new ArrayList<>();
101             this.jobs.forEach((key, value) -> filterData.add(value.getFilter()));
102             return FilterFactory.createAggregateFilter(filterData);
103         }
104
105         public String getId() {
106             return deliveryInfo.getTopic();
107         }
108
109         public Iterable<Job> getJobs() {
110             return this.jobs.values();
111         }
112     }
113
114     private static final Logger logger = LoggerFactory.getLogger(Jobs.class);
115
116     private Map<String, Job> allJobs = new HashMap<>();
117     private MultiMap<Job> jobsByType = new MultiMap<>();
118     private Map<String, JobGroup> jobGroups = new HashMap<>(); // Key is Topic
119     private final List<Observer> observers = new ArrayList<>();
120     private final ApplicationConfig appConfig;
121
122     public Jobs(@Autowired ApplicationConfig applicationConfig, @Autowired SecurityContext securityContext,
123             @Autowired ApplicationConfig appConfig) {
124         this.appConfig = appConfig;
125     }
126
127     public synchronized Job getJob(String id) throws ServiceException {
128         Job job = allJobs.get(id);
129         if (job == null) {
130             throw new ServiceException("Could not find job: " + id, HttpStatus.NOT_FOUND);
131         }
132         return job;
133     }
134
135     public synchronized Job get(String id) {
136         return allJobs.get(id);
137     }
138
139     public void addJob(String id, InfoType type, String owner, String lastUpdated, Parameters parameters)
140             throws ServiceException {
141
142         Job job = new Job(id, type, owner, lastUpdated, parameters, this.appConfig);
143         this.put(job);
144     }
145
146     public void addObserver(Observer obs) {
147         synchronized (observers) {
148             this.observers.add(obs);
149         }
150     }
151
152     private String jobGroupId(Job job) {
153         return job.getParameters().getDeliveryInfo().getTopic();
154     }
155
156     private synchronized void put(Job job) {
157         logger.debug("Put job: {}", job.getId());
158         remove(job.getId());
159
160         allJobs.put(job.getId(), job);
161         jobsByType.put(job.getType().getId(), job.getId(), job);
162
163         String jobGroupId = jobGroupId(job);
164         if (!this.jobGroups.containsKey(jobGroupId)) {
165             final JobGroup group = new JobGroup(job.getType(), job.getParameters().getDeliveryInfo());
166             this.jobGroups.put(jobGroupId, group);
167             group.add(job);
168             this.observers.forEach(obs -> obs.onJobbGroupAdded(group));
169         } else {
170             JobGroup group = this.jobGroups.get(jobGroupId);
171             group.add(job);
172         }
173     }
174
175     public synchronized Iterable<Job> getAll() {
176         return new Vector<>(allJobs.values());
177     }
178
179     public synchronized Job remove(String id) {
180         Job job = allJobs.get(id);
181         if (job != null) {
182             remove(job);
183         }
184         return job;
185     }
186
187     public void remove(Job job) {
188         String groupId = jobGroupId(job);
189         JobGroup group = this.jobGroups.get(groupId);
190         synchronized (this) {
191             this.allJobs.remove(job.getId());
192             jobsByType.remove(job.getType().getId(), job.getId());
193             group.remove(job);
194             if (group.isEmpty()) {
195                 this.jobGroups.remove(groupId);
196             }
197         }
198
199         if (group.isEmpty()) {
200             this.observers.forEach(obs -> obs.onJobGroupRemoved(group));
201         }
202     }
203
204     public synchronized int size() {
205         return allJobs.size();
206     }
207
208     public synchronized Collection<Job> getJobsForType(InfoType type) {
209         return jobsByType.get(type.getId());
210     }
211
212     public void clear() {
213
214         this.jobGroups.forEach((id, group) -> this.observers.forEach(obs -> obs.onJobGroupRemoved(group)));
215
216         synchronized (this) {
217             allJobs.clear();
218             jobsByType.clear();
219         }
220     }
221 }