Commit 0ea8555d authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

tmf 674 Rest API implementation

parent 8f43a2a0
Loading
Loading
Loading
Loading
+33 −32
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.security.Principal;
import java.util.List;
import java.util.Optional;

@@ -68,7 +70,6 @@ public interface GeographicSiteManagementApi {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }


    @Operation(summary = "Deletes a 'GeographicSite' by Id", operationId = "deleteGeographicSite", description = "", tags={ "GeographicSite", })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "204", description = "Deleted" ),
@@ -92,35 +93,35 @@ public interface GeographicSiteManagementApi {
    }


    @Operation(summary = "List or find 'GeographicSite' objects", operationId = "listGeographicSite", description = "" , tags={ "GeographicSite", })
    @ApiResponses(value = {
            @ApiResponse(responseCode ="200", description = "Ok" ),
            @ApiResponse(responseCode = "400", description = "Bad Request" ),
            @ApiResponse(responseCode = "401", description = "Unauthorized" ),
            @ApiResponse(responseCode = "403", description = "Forbidden" ),
            @ApiResponse(responseCode = "404", description = "Not Found" ),
            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
            @ApiResponse(responseCode = "409", description = "Conflict" ),
            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
    @RequestMapping(value = "/geographicSite",
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    default ResponseEntity<List<GeographicSite>> listGeographicSite(@Parameter(description = "Comma separated properties to display in response") @Valid @RequestParam(value = "fields", required = false) String fields, @Parameter(description = "Requested index for start of resources to be provided in response") @Valid @RequestParam(value = "offset", required = false) Integer offset, @Parameter(description = "Requested number of resources to be provided in response") @Valid @RequestParam(value = "limit", required = false) Integer limit) {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
            if (getAcceptHeader().get().contains("application/json")) {
                try {
                    return new ResponseEntity<>(getObjectMapper().get().readValue("[ {  \"@baseType\" : \"@baseType\",  \"validFor\" : {    \"startDateTime\" : \"2000-01-23T04:56:07.000+00:00\",    \"endDateTime\" : \"2000-01-23T04:56:07.000+00:00\"  },  \"@type\" : \"@type\",  \"name\" : \"name\",  \"description\" : \"description\",  \"href\" : \"href\",  \"id\" : \"id\",  \"@schemaLocation\" : \"@schemaLocation\",  \"objective\" : [ {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  }, {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  } ]}, {  \"@baseType\" : \"@baseType\",  \"validFor\" : {    \"startDateTime\" : \"2000-01-23T04:56:07.000+00:00\",    \"endDateTime\" : \"2000-01-23T04:56:07.000+00:00\"  },  \"@type\" : \"@type\",  \"name\" : \"name\",  \"description\" : \"description\",  \"href\" : \"href\",  \"id\" : \"id\",  \"@schemaLocation\" : \"@schemaLocation\",  \"objective\" : [ {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  }, {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  } ]} ]", List.class), HttpStatus.NOT_IMPLEMENTED);
                } catch (IOException e) {
                    log.error("Couldn't serialize response for content type application/json", e);
                    return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
            }
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default GeographicSiteManagementApi interface so no example is generated");
        }
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
//    @Operation(summary = "List or find 'GeographicSite' objects", operationId = "listGeographicSite", description = "" , tags={ "GeographicSite", })
//    @ApiResponses(value = {
//            @ApiResponse(responseCode ="200", description = "Ok" ),
//            @ApiResponse(responseCode = "400", description = "Bad Request" ),
//            @ApiResponse(responseCode = "401", description = "Unauthorized" ),
//            @ApiResponse(responseCode = "403", description = "Forbidden" ),
//            @ApiResponse(responseCode = "404", description = "Not Found" ),
//            @ApiResponse(responseCode = "405", description = "Method Not allowed" ),
//            @ApiResponse(responseCode = "409", description = "Conflict" ),
//            @ApiResponse(responseCode = "500", description = "Internal Server Error" ) })
//    @RequestMapping(value = "/geographicSite",
//            produces = { "application/json" },
//            consumes = { "application/json" },
//            method = RequestMethod.GET)
//    default ResponseEntity<List<GeographicSite>> listGeographicSite(@Parameter(description = "Comma separated properties to display in response") @Valid @RequestParam(value = "fields", required = false) String fields, @Parameter(description = "Requested index for start of resources to be provided in response") @Valid @RequestParam(value = "offset", required = false) Integer offset, @Parameter(description = "Requested number of resources to be provided in response") @Valid @RequestParam(value = "limit", required = false) Integer limit) {
//        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
//            if (getAcceptHeader().get().contains("application/json")) {
//                try {
//                    return new ResponseEntity<>(getObjectMapper().get().readValue("[ {  \"@baseType\" : \"@baseType\",  \"validFor\" : {    \"startDateTime\" : \"2000-01-23T04:56:07.000+00:00\",    \"endDateTime\" : \"2000-01-23T04:56:07.000+00:00\"  },  \"@type\" : \"@type\",  \"name\" : \"name\",  \"description\" : \"description\",  \"href\" : \"href\",  \"id\" : \"id\",  \"@schemaLocation\" : \"@schemaLocation\",  \"objective\" : [ {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  }, {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  } ]}, {  \"@baseType\" : \"@baseType\",  \"validFor\" : {    \"startDateTime\" : \"2000-01-23T04:56:07.000+00:00\",    \"endDateTime\" : \"2000-01-23T04:56:07.000+00:00\"  },  \"@type\" : \"@type\",  \"name\" : \"name\",  \"description\" : \"description\",  \"href\" : \"href\",  \"id\" : \"id\",  \"@schemaLocation\" : \"@schemaLocation\",  \"objective\" : [ {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  }, {    \"@referredType\" : \"@referredType\",    \"href\" : \"href\",    \"id\" : \"id\"  } ]} ]", List.class), HttpStatus.NOT_IMPLEMENTED);
//                } catch (IOException e) {
//                    log.error("Couldn't serialize response for content type application/json", e);
//                    return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
//                }
//            }
//        } else {
//            log.warn("ObjectMapper or HttpServletRequest not configured in default GeographicSiteManagementApi interface so no example is generated");
//        }
//        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
//    }


    @Operation(summary = "Updates partially a 'GeographicSite' by Id", operationId = "patchGeographicSite", description = "", tags={ "GeographicSite", })
@@ -137,7 +138,7 @@ public interface GeographicSiteManagementApi {
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.PATCH)
    default ResponseEntity<GeographicSite> patchGeographicSite(@Parameter(description = "Identifier of the Geographic site",required=true) @PathVariable("id") String id,@Parameter(description = "The Service Level Specification to be updated" ,required=true )  @Valid @RequestBody GeographicSite geographicSite) {
    default ResponseEntity<GeographicSite> patchGeographicalSite(@Parameter(description = "Identifier of the Geographic site",required=true) @PathVariable("id") String id,@Parameter(description = "The Service Level Specification to be updated" ,required=true )  @Valid @RequestBody GeographicSite geographicSite) {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
            if (getAcceptHeader().get().contains("application/json")) {
                try {
@@ -168,7 +169,7 @@ public interface GeographicSiteManagementApi {
            produces = { "application/json" },
            consumes = { "application/json" },
            method = RequestMethod.GET)
    default ResponseEntity<List<GeographicSite>> retrieveGeographicSite(@Parameter(description = "Identifier geographic site",required=true) @PathVariable("id") String id) {
    default ResponseEntity<List<GeographicSite>> retrieveGeographicSite() {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
            if (getAcceptHeader().get().contains("application/json")) {
                try {
+75 −0
Original line number Diff line number Diff line
package org.etsi.osl.tmf.gsm674.api;

import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import org.etsi.osl.tmf.gsm674.model.GeographicSite;
import org.etsi.osl.tmf.gsm674.reposervices.GeographicSiteManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/geographicSiteManagement/v4/")
public class GeographicSiteManagementApiController implements GeographicSiteManagementApi{

    private static final String COULD_NOT_SERIALIZE="Couldn't serialize response for content type application/json";
    private final GeographicSiteManagementService geographicSiteManagementService;

    @Autowired
    public GeographicSiteManagementApiController(GeographicSiteManagementService geographicSiteManagementService) {
        this.geographicSiteManagementService = geographicSiteManagementService;
    }

    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<List<GeographicSite>> retrieveGeographicSite() {


        try {
            return new ResponseEntity<>(geographicSiteManagementService.findAllGeographicSites(), HttpStatus.OK);

        } catch (Exception e) {
            log.error(COULD_NOT_SERIALIZE, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<GeographicSite> createGeographicSite(
            @Parameter(description = "The geographic site to be created", required = true) @Valid @RequestBody GeographicSite geographicSite
    ) {

        try {

            GeographicSite c = geographicSiteManagementService.createGeographicSite(geographicSite);

            return new ResponseEntity<>(c, HttpStatus.OK);


        } catch (Exception e) {
            log.error(COULD_NOT_SERIALIZE, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<Void> deleteGeographicSite(
            @Parameter(description = "Identifier of the geographic site", required = true) @PathVariable("id") String id) {

        try {
            return new ResponseEntity<>(geographicSiteManagementService.deleteGeographicSiteById(id), HttpStatus.OK);
        }catch (Exception e) {
            log.error(COULD_NOT_SERIALIZE, e);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


    @PreAuthorize("hasAnyAuthority('ROLE_USER')" )
    @Override
    public ResponseEntity<GeographicSite> patchGeographicalSite(
            @Parameter(description = "Identifier of the ServiceOrder", required = true) @PathVariable("id") String id,
            @Parameter(description = "The ServiceOrder to be updated", required = true) @Valid @RequestBody GeographicSite geographicSite) {
        GeographicSite c = geographicSiteManagementService.updateGeographicSite(id, geographicSite);

        return new ResponseEntity<>(c, HttpStatus.OK);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -5,9 +5,11 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface GeographicSiteManagementRepository extends CrudRepository<GeographicSite, Long>, PagingAndSortingRepository<GeographicSite, Long> {
    Optional<GeographicSite> findByUuid(String id);

}
+52 −0
Original line number Diff line number Diff line
package org.etsi.osl.tmf.gsm674.reposervices;

import org.etsi.osl.tmf.gsm674.model.GeographicSite;
import org.etsi.osl.tmf.gsm674.repo.GeographicSiteManagementRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class GeographicSiteManagementService {
    private static final Logger log = LoggerFactory.getLogger(GeographicSiteManagementService.class);


    private final GeographicSiteManagementRepository geographicSiteManagementRepository;

    @Autowired
    public GeographicSiteManagementService(GeographicSiteManagementRepository geographicSiteManagementRepository) {
        this.geographicSiteManagementRepository = geographicSiteManagementRepository;
    }

    public List<GeographicSite> findAllGeographicSites(){
        return (List<GeographicSite>) geographicSiteManagementRepository.findAll();
    }

    public Optional<GeographicSite> findGeographicSiteByUUID(String uuid){
        return geographicSiteManagementRepository.findByUuid(uuid);
    }

   public GeographicSite createGeographicSite(GeographicSite geographicSite){
        log.info("Add another geographic site: {}",geographicSite);
        return geographicSiteManagementRepository.save(geographicSite);

   }

   public GeographicSite updateGeographicSite(String id,GeographicSite geographicSite){
        Optional<GeographicSite> gs=geographicSiteManagementRepository.findByUuid(id);
       return gs.map(site -> updateFields(geographicSite, site)).orElse(null);
   }

   public Void deleteGeographicSiteById(String id){
        GeographicSite gs=geographicSiteManagementRepository.findByUuid(id).orElseThrow();
        geographicSiteManagementRepository.delete(gs);
       return null;
   }

   private GeographicSite updateFields(GeographicSite newSite, GeographicSite existingSite){


       geographicSiteManagementRepository.save(existingSite);
       return existingSite;
   }

}