aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/controllers/MaintenanceController.java
blob: 0976f4039fa8610ee2c63594eeeae36efad0a1bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.onap.vid.controllers;


import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.category.AddCategoryOptionResponse;
import org.onap.vid.category.AddCategoryOptionsRequest;
import org.onap.vid.category.CategoryParameterOptionRep;
import org.onap.vid.category.CategoryParametersResponse;
import org.onap.vid.model.CategoryParameter.Family;
import org.onap.vid.model.CategoryParameterOption;
import org.onap.vid.services.CategoryParameterService;
import org.onap.vid.services.CategoryParameterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.ForbiddenException;
import java.util.Arrays;
import java.util.Collections;

import static org.onap.vid.utils.Logging.getMethodCallerName;

/**
 * Controler for APIs that are used only by vid operators
 */

@RestController
@RequestMapping(MaintenanceController.MAINTENANCE)
public class MaintenanceController extends UnRestrictedBaseController {

    public static final String MAINTENANCE = "maintenance";

    @Autowired
    protected CategoryParameterService categoryParameterService;
    private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(MaintenanceController.class);

    /**
     * Add list of options to one category parameter
     * @param request the request
     * @return the new option
     * @throws Exception the exception
     */
    @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.POST)
    public ResponseEntity addCategoryOptions (
            HttpServletRequest request, @PathVariable String categoryName, @RequestBody AddCategoryOptionsRequest option) {
        debugStartLog();
        try {
            AddCategoryOptionResponse response = categoryParameterService.createCategoryParameterOptions(categoryName, option);
            HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
            debugEndLog(response);
            return new ResponseEntity<>(response, httpStatus);
        }
        catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
            return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
        }
        catch (Exception exception) {
            LOGGER.error("failed to add option to parameter category " + categoryName, exception);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/category_parameter/{categoryName}", method = RequestMethod.PUT)
    public ResponseEntity updateNameForOption (
            HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOptionRep option) {
        debugStartLog();
        try {
            AddCategoryOptionResponse response = categoryParameterService.updateCategoryParameterOption(categoryName, option);
            HttpStatus httpStatus = response.getErrors().size()>0 ? HttpStatus.MULTI_STATUS : HttpStatus.OK;
            debugEndLog(response);
            return new ResponseEntity<>(response, httpStatus);
        }
        catch (ForbiddenException exception) {
            return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.FORBIDDEN);
        }
        catch (CategoryParameterServiceImpl.UnfoundedCategoryException|CategoryParameterServiceImpl.UnfoundedCategoryOptionException exception) {
            return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.NOT_FOUND);
        }
        catch (CategoryParameterServiceImpl.AlreadyExistOptionNameException exception) {
            return new ResponseEntity<>(new AddCategoryOptionResponse(Collections.singletonList(exception.getMessage())), HttpStatus.CONFLICT);
        }
        catch (Exception exception) {
            LOGGER.error("failed to update option to parameter category " + categoryName, exception);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Gets the owning entity properties.
     * @param request the request
     * @return the property
     * @throws Exception the exception
     */
    @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
    public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) {
        debugStartLog();
        try {
            CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
            debugEndLog(response);
            return new ResponseEntity<>(response, HttpStatus.OK);
        }
        catch (Exception exception) {
            LOGGER.error("failed to retrieve category parameter list from DB.", exception);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * Delete option of the category.
     * @param request the request
     * @throws Exception the exception
     */
    @RequestMapping(value = "/delete_category_parameter/{categoryName}", method = RequestMethod.POST)
    public ResponseEntity deleteCategoryOption (
            HttpServletRequest request, @PathVariable String categoryName, @RequestBody CategoryParameterOption option) {
        debugStartLog();

        try {
            categoryParameterService.deleteCategoryOption(categoryName, option);
            debugEndLog(HttpStatus.OK);
            return new ResponseEntity<>(HttpStatus.OK);
        }
        catch (CategoryParameterServiceImpl.UnfoundedCategoryException exception) {
            return new ResponseEntity<>(new AddCategoryOptionResponse(Arrays.asList(exception.getMessage())), HttpStatus.NOT_FOUND);
        }
        catch (Exception exception) {
            LOGGER.error("failed to add/update owning entity option", exception);
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private void debugStartLog() {
        LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodCallerName());
    }

    private void debugEndLog(Object response) {
        LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodCallerName(), response);
    }
}