1 package org.onap.vid.controller;
4 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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.*;
39 import javax.ws.rs.ForbiddenException;
41 import static org.onap.vid.utils.Logging.getMethodCallerName;
44 * Controler for APIs that are used only by vid operators
48 @RequestMapping("maintenance")
49 public class MaintenanceController extends UnRestrictedBaseController {
51 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);
52 private CategoryParameterService categoryParameterService;
55 public MaintenanceController(CategoryParameterService categoryParameterService) {
56 this.categoryParameterService = categoryParameterService;
60 * Add list of options to one category parameter
62 @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
63 public ResponseEntity addCategoryOptions(@PathVariable String categoryName,
64 @RequestBody AddCategoryOptionsRequest option) {
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();
80 @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
81 public ResponseEntity updateNameForOption(@PathVariable String categoryName,
82 @RequestBody CategoryParameterOptionRep option) {
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()));
95 } catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
96 return createResponseWithBody(HttpStatus.CONFLICT, new AddCategoryOptionResponse(exception.getMessage()));
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();
105 * Gets the owning entity properties.
107 @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
108 public ResponseEntity getCategoryParameter(@RequestParam(value = "familyName", required = true) Family familyName) {
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();
121 * Delete option of the category.
123 @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.DELETE)
124 public ResponseEntity deleteCategoryOption(@PathVariable String categoryName,
125 @RequestBody CategoryParameterOption option) {
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();
140 private ResponseEntity createResponseWithBody(HttpStatus status, AddCategoryOptionResponse response) {
141 return ResponseEntity.status(status).body(response);
144 private void debugStartLog() {
145 LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodCallerName());
148 private void debugEndLog(Object response) {
149 LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodCallerName(), response);