5f691d2d3ad263fdaa84c46a149e235de75c214e
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / 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.portalapp.portal.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.portalapp.portal.logging.aop.EPAuditLog;
29 import org.openecomp.portalapp.portal.scheduler.policy.PolicyProperties;
30 import org.openecomp.portalapp.portal.scheduler.policy.PolicyResponseWrapper;
31 import org.openecomp.portalapp.portal.scheduler.policy.PolicyRestInterfaceFactory;
32 import org.openecomp.portalapp.portal.scheduler.policy.PolicyRestInterfaceIfc;
33 import org.openecomp.portalapp.portal.scheduler.policy.PolicyUtil;
34 import org.openecomp.portalapp.portal.scheduler.policy.RestObject;
35 import org.openecomp.portalapp.portal.utils.PortalConstants;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 /*import org.openecomp.vid.policy.PolicyProperties;
41 import org.openecomp.vid.policy.PolicyResponseWrapper;
42 import org.openecomp.vid.policy.PolicyRestInterfaceFactory;
43 import org.openecomp.vid.policy.PolicyRestInterfaceIfc;
44 import org.openecomp.vid.policy.PolicyUtil;*/
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51
52
53
54 /**
55  * Controller to handle Policy requests.
56  */
57
58 @RestController
59 @RequestMapping(PortalConstants.REST_AUX_API)
60 @Configuration
61 @EnableAspectJAutoProxy
62 @EPAuditLog
63 public class PolicyController implements BasicAuthenticationController{ 
64                 
65         /** The logger. */
66         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PolicyController.class);
67         
68         @RequestMapping(value="/get_policy",method = RequestMethod.POST)        
69         public ResponseEntity<String> getPolicyInfo( HttpServletRequest request, @RequestBody JSONObject policy_request) throws Exception {     
70                 
71                 logger.debug(EELFLoggerDelegate.debugLogger,  "#####################POLICY API CALL STARTED ###############"+ PolicyProperties.POLICY_GET_CONFIG_VAL);
72                 logger.debug(EELFLoggerDelegate.debugLogger,  "#####################Policy Request ###############"+policy_request.toString());
73
74                 String path = SystemProperties.getProperty(PolicyProperties.POLICY_GET_CONFIG_VAL);
75                 PolicyResponseWrapper policyResWrapper = getPolicyConfig(policy_request,path);
76                 
77                 logger.debug(EELFLoggerDelegate.debugLogger, "$$$$$$$$$$$$$$$$$$$$$$ " + new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.OK).toString());
78
79                 return ( new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.valueOf(policyResWrapper.getStatus())) );                
80         }
81         
82         protected static PolicyResponseWrapper getPolicyConfig(JSONObject request, String path) throws Exception {
83                 String methodName = "getPolicyConfig";
84                 String uuid = UUID.randomUUID().toString();
85                 logger.debug(EELFLoggerDelegate.debugLogger,  "starting getPolicyConfig ");
86
87                 try {
88                         //STARTING REST API CALL AS AN FACTORY INSTACE
89                         PolicyRestInterfaceIfc policyRestController = PolicyRestInterfaceFactory.getInstance(); 
90                         
91                         RestObject<String> restObjStr = new RestObject<String>();
92                         String str = new String();
93                         restObjStr.set(str);
94                         policyRestController.<String>Post(str, request, uuid, path, restObjStr );
95                         PolicyResponseWrapper policyRespWrapper = PolicyUtil.wrapResponse (restObjStr);
96                         logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " w=" + policyRespWrapper.getResponse());
97                         return policyRespWrapper;
98                 } catch (Exception e) { 
99                         logger.debug(EELFLoggerDelegate.debugLogger, "EXCEPTION in getPolicyConfig <== " + "." + methodName + e.toString());
100
101                         throw e;
102                 }
103         }
104 }
105