2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 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.enrichment.controllers.r1producer;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
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;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
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;
61 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
62 @RestController("Producer registry")
63 @Tag(name = ProducerConsts.PRODUCER_API_NAME)
64 public class ProducerController {
66 private static Gson gson = new GsonBuilder().create();
69 private EiJobs eiJobs;
72 private EiTypes eiTypes;
75 private EiProducers eiProducers;
77 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE) //
78 @Operation(summary = "EI type identifiers", description = "") //
83 description = "EI type identifiers", //
84 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
86 public ResponseEntity<Object> getEiTypeIdentifiers( //
88 List<String> result = new ArrayList<>();
89 for (EiType eiType : this.eiTypes.getAllInfoTypes()) {
90 result.add(eiType.getId());
93 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
96 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
97 @Operation(summary = "Individual EI type", description = "")
101 responseCode = "200",
102 description = "EI type", //
103 content = @Content(schema = @Schema(implementation = ProducerEiTypeInfo.class))), //
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) {
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);
119 @PutMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
123 responseCode = "200",
124 description = "Type updated", //
125 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
127 responseCode = "201",
128 description = "Type created", //
129 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
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) {
139 EiType previousDefinition = this.eiTypes.get(eiTypeId);
140 if (registrationInfo.jobDataSchema == null) {
141 return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST);
143 this.eiTypes.put(new EiType(eiTypeId, registrationInfo.jobDataSchema));
144 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
147 @DeleteMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) //
148 @Operation(summary = "Individual EI type", description = "") //
152 responseCode = "200",
153 description = "Not used", //
154 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
156 responseCode = "204",
157 description = "Producer deleted", //
158 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
160 responseCode = "404",
161 description = "Enrichment Information type is not found", //
162 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
164 responseCode = "406",
165 description = "The Enrichment Information type has one or several active producers", //
166 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
168 public ResponseEntity<Object> deleteEiType( //
169 @PathVariable("eiTypeId") String eiTypeId) {
171 EiType type = this.eiTypes.get(eiTypeId);
173 return ErrorResponse.create("Information type not found", HttpStatus.NOT_FOUND);
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);
179 this.eiTypes.remove(type);
180 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
183 @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
184 @Operation(summary = "EI producer identifiers", description = "")
188 responseCode = "200",
189 description = "EI producer identifiers", //
190 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
192 public ResponseEntity<Object> getEiProducerIdentifiers( //
196 description = "If given, only the producers for the EI Data type is returned.") //
197 @RequestParam(name = "ei_type_id", required = false) String typeId //
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());
205 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
209 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
210 produces = MediaType.APPLICATION_JSON_VALUE)
211 @Operation(summary = "Individual EI producer", description = "")
215 responseCode = "200",
216 description = "EI producer", //
217 content = @Content(schema = @Schema(implementation = ProducerRegistrationInfo.class))), //
219 responseCode = "404",
220 description = "Enrichment Information producer is not found", //
221 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))//
223 public ResponseEntity<Object> getEiProducer( //
224 @PathVariable("eiProducerId") String eiProducerId) {
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);
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")
241 responseCode = "404",
242 description = "Enrichment Information producer is not found", //
243 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
245 responseCode = "200",
246 description = "EI producer", //
247 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))), //
249 public ResponseEntity<Object> getEiProducerJobs( //
250 @PathVariable("eiProducerId") String eiProducerId) {
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);
261 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
262 } catch (Exception e) {
263 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
268 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
269 produces = MediaType.APPLICATION_JSON_VALUE) //
270 @Operation(summary = "EI producer status") //
274 responseCode = "200",
275 description = "EI producer status", //
276 content = @Content(schema = @Schema(implementation = ProducerStatusInfo.class))), //
278 responseCode = "404",
279 description = "Enrichment Information producer is not found", //
280 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
282 public ResponseEntity<Object> getEiProducerStatus( //
283 @PathVariable("eiProducerId") String eiProducerId) {
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);
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);
300 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}", //
301 produces = MediaType.APPLICATION_JSON_VALUE)
302 @Operation(summary = "Individual EI producer", description = "")
306 responseCode = "201",
307 description = "Producer created", //
308 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
310 responseCode = "200",
311 description = "Producer updated", //
312 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
314 responseCode = "404",
315 description = "Producer not found", //
316 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
318 public ResponseEntity<Object> putEiProducer( //
319 @PathVariable("eiProducerId") String eiProducerId, //
320 @RequestBody ProducerRegistrationInfo registrationInfo) {
322 EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
323 this.eiProducers.registerProducer(toEiProducerRegistrationInfo(eiProducerId, registrationInfo));
324 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
325 } catch (Exception e) {
326 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
331 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
332 produces = MediaType.APPLICATION_JSON_VALUE)
333 @Operation(summary = "Individual EI producer", description = "")
337 responseCode = "200",
338 description = "Not used", //
339 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
341 responseCode = "204",
342 description = "Producer deleted", //
343 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
345 responseCode = "404",
346 description = "Producer is not found", //
347 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
349 public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
351 final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
352 this.eiProducers.deregisterProducer(producer);
353 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
354 } catch (Exception e) {
355 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
359 private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
360 Collection<String> types = new ArrayList<>();
361 for (EiType type : p.getEiTypes()) {
362 types.add(type.getId());
364 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
367 private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
368 return new ProducerEiTypeInfo(t.getJobDataSchema());
371 private EiProducers.EiProducerRegistrationInfo toEiProducerRegistrationInfo(String eiProducerId,
372 ProducerRegistrationInfo info) throws ServiceException {
373 Collection<EiType> supportedTypes = new ArrayList<>();
374 for (String typeId : info.supportedTypeIds) {
375 EiType type = this.eiTypes.getType(typeId);
376 supportedTypes.add(type);
379 return ImmutableEiProducerRegistrationInfo.builder() //
381 .jobCallbackUrl(info.jobCallbackUrl) //
382 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
383 .supportedTypes(supportedTypes) //