2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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.oransc.enrichment.repository;
23 import java.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
28 import java.util.Vector;
30 import lombok.Builder;
33 import org.oransc.enrichment.controllers.a1e.A1eCallbacks;
34 import org.oransc.enrichment.controllers.r1producer.ProducerCallbacks;
35 import org.oransc.enrichment.exceptions.ServiceException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
42 * Dynamic representation of all EiProducers.
44 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 public class InfoProducers {
47 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48 private final Map<String, InfoProducer> allEiProducers = new HashMap<>();
49 private final MultiMap<InfoProducer> producersByType = new MultiMap<>();
52 private ProducerCallbacks producerCallbacks;
55 private A1eCallbacks consumerCallbacks;
58 private InfoJobs infoJobs;
62 public static class InfoProducerRegistrationInfo {
65 Collection<InfoType> supportedTypes;
67 String jobCallbackUrl;
69 String producerSupervisionCallbackUrl;
72 public InfoProducer registerProducer(InfoProducerRegistrationInfo producerInfo) {
73 final String producerId = producerInfo.getId();
74 InfoProducer previousDefinition = this.get(producerId);
75 if (previousDefinition != null) {
76 for (InfoType type : previousDefinition.getInfoTypes()) {
77 producersByType.remove(type.getId(), producerId);
79 allEiProducers.remove(producerId);
82 InfoProducer producer = createProducer(producerInfo);
83 allEiProducers.put(producer.getId(), producer);
84 for (InfoType type : producer.getInfoTypes()) {
85 producersByType.put(type.getId(), producer.getId(), producer);
88 Collection<InfoType> previousTypes =
89 previousDefinition != null ? previousDefinition.getInfoTypes() : new ArrayList<>();
91 producerCallbacks.startInfoJobs(producer, this.infoJobs) //
93 .flatMapMany(list -> consumerCallbacks.notifyJobStatus(producer.getInfoTypes())) //
95 .flatMapMany(list -> consumerCallbacks.notifyJobStatus(previousTypes)) //
101 private InfoProducer createProducer(InfoProducerRegistrationInfo producerInfo) {
102 return new InfoProducer(producerInfo.getId(), producerInfo.getSupportedTypes(),
103 producerInfo.getJobCallbackUrl(), producerInfo.getProducerSupervisionCallbackUrl());
106 public synchronized Collection<InfoProducer> getAllProducers() {
107 return new Vector<>(allEiProducers.values());
110 public synchronized InfoProducer getProducer(String id) throws ServiceException {
111 InfoProducer p = allEiProducers.get(id);
113 throw new ServiceException("Could not find Information Producer: " + id);
118 public synchronized InfoProducer get(String id) {
119 return allEiProducers.get(id);
122 public synchronized int size() {
123 return allEiProducers.size();
126 public synchronized void clear() {
127 this.allEiProducers.clear();
128 this.producersByType.clear();
131 public void deregisterProducer(InfoProducer producer) {
132 allEiProducers.remove(producer.getId());
133 for (InfoType type : producer.getInfoTypes()) {
134 if (producersByType.remove(type.getId(), producer.getId()) == null) {
135 this.logger.error("Bug, no producer found");
138 this.consumerCallbacks.notifyJobStatus(producer.getInfoTypes()) //
142 public synchronized Collection<InfoProducer> getProducersForType(InfoType type) {
143 return this.producersByType.get(type.getId());
146 public synchronized Collection<InfoProducer> getProducersForType(String typeId) {
147 return this.producersByType.get(typeId);
150 public synchronized Collection<String> getProducerIdsForType(String typeId) {
151 Collection<String> producerIds = new ArrayList<>();
152 for (InfoProducer p : this.getProducersForType(typeId)) {
153 producerIds.add(p.getId());
158 public synchronized boolean isJobEnabled(InfoJob job) {
159 for (InfoProducer producer : this.producersByType.get(job.getTypeId())) {
160 if (producer.isJobEnabled(job)) {