1 package org.onap.vid.controllers;
4 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
5 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.onap.vid.category.AddCategoryOptionResponse;
7 import org.onap.vid.category.AddCategoryOptionsRequest;
8 import org.onap.vid.category.CategoryParameterOptionRep;
9 import org.onap.vid.category.CategoryParametersResponse;
10 import org.onap.vid.model.CategoryParameter.Family;
11 import org.onap.vid.model.CategoryParameterOption;
12 import org.onap.vid.services.CategoryParameterService;
13 import org.onap.vid.services.CategoryParameterServiceImpl;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.web.bind.annotation.*;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.ws.rs.ForbiddenException;
21 import java.util.Arrays;
22 import java.util.Collections;
24 import static org.onap.vid.utils.Logging.getMethodCallerName;
27 * Controler for APIs that are used only by vid operators
31 @RequestMapping(MaintenanceController.MAINTENANCE)
32 public class MaintenanceController extends UnRestrictedBaseController {
34 public static final String MAINTENANCE = "maintenance";
37 protected CategoryParameterService categoryParameterService;
38 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);
41 * Add list of options to one category parameter
42 * @param request the request
43 * @return the new option
44 * @throws Exception the exception
46 @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
47 public ResponseEntity addCategoryOptions (
48 HttpServletRequest request, @PathVariable String categoryName, @RequestBody AddCategoryOptionsRequest option) {
51 AddCategoryOptionResponse response = categoryParameterService.createCategoryParameterOptions(categoryName, option);
52 HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
53 debugEndLog(response);
54 return new ResponseEntity<>(response, httpStatus);
56 catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
57 return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
59 catch (Exception exception) {
60 LOGGER.error("failed to add option to parameter category " + categoryName, exception);
61 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
65 @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
66 public ResponseEntity updateNameForOption (
67 HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOptionRep option) {
70 AddCategoryOptionResponse response = categoryParameterService.updateCategoryParameterOption(categoryName, option);
71 HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
72 debugEndLog(response);
73 return new ResponseEntity<>(response, httpStatus);
75 catch (ForbiddenException exception) {
76 return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.FORBIDDEN);
78 catch (CategoryParameterServiceImpl.UnfoundedCategoryException|CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) {
79 return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
81 catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
82 return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.CONFLICT);
84 catch (Exception exception) {
85 LOGGER.error("failed to update option to parameter category " + categoryName, exception);
86 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
91 * Gets the owning entity properties.
92 * @param request the request
93 * @return the property
94 * @throws Exception the exception
96 @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
97 public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) {
100 CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
101 debugEndLog(response);
102 return new ResponseEntity<>(response, HttpStatus.OK);
104 catch (Exception exception) {
105 LOGGER.error("failed to retrieve category parameter list from DB.", exception);
106 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
112 * Delete option of the category.
113 * @param request the request
114 * @throws Exception the exception
116 @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.POST)
117 public ResponseEntity deleteCategoryOption (
118 HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOption option) {
122 categoryParameterService.deleteCategoryOption(categoryName, option);
123 debugEndLog(HttpStatus.OK);
124 return new ResponseEntity<>(HttpStatus.OK);
126 catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
127 return new ResponseEntity<>(new AddCategoryOptionResponse(Arrays.asList(exception.getMessage())), HttpStatus.NOT_FOUND);
129 catch (Exception exception) {
130 LOGGER.error("failed to add/update owning entity option", exception);
131 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
135 private void debugStartLog() {
136 LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodCallerName());
139 private void debugEndLog(Object response) {
140 LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodCallerName(), response);