2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 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.pmproducer.tasks;
23 import java.util.HashMap;
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;
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
43 public class TopicListeners {
44 private static final Logger logger = LoggerFactory.getLogger(TopicListeners.class);
47 private final Map<String, TopicListener> topicListeners = new HashMap<>(); // Key is typeId
50 private final MultiMap<JobDataDistributor> dataDistributors = new MultiMap<>(); // Key is typeId, jobId
52 private final ApplicationConfig appConfig;
54 public TopicListeners(@Autowired ApplicationConfig appConfig, @Autowired InfoTypes types, @Autowired Jobs jobs) {
55 this.appConfig = appConfig;
57 for (InfoType type : types.getAll()) {
58 TopicListener topicConsumer = new TopicListener(appConfig, type);
59 topicListeners.put(type.getId(), topicConsumer);
62 jobs.addObserver(new Jobs.Observer() {
64 public void onJobbGroupAdded(JobGroup jobGroup) {
69 public void onJobGroupRemoved(JobGroup jobGroup) {
70 removeDistributor(jobGroup);
75 public synchronized void addJob(JobGroup jobGroup) {
76 removeDistributor(jobGroup);
77 logger.debug("Job added {}", jobGroup.getId());
78 addDistributor(jobGroup, dataDistributors, topicListeners);
81 private JobDataDistributor createDistributor(JobGroup jobGroup) {
82 return new JobDataDistributor(jobGroup, appConfig);
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);
90 distributor.start(topicListener.getFlux());
92 distributors.put(jobGroup.getType().getId(), jobGroup.getId(), distributor);
95 private synchronized void removeDistributor(JobGroup jobGroup) {
96 removeDistributor(jobGroup, dataDistributors);
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());