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.ics.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.ics.controllers.ErrorResponse;
42 import org.oransc.ics.controllers.VoidResponse;
43 import org.oransc.ics.exceptions.ServiceException;
44 import org.oransc.ics.repository.InfoJob;
45 import org.oransc.ics.repository.InfoJobs;
46 import org.oransc.ics.repository.InfoProducer;
47 import org.oransc.ics.repository.InfoProducers;
48 import org.oransc.ics.repository.InfoType;
49 import org.oransc.ics.repository.InfoTypeSubscriptions;
50 import org.oransc.ics.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, description = ProducerConsts.PRODUCER_API_DESCRIPTION)
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(ProducerConsts.INFO_TYPE_ID_PATH) 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 = "Input validation failed", //
142 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))})
143 @Operation(summary = "Individual Information Type", description = "")
144 public ResponseEntity<Object> putInfoType( //
145 @PathVariable(ProducerConsts.INFO_TYPE_ID_PATH) 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 =
153 new InfoType(infoTypeId, registrationInfo.jobDataSchema, registrationInfo.typeSpecificInformation);
154 this.infoTypes.put(newDefinition);
155 this.typeSubscriptions.notifyTypeRegistered(newDefinition);
156 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
160 path = ProducerConsts.API_ROOT + "/info-types/{infoTypeId}",
161 produces = MediaType.APPLICATION_JSON_VALUE) //
162 @Operation(summary = "Individual Information Type", description = "") //
166 responseCode = "200",
167 description = "Not used", //
168 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
170 responseCode = "204",
171 description = "Producer deleted", //
172 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
174 responseCode = "404",
175 description = "Information type is not found", //
176 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
178 responseCode = "409",
179 description = "The Information type has one or several active producers", //
180 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
182 public ResponseEntity<Object> deleteInfoType( //
183 @PathVariable(ProducerConsts.INFO_TYPE_ID_PATH) String infoTypeId) {
185 InfoType type = this.infoTypes.get(infoTypeId);
187 return ErrorResponse.create("Information type not found", HttpStatus.NOT_FOUND);
189 if (!this.infoProducers.getProducersForType(type).isEmpty()) {
190 String firstProducerId = this.infoProducers.getProducersForType(type).iterator().next().getId();
191 return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.CONFLICT);
193 this.infoTypes.remove(type);
194 infoJobs.getJobsForType(type).forEach(job -> infoJobs.remove(job, infoProducers)); // Delete jobs for the type
195 this.typeSubscriptions.notifyTypeRemoved(type);
196 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
199 @GetMapping(path = ProducerConsts.API_ROOT + "/info-producers", produces = MediaType.APPLICATION_JSON_VALUE)
200 @Operation(summary = "Information producer identifiers", description = "")
204 responseCode = "200",
205 description = "Information producer identifiers", //
206 content = @Content(array = @ArraySchema(schema = @Schema(implementation = String.class)))) //
208 public ResponseEntity<Object> getInfoProducerIdentifiers( //
210 name = ProducerConsts.INFO_TYPE_ID_PARAM,
212 description = "If given, only the producers for the EI Data type is returned.") //
213 @RequestParam(name = ProducerConsts.INFO_TYPE_ID_PARAM, required = false) String typeId //
215 List<String> result = new ArrayList<>();
216 for (InfoProducer infoProducer : typeId == null ? this.infoProducers.getAllProducers()
217 : this.infoProducers.getProducersForType(typeId)) {
218 result.add(infoProducer.getId());
221 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
225 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
226 produces = MediaType.APPLICATION_JSON_VALUE)
227 @Operation(summary = "Individual Information Producer", description = "")
231 responseCode = "200",
232 description = "Information producer", //
233 content = @Content(schema = @Schema(implementation = ProducerRegistrationInfo.class))), //
235 responseCode = "404",
236 description = "Information producer is not found", //
237 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))//
239 public ResponseEntity<Object> getInfoProducer( //
240 @PathVariable(ProducerConsts.INFO_PRODUCER_ID_PATH) String infoProducerId) {
242 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
243 ProducerRegistrationInfo info = toProducerRegistrationInfo(producer);
244 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
245 } catch (Exception e) {
246 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
251 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/info-jobs",
252 produces = MediaType.APPLICATION_JSON_VALUE)
254 summary = "Information Job definitions",
255 description = "Information Job definitions for one Information Producer")
259 responseCode = "404",
260 description = "Information producer is not found", //
261 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
263 responseCode = "200",
264 description = "Information producer", //
265 content = @Content(array = @ArraySchema(schema = @Schema(implementation = ProducerJobInfo.class)))), //
267 public ResponseEntity<Object> getInfoProducerJobs( //
268 @PathVariable(ProducerConsts.INFO_PRODUCER_ID_PATH) String infoProducerId) {
270 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
271 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
272 for (InfoType type : producer.getInfoTypes()) {
273 for (InfoJob infoJob : this.infoJobs.getJobsForType(type)) {
274 ProducerJobInfo request = new ProducerJobInfo(infoJob);
275 producerJobs.add(request);
279 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
280 } catch (Exception e) {
281 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
286 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}/status",
287 produces = MediaType.APPLICATION_JSON_VALUE) //
288 @Operation(summary = "Information producer status") //
292 responseCode = "200",
293 description = "Information producer status", //
294 content = @Content(schema = @Schema(implementation = ProducerStatusInfo.class))), //
296 responseCode = "404",
297 description = "Information producer is not found", //
298 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
300 public ResponseEntity<Object> getInfoProducerStatus( //
301 @PathVariable(ProducerConsts.INFO_PRODUCER_ID_PATH) String infoProducerId) {
303 InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
304 return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
305 } catch (Exception e) {
306 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
310 private ProducerStatusInfo producerStatusInfo(InfoProducer producer) {
311 var opState = producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
312 : ProducerStatusInfo.OperationalState.DISABLED;
313 return new ProducerStatusInfo(opState);
317 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}", //
318 produces = MediaType.APPLICATION_JSON_VALUE)
319 @Operation(summary = "Individual Information Producer", description = "")
323 responseCode = "201",
324 description = "Producer created", //
325 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
327 responseCode = "200",
328 description = "Producer updated", //
329 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
331 responseCode = "404",
332 description = "Producer type not found", //
333 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
335 responseCode = "400",
336 description = "Input validation failed", //
337 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
339 public ResponseEntity<Object> putInfoProducer( //
340 @PathVariable("infoProducerId") String infoProducerId, //
341 @RequestBody ProducerRegistrationInfo registrationInfo) {
343 validateUri(registrationInfo.jobCallbackUrl);
344 validateUri(registrationInfo.producerSupervisionCallbackUrl);
345 InfoProducer previousDefinition = this.infoProducers.get(infoProducerId);
346 this.infoProducers.registerProducer(toProducerRegistrationInfo(infoProducerId, registrationInfo));
347 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
348 } catch (ServiceException e) {
349 return ErrorResponse.create(e, e.getHttpStatus());
353 private void validateUri(String url) throws ServiceException {
354 if (url != null && !url.isEmpty()) {
356 URI uri = new URI(url);
357 if (!uri.isAbsolute()) {
358 throw new ServiceException("URI: " + url + " is not absolute", HttpStatus.BAD_REQUEST);
360 } catch (URISyntaxException e) {
361 throw new ServiceException(e.getMessage(), HttpStatus.BAD_REQUEST);
364 throw new ServiceException("Missing required URL", HttpStatus.BAD_REQUEST);
369 path = ProducerConsts.API_ROOT + "/info-producers/{infoProducerId}",
370 produces = MediaType.APPLICATION_JSON_VALUE)
371 @Operation(summary = "Individual Information Producer", description = "")
375 responseCode = "200",
376 description = "Not used", //
377 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
379 responseCode = "204",
380 description = "Producer deleted", //
381 content = @Content(schema = @Schema(implementation = VoidResponse.class))),
383 responseCode = "404",
384 description = "Producer is not found", //
385 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
387 public ResponseEntity<Object> deleteInfoProducer(
388 @PathVariable(ProducerConsts.INFO_PRODUCER_ID_PATH) String infoProducerId) {
390 final InfoProducer producer = this.infoProducers.getProducer(infoProducerId);
391 this.infoProducers.deregisterProducer(producer);
392 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
393 } catch (ServiceException e) {
394 return ErrorResponse.create(e, e.getHttpStatus());
398 private ProducerRegistrationInfo toProducerRegistrationInfo(InfoProducer p) {
399 Collection<String> types = new ArrayList<>();
400 for (InfoType type : p.getInfoTypes()) {
401 types.add(type.getId());
403 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
406 private ProducerInfoTypeInfo toInfoTypeInfo(InfoType t) {
407 return new ProducerInfoTypeInfo(t.getJobDataSchema(), t.getTypeSpecificInfo());
410 private InfoProducers.InfoProducerRegistrationInfo toProducerRegistrationInfo(String infoProducerId,
411 ProducerRegistrationInfo info) throws ServiceException {
412 Collection<InfoType> supportedTypes = new ArrayList<>();
413 for (String typeId : info.supportedTypeIds) {
414 InfoType type = this.infoTypes.getType(typeId);
415 supportedTypes.add(type);
418 return InfoProducers.InfoProducerRegistrationInfo.builder() //
419 .id(infoProducerId) //
420 .jobCallbackUrl(info.jobCallbackUrl) //
421 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
422 .supportedTypes(supportedTypes) //