Creating PM-producer
[nonrtric/plt/ranpm.git] / pmproducer / src / main / java / org / oran / pmproducer / tasks / TopicListeners.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.tasks;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import lombok.Getter;
27
28 import org.oran.pmproducer.configuration.ApplicationConfig;
29 import org.oran.pmproducer.repository.InfoType;
30 import org.oran.pmproducer.repository.InfoTypes;
31 import org.oran.pmproducer.repository.Jobs;
32 import org.oran.pmproducer.repository.Jobs.JobGroup;
33 import org.oran.pmproducer.repository.MultiMap;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.scheduling.annotation.EnableScheduling;
38 import org.springframework.stereotype.Component;
39
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
41 @Component
42 @EnableScheduling
43 public class TopicListeners {
44     private static final Logger logger = LoggerFactory.getLogger(TopicListeners.class);
45
46     @Getter
47     private final Map<String, TopicListener> topicListeners = new HashMap<>(); // Key is typeId
48
49     @Getter
50     private final MultiMap<JobDataDistributor> dataDistributors = new MultiMap<>(); // Key is typeId, jobId
51
52     private final ApplicationConfig appConfig;
53
54     public TopicListeners(@Autowired ApplicationConfig appConfig, @Autowired InfoTypes types, @Autowired Jobs jobs) {
55         this.appConfig = appConfig;
56
57         for (InfoType type : types.getAll()) {
58             TopicListener topicConsumer = new TopicListener(appConfig, type);
59             topicListeners.put(type.getId(), topicConsumer);
60         }
61
62         jobs.addObserver(new Jobs.Observer() {
63             @Override
64             public void onJobbGroupAdded(JobGroup jobGroup) {
65                 addJob(jobGroup);
66             }
67
68             @Override
69             public void onJobGroupRemoved(JobGroup jobGroup) {
70                 removeDistributor(jobGroup);
71             }
72         });
73     }
74
75     public synchronized void addJob(JobGroup jobGroup) {
76         removeDistributor(jobGroup);
77         logger.debug("Job added {}", jobGroup.getId());
78         addDistributor(jobGroup, dataDistributors, topicListeners);
79     }
80
81     private JobDataDistributor createDistributor(JobGroup jobGroup) {
82         return new JobDataDistributor(jobGroup, appConfig);
83     }
84
85     private void addDistributor(JobGroup jobGroup, MultiMap<JobDataDistributor> distributors,
86             Map<String, TopicListener> topicListeners) {
87         TopicListener topicListener = topicListeners.get(jobGroup.getType().getId());
88         JobDataDistributor distributor = createDistributor(jobGroup);
89
90         distributor.start(topicListener.getFlux());
91
92         distributors.put(jobGroup.getType().getId(), jobGroup.getId(), distributor);
93     }
94
95     private synchronized void removeDistributor(JobGroup jobGroup) {
96         removeDistributor(jobGroup, dataDistributors);
97     }
98
99     private static void removeDistributor(JobGroup jobGroup, MultiMap<JobDataDistributor> distributors) {
100         JobDataDistributor distributor = distributors.remove(jobGroup.getType().getId(), jobGroup.getId());
101         if (distributor != null) {
102             logger.debug("Job removed {}", jobGroup.getId());
103             distributor.stop();
104         }
105     }
106
107 }