df1e6e4212af08b7b46897f95184908516b4310c
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / MaintenanceController.java
1 package org.onap.vid.controller;
2
3
4 import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;
5 import org.openecomp.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.*;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.ws.rs.ForbiddenException;
21 import java.util.Arrays;
22 import java.util.Collections;
23
24 import static org.onap.vid.utils.Logging.getMethodName;
25
26 /**
27  * Controler for APIs that are used only by vid operators
28  */
29
30 @RestController
31 @RequestMapping(MaintenanceController.Maintenance)
32 public class MaintenanceController extends UnRestrictedBaseController {
33
34     public static final String Maintenance = "maintenance";
35
36     @Autowired
37     protected CategoryParameterService categoryParameterService;
38     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);
39
40     /**
41      * Add list of options to one category parameter
42      * @param request the request
43      * @return the new option
44      * @throws Exception the exception
45      */
46     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
47     public ResponseEntity addCategoryOptions (
48             HttpServletRequest request, @PathVariable String categoryName, @RequestBody AddCategoryOptionsRequest option) throws Exception {
49         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
50         try {
51             AddCategoryOptionResponse response = categoryParameterService.createCategoryParameterOptions(categoryName, option);
52             HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
53             LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
54             return new ResponseEntity<>(response, httpStatus);
55         }
56         catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
57             return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
58         }
59         catch (Exception exception) {
60             LOGGER.error("failed to add option to parameter category " + categoryName, exception);
61             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
62         }
63     }
64
65     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
66     public ResponseEntity updateNameForOption (
67             HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOptionRep option) throws Exception {
68         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
69         try {
70             AddCategoryOptionResponse response = categoryParameterService.updateCategoryParameterOption(categoryName, option);
71             HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
72             LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
73             return new ResponseEntity<>(response, httpStatus);
74         }
75         catch (ForbiddenException exception) {
76             return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.FORBIDDEN);
77         }
78         catch (CategoryParameterServiceImpl.UnfoundedCategoryException|CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) {
79             return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
80         }
81         catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
82             return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.CONFLICT);
83         }
84         catch (Exception exception) {
85             LOGGER.error("failed to update option to parameter category " + categoryName, exception);
86             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
87         }
88     }
89
90     /**
91      * Gets the owning entity properties.
92      * @param request the request
93      * @return the property
94      * @throws Exception the exception
95      */
96     @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
97     public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) throws Exception {
98         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
99         try {
100             CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
101             LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
102             return new ResponseEntity<>(response, HttpStatus.OK);
103         }
104         catch (Exception exception) {
105             LOGGER.error("failed to retrieve category parameter list from DB.", exception);
106             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
107         }
108     }
109
110
111     /**
112      * Delete option of the category.
113      * @param request the request
114      * @throws Exception the exception
115      */
116     @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.POST)
117     public ResponseEntity deleteCategoryOption (
118             HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOption option) throws Exception {
119         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
120
121         try {
122             categoryParameterService.deleteCategoryOption(categoryName, option);
123             LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), HttpStatus.OK);
124             return new ResponseEntity<>(HttpStatus.OK);
125         }
126         catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
127             return new ResponseEntity<>(new AddCategoryOptionResponse(Arrays.asList(exception.getMessage())), HttpStatus.NOT_FOUND);
128         }
129         catch (Exception exception) {
130             LOGGER.error("failed to add/update owning entity option", exception);
131             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
132         }
133     }
134
135 }