ed1c573696c90f0f461d700194e55f5fb7abc0f6
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / handler / PushPolicyHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017,2019 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.policy.pap.xacml.rest.handler;
22
23 import com.att.research.xacml.util.XACMLProperties;
24 import java.io.File;
25 import java.net.URI;
26 import java.util.List;
27 import javax.script.SimpleBindings;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.onap.policy.common.logging.eelf.PolicyLogger;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.rest.dao.CommonClassDao;
34 import org.onap.policy.rest.jpa.PolicyVersion;
35 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
36 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class PushPolicyHandler {
42     private static final Logger logger = FlexLogger.getLogger(PushPolicyHandler.class);
43
44     private static CommonClassDao commonClassDao;
45
46     @Autowired
47     public PushPolicyHandler(CommonClassDao commonClassDao) {
48         PushPolicyHandler.commonClassDao = commonClassDao;
49     }
50
51     public PushPolicyHandler() {
52         // Default Constructor
53     }
54
55     /*
56      * Get Active Version.
57      */
58     public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
59         String policyScope = request.getParameter("policyScope").replace(".", File.separator);
60         String filePrefix = request.getParameter("filePrefix");
61         String policyName = request.getParameter("policyName");
62
63         String pvName = policyScope + File.separator + filePrefix + policyName;
64         int activeVersion = 0;
65
66         // Get the Active Version to use in the ID
67         String query = "Select p from PolicyVersion p where p.policyName=:pname";
68         SimpleBindings params = new SimpleBindings();
69         params.put("pname", pvName);
70
71         @SuppressWarnings("rawtypes")
72         List result = commonClassDao.getDataByQuery(query, params);
73         PolicyVersion versionEntity = null;
74         if (!result.isEmpty()) {
75             versionEntity = (PolicyVersion) result.get(0);
76             commonClassDao.save(versionEntity);
77             activeVersion = versionEntity.getActiveVersion();
78         } else {
79             PolicyLogger.debug("No PolicyVersion using policyName found");
80         }
81
82         if (String.valueOf(activeVersion) != null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {
83             response.setStatus(HttpServletResponse.SC_OK);
84             response.addHeader("version", String.valueOf(activeVersion));
85         } else {
86             response.setStatus(HttpServletResponse.SC_NOT_FOUND);
87         }
88     }
89
90     /*
91      * Get Selected URI path.
92      */
93     public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
94         String gitPath = request.getParameter("gitPath");
95         File file = new File(gitPath);
96         PolicyLogger.debug("The fileItem is : " + file.toString());
97         URI selectedURI = file.toURI();
98         String uri = selectedURI.toString();
99         if (!uri.equalsIgnoreCase("")) {
100             response.setStatus(HttpServletResponse.SC_OK);
101             response.addHeader("selectedURI", uri);
102         } else {
103             response.setStatus(HttpServletResponse.SC_NOT_FOUND);
104         }
105     }
106
107     public boolean preSafetyCheck(StdPDPPolicy policy, String configHome){
108         return true;
109     }
110
111     public boolean preSafetyCheck(OnapPDPGroup policy, String configHome){
112         return true;
113     }
114
115     public static PushPolicyHandler getInstance() {
116         try {
117             Class<?> pushPolicyHandler = Class.forName(XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
118             PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance();
119             return instance;
120         } catch (Exception e) {
121             logger.error(e.getMessage(),e);
122         }
123         return null;
124     }
125 }