[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / handler / PushPolicyHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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 package org.openecomp.policy.pap.xacml.rest.handler;
21
22 import java.io.File;
23 import java.net.URI;
24 import java.util.List;
25
26 import javax.persistence.EntityManager;
27 import javax.persistence.Query;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.openecomp.policy.common.logging.eelf.MessageCodes;
32 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
33 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
34 import org.openecomp.policy.rest.jpa.PolicyVersion;
35 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
36 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
37
38 import com.att.research.xacml.util.XACMLProperties;
39
40 public class PushPolicyHandler {
41         
42         /*
43          * Get Active Version. 
44          */
45         public void getActiveVersion(HttpServletRequest request, HttpServletResponse response) {
46                 EntityManager em = null;
47                 if(XACMLPapServlet.getEmf()!=null){
48                         em = (EntityManager) XACMLPapServlet.getEmf().createEntityManager();
49                 }
50                 if (em==null){
51                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager with persistence unit: " + XACMLPapServlet.getPersistenceUnit());
52                         return;
53                 }
54                 String policyScope = request.getParameter("policyScope");
55                 String filePrefix = request.getParameter("filePrefix");
56                 String policyName = request.getParameter("policyName");
57
58                 String pvName = policyScope + File.separator + filePrefix + policyName;
59                 int activeVersion = 0;
60
61                 //Get the Active Version to use in the ID
62                 em.getTransaction().begin();
63                 Query query = em.createQuery("Select p from PolicyVersion p where p.policyName=:pname");
64                 query.setParameter("pname", pvName);
65
66                 @SuppressWarnings("rawtypes")
67                 List result = query.getResultList();
68                 PolicyVersion versionEntity = null;
69                 if (!result.isEmpty()) {
70                         versionEntity = (PolicyVersion) result.get(0);
71                         em.persist(versionEntity);
72                         activeVersion = versionEntity.getActiveVersion();
73                         em.getTransaction().commit();
74                 } else {
75                         PolicyLogger.debug("No PolicyVersion using policyName found");
76                 }
77
78                 //clean up connection
79                 em.close();
80                 if (String.valueOf(activeVersion)!=null || !String.valueOf(activeVersion).equalsIgnoreCase("")) {                                                       
81                         response.setStatus(HttpServletResponse.SC_OK);                                                          
82                         response.addHeader("version", String.valueOf(activeVersion));                                                           
83                 } else {
84                         response.setStatus(HttpServletResponse.SC_NOT_FOUND);                                                           
85                 }
86         }
87         
88         /*
89          * Get Selected URI path. 
90          */
91         public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) {
92                 String gitPath = request.getParameter("gitPath");
93                 File file = new File(gitPath);
94                 PolicyLogger.debug("The fileItem is : " + file.toString());
95                 URI selectedURI = file.toURI();
96                 String uri = selectedURI.toString();
97                 if (!uri.equalsIgnoreCase("")) {                                                        
98                         response.setStatus(HttpServletResponse.SC_OK);                                                          
99                         response.addHeader("selectedURI", uri);                                                         
100                 } else {                                                
101                         response.setStatus(HttpServletResponse.SC_NOT_FOUND);                                                           
102                 }                                               
103         }
104         
105         public boolean preSafetyCheck(StdPDPPolicy policy, String configHome){
106                 return true;
107         }
108         
109         public boolean preSafetyCheck(EcompPDPGroup policy, String configHome){
110                 return true;
111         }
112         
113         public static PushPolicyHandler getInstance() {
114                 try {
115                         Class<?> pushPolicyHandler = Class.forName(XACMLProperties.getProperty("pushPolicy.impl.className", PushPolicyHandler.class.getName()));
116                         PushPolicyHandler instance = (PushPolicyHandler) pushPolicyHandler.newInstance(); 
117                         return instance;
118                 } catch (Exception e) {
119                         PolicyLogger.error(e.getMessage());
120                 }
121                 return null;
122         }
123 }