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