Uplift springboot, swagger
[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.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.Parameter;
28 import io.swagger.v3.oas.annotations.media.ArraySchema;
29 import io.swagger.v3.oas.annotations.media.Content;
30 import io.swagger.v3.oas.annotations.media.Schema;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.responses.ApiResponses;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
38
39 import org.oransc.enrichment.controllers.ErrorResponse;
40 import org.oransc.enrichment.controllers.VoidResponse;
41 import org.oransc.enrichment.exceptions.ServiceException;
42 import org.oransc.enrichment.repository.EiJob;
43 import org.oransc.enrichment.repository.EiJobs;
44 import org.oransc.enrichment.repository.EiProducer;
45 import org.oransc.enrichment.repository.EiProducers;
46 import org.oransc.enrichment.repository.EiType;
47 import org.oransc.enrichment.repository.EiTypes;
48 import org.oransc.enrichment.repository.ImmutableEiProducerRegistrationInfo;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.MediaType;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.bind.annotation.DeleteMapping;
54 import org.springframework.web.bind.annotation.GetMapping;
55 import org.springframework.web.bind.annotation.PathVariable;
56 import org.springframework.web.bind.annotation.PutMapping;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestParam;
59 import org.springframework.web.bind.annotation.RestController;
60
61 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
62 @RestController("Producer registry")
63 @Tag(name = ProducerConsts.PRODUCER_API_NAME)
64 public class ProducerController {
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     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE) //
78     @Operation(summary = "EI type identifiers", description = "") //
79     @ApiResponses(
80         value = { //
81             @ApiResponse(
82                 responseCode = "200",
83                 description = "EI type identifiers", //
84                 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
85         })
86     public ResponseEntity<Object> getEiTypeIdentifiers( //
87     ) {
88         List<String> result = new ArrayList<>();
89         for (EiType eiType : this.eiTypes.getAllEiTypes()) {
90             result.add(eiType.getId());
91         }
92
93         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
94     }
95
96     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
97     @Operation(summary = "Individual EI type", description = "")
98     @ApiResponses(
99         value = { //
100             @ApiResponse(
101                 responseCode = "200",
102                 description = "EI type", //
103                 content = @Content(schema = @Schema(implementation = ProducerEiTypeInfo.class))), //
104             @ApiResponse(
105                 responseCode = "404",
106                 description = "Enrichment Information type is not found", //
107                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
108     public ResponseEntity<Object> getEiType( //
109         @PathVariable("eiTypeId") String eiTypeId) {
110         try {
111             EiType t = this.eiTypes.getType(eiTypeId);
112             ProducerEiTypeInfo info = toEiTypeInfo(t);
113             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
114         } catch (Exception e) {
115             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
116         }
117     }
118
119     @PutMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
120     @ApiResponses(
121         value = { //
122             @ApiResponse(
123                 responseCode = "200",
124                 description = "Type updated", //
125                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
126             @ApiResponse(
127                 responseCode = "201",
128                 description = "Type created", //
129                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
130             @ApiResponse(
131                 responseCode = "400",
132                 description = "Bad request", //
133                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
134     @Operation(summary = "Individual EI type", description = "")
135     public ResponseEntity<Object> putEiType( //
136         @PathVariable("eiTypeId") String eiTypeId, //
137         @RequestBody ProducerEiTypeInfo registrationInfo) {
138
139         EiType previousDefinition = this.eiTypes.get(eiTypeId);
140         if (registrationInfo.jobDataSchema == null) {
141             return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST);
142         }
143         this.eiTypes.put(new EiType(eiTypeId, registrationInfo.jobDataSchema));
144         return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
145     }
146
147     @DeleteMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) //
148     @Operation(summary = "Individual EI type", description = "") //
149     @ApiResponses(
150         value = { //
151             @ApiResponse(
152                 responseCode = "200",
153                 description = "Not used", //
154                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
155             @ApiResponse(
156                 responseCode = "204",
157                 description = "Producer deleted", //
158                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
159             @ApiResponse(
160                 responseCode = "404",
161                 description = "Enrichment Information type is not found", //
162                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
163             @ApiResponse(
164                 responseCode = "406",
165                 description = "The Enrichment Information type has one or several active producers", //
166                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
167         })
168     public ResponseEntity<Object> deleteEiType( //
169         @PathVariable("eiTypeId") String eiTypeId) {
170
171         EiType type = this.eiTypes.get(eiTypeId);
172         if (type == null) {
173             return ErrorResponse.create("EI type not found", HttpStatus.NOT_FOUND);
174         }
175         if (!this.eiProducers.getProducersForType(type).isEmpty()) {
176             String firstProducerId = this.eiProducers.getProducersForType(type).iterator().next().getId();
177             return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.NOT_ACCEPTABLE);
178         }
179         this.eiTypes.remove(type);
180         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
181     }
182
183     @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
184     @Operation(summary = "EI producer identifiers", description = "")
185     @ApiResponses(
186         value = { //
187             @ApiResponse(
188                 responseCode = "200",
189                 description = "EI producer identifiers", //
190                 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
191         })
192     public ResponseEntity<Object> getEiProducerIdentifiers( //
193         @Parameter(
194             name = "ei_type_id",
195             required = false,
196             description = "If given, only the producers for the EI Data type is returned.") //
197         @RequestParam(name = "ei_type_id", required = false) String typeId //
198     ) {
199         List<String> result = new ArrayList<>();
200         for (EiProducer eiProducer : typeId == null ? this.eiProducers.getAllProducers()
201             : this.eiProducers.getProducersForType(typeId)) {
202             result.add(eiProducer.getId());
203         }
204
205         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
206     }
207
208     @GetMapping(
209         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
210         produces = MediaType.APPLICATION_JSON_VALUE)
211     @Operation(summary = "Individual EI producer", description = "")
212     @ApiResponses(
213         value = { //
214             @ApiResponse(
215                 responseCode = "200",
216                 description = "EI producer", //
217                 content = @Content(schema = @Schema(implementation = ProducerRegistrationInfo.class))), //
218             @ApiResponse(
219                 responseCode = "404",
220                 description = "Enrichment Information producer is not found", //
221                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))//
222         })
223     public ResponseEntity<Object> getEiProducer( //
224         @PathVariable("eiProducerId") String eiProducerId) {
225         try {
226             EiProducer p = this.eiProducers.getProducer(eiProducerId);
227             ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
228             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
229         } catch (Exception e) {
230             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
231         }
232     }
233
234     @GetMapping(
235         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
236         produces = MediaType.APPLICATION_JSON_VALUE)
237     @Operation(summary = "EI job definitions", description = "EI job definitions for one EI producer")
238     @ApiResponses(
239         value = { //
240             @ApiResponse(
241                 responseCode = "404",
242                 description = "Enrichment Information producer is not found", //
243                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
244             @ApiResponse(
245                 responseCode = "200",
246                 description = "EI producer", //
247                 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))), //
248         })
249     public ResponseEntity<Object> getEiProducerJobs( //
250         @PathVariable("eiProducerId") String eiProducerId) {
251         try {
252             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
253             Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
254             for (EiType type : producer.getEiTypes()) {
255                 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
256                     ProducerJobInfo request = new ProducerJobInfo(eiJob);
257                     producerJobs.add(request);
258                 }
259             }
260
261             return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
262         } catch (Exception e) {
263             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
264         }
265     }
266
267     @GetMapping(
268         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
269         produces = MediaType.APPLICATION_JSON_VALUE) //
270     @Operation(summary = "EI producer status") //
271     @ApiResponses(
272         value = { //
273             @ApiResponse(
274                 responseCode = "200",
275                 description = "EI producer status", //
276                 content = @Content(schema = @Schema(implementation = ProducerStatusInfo.class))), //
277             @ApiResponse(
278                 responseCode = "404",
279                 description = "Enrichment Information producer is not found", //
280                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
281         })
282     public ResponseEntity<Object> getEiProducerStatus( //
283         @PathVariable("eiProducerId") String eiProducerId) {
284         try {
285             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
286             return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
287         } catch (Exception e) {
288             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
289         }
290     }
291
292     private ProducerStatusInfo producerStatusInfo(EiProducer producer) {
293         ProducerStatusInfo.OperationalState opState =
294             producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
295                 : ProducerStatusInfo.OperationalState.DISABLED;
296         return new ProducerStatusInfo(opState);
297     }
298
299     @PutMapping(
300         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}", //
301         produces = MediaType.APPLICATION_JSON_VALUE)
302     @Operation(summary = "Individual EI producer", description = "")
303     @ApiResponses(
304         value = { //
305             @ApiResponse(
306                 responseCode = "201",
307                 description = "Producer created", //
308                 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
309             @ApiResponse(
310                 responseCode = "200",
311                 description = "Producer updated", //
312                 content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
313         })
314     public ResponseEntity<Object> putEiProducer( //
315         @PathVariable("eiProducerId") String eiProducerId, //
316         @RequestBody ProducerRegistrationInfo registrationInfo) {
317         try {
318             EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
319             this.eiProducers.registerProducer(toEiProducerRegistrationInfo(eiProducerId, registrationInfo));
320             return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
321         } catch (Exception e) {
322             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
323         }
324     }
325
326     @DeleteMapping(
327         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
328         produces = MediaType.APPLICATION_JSON_VALUE)
329     @Operation(summary = "Individual EI producer", description = "")
330     @ApiResponses(
331         value = { //
332             @ApiResponse(
333                 responseCode = "200",
334                 description = "Not used", //
335                 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
336             @ApiResponse(
337                 responseCode = "204",
338                 description = "Producer deleted", //
339                 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
340             @ApiResponse(
341                 responseCode = "404",
342                 description = "Producer is not found", //
343                 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
344         })
345     public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
346         try {
347             final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
348             this.eiProducers.deregisterProducer(producer);
349             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
350         } catch (Exception e) {
351             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
352         }
353     }
354
355     private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
356         Collection<String> types = new ArrayList<>();
357         for (EiType type : p.getEiTypes()) {
358             types.add(type.getId());
359         }
360         return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
361     }
362
363     private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
364         return new ProducerEiTypeInfo(t.getJobDataSchema());
365     }
366
367     private EiProducers.EiProducerRegistrationInfo toEiProducerRegistrationInfo(String eiProducerId,
368         ProducerRegistrationInfo info) throws ServiceException {
369         Collection<EiType> supportedTypes = new ArrayList<>();
370         for (String typeId : info.supportedTypeIds) {
371             EiType type = this.eiTypes.getType(typeId);
372             supportedTypes.add(type);
373         }
374
375         return ImmutableEiProducerRegistrationInfo.builder() //
376             .id(eiProducerId) //
377             .jobCallbackUrl(info.jobCallbackUrl) //
378             .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
379             .supportedTypes(supportedTypes) //
380             .build();
381     }
382
383 }