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.InfoTypeSubscriptions;
50 import org.oransc.enrichment.repository.InfoTypes;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.http.MediaType;
54 import org.springframework.http.ResponseEntity;
55 import org.springframework.web.bind.annotation.DeleteMapping;
56 import org.springframework.web.bind.annotation.GetMapping;
57 import org.springframework.web.bind.annotation.PathVariable;
58 import org.springframework.web.bind.annotation.PutMapping;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestParam;
61 import org.springframework.web.bind.annotation.RestController;
63 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
64 @RestController("Producer registry")
65 @Tag(name = ProducerConsts.PRODUCER_API_NAME)
66 public class ProducerController {
68 private static Gson gson = new GsonBuilder().create();
71 private InfoJobs infoJobs;
74 private InfoTypes infoTypes;
77 private InfoProducers infoProducers;
80 private InfoTypeSubscriptions typeSubscriptions;
82 @GetMapping(path = ProducerConsts.API_ROOT + "/info-types", produces = MediaType.APPLICATION_JSON_VALUE) //
83 @Operation(summary = "Info Type identifiers", description = "") //
88 description = "Info Type identifiers", //
89 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
91 public ResponseEntity<Object> getInfoTypdentifiers( //
93 List<String> result = new ArrayList<>();
94 for (InfoType infoType : this.infoTypes.getAllInfoTypes()) {
95 result.add(infoType.getId());
98 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
102 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
103 produces = MediaType.APPLICATION_JSON_VALUE)
104 @Operation(summary = "Individual Information Type", description = "")
108 responseCode = "200",
109 description = "Info Type", //
110 content = @Content(schema = @Schema(implementation = ProducerInfoTypeInfo.class))), //
112 responseCode = "404",
113 description = "Information type is not found", //
114 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
115 public ResponseEntity<Object> getInfoType( //
116 @PathVariable("infoTypeId") String infoTypeId) {
118 InfoType t = this.infoTypes.getType(infoTypeId);
119 ProducerInfoTypeInfo info = toInfoTypeInfo(t);
120 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
121 } catch (Exception e) {
122 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
127 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
128 produces = MediaType.APPLICATION_JSON_VALUE)
132 responseCode = "200",
133 description = "Type updated", //
134 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
136 responseCode = "201",
137 description = "Type created", //
138 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
140 responseCode = "400",
141 description = "Bad request", //
142 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
143 @Operation(summary = "Individual Information Type", description = "")
144 public ResponseEntity<Object> putInfoType( //
145 @PathVariable("infoTypeId") String infoTypeId, //
146 @RequestBody ProducerInfoTypeInfo registrationInfo) {
148 InfoType previousDefinition = this.infoTypes.get(infoTypeId);
149 if (registrationInfo.jobDataSchema == null) {
150 return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST);
152 InfoType newDefinition = new InfoType(infoTypeId, registrationInfo.jobDataSchema);
153 this.infoTypes.put(newDefinition);
154 this.typeSubscriptions.notifyTypeRegistered(newDefinition);
155 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
159 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
160 produces = MediaType.APPLICATION_JSON_VALUE) //
161 @Operation(summary = "Individual Information Type", description = "") //
165 responseCode = "200",
166 description = "Not used", //
167 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
169 responseCode = "204",
170 description = "Producer deleted", //
171 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
173 responseCode = "404",
174 description = "Information type is not found", //
175 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
177 responseCode = "406",
178 description = "The Information type has one or several active producers", //
179 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
181 public ResponseEntity<Object> deleteInfoType( //
182 @PathVariable("infoTypeId") String infoTypeId) {
184 InfoType type = this.infoTypes.get(infoTypeId);
186 return ErrorResponse.create("Information type not found", HttpStatus.NOT_FOUND);
188 if (!this.infoProducers.getProducersForType(type).isEmpty()) {
189 String firstProducerId = this.infoProducers.getProducersForType(type).iterator().next().getId();
190 return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.NOT_ACCEPTABLE);
192 this.infoTypes.remove(type);
193 infoJobs.getJobsForType(type).forEach(job -> infoJobs.remove(job, infoProducers)); // Delete jobs for the type
194 this.typeSubscriptions.notifyTypeRemoved(type);
195 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
198 @GetMapping(path = ProducerConsts.API_ROOT + "/info-producers", produces = MediaType.APPLICATION_JSON_VALUE)
199 @Operation(summary = "Information producer identifiers", description = "")
203 responseCode = "200",
204 description = "Information producer identifiers", //
205 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
207 public ResponseEntity<Object> getInfoProducerIdentifiers( //
209 name = "info_type_id",
211 description = "If given, only the producers for the EI Data type is returned.") //
212 @RequestParam(name = "info_type_id", required = false) String typeId //
214 List<String> result = new ArrayList<>();
215 for (InfoProducer infoProducer : typeId == null ? this.infoProducers.getAllProducers()
216 : this.infoProducers.getProducersForType(typeId)) {
217 result.add(infoProducer.getId());
220 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
224 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
225 produces = MediaType.APPLICATION_JSON_VALUE)
226 @Operation(summary = "Individual Information Producer", description = "")
230 responseCode = "200",
231 description = "Information producer", //
232 content = @Content(schema = @Schema(implementation = ProducerRegistrationInfo.class))), //
234 responseCode = "404",
235 description = "Information producer is not found", //
236 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))//
238 public ResponseEntity<Object> getInfoProducer( //
239 @PathVariable("infoProducerId") String infoProducerId) {
241 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
242 ProducerRegistrationInfo info = toProducerRegistrationInfo(producer);
243 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
244 } catch (Exception e) {
245 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
250 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/info-jobs",
251 produces = MediaType.APPLICATION_JSON_VALUE)
253 summary = "Information Job definitions",
254 description = "Information Job definitions for one Information Producer")
258 responseCode = "404",
259 description = "Information producer is not found", //
260 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
262 responseCode = "200",
263 description = "Information producer", //
264 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))), //
266 public ResponseEntity<Object> getInfoProducerJobs( //
267 @PathVariable("infoProducerId") String infoProducerId) {
269 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
270 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
271 for (InfoType type : producer.getInfoTypes()) {
272 for (InfoJob infoJob : this.infoJobs.getJobsForType(type)) {
273 ProducerJobInfo request = new ProducerJobInfo(infoJob);
274 producerJobs.add(request);
278 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
279 } catch (Exception e) {
280 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
285 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/status",
286 produces = MediaType.APPLICATION_JSON_VALUE) //
287 @Operation(summary = "Information producer status") //
291 responseCode = "200",
292 description = "Information producer status", //
293 content = @Content(schema = @Schema(implementation = ProducerStatusInfo.class))), //
295 responseCode = "404",
296 description = "Information producer is not found", //
297 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
299 public ResponseEntity<Object> getInfoProducerStatus( //
300 @PathVariable("infoProducerId") String infoProducerId) {
302 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
303 return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
304 } catch (Exception e) {
305 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
309 private ProducerStatusInfo producerStatusInfo(InfoProducer producer) {
310 var opState = producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
311 : ProducerStatusInfo.OperationalState.DISABLED;
312 return new ProducerStatusInfo(opState);
316 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}", //
317 produces = MediaType.APPLICATION_JSON_VALUE)
318 @Operation(summary = "Individual Information Producer", description = "")
322 responseCode = "201",
323 description = "Producer created", //
324 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
326 responseCode = "200",
327 description = "Producer updated", //
328 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
330 responseCode = "404",
331 description = "Producer not found", //
332 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
334 public ResponseEntity<Object> putInfoProducer( //
335 @PathVariable("infoProducerId") String infoProducerId, //
336 @RequestBody ProducerRegistrationInfo registrationInfo) {
338 validateUri(registrationInfo.jobCallbackUrl);
339 validateUri(registrationInfo.producerSupervisionCallbackUrl);
340 InfoProducer previousDefinition = this.infoProducers.get(infoProducerId);
341 this.infoProducers.registerProducer(toProducerRegistrationInfo(infoProducerId, registrationInfo));
342 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
343 } catch (Exception e) {
344 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
348 private void validateUri(String url) throws URISyntaxException, ServiceException {
349 if (url != null && !url.isEmpty()) {
350 URI uri = new URI(url);
351 if (!uri.isAbsolute()) {
352 throw new ServiceException("URI: " + url + " is not absolute", HttpStatus.CONFLICT);
355 throw new ServiceException("Missing required URL", HttpStatus.CONFLICT);
360 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
361 produces = MediaType.APPLICATION_JSON_VALUE)
362 @Operation(summary = "Individual Information Producer", description = "")
366 responseCode = "200",
367 description = "Not used", //
368 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
370 responseCode = "204",
371 description = "Producer deleted", //
372 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
374 responseCode = "404",
375 description = "Producer is not found", //
376 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
378 public ResponseEntity<Object> deleteInfoProducer(@PathVariable("infoProducerId") String infoProducerId) {
380 final InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
381 this.infoProducers.deregisterProducer(producer);
382 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
383 } catch (Exception e) {
384 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
388 private ProducerRegistrationInfo toProducerRegistrationInfo(InfoProducer p) {
389 Collection<String> types = new ArrayList<>();
390 for (InfoType type : p.getInfoTypes()) {
391 types.add(type.getId());
393 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
396 private ProducerInfoTypeInfo toInfoTypeInfo(InfoType t) {
397 return new ProducerInfoTypeInfo(t.getJobDataSchema());
400 private InfoProducers.InfoProducerRegistrationInfo toProducerRegistrationInfo(String infoProducerId,
401 ProducerRegistrationInfo info) throws ServiceException {
402 Collection<InfoType> supportedTypes = new ArrayList<>();
403 for (String typeId : info.supportedTypeIds) {
404 InfoType type = this.infoTypes.getType(typeId);
405 supportedTypes.add(type);
408 return InfoProducers.InfoProducerRegistrationInfo.builder() //
409 .id(infoProducerId) //
410 .jobCallbackUrl(info.jobCallbackUrl) //
411 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
412 .supportedTypes(supportedTypes) //