2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.pap.xacml.rest.components;
23 import java.io.ByteArrayInputStream;
25 import java.io.FileFilter;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.StringReader;
33 import java.net.HttpURLConnection;
34 import java.net.MalformedURLException;
35 import java.net.ProtocolException;
37 import java.nio.charset.StandardCharsets;
38 import java.nio.file.FileSystems;
39 import java.nio.file.Files;
40 import java.nio.file.InvalidPathException;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.security.Key;
44 import java.sql.Timestamp;
45 import java.util.ArrayList;
46 import java.util.Base64;
47 import java.util.Calendar;
48 import java.util.Date;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.LinkedList;
53 import java.util.List;
55 import java.util.Properties;
57 import java.util.UUID;
59 import javax.crypto.Cipher;
60 import javax.crypto.spec.SecretKeySpec;
61 import javax.persistence.EntityManager;
62 import javax.persistence.EntityManagerFactory;
63 import javax.persistence.LockModeType;
64 import javax.persistence.PersistenceException;
65 import javax.persistence.Query;
66 import javax.persistence.RollbackException;
67 import javax.xml.parsers.DocumentBuilder;
68 import javax.xml.parsers.DocumentBuilderFactory;
69 import javax.xml.xpath.XPath;
70 import javax.xml.xpath.XPathFactory;
72 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
74 import org.apache.commons.io.FileUtils;
75 import org.apache.commons.io.FilenameUtils;
76 import org.apache.commons.io.IOUtils;
77 import org.openecomp.policy.common.logging.eelf.MessageCodes;
78 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
79 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
80 import org.openecomp.policy.common.logging.flexlogger.Logger;
81 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
82 import org.openecomp.policy.rest.XACMLRestProperties;
83 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
84 import org.openecomp.policy.rest.jpa.ActionBodyEntity;
85 import org.openecomp.policy.rest.jpa.ConfigurationDataEntity;
86 import org.openecomp.policy.rest.jpa.DatabaseLockEntity;
87 import org.openecomp.policy.rest.jpa.GroupEntity;
88 import org.openecomp.policy.rest.jpa.PdpEntity;
89 import org.openecomp.policy.rest.jpa.PolicyDBDaoEntity;
90 import org.openecomp.policy.rest.jpa.PolicyEntity;
91 import org.openecomp.policy.rest.jpa.PolicyVersion;
92 import org.openecomp.policy.rest.util.Webapps;
93 import org.openecomp.policy.xacml.api.pap.EcompPDP;
94 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
95 import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;
96 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
97 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
98 import org.openecomp.policy.xacml.util.XACMLPolicyScanner;
99 import org.openecomp.policy.xacml.util.XACMLPolicyWriter;
100 import org.w3c.dom.Document;
101 import org.xml.sax.InputSource;
103 import com.att.research.xacml.api.pap.PAPException;
104 import com.att.research.xacml.api.pap.PDP;
105 import com.att.research.xacml.api.pap.PDPPolicy;
106 import com.att.research.xacml.util.XACMLProperties;
108 public class PolicyDBDao {
109 private static final Logger logger = FlexLogger.getLogger(PolicyDBDao.class);
110 private List<?> otherServers;
111 private EntityManagerFactory emf;
112 private static PolicyDBDao currentInstance = null;
113 private PAPPolicyEngine papEngine;
115 public static final String JSON_CONFIG = "JSON";
116 public static final String XML_CONFIG = "XML";
117 public static final String PROPERTIES_CONFIG = "PROPERTIES";
118 public static final String OTHER_CONFIG = "OTHER";
119 public static final String AUDIT_USER = "audit";
122 * Get an instance of a PolicyDBDao. It creates one if it does not exist.
123 * Only one instance is allowed to be created per server.
124 * @param emf The EntityFactoryManager to be used for database connections
125 * @return The new instance of PolicyDBDao or throw exception if the given emf is null.
126 * @throws IllegalStateException if a PolicyDBDao has already been constructed. Call getPolicyDBDaoInstance() to get this.
128 public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf) throws Exception{
129 logger.debug("getPolicyDBDaoInstance(EntityManagerFactory emf) as getPolicyDBDaoInstance("+emf+") called");
130 if(currentInstance == null){
132 currentInstance = new PolicyDBDao(emf);
133 return currentInstance;
135 throw new IllegalStateException("The EntityManagerFactory is Null");
137 return currentInstance;
141 * Gets the current instance of PolicyDBDao.
142 * @return The instance of PolicyDBDao or throws exception if the given instance is null.
143 * @throws IllegalStateException if a PolicyDBDao instance is null. Call createPolicyDBDaoInstance(EntityManagerFactory emf) to get this.
145 public static PolicyDBDao getPolicyDBDaoInstance() throws Exception{
146 logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called");
147 if(currentInstance != null){
148 return currentInstance;
150 throw new IllegalStateException("The PolicyDBDao.currentInstance is Null. Use getPolicyDBDao(EntityManagerFactory emf)");
152 public void setPapEngine(PAPPolicyEngine papEngine2){
153 this.papEngine = (PAPPolicyEngine) papEngine2;
155 private PolicyDBDao(EntityManagerFactory emf){
156 logger.debug("PolicyDBDao(EntityManagerFactory emf) as PolicyDBDao("+emf+") called");
159 //not needed in this release
161 PolicyLogger.error("This server's PolicyDBDao instance could not be registered and may not reveive updates");
164 otherServers = getRemotePolicyDBDaoList();
165 if(logger.isDebugEnabled()){
166 logger.debug("Number of remote PolicyDBDao instances: "+otherServers.size());
168 if(otherServers.size() < 1){
169 logger.warn("List of PolicyDBDao servers is empty or could not be retrieved");
173 //not static because we are going to be using the instance's emf
174 //waitTime in ms to wait for lock, or -1 to wait forever (no)
175 private void startTransactionSynced(EntityManager entityMgr,int waitTime){
176 logger.debug("\n\nstartTransactionSynced(EntityManager entityMgr,int waitTime) as "
177 + "\n startTransactionSynced("+entityMgr+","+waitTime+") called\n\n");
178 DatabaseLockEntity lock = null;
180 entityMgr.setProperty("javax.persistence.query.timeout", waitTime);
181 entityMgr.getTransaction().begin();
183 if(logger.isDebugEnabled()){
184 Map<String,Object> properties = entityMgr.getProperties();
185 logger.debug("\n\nstartTransactionSynced():"
186 + "\n entityManager.getProperties() = " + properties
190 if(logger.isDebugEnabled()){
191 logger.debug("\n\nstartTransactionSynced():"
192 + "\n ATTEMPT to get the DB lock"
195 lock = entityMgr.find(DatabaseLockEntity.class, 1, LockModeType.PESSIMISTIC_WRITE);
196 if(logger.isDebugEnabled()){
197 logger.debug("\n\nstartTransactionSynced():"
198 + "\n GOT the DB lock"
201 } catch(Exception e){
202 System.out.println("Could not get lock entity");
203 logger.error("Exception Occured"+e);
206 throw new IllegalStateException("The lock row does not exist in the table. Please create a primary key with value = 1.");
211 * Gets the list of other registered PolicyDBDaos from the database
212 * @return List (type PolicyDBDaoEntity) of other PolicyDBDaos
214 private List<?> getRemotePolicyDBDaoList(){
215 logger.debug("getRemotePolicyDBDaoList() as getRemotePolicyDBDaoList() called");
216 List<?> policyDBDaoEntityList = new LinkedList<Object>();
217 EntityManager em = emf.createEntityManager();
218 startTransactionSynced(em, 1000);
220 Query getPolicyDBDaoEntityQuery = em.createNamedQuery("PolicyDBDaoEntity.findAll");
221 policyDBDaoEntityList = getPolicyDBDaoEntityQuery.getResultList();
223 } catch(Exception e){
224 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception querying for other registered PolicyDBDaos");
225 logger.warn("List of remote PolicyDBDaos will be empty");
228 em.getTransaction().commit();
229 } catch(Exception e){
231 em.getTransaction().rollback();
232 } catch(Exception e2){
237 return policyDBDaoEntityList;
240 public PolicyDBDaoTransaction getNewTransaction(){
241 logger.debug("getNewTransaction() as getNewTransaction() called");
242 return (PolicyDBDaoTransaction)(new PolicyDBDaoTransactionInstance());
246 * Because the normal transactions are not used in audits, we can use the same transaction
247 * mechanism to get a transaction and obtain the emlock and the DB lock. We just need to
248 * provide different transaction timeout values in ms because the audit will run longer
249 * than normal transactions.
251 public PolicyDBDaoTransaction getNewAuditTransaction(){
252 logger.debug("getNewAuditTransaction() as getNewAuditTransaction() called");
253 //Use the standard transaction wait time in ms
254 int auditWaitMs = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT));
255 //Use the (extended) audit timeout time in ms
256 int auditTimeoutMs = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_AUDIT_TIMEOUT));
257 return (PolicyDBDaoTransaction)(new PolicyDBDaoTransactionInstance(auditTimeoutMs, auditWaitMs));
262 * Checks if two strings are equal. Null strings ARE allowed.
263 * @param one A String or null to compare
264 * @param two A String or null to compare
266 private static boolean stringEquals(String one, String two){
267 logger.debug("stringEquals(String one, String two) as stringEquals("+one+", "+two+") called");
268 if(one == null && two == null){
271 if(one == null || two == null){
274 return one.equals(two);
278 * Computes the scope in dotted format based on an absolute path and a path that divides the scope.
279 * @param fullPath An absolute path including scope folders and other folders(does not have to be absolute, must just contain scope and other folders before)
280 * @param pathToExclude The path that acts as a division between the scope and the other folders
281 * @return The scope in dotted format (org.openecomp)
283 private static String computeScope(String fullPath, String pathToExclude){
284 logger.debug("computeScope(String fullPath, String pathToExclude) as computeScope("+fullPath+", "+pathToExclude+") called");
285 int excludeIndex = fullPath.indexOf(pathToExclude);
286 String scopePath = fullPath.substring(excludeIndex+pathToExclude.length());
287 String scope = scopePath.replace('\\', '.');
288 scope = scope.replace('/', '.');
289 if(scope.charAt(0) == '.'){
290 scope = scope.substring(1);
292 if(scope.charAt(scope.length()-1) == '.'){
293 scope = scope.substring(0, scope.length()-1);
299 * Returns the url of this local pap server, removing the username and password, if they are present
300 * @return The url of this local pap server
302 private String[] getPapUrlUserPass(){
303 logger.debug("getPapUrl() as getPapUrl() called");
304 String url = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
308 return splitPapUrlUserPass(url);
312 private String[] splitPapUrlUserPass(String url){
313 String[] urlUserPass = new String[3];
314 String[] commaSplit = url.split(",");
315 urlUserPass[0] = commaSplit[0];
316 if(commaSplit.length > 2){
317 urlUserPass[1] = commaSplit[1];
318 urlUserPass[2] = commaSplit[2];
320 if(urlUserPass[1] == null || urlUserPass[1].equals("")){
321 String usernamePropertyValue = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
322 if(usernamePropertyValue != null){
323 urlUserPass[1] = usernamePropertyValue;
326 if(urlUserPass[2] == null || urlUserPass[2].equals("")){
327 String passwordPropertyValue = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS);
328 if(passwordPropertyValue != null){
329 urlUserPass[2] = passwordPropertyValue;
332 //if there is no comma, for some reason there is no username and password, so don't try to cut them off
336 private static String encryptPassword(String password) throws Exception{
337 Cipher cipher = Cipher.getInstance("AES");
338 cipher.init(Cipher.ENCRYPT_MODE, aesKey());
339 byte[] encryption = cipher.doFinal(password.getBytes("UTF-8"));
340 System.out.println(encryption);
341 return new String(Base64.getMimeEncoder().encode(encryption),"UTF-8");
344 private static String decryptPassword(String encryptedPassword) throws Exception{
345 Cipher cipher = Cipher.getInstance("AES");
346 cipher.init(Cipher.DECRYPT_MODE, aesKey());
347 byte[] password = cipher.doFinal(Base64.getDecoder().decode(encryptedPassword.getBytes("UTF-8")));
348 return new String(password,"UTF-8");
350 private static Key aesKey(){
351 byte[] aesValue = (new String("njrmbklcxtoplawf")).getBytes();
352 return new SecretKeySpec(aesValue,"AES");
355 * Register the PolicyDBDao instance in the PolicyDBDaoEntity table
356 * @return Boolean, were we able to register?
358 private boolean register(){
359 logger.debug("register() as register() called");
360 String[] url = getPapUrlUserPass();
361 EntityManager em = emf.createEntityManager();
363 startTransactionSynced(em, 1000);
364 } catch(IllegalStateException e){
365 logger.debug ("\nPolicyDBDao.register() caught an IllegalStateException: \n" +e + "\n");
366 DatabaseLockEntity lock;
367 lock = em.find(DatabaseLockEntity.class, 1);
369 lock = new DatabaseLockEntity();
374 em.getTransaction().commit();
376 } catch(Exception e2){
377 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "COULD NOT CREATE DATABASELOCK ROW. WILL TRY ONE MORE TIME");
378 e2.printStackTrace();
381 em = emf.createEntityManager();
383 startTransactionSynced(em, 1000);
384 } catch(Exception e3){
385 String msg = "DATABASE LOCKING NOT WORKING. CONCURRENCY CONTROL NOT WORKING";
386 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e3, "PolicyDBDao", msg);
387 throw new IllegalStateException("msg" + "\n" + e3);
391 logger.debug("\nPolicyDBDao.register. Database locking and concurrency control is initialized\n");
392 PolicyDBDaoEntity foundPolicyDBDaoEntity = em.find(PolicyDBDaoEntity.class, url[0]);
393 Query getPolicyDBDaoEntityQuery = em.createQuery("SELECT e FROM PolicyDBDaoEntity e WHERE e.policyDBDaoUrl=:url");
394 getPolicyDBDaoEntityQuery.setParameter("url", url[0]);
395 if(foundPolicyDBDaoEntity == null){
396 PolicyDBDaoEntity newPolicyDBDaoEntity = new PolicyDBDaoEntity();
397 em.persist(newPolicyDBDaoEntity);
398 newPolicyDBDaoEntity.setPolicyDBDaoUrl(url[0]);
399 newPolicyDBDaoEntity.setDescription("PAP server at "+url[0]);
400 newPolicyDBDaoEntity.setUsername(url[1]);
402 newPolicyDBDaoEntity.setPassword(encryptPassword(url[2]));
403 } catch(Exception e){
404 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
407 em.getTransaction().commit();
408 } catch(Exception e){
410 em.getTransaction().rollback();
411 } catch(Exception e2){
412 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not add new PolicyDBDao to the database");
416 //just want to update in order to change modified date
417 String encryptedPassword = null;
419 encryptedPassword = encryptPassword(url[2]);
420 } catch(Exception e){
421 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
423 if(url[1] != null && !stringEquals(url[1], foundPolicyDBDaoEntity.getUsername())){
424 foundPolicyDBDaoEntity.setUsername(url[1]);
426 if(encryptedPassword != null && !stringEquals(encryptedPassword, foundPolicyDBDaoEntity.getPassword())){
427 foundPolicyDBDaoEntity.setPassword(encryptedPassword);
429 foundPolicyDBDaoEntity.preUpdate();
431 em.getTransaction().commit();
432 } catch(Exception e){
434 em.getTransaction().rollback();
435 } catch(Exception e2){
436 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not update PolicyDBDao in the database");
441 logger.debug("\nPolicyDBDao.register(). Success!!\n");
444 public void notifyOthers(long entityId,String entityType){
445 notifyOthers(entityId,entityType,null);
447 public void notifyOthers(long entityId, String entityType, String newGroupId){
448 logger.debug("notifyOthers(long entityId, String entityType, long newGroupId) as notifyOthers("+entityId+","+entityType+","+newGroupId+") called");
449 LinkedList<Thread> notifyThreads = new LinkedList<Thread>();
451 //we're going to run notiftions in parellel threads to speed things up
452 for(Object obj : otherServers){
454 Thread newNotifyThread = new Thread(new NotifyOtherThread(obj, entityId, entityType, newGroupId));
456 newNotifyThread.start();
458 notifyThreads.add(newNotifyThread);
461 //we want to wait for all notifications to complete or timeout before we unlock the interface and allow more changes
462 for(Thread t : notifyThreads){
465 } catch (Exception e) {
466 logger.warn("Could not join a notifcation thread");
473 private class NotifyOtherThread implements Runnable {
474 public NotifyOtherThread(Object obj, long entityId, String entityType, String newGroupId){
476 this.entityId = entityId;
477 this.entityType = entityType;
478 this.newGroupId = newGroupId;
481 private long entityId;
482 private String entityType;
483 private String newGroupId;
486 //naming of 'o' is for backwards compatibility with the rest of the function
487 PolicyDBDaoEntity dbdEntity = (PolicyDBDaoEntity)obj;
488 String o = dbdEntity.getPolicyDBDaoUrl();
489 String username = dbdEntity.getUsername();
492 password = decryptPassword(dbdEntity.getPassword());
493 } catch(Exception e){
494 //if we can't decrypt, might as well try it anyway
495 password = dbdEntity.getPassword();
497 Base64.Encoder encoder = Base64.getEncoder();
498 String encoding = encoder.encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));
499 HttpURLConnection connection = null;
500 UUID requestID = UUID.randomUUID();
503 String papUrl = getPapUrlUserPass()[0];
505 papUrl = "undefined";
507 logger.debug("We are going to try to notify "+o);
508 //is this our own url?
511 ourUrl = splitPapUrlUserPass((String)o)[0];
518 if(papUrl.equals(ourUrl)){
519 logger.debug(((String)o)+" is our url, skipping notify");
522 if(newGroupId == null){
523 url = new URL(((String)o)+"?policydbdaourl="+papUrl+"&entityid="+entityId+"&entitytype="+entityType);
525 url = new URL(((String)o)+"?policydbdaourl="+papUrl+"&entityid="+entityId+"&entitytype="+entityType+"&extradata="+newGroupId);
527 } catch (MalformedURLException e) {
528 logger.warn("Caught MalformedURLException on: new URL()", e);
532 // Open up the connection
534 logger.debug("Connecting with url: "+url);
536 connection = (HttpURLConnection)url.openConnection();
537 } catch (Exception e) {
538 logger.warn("Caught exception on: url.openConnection()",e);
542 // Setup our method and headers
545 connection.setRequestMethod("PUT");
546 } catch (ProtocolException e) {
547 //why would this error ever occur?
548 logger.warn("Caught ProtocolException on connection.setRequestMethod(\"PUT\");",e);
551 connection.setRequestProperty("Authorization", "Basic " + encoding);
552 connection.setRequestProperty("Accept", "text/x-java-properties");
553 connection.setRequestProperty("Content-Type", "text/x-java-properties");
554 connection.setRequestProperty("requestID", requestID.toString());
557 readTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_NOTIFY_TIMEOUT));
559 } catch(Exception e){
560 logger.error("xacml.rest.pap.notify.timeoutms property not set, using a default.");
563 connection.setReadTimeout(readTimeout);
564 connection.setConnectTimeout(readTimeout);
565 connection.setUseCaches(false);
567 // Adding this in. It seems the HttpUrlConnection class does NOT
568 // properly forward our headers for POST re-direction. It does so
569 // for a GET re-direction.
571 // So we need to handle this ourselves.
573 connection.setInstanceFollowRedirects(false);
574 connection.setDoOutput(true);
575 connection.setDoInput(true);
577 connection.connect();
578 } catch (Exception e) {
579 logger.warn("Caught exception on: connection.connect()",e);
583 if (connection.getResponseCode() == 200) {
584 logger.info("Received response 200 from pap server on notify");
587 logger.warn("connection response code not 200, received: "+connection.getResponseCode());
589 } catch (Exception e) {
590 logger.warn("Caught Exception on: connection.getResponseCode() ", e);
594 connection.disconnect();
598 private static String getElementFromXMLString(String element, String xml) {
599 InputSource source = new InputSource(new StringReader(xml));
601 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
602 String description = "";
604 DocumentBuilder db = dbf.newDocumentBuilder();
605 Document document = db.parse(source);
607 XPathFactory xpathFactory = XPathFactory.newInstance();
608 XPath xpath = xpathFactory.newXPath();
610 if (element.endsWith("/")){
611 element = element.substring(0, element.length() -1);
614 description = xpath.evaluate("/Policy" + element + "/text()", document);
620 System.out.println("description_" + description);
623 private static String evaluateXPath(String expression, String xml) {
624 InputSource source = new InputSource(new StringReader(xml));
626 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
627 String description = "";
629 DocumentBuilder db = dbf.newDocumentBuilder();
630 Document document = db.parse(source);
632 XPathFactory xpathFactory = XPathFactory.newInstance();
633 XPath xpath = xpathFactory.newXPath();
636 description = xpath.evaluate(expression, document);
642 System.out.println("description_" + description);
646 private static String getDescriptionFromXacml(String xacmlData){
647 String openTag = "<Description>";
648 String closeTag = "</Description>";
649 int descIndex = xacmlData.indexOf(openTag);
650 int endDescIndex = xacmlData.indexOf(closeTag);
651 String desc = xacmlData.substring(descIndex+openTag.length(),endDescIndex);
655 private final String POLICY_NOTIFICATION = "policy";
656 private final String PDP_NOTIFICATION = "pdp";
657 private final String GROUP_NOTIFICATION = "group";
658 public void handleIncomingHttpNotification(String url, String entityId, String entityType, String extraData, XACMLPapServlet xacmlPapServlet){
659 logger.info("DBDao url: " + url + " has reported an update on "+entityType+" entity "+entityId);
660 PolicyDBDaoTransaction transaction = this.getNewTransaction();
661 //although its named retries, this is the total number of tries
664 retries = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_INCOMINGNOTIFICATION_TRIES));
666 } catch(Exception e){
667 logger.error("xacml.rest.pap.incomingnotification.tries property not set, using a default of 3.");
670 //if someone sets it to some dumb value, we need to make sure it will try at least once
674 int pauseBetweenRetries = 1000;
677 case POLICY_NOTIFICATION:
678 for(int i=0; i<retries;i++){
680 handleIncomingPolicyChange(url, entityId,extraData);
682 } catch(Exception e){
683 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPolicyChange("+url+", "+entityId+", "+extraData+")");
686 Thread.sleep(pauseBetweenRetries);
687 }catch(InterruptedException ie){
692 case PDP_NOTIFICATION:
693 for(int i=0; i<retries;i++){
695 handleIncomingPdpChange(url, entityId, transaction);
697 } catch(Exception e){
698 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPdpChange("+url+", "+entityId+", "+transaction+")");
701 Thread.sleep(pauseBetweenRetries);
702 }catch(InterruptedException ie){
707 case GROUP_NOTIFICATION:
708 for(int i=0; i<retries;i++){
710 handleIncomingGroupChange(url, entityId, extraData, transaction, xacmlPapServlet);
713 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingGroupChange("+url+", "+entityId+", "+extraData+", "+transaction+", "+xacmlPapServlet+")");
716 Thread.sleep(pauseBetweenRetries);
717 }catch(InterruptedException ie){
723 //no changes should be being made in this function, we still need to close
724 transaction.rollbackTransaction();
726 private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException{
727 GroupEntity groupRecord = null;
728 long groupIdLong = -1;
730 groupIdLong = Long.parseLong(groupId);
731 } catch(NumberFormatException e){
732 throw new IllegalArgumentException("groupId "+groupId+" cannot be parsed into a long");
735 groupRecord = transaction.getGroup(groupIdLong);
736 } catch(Exception e){
737 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp group record with transaction.getGroup("+groupIdLong+");");
738 throw new PAPException("Could not get local group "+groupIdLong);
740 if(groupRecord == null){
741 throw new PersistenceException("The group record returned is null");
743 //compare to local fs
744 //does group folder exist
745 EcompPDPGroup localGroup = null;
747 localGroup = papEngine.getGroup(groupRecord.getGroupId());
748 } catch (Exception e) {
749 logger.warn("Caught PAPException trying to get local pdp group with papEngine.getGroup("+groupId+");",e);
751 if(localGroup == null && extraData != null){
752 //here we can try to load an old group id from the extraData
754 localGroup = papEngine.getGroup(extraData);
756 logger.warn("Caught PAPException trying to get local pdp group with papEngine.getGroup("+extraData+");",e);
759 if(localGroup != null && groupRecord.isDeleted()){
760 EcompPDPGroup newLocalGroup = null;
761 if(extraData != null){
763 newLocalGroup = papEngine.getGroup(extraData);
764 } catch (PAPException e) {
765 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get new pdp group with papEngine.getGroup("+extraData+");");
769 papEngine.removeGroup(localGroup, newLocalGroup);
770 } catch (NullPointerException | PAPException e) {
771 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get remove pdp group with papEngine.removeGroup("+localGroup+", "+newLocalGroup+");");
772 throw new PAPException("Could not remove group "+groupId);
775 else if(localGroup == null){
776 //creating a new group
778 papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());
779 } catch (NullPointerException | PAPException e) {
780 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to create pdp group with papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());");
781 throw new PAPException("Could not create group "+groupRecord);
784 localGroup = papEngine.getGroup(groupRecord.getGroupId());
785 } catch (PAPException e1) {
786 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Caught PAPException trying to get pdp group we just created with papEngine.getGroup(groupRecord.getGroupId());\nAny PDPs or policies in the new group may not have been added");
789 //add possible pdps to group
790 List<?> pdpsInGroup = transaction.getPdpsInGroup(Long.parseLong(groupRecord.getGroupId()));
791 for(Object pdpO : pdpsInGroup){
792 PdpEntity pdp = (PdpEntity)pdpO;
794 papEngine.newPDP(pdp.getPdpId(), localGroup, pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());
795 } catch (NullPointerException | PAPException e) {
796 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get create pdp with papEngine.newPDP(pdp.getPdpId(), localGroup, pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());");
797 throw new PAPException("Could not create pdp "+pdp);
800 //add possible policies to group (filesystem only, apparently)
802 if(!(localGroup instanceof StdPDPGroup)){
803 throw new PAPException("group is not a StdPDPGroup");
806 //because it will be comparing the new group to its own version
807 StdPDPGroup localGroupClone = new StdPDPGroup(localGroup.getId(),localGroup.isDefaultGroup(),localGroup.getName(),localGroup.getDescription(),((StdPDPGroup)localGroup).getDirectory());
808 localGroupClone.setEcompPdps(localGroup.getEcompPdps());
809 localGroupClone.setPipConfigs(localGroup.getPipConfigs());
810 localGroupClone.setStatus(localGroup.getStatus());
811 //we are updating a group or adding a policy or changing default
812 //set default if it should be
813 if(!localGroupClone.isDefaultGroup() && groupRecord.isDefaultGroup()){
815 papEngine.SetDefaultGroup(localGroup);
817 } catch (PAPException e) {
818 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to set default group with papEngine.SetDefaultGroup("+localGroupClone+");");
819 throw new PAPException("Could not set default group to "+localGroupClone);
822 boolean needToUpdate = false;
823 if(updateGroupPoliciesInFileSystem(localGroupClone,localGroup, groupRecord, transaction)){
826 if(!stringEquals(localGroupClone.getId(),groupRecord.getGroupId()) || !stringEquals(localGroupClone.getName(),groupRecord.getgroupName())){
828 //we do not want to change the id, the papEngine will do this for us, it needs to know the old id
829 localGroupClone.setName(groupRecord.getgroupName());
832 if(!stringEquals(localGroupClone.getDescription(),groupRecord.getDescription())){
833 localGroupClone.setDescription(groupRecord.getDescription());
839 papEngine.updateGroup(localGroupClone);
840 } catch (PAPException e) {
841 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to update group with papEngine.updateGroup("+localGroupClone+");");
842 throw new PAPException("Could not update group "+localGroupClone);
847 //call command that corresponds to the change that was made
849 //this will also handle removes, since incoming pdpGroup has no policies internally, we are just going to add them all in from the db
850 private boolean updateGroupPoliciesInFileSystem(EcompPDPGroup pdpGroup,EcompPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException{
851 if(!(pdpGroup instanceof StdPDPGroup)){
852 throw new PAPException("group is not a StdPDPGroup");
854 StdPDPGroup group = (StdPDPGroup)pdpGroup;
855 //this must always be true since we don't explicitly know when a delete is occuring
856 boolean didUpdate = true;
857 HashMap<String,PDPPolicy> currentPolicySet = new HashMap<String,PDPPolicy>(oldPdpGroup.getPolicies().size());
858 HashSet<PDPPolicy> newPolicySet = new HashSet<PDPPolicy>();
859 for(PDPPolicy pdpPolicy : oldPdpGroup.getPolicies()){
860 currentPolicySet.put(pdpPolicy.getId(), pdpPolicy);
862 for(PolicyEntity policy : groupRecord.getPolicies()){
863 String pdpPolicyName = getPdpPolicyName(policy.getPolicyName(), policy.getScope());
864 if(group.getPolicy(pdpPolicyName) == null){
866 if(currentPolicySet.containsKey(pdpPolicyName)){
867 newPolicySet.add(currentPolicySet.get(pdpPolicyName));
869 InputStream policyStream = new ByteArrayInputStream(policy.getPolicyData().getBytes());
870 group.copyPolicyToFile(pdpPolicyName,policyStream);
871 ((StdPDPPolicy)(group.getPolicy(pdpPolicyName))).setName(removeExtensionAndVersionFromPolicyName(policy.getPolicyName()));
873 policyStream.close();
874 } catch (IOException e) {
875 PolicyLogger.error(e.getMessage());
881 newPolicySet.addAll(group.getPolicies());
882 group.setPolicies(newPolicySet);
887 private String removeExtensionAndVersionFromPolicyName(String originalPolicyName){
888 return getPolicyNameAndVersionFromPolicyFileName(originalPolicyName)[0];
892 * Splits apart the policy name and version from a policy file path
893 * @param originalPolicyName: a policy file name ex: Config_policy.2.xml
894 * @return An array [0]: The policy name, [1]: the policy version, as a string
896 private String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){
897 String policyName = originalPolicyName;
898 String[] nameAndVersion = new String[2];
900 policyName = removeFileExtension(policyName);
901 nameAndVersion[0] = policyName.substring(0,policyName.lastIndexOf('.'));
902 if(isNullOrEmpty(nameAndVersion[0])){
903 throw new Exception();
905 } catch(Exception e){
906 nameAndVersion[0] = originalPolicyName;
909 nameAndVersion[1] = policyName.substring(policyName.lastIndexOf('.')+1);
910 if(isNullOrEmpty(nameAndVersion[1])){
911 throw new Exception();
913 } catch(Exception e){
914 nameAndVersion[1] = "1";
916 return nameAndVersion;
919 private void handleIncomingPdpChange(String url, String pdpId, PolicyDBDaoTransaction transaction) throws PAPException{
923 pdpIdLong = Long.parseLong(pdpId);
924 }catch(NumberFormatException e){
925 throw new IllegalArgumentException("pdpId "+pdpId+" cannot be parsed into a long");
927 PdpEntity pdpRecord = null;
929 pdpRecord = transaction.getPdp(pdpIdLong);
931 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp record with transaction.getPdp("+pdpIdLong+");");
932 throw new PAPException("Could not get local pdp "+pdpIdLong);
934 if(pdpRecord == null){
935 throw new PersistenceException("The pdpRecord returned is null");
939 localPdp = papEngine.getPDP(pdpRecord.getPdpId());
940 } catch (PAPException e) {
941 logger.warn("Caught PAPException trying to get local pdp with papEngine.getPDP("+pdpId+");",e);
943 if(localPdp != null && pdpRecord.isDeleted()){
945 papEngine.removePDP((EcompPDP) localPdp);
946 } catch (PAPException e) {
947 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get remove pdp with papEngine.removePDP("+localPdp+");");
948 throw new PAPException("Could not remove pdp "+pdpId);
951 else if(localPdp == null){
954 EcompPDPGroup localGroup = null;
956 localGroup = papEngine.getGroup(pdpRecord.getGroup().getGroupId());
957 } catch (PAPException e1) {
958 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Caught PAPException trying to get local group to add pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());");
959 throw new PAPException("Could not get local group");
962 papEngine.newPDP(pdpRecord.getPdpId(), localGroup, pdpRecord.getPdpName(), pdpRecord.getDescription(), pdpRecord.getJmxPort());
963 } catch (NullPointerException | PAPException e) {
964 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to create pdp with papEngine.newPDP("+pdpRecord.getPdpId()+", "+localGroup+", "+pdpRecord.getPdpName()+", "+pdpRecord.getDescription()+", "+pdpRecord.getJmxPort()+");");
965 throw new PAPException("Could not create pdp "+pdpRecord);
968 boolean needToUpdate = false;
969 if(!stringEquals(localPdp.getId(),pdpRecord.getPdpId()) || !stringEquals(localPdp.getName(),pdpRecord.getPdpName())){
970 //again, we don't want to change the id, the papEngine will do this
971 localPdp.setName(pdpRecord.getPdpName());
974 if(!stringEquals(localPdp.getDescription(),pdpRecord.getDescription())){
975 localPdp.setDescription(pdpRecord.getDescription());
978 String localPdpGroupId = null;
980 localPdpGroupId = papEngine.getPDPGroup((EcompPDP) localPdp).getId();
981 } catch(PAPException e){
982 //could be null or something, just warn at this point
983 logger.warn("Caught PAPException trying to get id of local group that pdp is in with localPdpGroupId = papEngine.getPDPGroup(localPdp).getId();",e);
985 if(!stringEquals(localPdpGroupId,pdpRecord.getGroup().getGroupId())){
986 EcompPDPGroup newPdpGroup = null;
988 newPdpGroup = papEngine.getGroup(pdpRecord.getGroup().getGroupId());
989 }catch(PAPException e){
990 //ok, now we have an issue. Time to stop things
991 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get id of local group to move pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());");
992 throw new PAPException("Could not get local group");
995 papEngine.movePDP((EcompPDP) localPdp, newPdpGroup);
996 }catch(PAPException e){
997 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to move pdp with papEngine.movePDP(localPdp, newPdpGroup);");
998 throw new PAPException("Could not move pdp "+localPdp);
1001 if(((PdpEntity) localPdp).getJmxPort() != pdpRecord.getJmxPort()){
1002 ((PdpEntity) localPdp).setJmxPort(pdpRecord.getJmxPort());
1003 needToUpdate = true;
1007 papEngine.updatePDP((EcompPDP) localPdp);
1008 } catch (PAPException e) {
1009 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to update pdp with papEngine.updatePdp("+localPdp+");");
1010 throw new PAPException("Could not update pdp "+localPdp);
1014 //compare to local situation
1015 //call command to update
1017 private void handleIncomingPolicyChange(String url, String policyId,String oldPathString){
1018 EntityManager em = emf.createEntityManager();
1019 Query getPolicyEntityQuery = em.createNamedQuery("PolicyEntity.FindById");
1020 getPolicyEntityQuery.setParameter("id", Long.valueOf(policyId));
1022 @SuppressWarnings("unchecked")
1023 List<PolicyEntity> policies = getPolicyEntityQuery.getResultList();
1024 PolicyEntity policy = null;
1025 if (policies.size() > 0){
1026 policy = policies.get(0);
1029 String policyRepo = buildPolicyScopeDirectory(policy);
1031 Path policyPath = Paths.get(policyRepo);
1032 String action = "unknown action";
1035 if(policy.isDeleted()){
1036 logger.debug("Deleting Policy: " + policy.getPolicyName());
1038 Path newPath = Paths.get(policyPath.toString(), policy.getPolicyName());
1039 Files.deleteIfExists(newPath);
1041 Path subFile = null;
1043 if (policy.getConfigurationData()!= null){
1044 subFile = getPolicySubFile(policy.getConfigurationData().getConfigurationName(), "Config");
1045 }else if(policy.getActionBodyEntity()!= null){
1046 subFile = getPolicySubFile(policy.getActionBodyEntity().getActionBodyName(), "Action");
1049 if(subFile != null){
1050 Files.deleteIfExists(subFile);
1054 logger.debug("Updating/Creating Policy: " + policy.getPolicyName());
1056 Files.createDirectories(policyPath);
1057 Path newPath = Paths.get(policyPath.toString(), policy.getPolicyName());
1058 Files.deleteIfExists(newPath);
1059 if(!isNullOrEmpty(oldPathString)){
1061 String[] scopeName = getScopeAndNameAndType(oldPathString);
1062 Path oldPath = Paths.get(buildPolicyScopeDirectory(scopeName[0]),scopeName[1]);
1063 Files.delete(oldPath.toAbsolutePath());
1064 }catch(Exception e){
1065 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy before rename: "+oldPathString);
1068 Object policyData = XACMLPolicyScanner.readPolicy(IOUtils.toInputStream(policy.getPolicyData()));
1069 XACMLPolicyWriter.writePolicyFile(newPath, (PolicyType) policyData);
1071 if (policy.getConfigurationData()!= null){
1072 if(!isNullOrEmpty(oldPathString)){
1074 String[] oldPolicyScopeName = getScopeAndNameAndType(oldPathString);
1075 String oldConfigFileName = getConfigFile(oldPolicyScopeName[1],policy.getConfigurationData().getConfigType());
1076 Path oldConfigFilePath = getPolicySubFile(oldConfigFileName, "Config");
1077 logger.debug("Trying to delete: "+oldConfigFilePath.toString());
1078 Files.delete(oldConfigFilePath);
1079 }catch(Exception e){
1080 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy config before rename for policy: "+oldPathString);
1083 writePolicySubFile(policy, "Config");
1085 }else if(policy.getActionBodyEntity()!= null){
1086 if(!isNullOrEmpty(oldPathString)){
1088 String[] oldPolicyScopeName = getScopeAndNameAndType(oldPathString);
1089 String oldActionFileName = getConfigFile(oldPolicyScopeName[1],ConfigPolicy.JSON_CONFIG);
1090 Path oldActionFilePath = getPolicySubFile(oldActionFileName, "Action");
1091 logger.debug("Trying to delete: "+oldActionFilePath.toString());
1092 Files.delete(oldActionFilePath);
1093 }catch(Exception e){
1094 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy action body before rename for policy: "+oldPathString);
1097 writePolicySubFile(policy, "Action");
1101 } catch (IOException e1) {
1102 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while performing [" + action + "] of Policy File: " + policy.getPolicyName());
1106 private void createGroupsFromDatabase(){
1107 //get list of groups
1108 boolean foundDefault = false;
1109 //need to avoid infinite loop, just in case
1110 boolean alreadyRunAdd = false;
1111 while(!foundDefault){
1113 EntityManager em = emf.createEntityManager();
1114 Query getGroups = em.createQuery("SELECT g FROM GroupEntity g WHERE g.deleted=:deleted");
1115 getGroups.setParameter("deleted", false);
1116 List<?> groups = getGroups.getResultList();
1118 //make a folder for each group in pdps folders
1119 Path pdpsPath = Paths.get(XACMLProperties.getProperty("xacml.pap.pdps"));
1120 if(!pdpsPath.toFile().exists()){
1122 FileUtils.forceMkdir(pdpsPath.toFile());
1123 } catch (IOException e) {
1124 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not make the new pdps folder; one does not exist");
1127 Properties propertyFileProperties = new Properties();
1128 String groupList = "";
1129 String defaultGroup = "";
1130 HashSet<String> currentGroupPaths = new HashSet<String>();
1131 for(Object o : groups){
1132 GroupEntity group = (GroupEntity)o;
1133 Path groupPath = Paths.get(pdpsPath.toString(), group.getGroupId());
1134 currentGroupPaths.add(groupPath.getFileName().toString());
1135 if(groupPath.toFile().exists()){
1137 FileUtils.forceDelete(Paths.get(groupPath.toString(), "xacml.policy.properties").toFile());
1138 } catch (IOException e) {
1139 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete old xacml.policy.properties file");
1141 File[] xmlFiles = groupPath.toFile().listFiles(new FileFilter(){
1144 public boolean accept(File pathname) {
1145 return pathname.toString().endsWith(".xml");
1149 for(File deleteMe : xmlFiles){
1151 FileUtils.forceDelete(deleteMe);
1152 } catch (IOException e) {
1153 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete one of the policy files that we are going to replace: "+deleteMe.toString());
1158 FileUtils.forceMkdir(groupPath.toFile());
1159 } catch (IOException e) {
1160 logger.error("Exception Occured"+e);
1163 Properties policyProperties = new Properties();
1164 String rootPolicies = "";
1165 for(PolicyEntity policy : group.getPolicies()){
1166 Path newPolicyPath = Paths.get(groupPath.toString(),getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
1167 File newPolicyFile = newPolicyPath.toFile();
1169 newPolicyFile.createNewFile();
1170 } catch (IOException e) {
1171 PolicyLogger.error(e.getMessage());
1174 FileOutputStream policyFileStream = new FileOutputStream(newPolicyFile);
1175 policyFileStream.write(policy.getPolicyData().getBytes("UTF-8"));
1176 policyFileStream.close();
1177 } catch (IOException e) {
1178 PolicyLogger.error(e.getMessage());
1180 policyProperties.setProperty(getPdpPolicyName(policy.getPolicyName(),policy.getScope())+".name",removeExtensionAndVersionFromPolicyName(policy.getPolicyName()));
1181 rootPolicies += ",".concat(getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
1183 Path xacmlPolicyPropertiesPath = Paths.get(groupPath.toString(),"xacml.policy.properties");
1184 File xacmlPolicyPropertiesFile = xacmlPolicyPropertiesPath.toFile();
1185 if(rootPolicies.length() > 0){
1186 rootPolicies = rootPolicies.substring(1);
1188 policyProperties.setProperty("xacml.referencedPolicies", "");
1189 policyProperties.setProperty("xacml.rootPolicies", rootPolicies);
1192 xacmlPolicyPropertiesFile.createNewFile();
1193 } catch (IOException e) {
1194 PolicyLogger.error(e.getMessage());
1197 FileOutputStream xacmlPolicyPropertiesFileStream = new FileOutputStream(xacmlPolicyPropertiesFile);
1198 policyProperties.store(xacmlPolicyPropertiesFileStream, "");
1199 xacmlPolicyPropertiesFileStream.close();
1200 } catch (IOException e) {
1201 PolicyLogger.error(e.getMessage());
1204 em = emf.createEntityManager();
1205 Query getPdpsQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group AND p.deleted=:deleted");
1206 getPdpsQuery.setParameter("group", group);
1207 getPdpsQuery.setParameter("deleted", false);
1208 List<?> pdps = getPdpsQuery.getResultList();
1210 String pdpLine = "";
1211 for(Object o2 : pdps){
1212 PdpEntity pdp = (PdpEntity)o2;
1213 pdpLine += ",".concat(pdp.getPdpId());
1214 propertyFileProperties.setProperty(pdp.getPdpId()+".description",pdp.getDescription());
1215 propertyFileProperties.setProperty(pdp.getPdpId()+".jmxport",String.valueOf(pdp.getJmxPort()));
1216 propertyFileProperties.setProperty(pdp.getPdpId()+".name",pdp.getPdpName());
1218 if(pdpLine.length() > 0){
1219 pdpLine = pdpLine.substring(1);
1221 propertyFileProperties.setProperty(group.getGroupId()+".description", group.getDescription());
1222 propertyFileProperties.setProperty(group.getGroupId()+".name", group.getgroupName());
1223 propertyFileProperties.setProperty(group.getGroupId()+".pdps",pdpLine);
1224 groupList += ",".concat(group.getGroupId());
1225 if(group.isDefaultGroup()){
1226 defaultGroup = group.getGroupId();
1227 foundDefault = true;
1230 //check the list of directories in the pdps folder and make sure none should be deleted
1231 File[] filesInPdpsFolder = pdpsPath.toFile().listFiles(new FileFilter(){
1233 public boolean accept(File pathname) {
1234 return pathname.isDirectory();
1237 for(File f : filesInPdpsFolder){
1238 if(f.isDirectory()){
1239 if(!currentGroupPaths.contains(f.toPath().getFileName().toString())){
1241 FileUtils.forceDelete(f);
1242 } catch (IOException e) {
1243 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete pdp group folder, which has been deleted from the database");
1248 if(!foundDefault && !alreadyRunAdd){
1249 alreadyRunAdd = true;
1250 //add default group to db
1252 em = emf.createEntityManager();
1253 em.getTransaction().begin();
1254 GroupEntity newDefaultGroup = new GroupEntity();
1255 em.persist(newDefaultGroup);
1256 newDefaultGroup.setDescription("The default group where new PDP's are put.");
1257 newDefaultGroup.setGroupId("default");
1258 newDefaultGroup.setGroupName("default");
1259 newDefaultGroup.setDefaultGroup(true);
1260 newDefaultGroup.setCreatedBy("automaticallyAdded");
1261 newDefaultGroup.setModifiedBy("automaticallyAdded");
1263 em.getTransaction().commit();
1265 } catch(Exception e){
1266 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not add a new default group to the database");
1270 Path xacmlPropertiesPath = Paths.get(pdpsPath.toString(),"xacml.properties");
1271 File xacmlPropertiesFile = xacmlPropertiesPath.toFile();
1272 if(groupList.length()>0){
1273 groupList = groupList.substring(1);
1275 propertyFileProperties.setProperty("xacml.pap.groups",groupList);
1276 propertyFileProperties.setProperty("xacml.pap.groups.default",defaultGroup);
1278 FileUtils.forceDelete(xacmlPropertiesFile);
1279 } catch (IOException e) {
1280 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old xacml.properties file");
1283 xacmlPropertiesFile.createNewFile();
1284 } catch (IOException e) {
1285 PolicyLogger.error(e.getMessage());
1288 FileOutputStream xacmlPropertiesFileStream = new FileOutputStream(xacmlPropertiesFile);
1289 propertyFileProperties.store(xacmlPropertiesFileStream, "");
1290 xacmlPropertiesFileStream.close();
1291 } catch (IOException e) {
1292 PolicyLogger.error(e.getMessage());
1294 //if we get this far down, something went wrong and we don't want to get stuck in the loop
1295 foundDefault = true;
1297 //put policies in group folder
1298 //create xacml.policy.properties in each folder with list of policies in that folder
1300 //create xacml.properties with list of groups and pdps and other info
1303 private String getPdpPolicyName(String name, String scope){
1304 String finalName = "";
1307 finalName += removeFileExtension(name);
1308 finalName += ".xml";
1311 private String removeFileExtension(String fileName){
1312 return fileName.substring(0, fileName.lastIndexOf('.'));
1315 private String buildPolicyScopeDirectory(PolicyEntity policy){
1316 String repo = buildPolicyDirectory();
1318 String policyScope = policy.getScope();
1319 if(policyScope == null){
1321 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1323 policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());
1325 if(policyScope == null){
1327 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1330 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
1333 Path returnPath = Paths.get(repo + FileSystems.getDefault().getSeparator() + policyScope);
1334 if(returnPath != null){
1335 return returnPath.toString();
1337 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
1343 private String buildPolicyScopeDirectory(String policyScope){
1344 String repo = buildPolicyDirectory();
1345 policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());
1346 return repo + FileSystems.getDefault().getSeparator() + policyScope;
1350 private static String buildPolicyDirectory(){
1351 Path workspacePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE), getDefaultWorkspace());
1352 Path repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY));
1353 Path gitPath = Paths.get(workspacePath.toString(), repositoryPath.getFileName().toString());
1356 * Getting and Setting the parent path for Admin Console use when reading the policy files
1358 //getting the fullpath of the gitPath and convert to string
1359 String policyDir = gitPath.toAbsolutePath().toString();
1362 if(policyDir.contains("\\")){
1363 policyDir = policyDir.replace("XACML-PAP-REST", "XACML-PAP-ADMIN");
1365 if (policyDir.contains("pap")){
1366 policyDir = policyDir.replace("pap", "console");
1369 logger.debug("policyDir: " + policyDir);
1373 private Path getPolicySubFile(String filename, String subFileType){
1374 logger.debug("getPolicySubFile(" + filename + ", " + subFileType + ")");
1375 Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), subFileType);
1378 filename = FilenameUtils.removeExtension(filename);
1380 for(File tmpFile : filePath.toFile().listFiles()){
1381 if (FilenameUtils.removeExtension(tmpFile.getName()).equals(filename)){
1386 Path finalPath = null;
1388 finalPath = Paths.get(file.getAbsolutePath());
1391 logger.debug("end of getPolicySubFile: " + finalPath);
1395 private boolean writePolicySubFile(PolicyEntity policy, String policyType){
1396 logger.info("writePolicySubFile with policyName[" + policy.getPolicyName() + "] and policyType[" + policyType + "]");
1398 String subTypeName = null;
1399 String subTypeBody = null;
1400 if (policyType.equalsIgnoreCase("config")){
1402 subTypeName = FilenameUtils.removeExtension(policy.getConfigurationData().getConfigurationName());
1403 subTypeBody = policy.getConfigurationData().getConfigBody();
1405 String configType = policy.getConfigurationData().getConfigType();
1408 if (configType != null) {
1409 if (configType.equals(JSON_CONFIG)) {
1410 subTypeName = subTypeName + ".json";
1412 if (configType.equals(XML_CONFIG)) {
1413 subTypeName = subTypeName + ".xml";
1415 if (configType.equals(PROPERTIES_CONFIG)) {
1416 subTypeName = subTypeName + ".properties";
1418 if (configType.equals(OTHER_CONFIG)) {
1419 subTypeName = subTypeName + ".txt";
1423 }else if (policyType.equalsIgnoreCase("action")){
1425 subTypeName = policy.getActionBodyEntity().getActionBodyName();
1426 subTypeBody = policy.getActionBodyEntity().getActionBody();
1430 Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), type);
1432 if(subTypeBody == null){
1435 boolean success = false;
1437 Files.deleteIfExists(Paths.get(filePath.toString(), subTypeName));
1438 File file = Paths.get(filePath.toString(),subTypeName).toFile();
1439 file.createNewFile();
1440 FileWriter fileWriter = new FileWriter(file, false); // false to overwrite
1441 fileWriter.write(subTypeBody);
1445 } catch (Exception e) {
1446 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception occured while creating Configuration File for Policy : " + policy.getPolicyName());
1453 private String getPolicySubType(String filename){
1456 if (filename != null) {
1457 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("json")) {
1458 type = ConfigPolicy.JSON_CONFIG;
1460 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("xml")) {
1461 type = ConfigPolicy.XML_CONFIG;
1463 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("properties")) {
1464 type = ConfigPolicy.PROPERTIES_CONFIG;
1466 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("txt")) {
1467 type = ConfigPolicy.OTHER_CONFIG;
1476 private void convertFileToDBEntry(Path path){
1477 logger.info("convertFileToDBEntry");
1479 if(path.toString().contains(".git")){
1483 String filename = path.getFileName().toString();
1484 if (filename.contains(".svnignore")){
1488 String[] scopeAndName = getScopeAndNameAndType(path.toString());
1490 if(scopeAndName == null){
1491 PolicyLogger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + path.toString() + " is null!");
1495 EntityManager em = emf.createEntityManager();
1496 em.getTransaction().begin();
1498 PolicyEntity policy = new PolicyEntity();
1500 String policyScope = scopeAndName[0];
1501 String policyName = scopeAndName[1];
1502 policy.setScope(policyScope);
1503 policy.setPolicyName(policyName);
1504 policy.setCreatedBy(AUDIT_USER);
1505 policy.setModifiedBy(AUDIT_USER);
1507 String newScope = policyScope.replace(".", File.separator);
1508 String newName = FilenameUtils.removeExtension(policyName);
1511 //we want the last index +1 because we don't want the dot
1512 version = Integer.parseInt(newName.substring(newName.lastIndexOf(".")+1));
1513 } catch(Exception e){
1514 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get the policy version number from "+newName);
1516 newName = newScope + File.separator + newName.substring(0, newName.lastIndexOf("."));
1518 Query query = em.createNamedQuery("PolicyVersion.findByPolicyName");
1519 query.setParameter("pname", newName);
1521 List<?> result = query.getResultList();
1522 PolicyVersion versionEntity = null;
1524 if (!result.isEmpty()) {
1525 logger.info("Result is not empty");
1526 versionEntity = (PolicyVersion) result.get(0);
1527 int highestVersion = Math.max(versionEntity.getHigherVersion(),version);
1528 versionEntity.setHigherVersion(highestVersion);
1529 versionEntity.setActiveVersion(highestVersion);
1531 logger.info("result is empty");
1532 Calendar calendar = Calendar.getInstance();
1533 Timestamp createdDate = new Timestamp(calendar.getTime().getTime());
1535 versionEntity = new PolicyVersion();
1536 em.persist(versionEntity);
1537 versionEntity.setPolicyName(newName);
1538 versionEntity.setHigherVersion(version);
1539 versionEntity.setActiveVersion(version);
1540 versionEntity.setCreatedBy(AUDIT_USER);
1541 versionEntity.setModifiedBy(AUDIT_USER);
1542 versionEntity.setCreatedDate(createdDate);
1543 versionEntity.setModifiedDate(createdDate);
1548 String policyContent = new String(Files.readAllBytes(path));
1549 policy.setDescription(getElementFromXMLString("/Description", policyContent));
1550 policy.setPolicyData(policyContent);
1551 } catch (IOException e1) {
1552 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "convertFileToDBEntry error settingPolicyData");
1553 em.getTransaction().rollback();
1558 if((scopeAndName[2].equalsIgnoreCase("Config"))){
1559 String scopeName = scopeAndName[0] + "." + scopeAndName[1];
1560 Path subFilePath = getPolicySubFile(scopeName, scopeAndName[2]);
1562 String content = new String(Files.readAllBytes(subFilePath));
1563 String configName = subFilePath.getFileName().toString();
1564 ConfigurationDataEntity configData = new ConfigurationDataEntity();
1565 em.persist(configData);
1566 configData.setConfigurationName(subFilePath.getFileName().toString());
1567 configData.setConfigBody(content);
1568 configData.setConfigType(getPolicySubType(configName));
1569 configData.setCreatedBy(AUDIT_USER);
1570 configData.setModifiedBy(AUDIT_USER);
1571 policy.setConfigurationData(configData);
1573 } catch (Exception e) {
1574 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "convertFileToDBEntry error for Config policy");
1575 em.getTransaction().rollback();
1579 }else if(scopeAndName[2].equalsIgnoreCase("Action")){
1580 String scopeName = scopeAndName[0] + "." + scopeAndName[1];
1581 Path subFilePath = getPolicySubFile(scopeName, scopeAndName[2]);
1583 String content = new String(Files.readAllBytes(subFilePath));
1584 ActionBodyEntity actionBody = new ActionBodyEntity();
1585 em.persist(actionBody);
1586 actionBody.setActionBodyName(subFilePath.getFileName().toString());
1587 actionBody.setActionBody(content);
1588 actionBody.setCreatedBy(AUDIT_USER);
1589 actionBody.setModifiedBy(AUDIT_USER);
1590 policy.setActionBodyEntity(actionBody);
1592 } catch (Exception e) {
1593 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "convertFileToDBEntry error for Action policy");
1594 em.getTransaction().rollback();
1599 logger.debug("convertFileToDBEntry commit transaction");
1600 em.getTransaction().commit();
1604 private void deleteAllPolicyTables(){
1605 EntityManager em = emf.createEntityManager();
1606 em.getTransaction().begin();
1607 Query deletePolicyEntityTableUpdate = em.createNamedQuery("PolicyEntity.deleteAll");
1608 Query deleteActionBodyEntityTableUpdate = em.createNamedQuery("ActionBodyEntity.deleteAll");
1609 Query deleteConfigurationDataEntityTableUpdate = em.createNamedQuery("ConfigurationDataEntity.deleteAll");
1610 Query deletePolicyVersionEntityTableUpdate = em.createNamedQuery("PolicyVersion.deleteAll");
1611 deletePolicyEntityTableUpdate.executeUpdate();
1612 deleteActionBodyEntityTableUpdate.executeUpdate();
1613 deleteConfigurationDataEntityTableUpdate.executeUpdate();
1614 deletePolicyVersionEntityTableUpdate.executeUpdate();
1615 em.getTransaction().commit();
1620 public void auditLocalDatabase(PAPPolicyEngine papEngine2){
1621 logger.debug("PolicyDBDao.auditLocalDatabase() is called");
1622 Path webappsPath = Paths.get(buildPolicyDirectory());
1624 deleteAllGroupTables();
1625 deleteAllPolicyTables();
1626 Files.createDirectories(webappsPath);
1627 Files.walk(webappsPath).filter(Files::isRegularFile).forEach(this::convertFileToDBEntry);
1628 auditGroups(papEngine2);
1629 } catch(Exception e){
1630 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "auditLocalDatabase() error");
1631 logger.error("Exception Occured"+e);
1636 * Audits and loads the local file system to match the database version.
1638 @SuppressWarnings("unchecked")
1639 public void auditLocalFileSystem(){
1640 logger.debug("PolicyDBDau.auditLocalFileSystem() is called");
1642 Path webappsPath = Paths.get(buildPolicyDirectory());
1643 Path configFilesPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), "Config");
1644 Path actionFilesPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), "Action");
1646 Files.createDirectories(configFilesPath);
1647 Files.createDirectories(actionFilesPath);
1648 FileUtils.cleanDirectory(actionFilesPath.toFile());
1649 FileUtils.cleanDirectory(configFilesPath.toFile());
1650 if (webappsPath.toFile().exists()){
1651 FileUtils.cleanDirectory(webappsPath.toFile());
1653 Path repoWithScope = Paths.get(webappsPath.toString(), XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DOMAIN));
1654 Files.createDirectories(repoWithScope);
1655 } catch (IOException e2) {
1656 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Error occurred while creating / clearing Config and Policy filesystem directories");
1659 List<PolicyEntity> policyEntityList;
1661 EntityManager em = emf.createEntityManager();
1662 Query getPolicyEntitiesQuery = em.createNamedQuery("PolicyEntity.findAllByDeletedFlag");
1663 getPolicyEntitiesQuery.setParameter("deleted", false);
1664 policyEntityList = getPolicyEntitiesQuery.getResultList();
1665 } catch(Exception e){
1666 policyEntityList = new LinkedList<PolicyEntity>();
1669 for (PolicyEntity policy: policyEntityList){
1672 if (!policy.isDeleted()){
1673 name = policy.getPolicyName();
1674 String scope = policy.getScope();
1676 scope = scope.replace(".", "//");
1677 if (policy.getConfigurationData()!=null){
1678 writePolicySubFile(policy, "Config");
1680 else if(policy.getActionBodyEntity()!=null){
1681 writePolicySubFile(policy, "Action");
1685 Path fileLocation = Paths.get(webappsPath.toString(), scope);
1687 Files.createDirectories(fileLocation);
1688 Path newPath = Paths.get(fileLocation.toString(), name);
1689 Object policyData = XACMLPolicyScanner.readPolicy(IOUtils.toInputStream(policy.getPolicyData()));
1690 XACMLPolicyWriter.writePolicyFile(newPath, (PolicyType) policyData);
1692 } catch (Exception e1) {
1693 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while creating Policy File: " + name);
1696 createGroupsFromDatabase();
1699 public void deleteAllGroupTables(){
1700 logger.debug("PolicyDBDao.deleteAllGroupTables() called");
1701 EntityManager em = emf.createEntityManager();
1702 em.getTransaction().begin();
1704 Query deletePdpEntityEntityTableUpdate = em.createNamedQuery("PdpEntity.deleteAll");
1705 deletePdpEntityEntityTableUpdate.executeUpdate();
1707 Query deleteGroupEntityTableUpdate = em.createNamedQuery("GroupEntity.deleteAll");
1708 deleteGroupEntityTableUpdate.executeUpdate();
1710 em.getTransaction().commit();
1714 @SuppressWarnings("unchecked")
1715 public void auditGroups(PAPPolicyEngine papEngine2){
1716 logger.debug("PolicyDBDao.auditGroups() called");
1718 EntityManager em = emf.createEntityManager();
1719 em.getTransaction().begin();
1720 final String AUDIT_STR = "Audit";
1723 Set<EcompPDPGroup> groups = papEngine2.getEcompPDPGroups();
1725 for (EcompPDPGroup grp : groups){
1727 GroupEntity groupEntity = new GroupEntity();
1728 em.persist(groupEntity);
1729 groupEntity.setGroupName(grp.getName());
1730 groupEntity.setDescription(grp.getDescription());
1731 groupEntity.setDefaultGroup(grp.isDefaultGroup());
1732 groupEntity.setCreatedBy(AUDIT_STR);
1733 groupEntity.setGroupId(createNewPDPGroupId(grp.getId()));
1734 groupEntity.setModifiedBy(AUDIT_STR);
1735 Set<EcompPDP> pdps = grp.getEcompPdps();
1737 for(EcompPDP pdp : pdps){
1738 PdpEntity pdpEntity = new PdpEntity();
1739 em.persist(pdpEntity);
1740 pdpEntity.setGroup(groupEntity);
1741 pdpEntity.setJmxPort(pdp.getJmxPort());
1742 pdpEntity.setPdpId(pdp.getId());
1743 pdpEntity.setPdpName(pdp.getName());
1744 pdpEntity.setModifiedBy(AUDIT_STR);
1745 pdpEntity.setCreatedBy(AUDIT_STR);
1749 Set<PDPPolicy> policies = grp.getPolicies();
1751 for(PDPPolicy policy : policies){
1753 String[] stringArray = getNameScopeAndVersionFromPdpPolicy(policy.getId());
1754 List<PolicyEntity> policyEntityList;
1755 Query getPolicyEntitiesQuery = em.createNamedQuery("PolicyEntity.findByNameAndScope");
1756 getPolicyEntitiesQuery.setParameter("name", stringArray[0]);
1757 getPolicyEntitiesQuery.setParameter("scope", stringArray[1]);
1759 policyEntityList = getPolicyEntitiesQuery.getResultList();
1760 PolicyEntity policyEntity = null;
1761 if(policyEntityList.size() < 1){
1762 policyEntity = addPolicyThatOnlyExistsInPdpGroup(policy.getId(),Paths.get(XACMLProperties.getProperty("xacml.pap.pdps"),grp.getId(),policy.getId()),em);
1764 policyEntity = policyEntityList.get(0);
1766 if(policyEntity != null){
1767 groupEntity.addPolicyToGroup(policyEntity);
1769 }catch(Exception e2){
1770 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Exception auditGroups inner catch");
1773 }catch(Exception e1){
1774 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Exception auditGroups middle catch");
1777 }catch(Exception e){
1778 em.getTransaction().rollback();
1779 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception auditGroups outer catch");
1784 em.getTransaction().commit();
1789 private PolicyEntity addPolicyThatOnlyExistsInPdpGroup(String polId, Path path,EntityManager em){
1790 String filename = path.getFileName().toString();
1791 if (filename.contains(".svnignore")){
1795 String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(polId);
1797 if(scopeAndName == null){
1798 PolicyLogger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + polId.toString() + " is null!");
1802 PolicyEntity policy = new PolicyEntity();
1804 String policyScope = scopeAndName[1];
1805 String policyName = scopeAndName[0];
1806 policy.setScope(policyScope);
1807 policy.setPolicyName(policyName);
1808 policy.setCreatedBy(AUDIT_USER);
1809 policy.setModifiedBy(AUDIT_USER);
1810 policy.setDeleted(true);
1813 String policyContent = new String(Files.readAllBytes(path));
1814 policy.setDescription(getElementFromXMLString("/Description", policyContent));
1815 policy.setPolicyData(policyContent);
1817 } catch (IOException e1) {
1818 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "convertFileToDBEntry error settingPolicyData");
1824 private String getConfigFile(String filename, PolicyRestAdapter policy){
1826 return getConfigFile(filename, (String)null);
1828 return getConfigFile(filename, policy.getConfigType());
1830 //copied from ConfigPolicy.java and modified
1831 // Here we are adding the extension for the configurations file based on the
1832 // config type selection for saving.
1833 private String getConfigFile(String filename, String configType) {
1834 logger.debug("getConfigFile(String filename, String scope, String configType) as getConfigFile("+filename+", "+configType+") called");
1835 filename = FilenameUtils.removeExtension(filename);
1836 String id = configType;
1839 if (id.equals(ConfigPolicy.JSON_CONFIG) || id.contains("Firewall")) {
1840 filename = filename + ".json";
1842 if (id.equals(ConfigPolicy.XML_CONFIG)) {
1843 filename = filename + ".xml";
1845 if (id.equals(ConfigPolicy.PROPERTIES_CONFIG)) {
1846 filename = filename + ".properties";
1848 if (id.equals(ConfigPolicy.OTHER_CONFIG)) {
1849 filename = filename + ".txt";
1856 * Constructs the file name of a policy.
1857 * @param policy The name of a policy (ex: mypolicy1)
1858 * @return The file name of the policy (ex: Config_mypolicy1.xml)
1861 @SuppressWarnings("unused")
1862 private String getName(PolicyRestAdapter policy){
1863 logger.debug("getName(PolicyRestAdapter policy) as getName("+policy+") called");
1864 String namePrefix = "";
1865 if(policy.getPolicyType().contains("Config")){
1866 namePrefix = namePrefix.concat(policy.getPolicyType());
1867 if(policy.getConfigType().contains("Firewall")){
1868 namePrefix = namePrefix.concat("_FW");
1871 String concats = namePrefix + "_" +policy.getPolicyName() + ".xml";
1875 private String stripPolicyName(String policyFileName){
1876 String policyName = policyFileName;
1878 policyName = policyName.substring(policyName.indexOf('_')+1);
1879 policyName = removeFileExtension(policyName);
1880 }catch(Exception e){
1881 throw new IllegalArgumentException("Could not get name out of policy file name: "+policyName);
1886 private String[] getNameScopeAndVersionFromPdpPolicy(String fileName){
1887 String[] splitByDots = fileName.split("\\.");
1888 if(splitByDots.length < 3){
1889 //should we throw something
1892 String policyName = splitByDots[splitByDots.length-3];
1893 String version = splitByDots[splitByDots.length-2];
1894 //policy names now include version
1896 for(int i=0;i<splitByDots.length-3;i++){
1897 scope += ".".concat(splitByDots[i]);
1899 //remove the first dot
1900 if(scope.length() > 0){
1901 scope = scope.substring(1);
1903 String[] returnArray = new String[3];
1904 returnArray[0] = policyName + "." + version + ".xml";
1905 returnArray[2] = version;
1906 returnArray[1] = scope;
1911 * Constructs the complete repository path based on the properties files
1912 * @return The repository path
1914 public static String getGitPath(){
1915 logger.debug("getGitPath() as getGitPath() called");
1916 Path workspacePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE), "admin");
1917 Path repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY));
1918 Path gitPath = Paths.get(workspacePath.toString(), repositoryPath.getFileName().toString());
1919 logger.debug("after gitPath: " + gitPath);
1920 return gitPath.toString();
1923 //copied from StdEngine.java
1924 public static String createNewPDPGroupId(String name) {
1926 // replace "bad" characters with sequences that will be ok for file names and properties keys.
1927 id = id.replace(" ", "_sp_");
1928 id = id.replace("\t", "_tab_");
1929 id = id.replace("\\", "_bksl_");
1930 id = id.replace("/", "_sl_");
1931 id = id.replace(":", "_col_");
1932 id = id.replace("*", "_ast_");
1933 id = id.replace("?", "_q_");
1934 id = id.replace("\"", "_quo_");
1935 id = id.replace("<", "_lt_");
1936 id = id.replace(">", "_gt_");
1937 id = id.replace("|", "_bar_");
1938 id = id.replace("=", "_eq_");
1939 id = id.replace(",", "_com_");
1940 id = id.replace(";", "_scom_");
1946 * Checks if any of the given strings are empty or null
1947 * @param strings One or more Strings (or nulls) to check if they are null or empty
1948 * @return true if one or more of the given strings are empty or null
1950 private static boolean isNullOrEmpty(String... strings){
1951 for(String s : strings){
1952 if(!(s instanceof String)){
1963 * Computes the scope, name, and type of a policy based on its file path
1964 * @param path The file path of the policy (including the xml policy file)
1965 * @return A string array of size 3. 1: the scope of the policy 2: the name of the policy (Config_mypol.xml) 3: the type (Config). Or, null if the path can not be parsed.
1967 private static String[] getScopeAndNameAndType(String path){
1968 logger.debug("getScopeAndNameAndType(String path) as getScopeAndNameAndType("+path+") called");
1972 String gitPath = getGitPath();
1974 ArrayList<String> gitPathParts = new ArrayList<String>();
1975 Iterator<?> gitPathIterator = Paths.get(gitPath).iterator();
1976 while(gitPathIterator.hasNext()){
1977 gitPathParts.add(gitPathIterator.next().toString());
1979 for(int i=0;i<gitPathParts.size();i++){
1980 Path testGitPath = Paths.get("");
1981 for(int j=i;j<gitPathParts.size();j++){
1982 testGitPath = Paths.get(testGitPath.toString(),gitPathParts.get(j));
1984 if(path.contains(testGitPath.toString())){
1985 gitPath = testGitPath.toString();
1989 if(gitPath == null){
1990 logger.debug("gitPath is null. Returning");
1993 if(gitPath.length() >= path.length()){
1994 logger.debug("gitPath length(): " + gitPath.length() + ">= path.length(): " + path.length() + ". Returning null");
1997 String scopeAndName = path.substring(path.indexOf(gitPath)+gitPath.length());
1999 logger.debug("scopeAndName: " + scopeAndName);
2000 String policyType = null;
2001 String[] policyTypes = {"Config_","Action_","Decision_"};
2002 for(String pType : policyTypes){
2003 if(scopeAndName.contains(pType)){
2007 if(policyType == null){
2010 String scope = scopeAndName.substring(0,scopeAndName.indexOf(policyType));
2011 String name = scopeAndName.substring(scopeAndName.indexOf(policyType), scopeAndName.length());
2012 scope = scope.replace('\\', '.');
2013 scope = scope.replace('/', '.');
2014 if(scope.length()<1){
2017 if(scope.charAt(0) == '.'){
2018 if(scope.length() < 2){
2019 logger.debug("getScopeAndNameAndType error: " + scope.length() + " < 2. " + "| scope.charAt(0)==.");
2022 scope = scope.substring(1);
2024 if(scope.charAt(scope.length()-1) == '.'){
2025 if(scope.length() < 2){
2026 logger.debug("getScopeAndNameAndType error: " + scope.length() + " < 2" + "| scope.charAt(scope.length()-1)==.");
2029 scope = scope.substring(0,scope.length()-1);
2031 if(name.length()<1){
2032 logger.debug("getScopeAndNameAndType error: name.length()<1");
2035 if(name.charAt(0) == '.'){
2036 if(name.length() < 2){
2037 logger.debug("getScopeAndNameAndType error: " + name.length() + " < 2. " + "| scope.charAt(0)==.");
2040 name = name.substring(1);
2042 String[] returnArray = new String[3];
2043 returnArray[0] = scope;
2044 returnArray[1] = name;
2045 //remove the underscore and return it
2046 returnArray[2] = policyType.substring(0, policyType.length()-1);
2051 private class PolicyDBDaoTransactionInstance implements PolicyDBDaoTransaction {
2052 private EntityManager em;
2053 private final Object emLock = new Object();
2058 private boolean operationRun = false;
2059 private final Thread transactionTimer;
2061 private PolicyDBDaoTransactionInstance(){
2062 //call the constructor with arguments
2063 this(Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_TIMEOUT)),
2064 Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT)));
2066 //timeout is how long the transaction can sit before rolling back
2067 //wait time is how long to wait for the transaction to start before throwing an exception
2068 private PolicyDBDaoTransactionInstance(int transactionTimeout, int transactionWaitTime){
2069 if(logger.isDebugEnabled()){
2070 logger.debug("\n\nPolicyDBDaoTransactionInstance() as PolicyDBDaoTransactionInstance() called:"
2071 + "\n transactionTimeout = " + transactionTimeout
2072 + "\n transactionWaitTime = " + transactionWaitTime + "\n\n");
2074 this.em = emf.createEntityManager();
2079 synchronized(emLock){
2081 startTransactionSynced(this.em,transactionWaitTime);
2082 } catch(Exception e){
2083 throw new PersistenceException("Could not lock transaction within "+transactionWaitTime+" milliseconds");
2086 class TransactionTimer implements Runnable {
2088 private int sleepTime;
2089 public TransactionTimer(int timeout){
2090 this.sleepTime = timeout;
2094 if(logger.isDebugEnabled()){
2095 Date date= new java.util.Date();
2096 logger.debug("\n\nTransactionTimer.run() - SLEEPING: "
2097 + "\n sleepTime (ms) = " + sleepTime
2098 + "\n TimeStamp = " + date.getTime()
2102 Thread.sleep(sleepTime);
2103 } catch (InterruptedException e) {
2104 //probably, the transaction was completed, the last thing we want to do is roll back
2105 if(logger.isDebugEnabled()){
2106 Date date= new java.util.Date();
2107 logger.debug("\n\nTransactionTimer.run() - WAKE Interrupt: "
2108 + "\n TimeStamp = " + date.getTime()
2113 if(logger.isDebugEnabled()){
2114 Date date= new java.util.Date();
2115 logger.debug("\n\nTransactionTimer.run() - WAKE Timeout: "
2116 + "\n TimeStamp = " + date.getTime()
2119 rollbackTransaction();
2124 transactionTimer = new Thread(new TransactionTimer(transactionTimeout),"transactionTimerThread");
2125 transactionTimer.start();
2130 private void checkBeforeOperationRun(){
2131 checkBeforeOperationRun(false);
2133 private void checkBeforeOperationRun(boolean justCheckOpen){
2134 if(!isTransactionOpen()){
2135 PolicyLogger.error("There is no transaction currently open");
2136 throw new IllegalStateException("There is no transaction currently open");
2138 if(operationRun && !justCheckOpen){
2139 PolicyLogger.error("An operation has already been performed and the current transaction should be committed");
2140 throw new IllegalStateException("An operation has already been performed and the current transaction should be committed");
2142 operationRun = true;
2145 public void commitTransaction() {
2146 synchronized(emLock){
2147 logger.debug("commitTransaction() as commitTransaction() called");
2148 if(!isTransactionOpen()){
2149 logger.warn("There is no open transaction to commit");
2152 } catch(Exception e){
2153 logger.error("Exception Occured"+e);
2158 em.getTransaction().commit();
2159 } catch(RollbackException e){
2160 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught RollbackException on em.getTransaction().commit()");
2161 throw new PersistenceException("The commit failed. Message:\n"+e.getMessage());
2166 if(newGroupId != null){
2168 notifyOthers(policyId,POLICY_NOTIFICATION,newGroupId);
2169 } catch(Exception e){
2170 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+","+newGroupId+")");
2174 notifyOthers(policyId,POLICY_NOTIFICATION);
2175 } catch(Exception e){
2176 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+")");
2181 //we don't want commit to fail just because this does
2182 if(newGroupId != null){
2184 notifyOthers(groupId,GROUP_NOTIFICATION,newGroupId);
2185 } catch(Exception e){
2186 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+","+newGroupId+")");
2190 notifyOthers(groupId,GROUP_NOTIFICATION);
2191 } catch(Exception e){
2192 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+")");
2197 //we don't want commit to fail just because this does
2199 notifyOthers(pdpId,PDP_NOTIFICATION);
2200 } catch(Exception e){
2201 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+pdpId+","+PDP_NOTIFICATION+")");
2205 if(transactionTimer instanceof Thread){
2206 transactionTimer.interrupt();
2211 public void rollbackTransaction() {
2212 logger.debug("rollbackTransaction() as rollbackTransaction() called");
2213 synchronized(emLock){
2214 if(isTransactionOpen()){
2217 em.getTransaction().rollback();
2218 } catch(Exception e){
2219 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not rollback transaction");
2223 }catch(Exception e){
2224 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not close EntityManager");
2230 }catch(Exception e){
2231 logger.warn("Could not close already closed transaction");
2236 if(transactionTimer instanceof Thread){
2237 transactionTimer.interrupt();
2243 private void createPolicy(PolicyRestAdapter policy, String username, String policyScope, String policyName, String policyDataString) {
2244 logger.debug("createPolicy(PolicyRestAdapter policy, String username, String policyScope, String policyName, String policyDataString) as createPolicy("+policy+", "+username+", "+policyScope+", "+policyName+", "+policyDataString+") called");
2245 synchronized(emLock){
2246 checkBeforeOperationRun();
2247 String configName = policyName;
2248 if(policyName.contains("Config_")){
2249 policyName = policyName.replace(".Config_", ":Config_");
2250 }else if(policyName.contains("Action_")){
2251 policyName = policyName.replace(".Action_", ":Action_");
2252 }else if(policyName.contains("Decision_")){
2253 policyName = policyName.replace(".Decision_", ":Decision_");
2255 policyName = policyName.split(":")[1];
2256 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName");
2257 createPolicyQuery.setParameter("scope", policyScope);
2258 createPolicyQuery.setParameter("policyName", policyName);
2259 List<?> createPolicyQueryList = createPolicyQuery.getResultList();
2260 PolicyEntity newPolicyEntity;
2262 if(createPolicyQueryList.size() < 1){
2263 newPolicyEntity = new PolicyEntity();
2265 } else if(createPolicyQueryList.size() > 1){
2266 PolicyLogger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2267 throw new PersistenceException("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2269 newPolicyEntity = (PolicyEntity)createPolicyQueryList.get(0);
2273 ActionBodyEntity newActionBodyEntity = null;
2274 if(policy.getPolicyType().equals("Action")){
2275 boolean abupdate = false;
2276 if(newPolicyEntity.getActionBodyEntity() == null){
2277 newActionBodyEntity = new ActionBodyEntity();
2279 newActionBodyEntity = em.find(ActionBodyEntity.class, newPolicyEntity.getActionBodyEntity().getActionBodyId());
2283 if(newActionBodyEntity != null){
2285 em.persist(newActionBodyEntity);
2287 //build the file path
2288 //trim the .xml off the end
2289 String policyNameClean = FilenameUtils.removeExtension(configName);
2290 String actionBodyName = policyNameClean + ".json";
2291 Path actionBodyPath = Paths.get(Webapps.getActionHome(), actionBodyName);
2292 if(logger.isDebugEnabled()){
2293 logger.debug("\nPolicyDBDao.createPolicy"
2294 + "\n actionBodyPath = " + actionBodyPath);
2296 //get the action body
2297 String actionBodyString = null;
2298 String actionBodyPathStr = null;
2299 InputStream fileContentStream = null;
2301 if (Files.exists(actionBodyPath)) {
2303 actionBodyPathStr = (actionBodyPath != null ? actionBodyPath.toString() : null);
2304 fileContentStream = new FileInputStream(actionBodyPathStr);
2305 actionBodyString = IOUtils.toString(fileContentStream);
2306 if(logger.isDebugEnabled()){
2307 logger.debug("\nPolicyDBDao.createPolicy"
2308 + "\n actionBodyPathStr = " + actionBodyPathStr
2309 + "\n actionBodyString = " + actionBodyString);
2311 } catch (FileNotFoundException e) {
2312 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new actionBodyPathStr FileInputStream("+actionBodyPathStr+")");
2313 throw new IllegalArgumentException("The actionBodyPathStr file path " + actionBodyPathStr + " does not exist"
2314 + "\nEXCEPTION: " + e);
2315 } catch(IOException e2){
2316 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on actionBodyPath newIOUtils.toString("+fileContentStream+")");
2317 throw new IllegalArgumentException("The actionBodyPath file path cannot be read" + fileContentStream
2318 + "\nEXCEPTION: " + e2);
2320 IOUtils.closeQuietly(fileContentStream);
2323 if(actionBodyString == null){
2324 throw new IllegalArgumentException("The file path (" + actionBodyPathStr + ") cannot be read");
2327 actionBodyString = "{}";
2330 newActionBodyEntity.setActionBody(actionBodyString);
2331 newActionBodyEntity.setActionBodyName(actionBodyName);
2332 newActionBodyEntity.setModifiedBy("PolicyDBDao.createPolicy()");
2333 newActionBodyEntity.setDeleted(false);
2335 newActionBodyEntity.setCreatedBy("PolicyDBDao.createPolicy()");
2337 if(logger.isDebugEnabled()){
2338 logger.debug("\nPolicyDBDao.createPolicy"
2339 + "\n newActionBodyEntity.getActionBody() = " + newActionBodyEntity.getActionBody()
2340 + "\n newActionBodyEntity.getActionBodyName() = " + newActionBodyEntity.getActionBodyName()
2341 + "\n newActionBodyEntity.getModifiedBy() = " + newActionBodyEntity.getModifiedBy()
2342 + "\n newActionBodyEntity.getCreatedBy() = " + newActionBodyEntity.getCreatedBy()
2343 + "\n newActionBodyEntity.isDeleted() = " + newActionBodyEntity.isDeleted()
2344 + "\n FLUSHING to DB");
2346 //push the actionBodyEntity to the DB
2349 //newActionBodyEntity == null
2350 //We have a actionBody in the policy but we found no actionBody in the DB
2351 String msg = "\n\nPolicyDBDao.createPolicy - Incoming Action policy had an "
2352 + "actionBody, but it could not be found in the DB for update."
2353 + "\n policyScope = " + policyScope
2354 + "\n policyName = " + policyName + "\n\n";
2355 PolicyLogger.error("PolicyDBDao.createPolicy - Incoming Action policy had an actionBody, but it could not be found in the DB for update: policyName = " + policyName);
2356 throw new IllegalArgumentException(msg);
2360 ConfigurationDataEntity newConfigurationDataEntity;
2361 if(policy.getPolicyType().equals("Config")){
2362 boolean configUpdate;
2363 if(newPolicyEntity.getConfigurationData() == null){
2364 newConfigurationDataEntity = new ConfigurationDataEntity();
2365 configUpdate = false;
2367 newConfigurationDataEntity = em.find(ConfigurationDataEntity.class, newPolicyEntity.getConfigurationData().getConfigurationDataId());
2368 configUpdate = true;
2371 if(newConfigurationDataEntity != null){
2373 em.persist(newConfigurationDataEntity);
2375 if(!stringEquals(newConfigurationDataEntity.getConfigurationName(),getConfigFile(configName,policy))){
2376 newConfigurationDataEntity.setConfigurationName(getConfigFile(configName,policy));
2378 if(newConfigurationDataEntity.getConfigType() == null || !newConfigurationDataEntity.getConfigType().equals(policy.getConfigType())){
2379 newConfigurationDataEntity.setConfigType(policy.getConfigType());
2382 newConfigurationDataEntity.setCreatedBy(username);
2384 if(newConfigurationDataEntity.getModifiedBy() == null || !newConfigurationDataEntity.getModifiedBy().equals(username)){
2385 newConfigurationDataEntity.setModifiedBy(username);
2387 if(newConfigurationDataEntity.getDescription() == null || !newConfigurationDataEntity.getDescription().equals("")){
2388 newConfigurationDataEntity.setDescription("");
2390 if(newConfigurationDataEntity.getConfigBody() == null || newConfigurationDataEntity.getConfigBody().isEmpty() ||
2391 (!newConfigurationDataEntity.getConfigBody().equals(policy.getConfigBodyData()))){
2392 //hopefully one of these won't be null
2393 if(policy.getConfigBodyData() == null || policy.getConfigBodyData().isEmpty()){
2394 newConfigurationDataEntity.setConfigBody(policy.getJsonBody());
2396 newConfigurationDataEntity.setConfigBody(policy.getConfigBodyData());
2399 if(newConfigurationDataEntity.isDeleted() == true){
2400 newConfigurationDataEntity.setDeleted(false);
2405 //We have a configurationData body in the policy but we found no configurationData body in the DB
2406 String msg = "\n\nPolicyDBDao.createPolicy - Incoming Config policy had a "
2407 + "configurationData body, but it could not be found in the DB for update."
2408 + "\n policyScope = " + policyScope
2409 + "\n policyName = " + policyName + "\n\n";
2410 PolicyLogger.error("PolicyDBDao.createPolicy - Incoming Config policy had a configurationData body, but it could not be found in the DB for update: policyName = " + policyName);
2411 throw new IllegalArgumentException(msg);
2415 newConfigurationDataEntity = null;
2418 em.persist(newPolicyEntity);
2421 policyId = newPolicyEntity.getPolicyId();
2423 if(!stringEquals(newPolicyEntity.getPolicyName(),policyName)){
2424 newPolicyEntity.setPolicyName(policyName);
2426 if(!stringEquals(newPolicyEntity.getCreatedBy(),username)){
2427 newPolicyEntity.setCreatedBy(username);
2429 if(!stringEquals(newPolicyEntity.getDescription(),policy.getPolicyDescription())){
2430 newPolicyEntity.setDescription(policy.getPolicyDescription());
2432 if(!stringEquals(newPolicyEntity.getModifiedBy(),username)){
2433 newPolicyEntity.setModifiedBy(username);
2435 if(!stringEquals(newPolicyEntity.getPolicyData(),policyDataString)){
2436 newPolicyEntity.setPolicyData(policyDataString);
2438 if(!stringEquals(newPolicyEntity.getScope(),policyScope)){
2439 newPolicyEntity.setScope(policyScope);
2441 if(newPolicyEntity.isDeleted() == true){
2442 newPolicyEntity.setDeleted(false);
2444 newPolicyEntity.setConfigurationData(newConfigurationDataEntity);
2445 newPolicyEntity.setActionBodyEntity(newActionBodyEntity);
2449 this.policyId = newPolicyEntity.getPolicyId();
2455 @SuppressWarnings("unused")
2456 public PolicyEntity getPolicy(int policyID){
2457 return getPolicy(policyID,null,null);
2459 public PolicyEntity getPolicy(String policyName,String scope){
2460 return getPolicy(-1,policyName,scope);
2462 private PolicyEntity getPolicy(int policyID, String policyName,String scope){
2463 logger.debug("getPolicy(int policyId, String policyName) as getPolicy("+policyID+","+policyName+") called");
2464 if(policyID < 0 && isNullOrEmpty(policyName,scope)){
2465 throw new IllegalArgumentException("policyID must be at least 0 or policyName must be not null or blank");
2468 synchronized(emLock){
2469 checkBeforeOperationRun(true);
2470 //check if group exists
2473 if(!isNullOrEmpty(policyName,scope)){
2474 policyId = policyName;
2475 policyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:name AND p.scope=:scope");
2476 policyQuery.setParameter("name", policyId);
2477 policyQuery.setParameter("scope", scope);
2479 policyId = String.valueOf(policyID);
2480 policyQuery = em.createNamedQuery("PolicyEntity.FindById");
2481 policyQuery.setParameter("id", policyId);
2483 List<?> policyQueryList;
2485 policyQueryList = policyQuery.getResultList();
2486 }catch(Exception e){
2487 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get policy with policyQuery.getResultList()");
2488 throw new PersistenceException("Query failed trying to get policy "+policyId);
2490 if(policyQueryList.size() < 1){
2491 PolicyLogger.error("Policy does not exist with id "+policyId);
2492 throw new PersistenceException("Group policy is being added to does not exist with id "+policyId);
2493 } else if(policyQueryList.size() > 1){
2494 PolicyLogger.error("Somehow, more than one policy with the id "+policyId+" were found in the database");
2495 throw new PersistenceException("Somehow, more than one policy with the id "+policyId+" were found in the database");
2497 return (PolicyEntity)policyQueryList.get(0);
2502 public void renamePolicy(String oldPath, String newPath,String username){
2503 String[] oldPolicy = getScopeAndNameAndType(oldPath);
2504 String[] newPolicy = getScopeAndNameAndType(newPath);
2505 if(oldPolicy == null || newPolicy == null){
2506 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2507 +oldPath+", "+newPath);
2508 throw new IllegalArgumentException("Could not parse one or more of the path names");
2510 synchronized (emLock) {
2511 checkBeforeOperationRun();
2513 PolicyEntity existingPolicy;
2514 boolean existingPolicyDeleted = false;
2515 List<?> groups = null;
2517 existingPolicy = getPolicy(newPolicy[1],newPolicy[0]);
2518 } catch(Exception e){
2519 existingPolicy = null;
2521 if(existingPolicy != null && !existingPolicy.isDeleted()){
2522 logger.error("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
2523 throw new IllegalArgumentException("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
2524 } else if(existingPolicy != null && existingPolicy.isDeleted()){
2526 Query getGroups = em.createQuery("SELECT g FROM GroupEntity g JOIN g.policies p WHERE p.policyId=:pid");
2528 getGroups.setParameter("pid", existingPolicy.getPolicyId());
2529 groups = getGroups.getResultList();
2530 }catch(Exception e){
2531 groups = new LinkedList<GroupEntity>();
2533 for(Object o : groups){
2534 GroupEntity group = (GroupEntity)o;
2535 group.removePolicyFromGroup(existingPolicy);
2539 }catch(Exception e){
2540 logger.error("Error while removing the policy from groups: "+existingPolicy.getPolicyName());
2543 em.remove(existingPolicy);
2545 }catch(Exception e){
2546 logger.error("Could not remove the existing deleted policy: "+existingPolicy.getPolicyName());
2548 existingPolicyDeleted = true;
2549 //create the new policy
2550 //for each of the groups, add the new policy
2553 PolicyEntity policyToRename;
2555 policyToRename = getPolicy(oldPolicy[1],oldPolicy[0]);
2556 } catch(Exception e){
2557 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "PolicyDBDao", "Could not get policy record to rename: "
2559 throw new PersistenceException("Could not get policy record to rename");
2561 String policyDataString = null;
2562 InputStream fileContentStream = null;
2563 String policyFilePath = Paths.get(oldPath).toAbsolutePath().toString();
2564 //I want to try the old path first, then if it doesn't work, try the new path
2565 for(int i=0;i<2;i++){
2567 fileContentStream = new FileInputStream(policyFilePath);
2568 policyDataString = IOUtils.toString(fileContentStream);
2569 } catch (FileNotFoundException e) {
2570 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new FileInputStream("+policyFilePath+")");
2571 //if we can't find the oldPath, we'll try the new path
2573 policyFilePath = Paths.get(newPath).toAbsolutePath().toString();
2576 throw new IllegalArgumentException("The file path does not exist");
2577 } catch(IOException e2){
2578 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on newIOUtils.toString("+fileContentStream+")");
2579 throw new IllegalArgumentException("The file path cannot be read");
2581 IOUtils.closeQuietly(fileContentStream);
2583 if(policyDataString == null){
2584 throw new IllegalArgumentException("The file path cannot be read");
2589 policyToRename.setPolicyName(newPolicy[1]);
2590 policyToRename.setPolicyData(policyDataString);
2591 policyToRename.setScope(newPolicy[0]);
2592 policyToRename.setModifiedBy(username);
2593 if(policyToRename.getConfigurationData() != null){
2594 String configType = policyToRename.getConfigurationData().getConfigType();
2595 policyToRename.getConfigurationData().setConfigurationName(getConfigFile(newPolicy[1], configType));
2596 policyToRename.getConfigurationData().setModifiedBy(username);
2598 if(policyToRename.getActionBodyEntity() != null){
2599 String newActionName = newPolicy[0]+"."+removeFileExtension(newPolicy[1])+".json";
2600 policyToRename.getActionBodyEntity().setActionBodyName(newActionName);
2601 policyToRename.getActionBodyEntity().setModifiedBy(username);
2603 if(existingPolicyDeleted){
2604 for(Object o : groups){
2606 GroupEntity group = (GroupEntity)o;
2607 group.addPolicyToGroup(policyToRename);
2611 this.policyId = policyToRename.getPolicyId();
2612 this.newGroupId = oldPath;
2617 public GroupEntity getGroup(long groupKey){
2618 logger.debug("getGroup(int groupKey) as getGroup("+groupKey+") called");
2620 throw new IllegalArgumentException("groupKey must be at least 0");
2622 synchronized(emLock){
2623 checkBeforeOperationRun(true);
2624 //check if group exists
2625 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupKey=:groupKey");
2626 groupQuery.setParameter("groupKey", groupKey);
2627 List<?> groupQueryList;
2629 groupQueryList = groupQuery.getResultList();
2630 }catch(Exception e){
2631 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group with groupQuery.getResultList()");
2632 throw new PersistenceException("Query failed trying to get group "+groupKey);
2634 if(groupQueryList.size() < 1){
2635 PolicyLogger.error("Group does not exist with groupKey "+groupKey);
2636 throw new PersistenceException("Group does not exist with groupKey "+groupKey);
2637 } else if(groupQueryList.size() > 1){
2638 PolicyLogger.error("Somehow, more than one group with the groupKey "+groupKey+" were found in the database");
2639 throw new PersistenceException("Somehow, more than one group with the groupKey "+groupKey+" were found in the database");
2641 return (GroupEntity)groupQueryList.get(0);
2646 public GroupEntity getGroup(String groupId){
2647 logger.debug("getGroup(String groupId) as getGroup("+groupId+") called");
2648 if(isNullOrEmpty(groupId)){
2649 throw new IllegalArgumentException("groupId must not be null or empty");
2651 synchronized(emLock){
2652 checkBeforeOperationRun(true);
2653 //check if group exists
2654 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId");
2655 groupQuery.setParameter("groupId", groupId);
2656 List<?> groupQueryList;
2658 groupQueryList = groupQuery.getResultList();
2659 }catch(Exception e){
2660 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group with groupQuery.getResultList()");
2661 throw new PersistenceException("Query failed trying to get group "+groupId);
2663 if(groupQueryList.size() < 1){
2664 PolicyLogger.error("Group does not exist with id "+groupId);
2665 throw new PersistenceException("Group does not exist with id "+groupId);
2666 } else if(groupQueryList.size() > 1){
2667 PolicyLogger.error("Somehow, more than one group with the id "+groupId+" were found in the database");
2668 throw new PersistenceException("Somehow, more than one group with the id "+groupId+" were found in the database");
2670 return (GroupEntity)groupQueryList.get(0);
2674 public List<?> getPdpsInGroup(long groupKey){
2675 logger.debug("getPdpsInGroup(int groupKey) as getPdpsInGroup("+groupKey+") called");
2677 throw new IllegalArgumentException("groupId must not be < 0");
2679 synchronized(emLock){
2680 checkBeforeOperationRun(true);
2681 Query pdpsQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group");
2682 pdpsQuery.setParameter("group", getGroup(groupKey));
2683 return pdpsQuery.getResultList();
2687 public PdpEntity getPdp(long pdpKey){
2688 logger.debug("getPdp(int pdpKey) as getPdp("+pdpKey+") called");
2690 throw new IllegalArgumentException("pdpKey must be at least 0");
2692 synchronized(emLock){
2693 checkBeforeOperationRun(true);
2694 //check if group exists
2695 Query pdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpKey=:pdpKey");
2696 pdpQuery.setParameter("pdpKey", pdpKey);
2697 List<?> pdpQueryList;
2699 pdpQueryList = pdpQuery.getResultList();
2700 }catch(Exception e){
2701 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp with pdpQuery.getResultList()");
2702 throw new PersistenceException("Query failed trying to get pdp "+pdpKey);
2704 if(pdpQueryList.size() < 1){
2705 PolicyLogger.error("Pdp does not exist with pdpKey "+pdpKey);
2706 throw new PersistenceException("Pdp does not exist with pdpKey "+pdpKey);
2707 } else if(pdpQueryList.size() > 1){
2708 PolicyLogger.error("Somehow, more than one pdp with the pdpKey "+pdpKey+" were found in the database");
2709 throw new PersistenceException("Somehow, more than one pdp with the pdpKey "+pdpKey+" were found in the database");
2711 return (PdpEntity)pdpQueryList.get(0);
2715 public void deletePolicy(String policyToDeletes){
2716 synchronized(emLock){
2717 checkBeforeOperationRun();
2718 logger.debug("deletePolicy(String policyToDeletes) as deletePolicy("+policyToDeletes+") called");
2719 String[] scopeNameAndType = getScopeAndNameAndType(policyToDeletes);
2720 if(scopeNameAndType == null){
2721 throw new IllegalArgumentException("Could not parse file path");
2723 String realScope = scopeNameAndType[0];
2724 String realName = scopeNameAndType[1];
2725 Query deletePolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName AND p.deleted=:deleted");
2726 deletePolicyQuery.setParameter("scope",realScope);
2727 deletePolicyQuery.setParameter("policyName", realName);
2728 deletePolicyQuery.setParameter("deleted", false);
2729 List<?> deletePolicyQueryList = deletePolicyQuery.getResultList();
2730 if(deletePolicyQueryList.size() < 1){
2731 logger.warn("The policy being deleted could not be found.");
2733 } else if(deletePolicyQueryList.size() > 1){
2734 PolicyLogger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2735 throw new PersistenceException("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2737 PolicyEntity policyToDelete = (PolicyEntity)deletePolicyQueryList.get(0);
2738 policyToDelete.setDeleted(true);
2739 if(policyToDelete.getConfigurationData() != null){
2740 ConfigurationDataEntity cde = em.find(ConfigurationDataEntity.class,policyToDelete.getConfigurationData().getConfigurationDataId());
2742 cde.setDeleted(true);
2745 if(policyToDelete.getActionBodyEntity() != null){
2746 ActionBodyEntity abe = em.find(ActionBodyEntity.class,policyToDelete.getActionBodyEntity().getActionBodyId());
2748 abe.setDeleted(true);
2753 this.policyId = policyToDelete.getPolicyId();
2762 public boolean isTransactionOpen() {
2763 logger.debug("isTransactionOpen() as isTransactionOpen() called");
2764 synchronized(emLock){
2765 return em.isOpen() && em.getTransaction().isActive();
2771 public void clonePolicy(String oldPolicyPath, String newPolicyPath, String username){
2772 String[] oldPolicyData = getScopeAndNameAndType(oldPolicyPath);
2773 String[] newPolicyData = getScopeAndNameAndType(newPolicyPath);
2774 if(oldPolicyData == null || newPolicyData == null){
2775 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2776 +oldPolicyPath+", "+newPolicyPath);
2777 throw new IllegalArgumentException("Could not parse the oldPolicyPath or newPolicyPath");
2779 PolicyEntity oldPolicy;
2781 oldPolicy = getPolicy(oldPolicyData[1],oldPolicyData[0]);
2782 }catch(Exception e){
2783 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "PolicyDBDao", "Could not get policy record to clone: "
2785 throw new PersistenceException("Could not get policy record to clone");
2787 ConfigurationDataEntity clonedConfig = null;
2788 if(oldPolicy.getConfigurationData() != null){
2789 clonedConfig = new ConfigurationDataEntity();
2790 em.persist(clonedConfig);
2791 clonedConfig.setConfigBody(oldPolicy.getConfigurationData().getConfigBody());
2792 clonedConfig.setConfigType(oldPolicy.getConfigurationData().getConfigType());
2793 clonedConfig.setCreatedBy(username);
2794 clonedConfig.setConfigurationName(getConfigFile(newPolicyData[1], oldPolicy.getConfigurationData().getConfigType()));
2795 clonedConfig.setDescription(oldPolicy.getConfigurationData().getDescription());
2796 clonedConfig.setModifiedBy(username);
2799 ActionBodyEntity clonedAction = null;
2800 if(oldPolicy.getActionBodyEntity() != null){
2801 clonedAction = new ActionBodyEntity();
2802 em.persist(clonedAction);
2803 clonedAction.setActionBody(oldPolicy.getActionBodyEntity().getActionBody());
2804 clonedAction.setActionBodyName(newPolicyData[0]+"."+newPolicyData[1]+".json");
2805 clonedAction.setCreatedBy(username);
2806 clonedAction.setModifiedBy(username);
2814 public void createPolicy(String filePath, String username) {
2815 logger.debug("createPolicy(String filePath, String username) as createPolicy("+filePath+","+username+") called");
2816 //get just the scope and file name
2817 //its actually scope, name, and type now
2818 String[] scopeAndName = getScopeAndNameAndType(filePath);
2819 if(scopeAndName == null){
2820 throw new IllegalArgumentException("The file path could not be parsed");
2822 PolicyRestAdapter policy = new PolicyRestAdapter();
2824 policy.setPolicyType(scopeAndName[2]);
2825 policy.setPolicyDescription("");
2827 String policyName = scopeAndName[1];
2829 policyName = stripPolicyName(policyName);
2830 }catch(IllegalArgumentException e){
2831 if(scopeAndName[2].equals("Config")){
2832 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception calling stripPolicyName with policy name: "+policyName);
2833 throw new IllegalArgumentException(e.getMessage(),e);
2835 logger.warn(e.getMessage());
2838 policy.setPolicyName(policyName);
2839 String policyDataString = null;
2840 InputStream fileContentStream = null;
2842 fileContentStream = new FileInputStream(filePath);
2843 policyDataString = IOUtils.toString(fileContentStream);
2844 } catch (FileNotFoundException e) {
2845 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new FileInputStream("+filePath+")");
2846 throw new IllegalArgumentException("The file path does not exist");
2847 } catch(IOException e2){
2848 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on newIOUtils.toString("+fileContentStream+")");
2849 throw new IllegalArgumentException("The file path cannot be read");
2851 IOUtils.closeQuietly(fileContentStream);
2853 if(policyDataString == null){
2854 throw new IllegalArgumentException("The file path cannot be read");
2857 String policyDescription = getElementFromXMLString("/Description", policyDataString);
2858 if(policyDescription != null){
2859 policy.setPolicyDescription(policyDescription);
2861 } catch(Exception e){
2862 logger.warn("Could not get description from the policy file");
2864 if(scopeAndName[2].equals("Config")){
2865 //this method is not used for config, since there is no way to get config info (could be modified to)
2868 configPath = evaluateXPath("/Policy/Rule/AdviceExpressions/AdviceExpression[contains(@AdviceId,'ID')]/AttributeAssignmentExpression[@AttributeId='URLID']/AttributeValue/text()", policyDataString);
2869 if(configPath == null){
2870 throw new NullPointerException("configPath is null");
2872 } catch(Exception e){
2873 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get config file path from policy file");
2874 throw new IllegalArgumentException("Could not get config file path from policy file");
2876 configPath = processConfigPath(configPath);
2877 logger.debug("The location of our config file is: "+configPath);
2878 policy.setConfigType(getPolicySubType(configPath));
2879 logger.debug("Config type is: "+policy.getConfigType());
2881 String configDataString = readConfigFile(configPath);
2882 policy.setConfigBodyData(configDataString);
2884 createPolicy(policy,username,scopeAndName[0],scopeAndName[1],policyDataString);
2887 private String processConfigPath(String configPath){
2888 String webappsPath = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS);
2889 if(webappsPath == null){
2890 logger.error("Webapps property does not exist");
2891 throw new IllegalArgumentException("Webapps property does not exist");
2893 configPath = configPath.replace("$URL", webappsPath);
2894 //make sure the correct slashes are in
2896 configPath = Paths.get(configPath).toString();
2897 } catch(InvalidPathException e){
2898 logger.error("Invalid config path: "+configPath);
2899 throw new IllegalArgumentException("Invalid config path: "+configPath);
2903 private String readConfigFile(String configPath){
2904 String configDataString = null;
2905 InputStream configContentStream = null;
2907 configContentStream = new FileInputStream(configPath);
2908 configDataString = IOUtils.toString(configContentStream);
2909 } catch (FileNotFoundException e) {
2910 logger.error("Caught FileNotFoundException on new FileInputStream("+configPath+")",e);
2911 throw new IllegalArgumentException("The config file path does not exist");
2912 } catch(IOException e2){
2913 logger.error("Caught IOException on newIOUtils.toString("+configContentStream+")",e2);
2914 throw new IllegalArgumentException("The config file path cannot be read");
2916 IOUtils.closeQuietly(configContentStream);
2918 if(configDataString == null){
2919 throw new IllegalArgumentException("The config file path cannot be read");
2921 return configDataString;
2925 public void createPolicy(Policy policy, String username){
2926 logger.debug("createPolicy(PolicyRestAdapter policy, String username) as createPolicy("+policy+","+username+") called");
2927 String policyScope = policy.policyAdapter.getDomainDir().replace(File.separator, ".");
2928 //Does not need to be XACMLPolicyWriterWithPapNotify since it is already in the PAP
2929 //and this transaction is intercepted up stream.
2930 InputStream policyXmlStream = XACMLPolicyWriter.getXmlAsInputStream((PolicyType)policy.getCorrectPolicyDataObject());
2931 String policyDataString;
2933 policyDataString = IOUtils.toString(policyXmlStream);
2934 } catch (IOException e) {
2935 policyDataString = "could not read";
2936 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught IOException on IOUtils.toString("+policyXmlStream+")");
2937 throw new IllegalArgumentException("Cannot parse the policy xml from the PolicyRestAdapter.");
2939 IOUtils.closeQuietly(policyXmlStream);
2940 String configPath = "";
2941 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Config")) {
2942 configPath = evaluateXPath("/Policy/Rule/AdviceExpressions/AdviceExpression[contains(@AdviceId,'ID')]/AttributeAssignmentExpression[@AttributeId='URLID']/AttributeValue/text()", policyDataString);
2943 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")) {
2944 configPath = evaluateXPath("/Policy/Rule/ObligationExpressions/ObligationExpression[contains(@ObligationId, " +policy.policyAdapter.getActionAttribute()+ ")]/AttributeAssignmentExpression[@AttributeId='body']/AttributeValue/text()", policyDataString);
2947 String prefix = null;
2948 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Config")) {
2950 prefix = configPath.substring(configPath.indexOf(policyScope+".")+policyScope.concat(".").length(), configPath.indexOf(policy.policyAdapter.getPolicyName()));
2951 if(isNullOrEmpty(policy.policyAdapter.getConfigBodyData())){
2952 String configData = "";
2954 String newConfigPath = configPath;
2956 newConfigPath = processConfigPath(newConfigPath);
2957 }catch(Exception e2){
2958 logger.error("Could not process config path: "+newConfigPath,e2);
2960 configData = readConfigFile(newConfigPath);
2961 }catch(Exception e){
2962 logger.error("Could not read config body data for "+configPath,e);
2964 policy.policyAdapter.setConfigBodyData(configData);
2966 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")) {
2968 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Decision")) {
2969 prefix = "Decision_";
2972 if(!(policy.policyAdapter.getData() instanceof PolicyType)){
2973 PolicyLogger.error("The data field is not an instance of PolicyType");
2974 throw new IllegalArgumentException("The data field is not an instance of PolicyType");
2976 String finalName = policyScope + "." + prefix+policy.policyAdapter.getPolicyName()+"."+((PolicyType)policy.policyAdapter.getData()).getVersion()+".xml";
2977 if(policy.policyAdapter.getConfigType() == null || policy.policyAdapter.getConfigType().equals("")){
2978 //get the config file extension
2980 if (configPath != null) {
2981 if (!configPath.equalsIgnoreCase("")) {
2982 ext = configPath.substring(configPath.lastIndexOf('.'), configPath.length());;
2986 if(ext.contains("txt")){
2987 policy.policyAdapter.setConfigType(OTHER_CONFIG);
2988 } else if(ext.contains("json")){
2989 policy.policyAdapter.setConfigType(JSON_CONFIG);
2990 } else if(ext.contains("xml")){
2991 policy.policyAdapter.setConfigType(XML_CONFIG);
2992 } else if(ext.contains("properties")){
2993 policy.policyAdapter.setConfigType(PROPERTIES_CONFIG);
2995 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")){
2996 policy.policyAdapter.setConfigType(JSON_CONFIG);
3000 createPolicy(policy.policyAdapter, username, policyScope,finalName,policyDataString);
3005 public void close(){
3006 synchronized(emLock){
3008 if(em.getTransaction().isActive()){
3009 em.getTransaction().rollback();
3013 if(transactionTimer instanceof Thread){
3014 transactionTimer.interrupt();
3022 public void createGroup(String groupId, String groupName, String groupDescription, String username) {
3023 logger.debug("deletePolicy(String policyToDeletes) as createGroup("+groupId+", "+groupName+", "+groupDescription+") called");
3024 if(isNullOrEmpty(groupId, groupName, username)){
3025 throw new IllegalArgumentException("groupId, groupName, and username must not be null or empty");
3027 if(!(groupDescription instanceof String)){
3028 groupDescription = "";
3031 synchronized(emLock){
3032 checkBeforeOperationRun();
3033 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3034 checkGroupQuery.setParameter("groupId", groupId);
3035 checkGroupQuery.setParameter("deleted", false);
3036 List<?> checkGroupQueryList;
3038 checkGroupQueryList = checkGroupQuery.getResultList();
3039 } catch(Exception e){
3040 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on checkGroupQuery.getResultList()");
3041 throw new PersistenceException("Query failed trying to check for existing group");
3043 if(checkGroupQueryList.size() > 0){
3044 PolicyLogger.error("The group being added already exists with id "+groupId);
3045 throw new PersistenceException("The group being added already exists with id "+groupId);
3047 GroupEntity newGroup = new GroupEntity();
3048 em.persist(newGroup);
3049 newGroup.setCreatedBy(username);
3050 newGroup.setModifiedBy(username);
3051 newGroup.setGroupName(groupName);
3052 newGroup.setGroupId(groupId);
3053 newGroup.setDescription(groupDescription);
3056 this.groupId = newGroup.getGroupKey();
3061 public void updateGroup(EcompPDPGroup group, String username){
3062 logger.debug("updateGroup(PDPGroup group) as updateGroup("+group+","+username+") called");
3064 throw new IllegalArgumentException("PDPGroup group must not be null");
3066 if(isNullOrEmpty(group.getId(), username)){
3067 throw new IllegalArgumentException("group.getId() and username must not be null or empty");
3070 synchronized(emLock){
3071 checkBeforeOperationRun();
3072 Query getGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3073 getGroupQuery.setParameter("groupId", group.getId());
3074 getGroupQuery.setParameter("deleted", false);
3075 List<?> getGroupQueryList;
3077 getGroupQueryList = getGroupQuery.getResultList();
3078 } catch(Exception e){
3079 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getGroupQuery.getResultList()");
3080 throw new PersistenceException("Query failed trying to get group "+group.getId()+" for editing");
3082 if(getGroupQueryList.size() < 1){
3083 PolicyLogger.error("The group cannot be found to update with id "+group.getId());
3084 throw new PersistenceException("The group cannot be found to update with id "+group.getId());
3085 } else if(getGroupQueryList.size() > 1){
3086 PolicyLogger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3087 throw new PersistenceException("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3089 GroupEntity groupToUpdate = (GroupEntity)getGroupQueryList.get(0);
3090 if(!stringEquals(groupToUpdate.getModifiedBy(), username)){
3091 groupToUpdate.setModifiedBy(username);
3093 if(group.getDescription() != null && !stringEquals(group.getDescription(),groupToUpdate.getDescription())){
3094 groupToUpdate.setDescription(group.getDescription());
3096 //let's find out what policies have been deleted
3097 StdPDPGroup oldGroup = null;
3099 oldGroup = (StdPDPGroup) papEngine.getGroup(group.getId());
3100 } catch (PAPException e1) {
3101 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "We cannot get the group from the papEngine to delete policies");
3103 if(oldGroup == null){
3104 PolicyLogger.error("We cannot get the group from the papEngine to delete policies");
3107 Set<String> newPolicySet = new HashSet<String>(group.getPolicies().size());
3108 //a multiple of n runtime is faster than n^2, so I am using a hashset to do the comparison
3109 for(PDPPolicy pol: group.getPolicies()){
3110 newPolicySet.add(pol.getId());
3112 for(PDPPolicy pol : oldGroup.getPolicies()){
3113 //should be fast since getPolicies uses a HashSet in StdPDPGroup
3114 if(!newPolicySet.contains(pol.getId())){
3115 String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(pol.getId());
3116 PolicyEntity policyToDelete;
3118 policyToDelete = getPolicy(scopeAndName[0],scopeAndName[1]);
3119 }catch(Exception e){
3120 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get policy to remove: "+pol.getId());
3121 throw new PersistenceException("Could not get policy to remove: "+pol.getId());
3123 groupToUpdate.getPolicies().remove(policyToDelete);
3128 if(group.getName() != null && !stringEquals(group.getName(),groupToUpdate.getgroupName())){
3129 //we need to check if the new id exists in the database
3130 String newGroupId = createNewPDPGroupId(group.getName());
3131 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3132 checkGroupQuery.setParameter("groupId", newGroupId);
3133 checkGroupQuery.setParameter("deleted", false);
3134 List<?> checkGroupQueryList;
3136 checkGroupQueryList = checkGroupQuery.getResultList();
3137 } catch(Exception e){
3138 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on checkGroupQuery.getResultList()");
3139 throw new PersistenceException("Query failed trying to check for existing group");
3141 if(checkGroupQueryList.size() != 0){
3142 PolicyLogger.error("The new group name already exists, group id "+newGroupId);
3143 throw new PersistenceException("The new group name already exists, group id "+newGroupId);
3145 groupToUpdate.setGroupId(newGroupId);
3146 groupToUpdate.setGroupName(group.getName());
3147 this.newGroupId = group.getId();
3151 this.groupId = groupToUpdate.getGroupKey();
3156 public void addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) {
3157 logger.debug("addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) as addPdpToGroup("+pdpID+", "+groupID+", "+pdpName+", "+pdpDescription+", "+pdpJmxPort+", "+username+") called");
3158 if(isNullOrEmpty(pdpID, groupID,pdpName,username)){
3159 throw new IllegalArgumentException("pdpID, groupID, pdpName, and username must not be null or empty");
3161 if(!(pdpDescription instanceof String)){
3162 pdpDescription = "";
3164 synchronized(emLock){
3165 checkBeforeOperationRun();
3166 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3167 checkGroupQuery.setParameter("groupId", groupID);
3168 checkGroupQuery.setParameter("deleted", false);
3169 List<?> checkGroupQueryList;
3171 checkGroupQueryList = checkGroupQuery.getResultList();
3172 } catch(Exception e){
3173 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check for existing group on checkGroupQuery.getResultList()");
3174 throw new PersistenceException("Query failed trying to check for existing group");
3176 if(checkGroupQueryList.size() != 1){
3177 PolicyLogger.error("The group does not exist");
3178 throw new PersistenceException("The group does not exist");
3180 Query checkDuplicateQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3181 checkDuplicateQuery.setParameter("pdpId", pdpID);
3182 checkDuplicateQuery.setParameter("deleted", false);
3183 List<?> checkDuplicateList;
3185 checkDuplicateList = checkDuplicateQuery.getResultList();
3186 } catch(Exception e){
3187 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check for duplicate PDP "+pdpID+" on checkDuplicateQuery.getResultList()");
3188 throw new PersistenceException("Query failed trying to check for duplicate PDP "+pdpID);
3191 if(checkDuplicateList.size() > 0){
3192 logger.warn("PDP already exists with id "+pdpID);
3193 newPdp = (PdpEntity)checkDuplicateList.get(0);
3195 newPdp = new PdpEntity();
3199 newPdp.setCreatedBy(username);
3200 newPdp.setDeleted(false);
3201 newPdp.setDescription(pdpDescription);
3202 newPdp.setGroup((GroupEntity)checkGroupQueryList.get(0));
3203 newPdp.setJmxPort(pdpJmxPort);
3204 newPdp.setModifiedBy(username);
3205 newPdp.setPdpId(pdpID);
3206 newPdp.setPdpName(pdpName);
3209 this.pdpId = newPdp.getPdpKey();
3216 public void updatePdp(EcompPDP pdp, String username){
3217 logger.debug("updatePdp(PDP pdp, String username) as updatePdp("+pdp+","+username+") called");
3219 throw new IllegalArgumentException("PDP pdp must not be null");
3221 if(isNullOrEmpty(pdp.getId(),username)){
3222 throw new IllegalArgumentException("pdp.getId() and username must not be null or empty");
3225 synchronized(emLock){
3226 checkBeforeOperationRun();
3227 Query getPdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3228 getPdpQuery.setParameter("pdpId", pdp.getId());
3229 getPdpQuery.setParameter("deleted", false);
3230 List<?> getPdpQueryList;
3232 getPdpQueryList = getPdpQuery.getResultList();
3233 } catch(Exception e){
3234 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getPdpQuery.getResultList()");
3235 throw new PersistenceException("Query failed trying to get PDP "+pdp.getId());
3237 if(getPdpQueryList.size() < 1){
3238 PolicyLogger.error("The pdp cannot be found to update with id "+pdp.getId());
3239 throw new PersistenceException("The pdp cannot be found to update with id "+pdp.getId());
3240 } else if(getPdpQueryList.size() > 1){
3241 PolicyLogger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3242 throw new PersistenceException("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3244 PdpEntity pdpToUpdate = (PdpEntity)getPdpQueryList.get(0);
3245 if(!stringEquals(pdpToUpdate.getModifiedBy(), username)){
3246 pdpToUpdate.setModifiedBy(username);
3248 if(pdp.getDescription() != null && !stringEquals(pdp.getDescription(),pdpToUpdate.getDescription())){
3249 pdpToUpdate.setDescription(pdp.getDescription());
3251 if(pdp.getName() != null && !stringEquals(pdp.getName(),pdpToUpdate.getPdpName())){
3252 pdpToUpdate.setPdpName(pdp.getName());
3254 if(pdp.getJmxPort() != null && !pdp.getJmxPort().equals(pdpToUpdate.getJmxPort())){
3255 pdpToUpdate.setJmxPort(pdp.getJmxPort());
3259 this.pdpId = pdpToUpdate.getPdpKey();
3264 public void movePdp(EcompPDP pdp, EcompPDPGroup group, String username){
3265 logger.debug("movePdp(PDP pdp, PDPGroup group, String username) as movePdp("+pdp+","+group+","+username+") called");
3266 if(pdp == null || group == null){
3267 throw new IllegalArgumentException("PDP pdp and PDPGroup group must not be null");
3269 if(isNullOrEmpty(username,pdp.getId(),group.getId())){
3270 throw new IllegalArgumentException("pdp.getId(), group.getId(), and username must not be null or empty");
3273 synchronized(emLock){
3274 checkBeforeOperationRun();
3275 //check if pdp exists
3276 Query getPdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3277 getPdpQuery.setParameter("pdpId", pdp.getId());
3278 getPdpQuery.setParameter("deleted", false);
3279 List<?> getPdpQueryList;
3281 getPdpQueryList = getPdpQuery.getResultList();
3282 } catch(Exception e){
3283 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getPdpQuery.getResultList()");
3284 throw new PersistenceException("Query failed trying to get pdp to move with id "+pdp.getId());
3286 if(getPdpQueryList.size() < 1){
3287 PolicyLogger.error("The pdp cannot be found to move with id "+pdp.getId());
3288 throw new PersistenceException("The pdp cannot be found to move with id "+pdp.getId());
3289 } else if(getPdpQueryList.size() > 1){
3290 PolicyLogger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3291 throw new PersistenceException("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3294 //check if new group exists
3295 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3296 checkGroupQuery.setParameter("groupId", group.getId());
3297 checkGroupQuery.setParameter("deleted", false);
3298 List<?> checkGroupQueryList;
3300 checkGroupQueryList = checkGroupQuery.getResultList();
3301 } catch(Exception e){
3302 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group on checkGroupQuery.getResultList()");
3303 throw new PersistenceException("Query failed trying to get new group "+group.getId());
3305 if(checkGroupQueryList.size() != 1){
3306 PolicyLogger.error("The group "+group.getId()+" does not exist");
3307 throw new PersistenceException("The group "+group.getId()+" does not exist");
3309 GroupEntity groupToMoveInto = (GroupEntity)checkGroupQueryList.get(0);
3310 PdpEntity pdpToUpdate = (PdpEntity)getPdpQueryList.get(0);
3311 pdpToUpdate.setGroup(groupToMoveInto);
3312 if(!stringEquals(pdpToUpdate.getModifiedBy(), username)){
3313 pdpToUpdate.setModifiedBy(username);
3317 this.pdpId = pdpToUpdate.getPdpKey();
3322 public void changeDefaultGroup(EcompPDPGroup group, String username){
3323 logger.debug("changeDefaultGroup(PDPGroup group, String username) as changeDefaultGroup("+group+","+username+") called");
3325 throw new IllegalArgumentException("PDPGroup group must not be null");
3327 if(isNullOrEmpty(group.getId(),username)){
3328 throw new IllegalArgumentException("group.getId() and username must not be null or empty");
3331 synchronized(emLock){
3332 checkBeforeOperationRun();
3333 Query getGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3334 getGroupQuery.setParameter("groupId", group.getId());
3335 getGroupQuery.setParameter("deleted", false);
3336 List<?> getGroupQueryList;
3338 getGroupQueryList = getGroupQuery.getResultList();
3339 } catch(Exception e){
3340 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getGroupQuery.getResultList()");
3341 throw new PersistenceException("Query failed trying to get group "+group.getId());
3343 if(getGroupQueryList.size() < 1){
3344 PolicyLogger.error("The group cannot be found to set default with id "+group.getId());
3345 throw new PersistenceException("The group cannot be found to set default with id "+group.getId());
3346 } else if(getGroupQueryList.size() > 1){
3347 PolicyLogger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3348 throw new PersistenceException("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3350 GroupEntity newDefaultGroup = (GroupEntity)getGroupQueryList.get(0);
3351 newDefaultGroup.setDefaultGroup(true);
3352 if(!stringEquals(newDefaultGroup.getModifiedBy(), username)){
3353 newDefaultGroup.setModifiedBy(username);
3357 this.groupId = newDefaultGroup.getGroupKey();
3358 Query setAllGroupsNotDefault = em.createQuery("UPDATE GroupEntity g SET g.defaultGroup=:defaultGroup WHERE g.deleted=:deleted AND g.groupKey<>:groupKey");
3359 //not going to set modified by for all groups
3360 setAllGroupsNotDefault.setParameter("defaultGroup", false);
3361 setAllGroupsNotDefault.setParameter("deleted", false);
3362 setAllGroupsNotDefault.setParameter("groupKey", newDefaultGroup.getGroupKey());
3364 logger.info("set " + setAllGroupsNotDefault.executeUpdate() + " groups as not default");
3365 } catch(Exception e){
3366 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on setAllGroupsNotDefault.executeUpdate()");
3367 throw new PersistenceException("Could not set all other groups default to false");
3376 public void deleteGroup(EcompPDPGroup group, EcompPDPGroup moveToGroup, String username) throws PAPException {
3377 logger.debug("deleteGroup(PDPGroup group, PDPGroup moveToGroup, String username) as deleteGroup("+group+", "+moveToGroup+","+username+") called");
3379 throw new IllegalArgumentException("PDPGroup group cannot be null");
3381 if(isNullOrEmpty(username,group.getId())){
3382 throw new IllegalArgumentException("group.getId() and and username must not be null or empty");
3385 if(group.isDefaultGroup()){
3386 PolicyLogger.error("The default group "+group.getId()+" was attempted to be deleted. It cannot be.");
3387 throw new PAPException("You cannot delete the default group.");
3389 synchronized(emLock){
3390 checkBeforeOperationRun();
3391 Query deleteGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3392 deleteGroupQuery.setParameter("groupId", group.getId());
3393 deleteGroupQuery.setParameter("deleted", false);
3394 List<?> deleteGroupQueryList;
3396 deleteGroupQueryList = deleteGroupQuery.getResultList();
3397 } catch(Exception e){
3398 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists deleteGroupQuery.getResultList()");
3399 throw new PersistenceException("Query failed trying to check if group exists");
3401 if(deleteGroupQueryList.size() < 1){
3402 logger.warn("The group could not be found with id " + group.getId());
3404 } else if(deleteGroupQueryList.size() > 1){
3405 PolicyLogger.error("Somehow, more than one group with the id "+group.getId()+" were found in the database that are not deleted");
3406 throw new PersistenceException("Somehow, more than one group with the id "+group.getId()+" were found in the database that are not deleted");
3409 Query pdpsInGroupQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group and p.deleted=:deleted");
3410 pdpsInGroupQuery.setParameter("group", ((GroupEntity)deleteGroupQueryList.get(0)));
3411 pdpsInGroupQuery.setParameter("deleted", false);
3412 List<?> pdpsInGroupList;
3414 pdpsInGroupList = pdpsInGroupQuery.getResultList();
3415 } catch(Exception e){
3416 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get PDPs in group on pdpsInGroupQuery.getResultList()");
3417 throw new PersistenceException("Query failed trying to get PDPs in group");
3419 if(pdpsInGroupList.size() > 0){
3420 if(moveToGroup != null){
3421 Query checkMoveToGroupQuery = em.createQuery("SELECT o FROM GroupEntity o WHERE o.groupId=:groupId AND o.deleted=:deleted");
3422 checkMoveToGroupQuery.setParameter("groupId", moveToGroup.getId());
3423 checkMoveToGroupQuery.setParameter("deleted", false);
3424 List<?> checkMoveToGroupList;
3426 checkMoveToGroupList = checkMoveToGroupQuery.getResultList();
3427 } catch(Exception e){
3428 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists checkMoveToGroupQuery.getResultList()");
3429 throw new PersistenceException("Query failed trying to check if group exists");
3431 if(checkMoveToGroupList.size() < 1){
3432 PolicyLogger.error("The group could not be found with id " + moveToGroup.getId());
3433 throw new PersistenceException("The group could not be found with id " + moveToGroup.getId());
3434 } else if(checkMoveToGroupList.size() > 1){
3435 PolicyLogger.error("Somehow, more than one group with the id "+moveToGroup.getId()+" were found in the database that are not deleted");
3436 throw new PersistenceException("Somehow, more than one group with the id "+moveToGroup.getId()+" were found in the database that are not deleted");
3438 GroupEntity newGroup = (GroupEntity)checkMoveToGroupList.get(0);
3439 for(Object pdpObject : pdpsInGroupList){
3440 PdpEntity pdp = (PdpEntity)pdpObject;
3441 pdp.setGroup(newGroup);
3442 if(!stringEquals(pdp.getModifiedBy(),username)){
3443 pdp.setModifiedBy(username);
3448 this.newGroupId = newGroup.getGroupId();
3449 } catch(PersistenceException e){
3450 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PersistenceException trying to set pdp group to null on em.flush()");
3451 throw new PersistenceException("Query failed trying to set pdp group to ");
3456 PolicyLogger.error("Group "+group.getId()+" is trying to be delted with PDPs. No group was provided to move them to");
3457 throw new PAPException("Group has PDPs. Must provide a group for them to move to");
3462 GroupEntity groupToDelete = (GroupEntity)deleteGroupQueryList.get(0);
3463 groupToDelete.setDeleted(true);
3464 if(!stringEquals(groupToDelete.getModifiedBy(), username)){
3465 groupToDelete.setModifiedBy(username);
3468 this.groupId = groupToDelete.getGroupKey();
3473 public void addPolicyToGroup(String groupID, String policyID, String username) {
3474 logger.debug("addPolicyToGroup(String groupID, String policyID, String username) as addPolicyToGroup("+groupID+", "+policyID+","+username+") called");
3475 if(isNullOrEmpty(groupID, policyID, username)){
3476 throw new IllegalArgumentException("groupID, policyID, and username must not be null or empty");
3478 synchronized(emLock){
3479 checkBeforeOperationRun();
3480 //check if group exists
3481 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3482 groupQuery.setParameter("groupId", groupID);
3483 groupQuery.setParameter("deleted", false);
3484 List<?> groupQueryList;
3486 groupQueryList = groupQuery.getResultList();
3487 }catch(Exception e){
3488 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists groupQuery.getResultList()");
3489 throw new PersistenceException("Query failed trying to check if group "+groupID+" exists");
3491 if(groupQueryList.size() < 1){
3492 PolicyLogger.error("Group policy is being added to does not exist with id "+groupID);
3493 throw new PersistenceException("Group policy is being added to does not exist with id "+groupID);
3494 } else if(groupQueryList.size() > 1){
3495 PolicyLogger.error("Somehow, more than one group with the id "+groupID+" were found in the database that are not deleted");
3496 throw new PersistenceException("Somehow, more than one group with the id "+groupID+" were found in the database that are not deleted");
3498 //we need to convert the form of the policy id that is used groups into the form that is used
3499 //for the database. (com.Config_mypol.1.xml) to (Config_mypol.xml)
3500 String[] policyNameScopeAndVersion = getNameScopeAndVersionFromPdpPolicy(policyID);
3501 Query policyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:policyName AND p.scope=:scope AND p.deleted=:deleted");
3502 policyQuery.setParameter("policyName", policyNameScopeAndVersion[0]);
3503 policyQuery.setParameter("scope", policyNameScopeAndVersion[1]);
3504 policyQuery.setParameter("deleted", false);
3505 List<?> policyQueryList;
3507 policyQueryList = policyQuery.getResultList();
3508 } catch(Exception e){
3509 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if policy exists policyQuery.getResultList()");
3510 throw new PersistenceException("Query failed trying to check if policy "+policyNameScopeAndVersion[0]+" exists");
3512 if(policyQueryList.size() < 1){
3513 PolicyLogger.error("Policy being added to the group does not exist with policy id "+policyNameScopeAndVersion[0]);
3514 throw new PersistenceException("Policy being added to the group does not exist with policy id "+policyNameScopeAndVersion[0]);
3515 } else if(policyQueryList.size() > 1){
3516 PolicyLogger.error("Somehow, more than one policy with the id "+policyNameScopeAndVersion[0]+" were found in the database that are not deleted");
3517 throw new PersistenceException("Somehow, more than one group with the id "+policyNameScopeAndVersion[0]+" were found in the database that are not deleted");
3519 GroupEntity group = (GroupEntity)groupQueryList.get(0);
3520 PolicyEntity policy = (PolicyEntity)policyQueryList.get(0);
3521 Iterator<PolicyEntity> policyIt = group.getPolicies().iterator();
3522 String policyName = getPolicyNameAndVersionFromPolicyFileName(policy.getPolicyName())[0];
3524 while(policyIt.hasNext()){
3525 PolicyEntity pol = policyIt.next();
3526 if(getPolicyNameAndVersionFromPolicyFileName(pol.getPolicyName())[0].equals(policyName)){
3530 }catch(Exception e){
3531 PolicyLogger.error("Could not delete old versions for policy "+policy.getPolicyName()+", ID: "+policy.getPolicyId());
3533 group.addPolicyToGroup(policy);
3538 //this means delete pdp not just remove from group
3540 public void removePdpFromGroup(String pdpID, String username) {
3541 logger.debug("removePdpFromGroup(String pdpID, String username) as removePdpFromGroup("+pdpID+","+username+") called");
3542 if(isNullOrEmpty(pdpID,username)){
3543 throw new IllegalArgumentException("pdpID and username must not be null or empty");
3545 synchronized(emLock){
3546 checkBeforeOperationRun();
3547 Query pdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3548 pdpQuery.setParameter("pdpId", pdpID);
3549 pdpQuery.setParameter("deleted", false);
3552 pdpList = pdpQuery.getResultList();
3553 } catch(Exception e){
3554 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if pdp exists pdpQuery.getResultList()");
3555 throw new PersistenceException("Query failed trying to check if pdp "+pdpID+" exists");
3557 if(pdpList.size() > 1){
3558 PolicyLogger.error("Somehow, more than one pdp with the id "+pdpID+" were found in the database that are not deleted");
3559 throw new PersistenceException("Somehow, more than one pdp with the id "+pdpID+" were found in the database that are not deleted");
3560 } else if(pdpList.size() < 1){
3561 PolicyLogger.error("Pdp being removed does not exist with id "+pdpID);
3564 PdpEntity pdp = (PdpEntity)pdpList.get(0);
3566 if(!stringEquals(pdp.getModifiedBy(),username)){
3567 pdp.setModifiedBy(username);
3569 pdp.setDeleted(true);
3572 this.pdpId = pdp.getPdpKey();
3579 private static String getDefaultWorkspace(){
3583 private PolicyDBDao(){
3587 public static PolicyDBDaoTestClass getPolicyDBDaoTestClass(){
3588 return new PolicyDBDao().new PolicyDBDaoTestClass();
3591 final class PolicyDBDaoTestClass {
3592 String[] getScopeAndNameAndType(final String path){
3593 return PolicyDBDao.getScopeAndNameAndType(path);
3595 String getGitPath(){
3596 return PolicyDBDao.getGitPath();
3598 String getConfigFile(String filename, String scope, PolicyRestAdapter policy){
3599 return PolicyDBDao.this.getConfigFile(filename, policy);
3601 String computeScope(String fullPath, String pathToExclude){
3602 return PolicyDBDao.computeScope(fullPath, pathToExclude);
3604 String encryptPassword(String password) throws Exception{
3605 return PolicyDBDao.encryptPassword(password);
3607 String decryptPassword(String password) throws Exception{
3608 return PolicyDBDao.decryptPassword(password);
3610 String getDescriptionFromXacml(String xacmlData){
3611 return PolicyDBDao.getDescriptionFromXacml(xacmlData);
3613 String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName){
3614 return PolicyDBDao.this.getPolicyNameAndVersionFromPolicyFileName(originalPolicyName);