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