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