X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=enrichment-coordinator-service%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fenrichment%2Fcontrollers%2Fproducer%2FProducerController.java;h=7e905dab5973c79f2d2db5e55ac59c45223513a3;hb=6296305fad90e21103fa92e05e0d21a3ba1356a2;hp=af259510fc2975d635af4783788fff35f7b325c8;hpb=66e22e301bd3a4a0a03c7a62a6b399c037c297d6;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerController.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerController.java index af259510..7e905dab 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerController.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/controllers/producer/ProducerController.java @@ -1,9 +1,9 @@ /*- * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved. - * ====================================================================== + * O-RAN-SC + * %% + * Copyright (C) 2020 Nordix Foundation + * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -25,27 +25,24 @@ import com.google.gson.GsonBuilder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; -import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.oransc.enrichment.clients.ProducerCallbacks; -import org.oransc.enrichment.clients.ProducerJobInfo; import org.oransc.enrichment.controllers.ErrorResponse; -import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo; +import org.oransc.enrichment.controllers.VoidResponse; +import org.oransc.enrichment.exceptions.ServiceException; import org.oransc.enrichment.repository.EiJob; import org.oransc.enrichment.repository.EiJobs; import org.oransc.enrichment.repository.EiProducer; import org.oransc.enrichment.repository.EiProducers; import org.oransc.enrichment.repository.EiType; import org.oransc.enrichment.repository.EiTypes; -import org.oransc.enrichment.repository.ImmutableEiProducer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.oransc.enrichment.repository.ImmutableEiProducerRegistrationInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -55,6 +52,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally @@ -62,11 +60,7 @@ import org.springframework.web.bind.annotation.RestController; @Api(tags = {ProducerConsts.PRODUCER_API_NAME}) public class ProducerController { - private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - private static Gson gson = new GsonBuilder() // - .serializeNulls() // - .create(); // + private static Gson gson = new GsonBuilder().create(); @Autowired private EiJobs eiJobs; @@ -77,9 +71,6 @@ public class ProducerController { @Autowired private EiProducers eiProducers; - @Autowired - ProducerCallbacks producerCallbacks; - @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "EI type identifiers", notes = "") @ApiResponses( @@ -101,7 +92,7 @@ public class ProducerController { } @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Individual EI Type", notes = "") + @ApiOperation(value = "Individual EI type", notes = "") @ApiResponses( value = { // @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), // @@ -120,6 +111,54 @@ public class ProducerController { } } + @PutMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiResponses( + value = { // + @ApiResponse(code = 200, message = "Type updated", response = VoidResponse.class), // + @ApiResponse(code = 201, message = "Type created", response = VoidResponse.class), // + @ApiResponse(code = 400, message = "Bad request", response = ErrorResponse.ErrorInfo.class)}) + + @ApiOperation(value = "Individual EI type", notes = "") + public ResponseEntity putEiType( // + @PathVariable("eiTypeId") String eiTypeId, @RequestBody ProducerEiTypeInfo registrationInfo) { + + EiType previousDefinition = this.eiTypes.get(eiTypeId); + if (registrationInfo.jobDataSchema == null) { + return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST); + } + this.eiTypes.put(new EiType(eiTypeId, registrationInfo.jobDataSchema)); + return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK); + } + + @DeleteMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Individual EI type", notes = "") + @ApiResponses( + value = { // + @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class), + @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class), + @ApiResponse( + code = 404, + message = "Enrichment Information type is not found", + response = ErrorResponse.ErrorInfo.class), + @ApiResponse( + code = 406, + message = "The Enrichment Information type has one or several active producers", + response = ErrorResponse.ErrorInfo.class)}) + public ResponseEntity deleteEiType( // + @PathVariable("eiTypeId") String eiTypeId) { + + EiType type = this.eiTypes.get(eiTypeId); + if (type == null) { + return ErrorResponse.create("EI type not found", HttpStatus.NOT_FOUND); + } + if (!this.eiProducers.getProducersForType(type).isEmpty()) { + String firstProducerId = this.eiProducers.getProducersForType(type).iterator().next().getId(); + return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.NOT_ACCEPTABLE); + } + this.eiTypes.remove(type); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "EI producer identifiers", notes = "") @ApiResponses( @@ -131,10 +170,16 @@ public class ProducerController { responseContainer = "List"), // }) public ResponseEntity getEiProducerIdentifiers( // + @ApiParam( + name = "ei_type_id", + required = false, + value = "If given, only the producers for the EI Data type is returned.") // + @RequestParam(name = "ei_type_id", required = false) String typeId // ) { List result = new ArrayList<>(); - for (EiProducer eiProducer : this.eiProducers.getAllProducers()) { - result.add(eiProducer.id()); + for (EiProducer eiProducer : typeId == null ? this.eiProducers.getAllProducers() + : this.eiProducers.getProducersForType(typeId)) { + result.add(eiProducer.getId()); } return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); @@ -146,7 +191,7 @@ public class ProducerController { @ApiOperation(value = "Individual EI producer", notes = "") @ApiResponses( value = { // - @ApiResponse(code = 200, message = "EI Jobs", response = ProducerRegistrationInfo.class), // + @ApiResponse(code = 200, message = "EI producer", response = ProducerRegistrationInfo.class), // @ApiResponse( code = 404, message = "Enrichment Information producer is not found", @@ -168,7 +213,11 @@ public class ProducerController { @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer") @ApiResponses( value = { // - @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), // + @ApiResponse( + code = 200, + message = "EI producer", + response = ProducerJobInfo.class, + responseContainer = "List"), // @ApiResponse( code = 404, message = "Enrichment Information producer is not found", @@ -178,7 +227,7 @@ public class ProducerController { try { EiProducer producer = this.eiProducers.getProducer(eiProducerId); Collection producerJobs = new ArrayList<>(); - for (EiType type : producer.eiTypes()) { + for (EiType type : producer.getEiTypes()) { for (EiJob eiJob : this.eiJobs.getJobsForType(type)) { ProducerJobInfo request = new ProducerJobInfo(eiJob); producerJobs.add(request); @@ -191,26 +240,49 @@ public class ProducerController { } } + @GetMapping( + path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status", + produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "EI producer status") + @ApiResponses( + value = { // + @ApiResponse(code = 200, message = "EI producer status", response = ProducerStatusInfo.class), // + @ApiResponse( + code = 404, + message = "Enrichment Information producer is not found", + response = ErrorResponse.ErrorInfo.class)}) + public ResponseEntity getEiProducerStatus( // + @PathVariable("eiProducerId") String eiProducerId) { + try { + EiProducer producer = this.eiProducers.getProducer(eiProducerId); + return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK); + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.NOT_FOUND); + } + } + + private ProducerStatusInfo producerStatusInfo(EiProducer producer) { + ProducerStatusInfo.OperationalState opState = + producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED + : ProducerStatusInfo.OperationalState.DISABLED; + return new ProducerStatusInfo(opState); + } + @PutMapping( - path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}", + path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}", // produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "Individual EI producer", notes = "") @ApiResponses( value = { // - @ApiResponse(code = 201, message = "Producer created", response = void.class), // - @ApiResponse(code = 200, message = "Producer updated", response = void.class)}// + @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), // + @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)} // ) public ResponseEntity putEiProducer( // @PathVariable("eiProducerId") String eiProducerId, // @RequestBody ProducerRegistrationInfo registrationInfo) { try { - final EiProducer previousDefinition = this.eiProducers.get(eiProducerId); - if (previousDefinition != null) { - deregisterProducer(previousDefinition, false); - } - - registerProducer(eiProducerId, registrationInfo); - + EiProducer previousDefinition = this.eiProducers.get(eiProducerId); + this.eiProducers.registerProducer(toEiProducerRegistrationInfo(eiProducerId, registrationInfo)); return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); @@ -223,84 +295,45 @@ public class ProducerController { @ApiOperation(value = "Individual EI producer", notes = "") @ApiResponses( value = { // - @ApiResponse(code = 200, message = "Not used", response = void.class), - @ApiResponse(code = 204, message = "Producer deleted", response = void.class), + @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class), + @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class), @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)}) public ResponseEntity deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) { try { final EiProducer producer = this.eiProducers.getProducer(eiProducerId); - deregisterProducer(producer, true); + this.eiProducers.deregisterProducer(producer); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { return ErrorResponse.create(e, HttpStatus.NOT_FOUND); } } - private EiType registerType(ProducerEiTypeRegistrationInfo typeInfo) { - EiType type = this.eiTypes.get(typeInfo.eiTypeId); - if (type == null) { - type = new EiType(typeInfo.eiTypeId, typeInfo.jobDataSchema); - this.eiTypes.put(type); - } - return type; - - } - - EiProducer createProducer(Collection types, String producerId, ProducerRegistrationInfo registrationInfo) { - return ImmutableEiProducer.builder() // - .id(producerId) // - .eiTypes(types) // - .jobCreationCallbackUrl(registrationInfo.jobCreationCallbackUrl) // - .jobDeletionCallbackUrl(registrationInfo.jobDeletionCallbackUrl) // - .build(); - } - - private void registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) { - ArrayList types = new ArrayList<>(); - for (ProducerEiTypeRegistrationInfo typeInfo : registrationInfo.types) { - types.add(registerType(typeInfo)); - } - EiProducer producer = createProducer(types, producerId, registrationInfo); - this.eiProducers.put(producer); - - for (EiType type : types) { - for (EiJob job : this.eiJobs.getJobsForType(type)) { - this.producerCallbacks.notifyProducerJobStarted(producer, job); - } - type.addProducer(producer); + private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) { + Collection types = new ArrayList<>(); + for (EiType type : p.getEiTypes()) { + types.add(type.getId()); } + return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl()); } - private void deregisterProducer(EiProducer producer, boolean deleteJobs) { - this.eiProducers.remove(producer); - for (EiType type : producer.eiTypes()) { - boolean removed = type.removeProducer(producer) != null; - if (!removed) { - this.logger.error("Bug, no producer found"); - } - if (type.getProducerIds().isEmpty() && deleteJobs) { - this.eiTypes.remove(type); - for (EiJob job : this.eiJobs.getJobsForType(type.getId())) { - this.eiJobs.remove(job); - this.logger.warn("Deleted job {} because no producers left", job.id()); - } - } - } + private ProducerEiTypeInfo toEiTypeInfo(EiType t) { + return new ProducerEiTypeInfo(t.getJobDataSchema()); } - ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) { - Collection types = new ArrayList<>(); - for (EiType type : p.eiTypes()) { - types.add(toEiTypeRegistrationInfo(type)); + private EiProducers.EiProducerRegistrationInfo toEiProducerRegistrationInfo(String eiProducerId, + ProducerRegistrationInfo info) throws ServiceException { + Collection supportedTypes = new ArrayList<>(); + for (String typeId : info.supportedTypeIds) { + EiType type = this.eiTypes.getType(typeId); + supportedTypes.add(type); } - return new ProducerRegistrationInfo(types, p.jobCreationCallbackUrl(), p.jobDeletionCallbackUrl()); - } - private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) { - return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId()); + return ImmutableEiProducerRegistrationInfo.builder() // + .id(eiProducerId) // + .jobCallbackUrl(info.jobCallbackUrl) // + .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) // + .supportedTypes(supportedTypes) // + .build(); } - private ProducerEiTypeInfo toEiTypeInfo(EiType t) { - return new ProducerEiTypeInfo(t.getJobDataSchema(), t.getProducerIds()); - } }