1a3eb42faef18e6752336e480282a40710c63645
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / MaintenanceController.java
1 package org.onap.vid.controllers;
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 static org.onap.vid.utils.Logging.getMethodCallerName;
26
27 import javax.ws.rs.ForbiddenException;
28 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.vid.category.AddCategoryOptionResponse;
31 import org.onap.vid.category.AddCategoryOptionsRequest;
32 import org.onap.vid.category.CategoryParameterOptionRep;
33 import org.onap.vid.category.CategoryParametersResponse;
34 import org.onap.vid.model.CategoryParameter.Family;
35 import org.onap.vid.model.CategoryParameterOption;
36 import org.onap.vid.services.CategoryParameterService;
37 import org.onap.vid.services.CategoryParameterServiceImpl;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestMethod;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47
48 /**
49  * Controler for APIs that are used only by vid operators
50  */
51
52 @RestController
53 @RequestMapping("maintenance")
54 public class MaintenanceController extends UnRestrictedBaseController {
55
56     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);
57     private CategoryParameterService categoryParameterService;
58
59     @Autowired
60     public MaintenanceController(CategoryParameterService categoryParameterService) {
61         this.categoryParameterService = categoryParameterService;
62     }
63
64     /**
65      * Add list of options to one category parameter
66      */
67     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
68     public ResponseEntity addCategoryOptions(@PathVariable String categoryName,
69         @RequestBody AddCategoryOptionsRequest option) {
70         debugStartLog();
71         try {
72             AddCategoryOptionResponse response = categoryParameterService
73                 .createCategoryParameterOptions(categoryName, option);
74             HttpStatus httpStatus = response.getErrors().isEmpty() ? HttpStatus.OK : HttpStatus.MULTI_STATUS;
75             debugEndLog(response);
76             return createResponseWithBody(httpStatus, response);
77         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
78             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
79         } catch (RuntimeException exception) {
80             LOGGER.error("failed to add option to parameter category " + categoryName, exception);
81             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
82         }
83     }
84
85     @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
86     public ResponseEntity updateNameForOption(@PathVariable String categoryName,
87         @RequestBody CategoryParameterOptionRep option) {
88         debugStartLog();
89         try {
90             AddCategoryOptionResponse response = categoryParameterService
91                 .updateCategoryParameterOption(categoryName, option);
92             HttpStatus httpStatus = response.getErrors().isEmpty() ? HttpStatus.OK : HttpStatus.MULTI_STATUS;
93             debugEndLog(response);
94             return createResponseWithBody(httpStatus, response);
95         } catch (ForbiddenException exception) {
96             return createResponseWithBody(HttpStatus.FORBIDDEN, new AddCategoryOptionResponse(exception.getMessage()));
97         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException | CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) {
98             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
99
100         } catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
101             return createResponseWithBody(HttpStatus.CONFLICT, new AddCategoryOptionResponse(exception.getMessage()));
102
103         } catch (RuntimeException exception) {
104             LOGGER.error("failed to update option to parameter category " + categoryName, exception);
105             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
106         }
107     }
108
109     /**
110      * Gets the owning entity properties.
111      */
112     @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
113     public ResponseEntity getCategoryParameter(@RequestParam(value = "familyName", required = true) Family familyName) {
114         debugStartLog();
115         try {
116             CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
117             debugEndLog(response);
118             return ResponseEntity.ok().body(response);
119         } catch (RuntimeException exception) {
120             LOGGER.error("failed to retrieve category parameter list from DB.", exception);
121             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
122         }
123     }
124
125     /**
126      * Delete option of the category.
127      */
128     @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.DELETE)
129     public ResponseEntity deleteCategoryOption(@PathVariable String categoryName,
130         @RequestBody CategoryParameterOption option) {
131         debugStartLog();
132
133         try {
134             categoryParameterService.deleteCategoryOption(categoryName, option);
135             debugEndLog(HttpStatus.OK);
136             return ResponseEntity.status(HttpStatus.OK).build();
137         } catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
138             return createResponseWithBody(HttpStatus.NOT_FOUND, new AddCategoryOptionResponse(exception.getMessage()));
139         } catch (RuntimeException exception) {
140             LOGGER.error("failed to add/update owning entity option", exception);
141             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
142         }
143     }
144
145     private ResponseEntity createResponseWithBody(HttpStatus status, AddCategoryOptionResponse response) {
146         return ResponseEntity.status(status).body(response);
147     }
148
149     private void debugStartLog() {
150         LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodCallerName());
151     }
152
153     private void debugEndLog(Object response) {
154         LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodCallerName(), response);
155     }
156 }