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