Merge "Logging improvements"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / PolicyController.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.controllers;
22
23 import org.json.simple.JSONObject;
24 import org.onap.vid.policy.*;
25 import org.onap.portalsdk.core.controller.RestrictedBaseController;
26 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.web.bind.annotation.RequestBody;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RestController;
33
34 import javax.servlet.http.HttpServletRequest;
35 import java.util.UUID;
36
37 /**
38  * Controller to handle Policy requests.
39  */
40
41 @RestController
42 public class PolicyController extends RestrictedBaseController{ 
43                 
44         /** The logger. */
45         private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(PolicyController.class);
46         
47         @RequestMapping(value="/get_policy",method = RequestMethod.POST)        
48         public ResponseEntity<String> getPolicyInfo( HttpServletRequest request, @RequestBody JSONObject policy_request) {
49                 
50                 LOGGER.debug("#####################POLICY API CALL STARTED ###############"+ PolicyProperties.POLICY_GET_CONFIG_VAL);
51                 LOGGER.debug("#####################Policy Request ###############"+policy_request.toString());
52
53                 String path = PolicyProperties.getProperty(PolicyProperties.POLICY_GET_CONFIG_VAL);
54                 PolicyResponseWrapper policyResWrapper = getPolicyConfig(policy_request,path);
55                 
56                 LOGGER.debug("$$$$$$$$$$$$$$$$$$$$$$ " + new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.OK).toString());
57                 
58                 return ( new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.valueOf(policyResWrapper.getStatus())) );                
59         }
60         
61         protected static PolicyResponseWrapper getPolicyConfig(JSONObject request, String path) {
62                 String methodName = "getPolicyConfig";
63                 String uuid = UUID.randomUUID().toString();
64                 LOGGER.debug(  "starting getPolicyConfig ");
65                 
66                 try {
67                         //STARTING REST API CALL AS AN FACTORY INSTACE
68                         PolicyRestInterfaceIfc restController = PolicyRestInterfaceFactory.getInstance();       
69                         
70                         RestObject<String> restObjStr = new RestObject<String>();
71                         String str = new String();
72                         restObjStr.set(str);
73                         restController.<String>Post(str, request, uuid, path, restObjStr );
74                         PolicyResponseWrapper policyRespWrapper = PolicyUtil.wrapResponse (restObjStr);
75                         
76                         LOGGER.debug( "<== " + methodName + " w=" + policyRespWrapper.getResponse());
77                         return policyRespWrapper;
78                 } catch (Exception e) {
79                         LOGGER.debug(  "EXCEPTION in getPolicyConfig <== " + "." + methodName + e.toString());
80                         throw e;
81                 }
82         }
83 }
84