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