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.ics.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.ics.controllers.a1e.A1eCallbacks;
34 import org.oransc.ics.controllers.r1producer.ProducerCallbacks;
35 import org.oransc.ics.exceptions.ServiceException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.stereotype.Component;
43 * Dynamic representation of all EiProducers.
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
47 public class InfoProducers {
48 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49 private final Map<String, InfoProducer> allEiProducers = new HashMap<>();
50 private final MultiMap<InfoProducer> producersByType = new MultiMap<>();
53 private ProducerCallbacks producerCallbacks;
56 private A1eCallbacks consumerCallbacks;
59 private InfoJobs infoJobs;
63 public static class InfoProducerRegistrationInfo {
66 Collection<InfoType> supportedTypes;
68 String jobCallbackUrl;
70 String producerSupervisionCallbackUrl;
73 public InfoProducer registerProducer(InfoProducerRegistrationInfo producerInfo) {
74 final String producerId = producerInfo.getId();
75 InfoProducer previousDefinition = this.get(producerId);
76 if (previousDefinition != null) {
77 for (InfoType type : previousDefinition.getInfoTypes()) {
78 producersByType.remove(type.getId(), producerId);
80 allEiProducers.remove(producerId);
83 InfoProducer producer = createProducer(producerInfo);
84 allEiProducers.put(producer.getId(), producer);
85 for (InfoType type : producer.getInfoTypes()) {
86 producersByType.put(type.getId(), producer.getId(), producer);
89 Collection<InfoType> previousTypes =
90 previousDefinition != null ? previousDefinition.getInfoTypes() : new ArrayList<>();
92 producerCallbacks.startInfoJobs(producer, this.infoJobs) //
94 .flatMapMany(list -> consumerCallbacks.notifyJobStatus(producer.getInfoTypes())) //
96 .flatMapMany(list -> consumerCallbacks.notifyJobStatus(previousTypes)) //
102 private InfoProducer createProducer(InfoProducerRegistrationInfo producerInfo) {
103 return new InfoProducer(producerInfo.getId(), producerInfo.getSupportedTypes(),
104 producerInfo.getJobCallbackUrl(), producerInfo.getProducerSupervisionCallbackUrl());
107 public synchronized Collection<InfoProducer> getAllProducers() {
108 return new Vector<>(allEiProducers.values());
111 public synchronized InfoProducer getProducer(String id) throws ServiceException {
112 InfoProducer p = allEiProducers.get(id);
114 throw new ServiceException("Could not find Information Producer: " + id, HttpStatus.NOT_FOUND);
119 public synchronized InfoProducer get(String id) {
120 return allEiProducers.get(id);
123 public synchronized int size() {
124 return allEiProducers.size();
127 public synchronized void clear() {
128 this.allEiProducers.clear();
129 this.producersByType.clear();
132 public void deregisterProducer(InfoProducer producer) {
133 allEiProducers.remove(producer.getId());
134 for (InfoType type : producer.getInfoTypes()) {
135 if (producersByType.remove(type.getId(), producer.getId()) == null) {
136 this.logger.error("Bug, no producer found");
139 this.consumerCallbacks.notifyJobStatus(producer.getInfoTypes()) //
143 public synchronized Collection<InfoProducer> getProducersForType(InfoType type) {
144 return this.producersByType.get(type.getId());
147 public synchronized Collection<InfoProducer> getProducersForType(String typeId) {
148 return this.producersByType.get(typeId);
151 public synchronized Collection<String> getProducerIdsForType(String typeId) {
152 Collection<String> producerIds = new ArrayList<>();
153 for (InfoProducer p : this.getProducersForType(typeId)) {
154 producerIds.add(p.getId());
159 public synchronized boolean isJobEnabled(InfoJob job) {
160 for (InfoProducer producer : this.producersByType.get(job.getTypeId())) {
161 if (producer.isJobEnabled(job)) {