Rename enrichment coordinator service to information coordinator service
[nonrtric.git] / information-coordinator-service / src / main / java / org / oransc / ics / repository / InfoProducers.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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.oransc.ics.repository;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import lombok.Builder;
31 import lombok.Getter;
32
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;
41
42 /**
43  * Dynamic representation of all EiProducers.
44  */
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 @Component
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<>();
51
52     @Autowired
53     private ProducerCallbacks producerCallbacks;
54
55     @Autowired
56     private A1eCallbacks consumerCallbacks;
57
58     @Autowired
59     private InfoJobs infoJobs;
60
61     @Builder
62     @Getter
63     public static class InfoProducerRegistrationInfo {
64         String id;
65
66         Collection<InfoType> supportedTypes;
67
68         String jobCallbackUrl;
69
70         String producerSupervisionCallbackUrl;
71     }
72
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);
79             }
80             allEiProducers.remove(producerId);
81         }
82
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);
87         }
88
89         Collection<InfoType> previousTypes =
90             previousDefinition != null ? previousDefinition.getInfoTypes() : new ArrayList<>();
91
92         producerCallbacks.startInfoJobs(producer, this.infoJobs) //
93             .collectList() //
94             .flatMapMany(list -> consumerCallbacks.notifyJobStatus(producer.getInfoTypes())) //
95             .collectList() //
96             .flatMapMany(list -> consumerCallbacks.notifyJobStatus(previousTypes)) //
97             .subscribe();
98
99         return producer;
100     }
101
102     private InfoProducer createProducer(InfoProducerRegistrationInfo producerInfo) {
103         return new InfoProducer(producerInfo.getId(), producerInfo.getSupportedTypes(),
104             producerInfo.getJobCallbackUrl(), producerInfo.getProducerSupervisionCallbackUrl());
105     }
106
107     public synchronized Collection<InfoProducer> getAllProducers() {
108         return new Vector<>(allEiProducers.values());
109     }
110
111     public synchronized InfoProducer getProducer(String id) throws ServiceException {
112         InfoProducer p = allEiProducers.get(id);
113         if (p == null) {
114             throw new ServiceException("Could not find Information Producer: " + id, HttpStatus.NOT_FOUND);
115         }
116         return p;
117     }
118
119     public synchronized InfoProducer get(String id) {
120         return allEiProducers.get(id);
121     }
122
123     public synchronized int size() {
124         return allEiProducers.size();
125     }
126
127     public synchronized void clear() {
128         this.allEiProducers.clear();
129         this.producersByType.clear();
130     }
131
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");
137             }
138         }
139         this.consumerCallbacks.notifyJobStatus(producer.getInfoTypes()) //
140             .subscribe();
141     }
142
143     public synchronized Collection<InfoProducer> getProducersForType(InfoType type) {
144         return this.producersByType.get(type.getId());
145     }
146
147     public synchronized Collection<InfoProducer> getProducersForType(String typeId) {
148         return this.producersByType.get(typeId);
149     }
150
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());
155         }
156         return producerIds;
157     }
158
159     public synchronized boolean isJobEnabled(InfoJob job) {
160         for (InfoProducer producer : this.producersByType.get(job.getTypeId())) {
161             if (producer.isJobEnabled(job)) {
162                 return true;
163             }
164         }
165         return false;
166     }
167
168 }