Run tomcat as onap user
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / MaintenanceController.java
1 package org.onap.vid.controller;
2
3 /*-
4  * ============LICENSE_START=======================================================
5  * VID
6  * ================================================================================
7  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
8  * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.category.AddCategoryOptionResponse;
27 import org.onap.vid.category.AddCategoryOptionsRequest;
28 import org.onap.vid.category.CategoryParameterOptionRep;
29 import org.onap.vid.category.CategoryParametersResponse;
30 import org.onap.vid.model.CategoryParameter.Family;
31 import org.onap.vid.model.CategoryParameterOption;
32 import org.onap.vid.services.CategoryParameterService;
33 import org.onap.vid.services.CategoryParameterServiceImpl;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.*;
38
39 import javax.ws.rs.ForbiddenException;
40
41 import static org.onap.vid.utils.Logging.getMethodCallerName;
42
43 /**
44  * Controler for APIs that are used only by vid operators
45  */
46
47 @RestController
48 @RequestMapping("maintenance")
49 public class MaintenanceController extends UnRestrictedBaseController {
50
51     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);
52     private CategoryParameterService categoryParameterService;
53
54     @Autowired
55     public MaintenanceController(CategoryParameterService categoryParameterService) {
56         this.categoryParameterService = categoryParameterService;
57     }
58
59     /**
60      * Add list of options to one category parameter
61      */
62     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
63     public ResponseEntity addCategoryOptions(@PathVariable String categoryName,
64         @RequestBody AddCategoryOptionsRequest option) {
65         debugStartLog();
66         try {
67             AddCategoryOptionResponse response = categoryParameterService
68                 .createCategoryParameterOptions(categoryName, option);
69             HttpStatus httpStatus = response.getErrors().isEmpty() ? HttpStatus.OK : HttpStatus.MULTI_STATUS;
70             debugEndLog(response);
71             return createResponseWithBody(httpStatus, response);
72         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
73             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
74         } catch (RuntimeException exception) {
75             LOGGER.error("failed to add option to parameter category " + categoryName, exception);
76             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
77         }
78     }
79
80     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
81     public ResponseEntity updateNameForOption(@PathVariable String categoryName,
82         @RequestBody CategoryParameterOptionRep option) {
83         debugStartLog();
84         try {
85             AddCategoryOptionResponse response = categoryParameterService
86                 .updateCategoryParameterOption(categoryName, option);
87             HttpStatus httpStatus = response.getErrors().isEmpty() ? HttpStatus.OK : HttpStatus.MULTI_STATUS;
88             debugEndLog(response);
89             return createResponseWithBody(httpStatus, response);
90         } catch (ForbiddenException exception) {
91             return createResponseWithBody(HttpStatus.FORBIDDEN, new AddCategoryOptionResponse(exception.getMessage()));
92         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException | CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) {
93             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
94
95         } catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
96             return createResponseWithBody(HttpStatus.CONFLICT, new AddCategoryOptionResponse(exception.getMessage()));
97
98         } catch (RuntimeException exception) {
99             LOGGER.error("failed to update option to parameter category " + categoryName, exception);
100             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
101         }
102     }
103
104     /**
105      * Gets the owning entity properties.
106      */
107     @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
108     public ResponseEntity getCategoryParameter(@RequestParam(value = "familyName", required = true) Family familyName) {
109         debugStartLog();
110         try {
111             CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
112             debugEndLog(response);
113             return ResponseEntity.ok().body(response);
114         } catch (RuntimeException exception) {
115             LOGGER.error("failed to retrieve category parameter list from DB.", exception);
116             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
117         }
118     }
119
120     /**
121      * Delete option of the category.
122      */
123     @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.DELETE)
124     public ResponseEntity deleteCategoryOption(@PathVariable String categoryName,
125         @RequestBody CategoryParameterOption option) {
126         debugStartLog();
127
128         try {
129             categoryParameterService.deleteCategoryOption(categoryName, option);
130             debugEndLog(HttpStatus.OK);
131             return ResponseEntity.status(HttpStatus.OK).build();
132         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
133             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
134         } catch (RuntimeException exception) {
135             LOGGER.error("failed to add/update owning entity option", exception);
136             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
137         }
138     }
139
140     private ResponseEntity createResponseWithBody(HttpStatus status, AddCategoryOptionResponse response) {
141         return ResponseEntity.status(status).body(response);
142     }
143
144     private void debugStartLog() {
145         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodCallerName());
146     }
147
148     private void debugEndLog(Object response) {
149         LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodCallerName(), response);
150     }
151 }