org.onap migration
[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.openecomp.portalsdk.core.controller.RestrictedBaseController;
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.openecomp.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 import java.text.DateFormat;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39
40 import static org.onap.vid.utils.Logging.getMethodName;
41
42 /**
43  * The Class PropertyController.
44  */
45 @RestController
46 public class PropertyController extends RestrictedBaseController{
47         
48
49         /** The logger. */
50         private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PropertyController.class);
51         
52         /** The Constant dateFormat. */
53         final protected static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
54
55         @Autowired
56         protected CategoryParameterService categoryParameterService;
57
58         
59         /**
60          * Welcome.
61          *
62          * @param request the request
63          * @return the model and view
64          */
65         @RequestMapping(value = {"/propertyhome" }, method = RequestMethod.GET)
66         public ModelAndView welcome(HttpServletRequest request) {
67                 LOGGER.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== PropertyController welcome start");
68                 return new ModelAndView(getViewName());         
69         }
70         
71         /**
72          * Gets the property.
73          *
74          * @param name the name
75          * @param defaultvalue the defaultvalue
76          * @param request the request
77          * @return the property
78          * @throws Exception the exception
79          */
80         @RequestMapping(value = "/get_property/{name}/{defaultvalue}", method = RequestMethod.GET)
81         public ResponseEntity<String> getProperty (@PathVariable("name") String name, @PathVariable("defaultvalue") String defaultvalue,
82                         HttpServletRequest request) throws Exception {
83                 
84                 String methodName = "getProperty";      
85                 ResponseEntity<String> resp = null;
86                 String pvalue = null;
87                 LOGGER.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
88                 
89                 try {
90                         // convert "_" to "." in the property name
91                         if (name == null || name.length() == 0 ) {
92                                 return ( new ResponseEntity<String> (defaultvalue, HttpStatus.OK));
93                         }
94                         // convert "_" to "." in the property name
95                         String propertyName = name.replace('_', '.');
96                         pvalue = SystemProperties.getProperty(propertyName);
97                         if ( ( pvalue == null ) || ( pvalue.length() == 0 ) ) {
98                                 pvalue = defaultvalue;
99                         }
100                         resp = new ResponseEntity<String>(pvalue, HttpStatus.OK);
101                 }
102                 catch (Exception e) {
103                         LOGGER.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
104                         LOGGER.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
105                         throw e;
106                 }
107                 LOGGER.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " returning " + pvalue);
108                 return ( resp );
109         }
110
111         /**
112          * Gets the owning entity properties.
113          * @param request the request
114          * @return the property
115          * @throws Exception the exception
116          */
117         @RequestMapping(value = "/category_parameter", method = RequestMethod.GET)
118         public ResponseEntity getCategoryParameter(HttpServletRequest request, @RequestParam(value="familyName", required = true) Family familyName) throws Exception {
119                 LOGGER.debug(EELFLoggerDelegate.debugLogger, "start {}({})", getMethodName());
120                 try {
121                         CategoryParametersResponse response = categoryParameterService.getCategoryParameters(familyName);
122                         LOGGER.debug(EELFLoggerDelegate.debugLogger, "end {}() => {}", getMethodName(), response);
123                         return new ResponseEntity<>(response, HttpStatus.OK);
124                 }
125                 catch (Exception exception) {
126                         LOGGER.error("failed to retrieve category parameter list from DB.", exception);
127                         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
128                 }
129         }
130
131
132 }