diff options
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java b/vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java new file mode 100644 index 000000000..c961f5930 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java @@ -0,0 +1,135 @@ +package org.onap.vid.controllers; + + +import org.onap.portalsdk.core.controller.UnRestrictedBaseController; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.vid.category.AddCategoryOptionResponse; +import org.onap.vid.category.AddCategoryOptionsRequest; +import org.onap.vid.category.CategoryParameterOptionRep; +import org.onap.vid.category.CategoryParametersResponse; +import org.onap.vid.model.CategoryParameter.Family; +import org.onap.vid.model.CategoryParameterOption; +import org.onap.vid.services.CategoryParameterService; +import org.onap.vid.services.CategoryParameterServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.ForbiddenException; +import java.util.Arrays; +import java.util.Collections; + +import static org.onap.vid.utils.Logging.getMethodName; + +/** + * Controler for APIs that are used only by vid operators + */ + +@RestController +@RequestMapping(MaintenanceController.Maintenance) +public class MaintenanceController extends UnRestrictedBaseController { + + public static final String Maintenance = "maintenance"; + + @Autowired + protected CategoryParameterService categoryParameterService; + private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class); + + /** + * Add list of options to one category parameter + * @param request the request + * @return the new option + * @throws Exception the exception + */ + @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST) + public ResponseEntity addCategoryOptions ( + HttpServletRequest request, @PathVariable String categoryName, @RequestBody AddCategoryOptionsRequest option) throws Exception { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName()); + try { + AddCategoryOptionResponse response = categoryParameterService.createCategoryParameterOptions(categoryName, option); + HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK; + LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response); + return new ResponseEntity<>(response, httpStatus); + } + catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) { + return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND); + } + catch (Exception exception) { + LOGGER.error("failed to add option to parameter category " + categoryName, exception); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT) + public ResponseEntity updateNameForOption ( + HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOptionRep option) throws Exception { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName()); + try { + AddCategoryOptionResponse response = categoryParameterService.updateCategoryParameterOption(categoryName, option); + HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK; + LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response); + return new ResponseEntity<>(response, httpStatus); + } + catch (ForbiddenException exception) { + return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.FORBIDDEN); + } + catch (CategoryParameterServiceImpl.UnfoundedCategoryException|CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) { + return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND); + } + catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) { + return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.CONFLICT); + } + catch (Exception exception) { + LOGGER.error("failed to update option to parameter category " + categoryName, exception); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + /** + * Gets the owning entity properties. + * @param request the request + * @return the property + * @throws Exception the exception + */ + @RequestMapping(value = "/category_parameter", method = RequestMethod.GET) + public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) throws Exception { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName()); + try { + CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response); + return new ResponseEntity<>(response, HttpStatus.OK); + } + catch (Exception exception) { + LOGGER.error("failed to retrieve category parameter list from DB.", exception); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + + /** + * Delete option of the category. + * @param request the request + * @throws Exception the exception + */ + @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.POST) + public ResponseEntity deleteCategoryOption ( + HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOption option) throws Exception { + LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName()); + + try { + categoryParameterService.deleteCategoryOption(categoryName, option); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), HttpStatus.OK); + return new ResponseEntity<>(HttpStatus.OK); + } + catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) { + return new ResponseEntity<>(new AddCategoryOptionResponse(Arrays.asList(exception.getMessage())), HttpStatus.NOT_FOUND); + } + catch (Exception exception) { + LOGGER.error("failed to add/update owning entity option", exception); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} |