Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / PropertyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23 import org.onap.portalsdk.core.controller.RestrictedBaseController;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.portalsdk.core.util.SystemProperties;
26 import org.onap.vid.category.CategoryParametersResponse;
27 import org.onap.vid.model.CategoryParameter.Family;
28 import org.onap.vid.services.CategoryParameterService;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.*;
33 import org.springframework.web.servlet.ModelAndView;
34
35 import javax.servlet.http.HttpServletRequest;
36
37 import static org.onap.vid.utils.Logging.getMethodName;
38
39 /**
40  * The Class PropertyController.
41  */
42 @RestController
43 public class PropertyController extends RestrictedBaseController{
44         
45
46         /** The logger. */
47         private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PropertyController.class);
48
49         @Autowired
50         protected CategoryParameterService categoryParameterService;
51
52         
53         /**
54          * Welcome.
55          *
56          * @param request the request
57          * @return the model and view
58          */
59         @RequestMapping(value = {"/propertyhome" }, method = RequestMethod.GET)
60         public ModelAndView welcome(HttpServletRequest request) {
61                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== PropertyController welcome start");
62                 return new ModelAndView(getViewName());         
63         }
64         
65         /**
66          * Gets the property.
67          *
68          * @param name the name
69          * @param defaultvalue the defaultvalue
70          * @param request the request
71          * @return the property
72          * @throws Exception the exception
73          */
74         @RequestMapping(value = "/get_property/{name}/{defaultvalue}", method = RequestMethod.GET)
75         public ResponseEntity<String> getProperty (@PathVariable("name") String name, @PathVariable("defaultvalue") String defaultvalue,
76                         HttpServletRequest request) {
77                 
78                 String methodName = "getProperty";      
79                 ResponseEntity<String> resp = null;
80                 String pvalue = null;
81                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " start");
82                 
83                 try {
84                         // convert "_" to "." in the property name
85                         if (name == null || name.length() == 0 ) {
86                                 return ( new ResponseEntity<String> (defaultvalue, HttpStatus.OK));
87                         }
88                         // convert "_" to "." in the property name
89                         String propertyName = name.replace('_', '.');
90                         pvalue = SystemProperties.getProperty(propertyName);
91                         if ( ( pvalue == null ) || ( pvalue.length() == 0 ) ) {
92                                 pvalue = defaultvalue;
93                         }
94                         resp = new ResponseEntity<>(pvalue, HttpStatus.OK);
95                 }
96                 catch (Exception e) {
97                         LOGGER.info(EELFLoggerDelegate.errorLogger,  "<== " + "." + methodName + e.toString());
98                         LOGGER.debug(EELFLoggerDelegate.debugLogger,  "<== " + "." + methodName + e.toString());
99                         throw e;
100                 }
101                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " returning " + pvalue);
102                 return ( resp );
103         }
104
105         /**
106          * Gets the owning entity properties.
107          * @param request the request
108          * @return the property
109          * @throws Exception the exception
110          */
111         @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
112         public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) {
113                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
114                 try {
115                         CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
116                         LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
117                         return new ResponseEntity<>(response, HttpStatus.OK);
118                 }
119                 catch (Exception exception) {
120                         LOGGER.error("failed to retrieve category parameter list from DB.", exception);
121                         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
122                 }
123         }
124
125
126 }