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;
36 import java.net.URISyntaxException;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
41 import org.oransc.enrichment.controllers.ErrorResponse;
42 import org.oransc.enrichment.controllers.VoidResponse;
43 import org.oransc.enrichment.exceptions.ServiceException;
44 import org.oransc.enrichment.repository.InfoJob;
45 import org.oransc.enrichment.repository.InfoJobs;
46 import org.oransc.enrichment.repository.InfoProducer;
47 import org.oransc.enrichment.repository.InfoProducers;
48 import org.oransc.enrichment.repository.InfoType;
49 import org.oransc.enrichment.repository.InfoTypes;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.MediaType;
53 import org.springframework.http.ResponseEntity;
54 import org.springframework.web.bind.annotation.DeleteMapping;
55 import org.springframework.web.bind.annotation.GetMapping;
56 import org.springframework.web.bind.annotation.PathVariable;
57 import org.springframework.web.bind.annotation.PutMapping;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestParam;
60 import org.springframework.web.bind.annotation.RestController;
62 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
63 @RestController("Producer registry")
64 @Tag(name = ProducerConsts.PRODUCER_API_NAME)
65 public class ProducerController {
67 private static Gson gson = new GsonBuilder().create();
70 private InfoJobs infoJobs;
73 private InfoTypes infoTypes;
76 private InfoProducers infoProducers;
78 @GetMapping(path = ProducerConsts.API_ROOT + "/info-types", produces = MediaType.APPLICATION_JSON_VALUE) //
79 @Operation(summary = "Info Type identifiers", description = "") //
84 description = "Info Type identifiers", //
85 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
87 public ResponseEntity<Object> getInfoTypdentifiers( //
89 List<String> result = new ArrayList<>();
90 for (InfoType infoType : this.infoTypes.getAllInfoTypes()) {
91 result.add(infoType.getId());
94 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
98 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
99 produces = MediaType.APPLICATION_JSON_VALUE)
100 @Operation(summary = "Individual Information Type", description = "")
104 responseCode = "200",
105 description = "Info Type", //
106 content = @Content(schema = @Schema(implementation = ProducerInfoTypeInfo.class))), //
108 responseCode = "404",
109 description = "Information type is not found", //
110 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
111 public ResponseEntity<Object> getInfoType( //
112 @PathVariable("infoTypeId") String infoTypeId) {
114 InfoType t = this.infoTypes.getType(infoTypeId);
115 ProducerInfoTypeInfo info = toInfoTypeInfo(t);
116 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
117 } catch (Exception e) {
118 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
123 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
124 produces = MediaType.APPLICATION_JSON_VALUE)
128 responseCode = "200",
129 description = "Type updated", //
130 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
132 responseCode = "201",
133 description = "Type created", //
134 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
136 responseCode = "400",
137 description = "Bad request", //
138 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
139 @Operation(summary = "Individual Information Type", description = "")
140 public ResponseEntity<Object> putInfoType( //
141 @PathVariable("infoTypeId") String infoTypeId, //
142 @RequestBody ProducerInfoTypeInfo registrationInfo) {
144 InfoType previousDefinition = this.infoTypes.get(infoTypeId);
145 if (registrationInfo.jobDataSchema == null) {
146 return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST);
148 this.infoTypes.put(new InfoType(infoTypeId, registrationInfo.jobDataSchema));
149 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
153 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
154 produces = MediaType.APPLICATION_JSON_VALUE) //
155 @Operation(summary = "Individual Information Type", description = "") //
159 responseCode = "200",
160 description = "Not used", //
161 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
163 responseCode = "204",
164 description = "Producer deleted", //
165 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
167 responseCode = "404",
168 description = "Information type is not found", //
169 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
171 responseCode = "406",
172 description = "The Information type has one or several active producers", //
173 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
175 public ResponseEntity<Object> deleteInfoType( //
176 @PathVariable("infoTypeId") String infoTypeId) {
178 InfoType type = this.infoTypes.get(infoTypeId);
180 return ErrorResponse.create("Information type not found", HttpStatus.NOT_FOUND);
182 if (!this.infoProducers.getProducersForType(type).isEmpty()) {
183 String firstProducerId = this.infoProducers.getProducersForType(type).iterator().next().getId();
184 return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.NOT_ACCEPTABLE);
186 this.infoTypes.remove(type);
187 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
190 @GetMapping(path = ProducerConsts.API_ROOT + "/info-producers", produces = MediaType.APPLICATION_JSON_VALUE)
191 @Operation(summary = "Information producer identifiers", description = "")
195 responseCode = "200",
196 description = "Information producer identifiers", //
197 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
199 public ResponseEntity<Object> getInfoProducerIdentifiers( //
201 name = "info_type_id",
203 description = "If given, only the producers for the EI Data type is returned.") //
204 @RequestParam(name = "info_type_id", required = false) String typeId //
206 List<String> result = new ArrayList<>();
207 for (InfoProducer infoProducer : typeId == null ? this.infoProducers.getAllProducers()
208 : this.infoProducers.getProducersForType(typeId)) {
209 result.add(infoProducer.getId());
212 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
216 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
217 produces = MediaType.APPLICATION_JSON_VALUE)
218 @Operation(summary = "Individual Information Producer", description = "")
222 responseCode = "200",
223 description = "Information producer", //
224 content = @Content(schema = @Schema(implementation = ProducerRegistrationInfo.class))), //
226 responseCode = "404",
227 description = "Information producer is not found", //
228 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))//
230 public ResponseEntity<Object> getInfoProducer( //
231 @PathVariable("infoProducerId") String infoProducerId) {
233 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
234 ProducerRegistrationInfo info = toProducerRegistrationInfo(producer);
235 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
236 } catch (Exception e) {
237 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
242 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/info-jobs",
243 produces = MediaType.APPLICATION_JSON_VALUE)
245 summary = "Information Job definitions",
246 description = "Information Job definitions for one Information Producer")
250 responseCode = "404",
251 description = "Information producer is not found", //
252 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
254 responseCode = "200",
255 description = "Information producer", //
256 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))), //
258 public ResponseEntity<Object> getInfoProducerJobs( //
259 @PathVariable("infoProducerId") String infoProducerId) {
261 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
262 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
263 for (InfoType type : producer.getInfoTypes()) {
264 for (InfoJob infoJob : this.infoJobs.getJobsForType(type)) {
265 ProducerJobInfo request = new ProducerJobInfo(infoJob);
266 producerJobs.add(request);
270 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
271 } catch (Exception e) {
272 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
277 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/status",
278 produces = MediaType.APPLICATION_JSON_VALUE) //
279 @Operation(summary = "Information producer status") //
283 responseCode = "200",
284 description = "Information producer status", //
285 content = @Content(schema = @Schema(implementation = ProducerStatusInfo.class))), //
287 responseCode = "404",
288 description = "Information producer is not found", //
289 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
291 public ResponseEntity<Object> getInfoProducerStatus( //
292 @PathVariable("infoProducerId") String infoProducerId) {
294 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
295 return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
296 } catch (Exception e) {
297 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
301 private ProducerStatusInfo producerStatusInfo(InfoProducer producer) {
302 var opState = producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
303 : ProducerStatusInfo.OperationalState.DISABLED;
304 return new ProducerStatusInfo(opState);
308 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}", //
309 produces = MediaType.APPLICATION_JSON_VALUE)
310 @Operation(summary = "Individual Information Producer", description = "")
314 responseCode = "201",
315 description = "Producer created", //
316 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
318 responseCode = "200",
319 description = "Producer updated", //
320 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
322 responseCode = "404",
323 description = "Producer not found", //
324 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
326 public ResponseEntity<Object> putInfoProducer( //
327 @PathVariable("infoProducerId") String infoProducerId, //
328 @RequestBody ProducerRegistrationInfo registrationInfo) {
330 validateUri(registrationInfo.jobCallbackUrl);
331 validateUri(registrationInfo.producerSupervisionCallbackUrl);
332 InfoProducer previousDefinition = this.infoProducers.get(infoProducerId);
333 this.infoProducers.registerProducer(toProducerRegistrationInfo(infoProducerId, registrationInfo));
334 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
335 } catch (Exception e) {
336 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
340 private void validateUri(String url) throws URISyntaxException, ServiceException {
341 if (url != null && !url.isEmpty()) {
342 URI uri = new URI(url);
343 if (!uri.isAbsolute()) {
344 throw new ServiceException("URI: " + url + " is not absolute", HttpStatus.CONFLICT);
347 throw new ServiceException("Missing required URL", HttpStatus.CONFLICT);
352 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
353 produces = MediaType.APPLICATION_JSON_VALUE)
354 @Operation(summary = "Individual Information Producer", description = "")
358 responseCode = "200",
359 description = "Not used", //
360 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
362 responseCode = "204",
363 description = "Producer deleted", //
364 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
366 responseCode = "404",
367 description = "Producer is not found", //
368 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
370 public ResponseEntity<Object> deleteInfoProducer(@PathVariable("infoProducerId") String infoProducerId) {
372 final InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
373 this.infoProducers.deregisterProducer(producer);
374 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
375 } catch (Exception e) {
376 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
380 private ProducerRegistrationInfo toProducerRegistrationInfo(InfoProducer p) {
381 Collection<String> types = new ArrayList<>();
382 for (InfoType type : p.getInfoTypes()) {
383 types.add(type.getId());
385 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
388 private ProducerInfoTypeInfo toInfoTypeInfo(InfoType t) {
389 return new ProducerInfoTypeInfo(t.getJobDataSchema());
392 private InfoProducers.InfoProducerRegistrationInfo toProducerRegistrationInfo(String infoProducerId,
393 ProducerRegistrationInfo info) throws ServiceException {
394 Collection<InfoType> supportedTypes = new ArrayList<>();
395 for (String typeId : info.supportedTypeIds) {
396 InfoType type = this.infoTypes.getType(typeId);
397 supportedTypes.add(type);
400 return InfoProducers.InfoProducerRegistrationInfo.builder() //
401 .id(infoProducerId) //
402 .jobCallbackUrl(info.jobCallbackUrl) //
403 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
404 .supportedTypes(supportedTypes) //