Security/ Package Name changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / SchedulerAuxController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.UUID;
44
45 import javax.servlet.http.HttpServletRequest;
46
47 import org.json.simple.JSONObject;
48 import org.onap.portalapp.controller.EPRestrictedBaseController;
49 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
50 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
51 import org.onap.portalapp.portal.scheduler.SchedulerProperties;
52 import org.onap.portalapp.portal.scheduleraux.RestObject;
53 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxResponseWrapper;
54 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxRestInterfaceFactory;
55 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxRestInterfaceIfc;
56 import org.onap.portalapp.portal.scheduleraux.SchedulerAuxUtil;
57 import org.onap.portalapp.portal.utils.PortalConstants;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.springframework.context.annotation.Configuration;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 /*import org.openecomp.vid.policy.PolicyProperties;
62 import org.openecomp.vid.policy.PolicyResponseWrapper;
63 import org.openecomp.vid.policy.PolicyRestInterfaceFactory;
64 import org.openecomp.vid.policy.PolicyRestInterfaceIfc;
65 import org.openecomp.vid.policy.PolicyUtil;*/
66 import org.springframework.http.HttpStatus;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.web.bind.annotation.RequestMapping;
69 import org.springframework.web.bind.annotation.RequestMethod;
70 import org.springframework.web.bind.annotation.RestController;
71
72 /**
73  * Controller to handle Policy requests.
74  */
75
76 @RestController
77 @RequestMapping(PortalConstants.PORTAL_AUX_API)
78 @Configuration
79 @EnableAspectJAutoProxy
80 @EPAuditLog
81 public class SchedulerAuxController extends EPRestrictedBaseController {
82
83         /** The logger. */
84         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerAuxController.class);
85
86         @RequestMapping(value = "/get_policy", method = RequestMethod.GET, produces = "application/json")
87         public ResponseEntity<String> getPolicyInfo(HttpServletRequest request) throws Exception {
88                 try {
89
90                         logger.debug(EELFLoggerDelegate.debugLogger,
91                                         "SchedulerAux Controller Call Started: " + SchedulerProperties.SCHEDULERAUX_GET_CONFIG_VAL);
92                         String path = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULERAUX_GET_CONFIG_VAL);
93                         SchedulerAuxResponseWrapper policyResWrapper = getPolicyConfig(path);
94
95                         logger.debug(EELFLoggerDelegate.debugLogger, "SchedulerAux Request END : Response: ",
96                                         new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.OK).toString());
97
98                         return (new ResponseEntity<String>(policyResWrapper.getResponse(),
99                                         HttpStatus.valueOf(policyResWrapper.getStatus())));
100                 } catch (Exception e) {
101                         SchedulerAuxResponseWrapper policyResWrapper = new SchedulerAuxResponseWrapper();
102                         policyResWrapper.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
103                         policyResWrapper.setEntity(e.getMessage());
104                         logger.error(EELFLoggerDelegate.errorLogger, "Exception with getpolicy ", e);
105                         return (new ResponseEntity<String>(policyResWrapper.getResponse(), HttpStatus.INTERNAL_SERVER_ERROR));
106                 }
107         }
108
109         protected static SchedulerAuxResponseWrapper getPolicyConfig(String path) throws Exception {
110                 String methodName = "getPolicyConfig";
111                 String uuid = UUID.randomUUID().toString();
112                 logger.debug(EELFLoggerDelegate.debugLogger, "starting getPolicyConfig ");
113
114                 try {
115                         // STARTING REST API CALL AS AN FACTORY INSTACE
116                         SchedulerAuxRestInterfaceIfc policyRestController = SchedulerAuxRestInterfaceFactory.getInstance();
117                         JSONObject request = new JSONObject();
118                         String policyName = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_POLICY_NAME);
119                         request.put("policyName", policyName);
120                         RestObject<String> restObjStr = new RestObject<String>();
121                         String str = new String();
122                         restObjStr.set(str);
123                         policyRestController.<String>Post(str, request, uuid, path, restObjStr);
124                         SchedulerAuxResponseWrapper policyRespWrapper = SchedulerAuxUtil.wrapResponse(restObjStr);
125                         logger.debug(EELFLoggerDelegate.debugLogger, "Getpolicy Request END : Response: ", methodName,
126                                         policyRespWrapper.getResponse());
127                         if (policyRespWrapper.getStatus() != 200) {
128                                 String message = String.format(
129                                                 " get policy Information failed  . MethodName: %s, PolicyRespWrapperResponse: %s", methodName,
130                                                 policyRespWrapper.getResponse());
131                                 logger.error(EELFLoggerDelegate.errorLogger, message);
132                                 EPLogUtil.schedulerAccessAlarm(logger, policyRespWrapper.getStatus());
133                         }
134                         return policyRespWrapper;
135                 } catch (Exception e) {
136                         String message = String.format(" EXCEPTION in getPolicyConfig  . MethodName: %s and Exception:  %s", methodName, e);
137                         logger.error(EELFLoggerDelegate.errorLogger, "EXCEPTION in getPolicyConfig", message);
138                         throw e;
139                 }
140         }
141 }