Improvments of the producer API callbacks
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / producer / ProducerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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.controllers.producer;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30
31 import java.lang.invoke.MethodHandles;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36 import org.oransc.enrichment.controllers.ErrorResponse;
37 import org.oransc.enrichment.controllers.VoidResponse;
38 import org.oransc.enrichment.controllers.consumer.ConsumerCallbacks;
39 import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo;
40 import org.oransc.enrichment.repository.EiJob;
41 import org.oransc.enrichment.repository.EiJobs;
42 import org.oransc.enrichment.repository.EiProducer;
43 import org.oransc.enrichment.repository.EiProducers;
44 import org.oransc.enrichment.repository.EiType;
45 import org.oransc.enrichment.repository.EiTypes;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.HttpStatus;
50 import org.springframework.http.MediaType;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.web.bind.annotation.DeleteMapping;
53 import org.springframework.web.bind.annotation.GetMapping;
54 import org.springframework.web.bind.annotation.PathVariable;
55 import org.springframework.web.bind.annotation.PutMapping;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RestController;
58
59 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
60 @RestController("ProducerController")
61 @Api(tags = {ProducerConsts.PRODUCER_API_NAME})
62 public class ProducerController {
63
64     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65
66     private static Gson gson = new GsonBuilder().create();
67
68     @Autowired
69     private EiJobs eiJobs;
70
71     @Autowired
72     private EiTypes eiTypes;
73
74     @Autowired
75     private EiProducers eiProducers;
76
77     @Autowired
78     ProducerCallbacks producerCallbacks;
79
80     @Autowired
81     ConsumerCallbacks consumerCallbacks;
82
83     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
84     @ApiOperation(value = "EI type identifiers", notes = "")
85     @ApiResponses(
86         value = { //
87             @ApiResponse(
88                 code = 200,
89                 message = "EI type identifiers",
90                 response = String.class,
91                 responseContainer = "List"), //
92         })
93     public ResponseEntity<Object> getEiTypeIdentifiers( //
94     ) {
95         List<String> result = new ArrayList<>();
96         for (EiType eiType : this.eiTypes.getAllEiTypes()) {
97             result.add(eiType.getId());
98         }
99
100         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
101     }
102
103     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
104     @ApiOperation(value = "Individual EI type", notes = "")
105     @ApiResponses(
106         value = { //
107             @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
108             @ApiResponse(
109                 code = 404,
110                 message = "Enrichment Information type is not found",
111                 response = ErrorResponse.ErrorInfo.class)})
112     public ResponseEntity<Object> getEiType( //
113         @PathVariable("eiTypeId") String eiTypeId) {
114         try {
115             EiType t = this.eiTypes.getType(eiTypeId);
116             ProducerEiTypeInfo info = toEiTypeInfo(t);
117             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
118         } catch (Exception e) {
119             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
120         }
121     }
122
123     @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
124     @ApiOperation(value = "EI producer identifiers", notes = "")
125     @ApiResponses(
126         value = { //
127             @ApiResponse(
128                 code = 200,
129                 message = "EI producer identifiers",
130                 response = String.class,
131                 responseContainer = "List"), //
132         })
133     public ResponseEntity<Object> getEiProducerIdentifiers( //
134     ) {
135         List<String> result = new ArrayList<>();
136         for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
137             result.add(eiProducer.getId());
138         }
139
140         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
141     }
142
143     @GetMapping(
144         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
145         produces = MediaType.APPLICATION_JSON_VALUE)
146     @ApiOperation(value = "Individual EI producer", notes = "")
147     @ApiResponses(
148         value = { //
149             @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
150             @ApiResponse(
151                 code = 404,
152                 message = "Enrichment Information producer is not found",
153                 response = ErrorResponse.ErrorInfo.class)})
154     public ResponseEntity<Object> getEiProducer( //
155         @PathVariable("eiProducerId") String eiProducerId) {
156         try {
157             EiProducer p = this.eiProducers.getProducer(eiProducerId);
158             ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
159             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
160         } catch (Exception e) {
161             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
162         }
163     }
164
165     @GetMapping(
166         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
167         produces = MediaType.APPLICATION_JSON_VALUE)
168     @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
169     @ApiResponses(
170         value = { //
171             @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
172             @ApiResponse(
173                 code = 404,
174                 message = "Enrichment Information producer is not found",
175                 response = ErrorResponse.ErrorInfo.class)})
176     public ResponseEntity<Object> getEiProducerJobs( //
177         @PathVariable("eiProducerId") String eiProducerId) {
178         try {
179             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
180             Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
181             for (EiType type : producer.getEiTypes()) {
182                 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
183                     ProducerJobInfo request = new ProducerJobInfo(eiJob);
184                     producerJobs.add(request);
185                 }
186             }
187
188             return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
189         } catch (Exception e) {
190             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
191         }
192     }
193
194     @GetMapping(
195         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
196         produces = MediaType.APPLICATION_JSON_VALUE)
197     @ApiOperation(value = "EI producer status")
198     @ApiResponses(
199         value = { //
200             @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
201             @ApiResponse(
202                 code = 404,
203                 message = "Enrichment Information producer is not found",
204                 response = ErrorResponse.ErrorInfo.class)})
205     public ResponseEntity<Object> getEiProducerStatus( //
206         @PathVariable("eiProducerId") String eiProducerId) {
207         try {
208             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
209             return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
210         } catch (Exception e) {
211             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
212         }
213     }
214
215     private ProducerStatusInfo producerStatusInfo(EiProducer producer) {
216         ProducerStatusInfo.OperationalState opState =
217             producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
218                 : ProducerStatusInfo.OperationalState.DISABLED;
219         this.logger.debug("opState {}", opState);
220         return new ProducerStatusInfo(opState);
221     }
222
223     @PutMapping(
224         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
225         produces = MediaType.APPLICATION_JSON_VALUE)
226     @ApiOperation(value = "Individual EI producer", notes = "")
227     @ApiResponses(
228         value = { //
229             @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
230             @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
231     )
232     public ResponseEntity<Object> putEiProducer( //
233         @PathVariable("eiProducerId") String eiProducerId, //
234         @RequestBody ProducerRegistrationInfo registrationInfo) {
235         try {
236             EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
237             if (previousDefinition != null) {
238                 for (EiType type : previousDefinition.getEiTypes()) {
239                     type.removeProducer(previousDefinition);
240                 }
241             }
242
243             registerProducer(eiProducerId, registrationInfo);
244             if (previousDefinition != null) {
245                 purgeTypes(previousDefinition.getEiTypes());
246             }
247
248             return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
249         } catch (Exception e) {
250             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
251         }
252     }
253
254     private void purgeTypes(Collection<EiType> types) {
255         for (EiType type : types) {
256             if (type.getProducerIds().isEmpty()) {
257                 this.eiTypes.remove(type);
258             }
259         }
260     }
261
262     @DeleteMapping(
263         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
264         produces = MediaType.APPLICATION_JSON_VALUE)
265     @ApiOperation(value = "Individual EI producer", notes = "")
266     @ApiResponses(
267         value = { //
268             @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class),
269             @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class),
270             @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)})
271     public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
272         try {
273             final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
274             this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
275             this.consumerCallbacks.notifyConsumersProducerDeleted(producer);
276             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
277         } catch (Exception e) {
278             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
279         }
280     }
281
282     private EiType registerType(ProducerEiTypeRegistrationInfo typeInfo) {
283         EiType type = this.eiTypes.get(typeInfo.eiTypeId);
284         if (type == null) {
285             type = new EiType(typeInfo.eiTypeId, typeInfo.jobDataSchema);
286             this.eiTypes.put(type);
287             this.consumerCallbacks.notifyConsumersTypeAdded(type);
288         }
289         return type;
290     }
291
292     EiProducer createProducer(Collection<EiType> types, String producerId, ProducerRegistrationInfo registrationInfo) {
293         return new EiProducer(producerId, types, registrationInfo.jobCallbackUrl,
294             registrationInfo.producerSupervisionCallbackUrl);
295     }
296
297     private EiProducer registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) {
298         ArrayList<EiType> types = new ArrayList<>();
299         for (ProducerEiTypeRegistrationInfo typeInfo : registrationInfo.types) {
300             types.add(registerType(typeInfo));
301         }
302         EiProducer producer = createProducer(types, producerId, registrationInfo);
303         this.eiProducers.put(producer);
304
305         for (EiType type : types) {
306             for (EiJob job : this.eiJobs.getJobsForType(type)) {
307                 this.producerCallbacks.notifyProducerJobStarted(producer, job) //
308                     .subscribe();
309             }
310             type.addProducer(producer);
311         }
312         return producer;
313     }
314
315     ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
316         Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
317         for (EiType type : p.getEiTypes()) {
318             types.add(toEiTypeRegistrationInfo(type));
319         }
320         return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
321     }
322
323     private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
324         return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
325     }
326
327     private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
328         return new ProducerEiTypeInfo(t.getJobDataSchema(), t.getProducerIds());
329     }
330 }