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.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.StringReader;
32 import java.net.HttpURLConnection;
33 import java.net.MalformedURLException;
34 import java.net.ProtocolException;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.FileSystems;
38 import java.nio.file.Files;
39 import java.nio.file.InvalidPathException;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.security.Key;
43 import java.sql.Timestamp;
44 import java.util.ArrayList;
45 import java.util.Base64;
46 import java.util.Calendar;
47 import java.util.Date;
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.Iterator;
51 import java.util.LinkedList;
52 import java.util.List;
54 import java.util.Properties;
56 import java.util.UUID;
58 import javax.crypto.Cipher;
59 import javax.crypto.spec.SecretKeySpec;
60 import javax.persistence.EntityManager;
61 import javax.persistence.EntityManagerFactory;
62 import javax.persistence.LockModeType;
63 import javax.persistence.PersistenceException;
64 import javax.persistence.Query;
65 import javax.persistence.RollbackException;
66 import javax.xml.parsers.DocumentBuilder;
67 import javax.xml.parsers.DocumentBuilderFactory;
68 import javax.xml.xpath.XPath;
69 import javax.xml.xpath.XPathFactory;
71 import org.apache.commons.io.FileUtils;
72 import org.apache.commons.io.FilenameUtils;
73 import org.apache.commons.io.IOUtils;
74 import org.apache.commons.logging.Log;
75 import org.apache.commons.logging.LogFactory;
76 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
77 import org.openecomp.policy.pap.xacml.rest.adapters.PolicyRestAdapter;
78 import org.openecomp.policy.rest.XACMLRestProperties;
79 import org.openecomp.policy.rest.jpa.ActionBodyEntity;
80 import org.openecomp.policy.rest.jpa.ConfigurationDataEntity;
81 import org.openecomp.policy.rest.jpa.DatabaseLockEntity;
82 import org.openecomp.policy.rest.jpa.GroupEntity;
83 import org.openecomp.policy.rest.jpa.PdpEntity;
84 import org.openecomp.policy.rest.jpa.PolicyDBDaoEntity;
85 import org.openecomp.policy.rest.jpa.PolicyEntity;
86 import org.openecomp.policy.rest.jpa.PolicyVersion;
87 import org.openecomp.policy.rest.util.Webapps;
88 import org.openecomp.policy.common.logging.eelf.MessageCodes;
89 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
91 import org.xml.sax.InputSource;
93 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
94 import org.openecomp.policy.xacml.api.pap.EcompPDP;
95 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
96 import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;
98 import com.att.research.xacml.api.pap.PAPEngine;
99 import com.att.research.xacml.api.pap.PAPException;
100 import com.att.research.xacml.api.pap.PDP;
101 //import com.att.research.xacml.api.pap.PDPGroup;
102 import com.att.research.xacml.api.pap.PDPPolicy;
103 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
104 import org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
105 import org.openecomp.policy.xacml.util.XACMLPolicyScanner;
106 import org.openecomp.policy.xacml.util.XACMLPolicyWriter;
107 import com.att.research.xacml.util.XACMLProperties;
109 import org.w3c.dom.Document;
110 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
111 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
112 import org.openecomp.policy.common.logging.flexlogger.Logger;
114 public class PolicyDBDao {
115 private static final Logger logger = FlexLogger.getLogger(PolicyDBDao.class);
116 private List<?> otherServers;
117 private EntityManagerFactory emf;
118 private static PolicyDBDao currentInstance = null;
119 private PAPPolicyEngine papEngine;
121 public static final String JSON_CONFIG = "JSON";
122 public static final String XML_CONFIG = "XML";
123 public static final String PROPERTIES_CONFIG = "PROPERTIES";
124 public static final String OTHER_CONFIG = "OTHER";
125 public static final String AUDIT_USER = "audit";
128 * Get an instance of a PolicyDBDao. It creates one if it does not exist.
129 * Only one instance is allowed to be created per server.
130 * @param emf The EntityFactoryManager to be used for database connections
131 * @return The new instance of PolicyDBDao or throw exception if the given emf is null.
132 * @throws IllegalStateException if a PolicyDBDao has already been constructed. Call getPolicyDBDaoInstance() to get this.
134 public static PolicyDBDao getPolicyDBDaoInstance(EntityManagerFactory emf) throws Exception{
135 logger.debug("getPolicyDBDaoInstance(EntityManagerFactory emf) as getPolicyDBDaoInstance("+emf+") called");
136 if(currentInstance == null){
138 currentInstance = new PolicyDBDao(emf);
139 return currentInstance;
141 throw new IllegalStateException("The EntityManagerFactory is Null");
143 return currentInstance;
147 * Gets the current instance of PolicyDBDao.
148 * @return The instance of PolicyDBDao or throws exception if the given instance is null.
149 * @throws IllegalStateException if a PolicyDBDao instance is null. Call createPolicyDBDaoInstance(EntityManagerFactory emf) to get this.
151 public static PolicyDBDao getPolicyDBDaoInstance() throws Exception{
152 logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called");
153 if(currentInstance != null){
154 return currentInstance;
156 throw new IllegalStateException("The PolicyDBDao.currentInstance is Null. Use getPolicyDBDao(EntityManagerFactory emf)");
158 public void setPapEngine(PAPPolicyEngine papEngine2){
159 this.papEngine = (PAPPolicyEngine) papEngine2;
161 private PolicyDBDao(EntityManagerFactory emf){
162 logger.debug("PolicyDBDao(EntityManagerFactory emf) as PolicyDBDao("+emf+") called");
165 //not needed in this release
167 //TODO:EELF Cleanup - Remove logger
168 //logger.error("This server's PolicyDBDao instance could not be registered and may not reveive updates");
169 PolicyLogger.error("This server's PolicyDBDao instance could not be registered and may not reveive updates");
172 otherServers = getRemotePolicyDBDaoList();
173 if(logger.isDebugEnabled()){
174 logger.debug("Number of remote PolicyDBDao instances: "+otherServers.size());
176 if(otherServers.size() < 1){
177 logger.warn("List of PolicyDBDao servers is empty or could not be retrieved");
179 //otherServers = new LinkedList();
180 //otherServers.add((Object)"http://localhost:8071/pap/");
183 //not static because we are going to be using the instance's emf
184 //waitTime in ms to wait for lock, or -1 to wait forever (no)
185 private void startTransactionSynced(EntityManager entityMgr,int waitTime){
186 logger.debug("\n\nstartTransactionSynced(EntityManager entityMgr,int waitTime) as "
187 + "\n startTransactionSynced("+entityMgr+","+waitTime+") called\n\n");
188 DatabaseLockEntity lock = null;
190 entityMgr.setProperty("javax.persistence.query.timeout", waitTime);
191 entityMgr.getTransaction().begin();
193 if(logger.isDebugEnabled()){
194 Map<String,Object> properties = entityMgr.getProperties();
195 logger.debug("\n\nstartTransactionSynced():"
196 + "\n entityManager.getProperties() = " + properties
200 if(logger.isDebugEnabled()){
201 logger.debug("\n\nstartTransactionSynced():"
202 + "\n ATTEMPT to get the DB lock"
205 lock = entityMgr.find(DatabaseLockEntity.class, 1, LockModeType.PESSIMISTIC_WRITE);
206 if(logger.isDebugEnabled()){
207 logger.debug("\n\nstartTransactionSynced():"
208 + "\n GOT the DB lock"
211 } catch(Exception e){
212 System.out.println("Could not get lock entity");
216 throw new IllegalStateException("The lock row does not exist in the table. Please create a primary key with value = 1.");
221 * Gets the list of other registered PolicyDBDaos from the database
222 * @return List (type PolicyDBDaoEntity) of other PolicyDBDaos
224 private List<?> getRemotePolicyDBDaoList(){
225 logger.debug("getRemotePolicyDBDaoList() as getRemotePolicyDBDaoList() called");
226 List<?> policyDBDaoEntityList = new LinkedList<Object>();
227 EntityManager em = emf.createEntityManager();
228 startTransactionSynced(em, 1000);
230 Query getPolicyDBDaoEntityQuery = em.createNamedQuery("PolicyDBDaoEntity.findAll");
231 policyDBDaoEntityList = getPolicyDBDaoEntityQuery.getResultList();
233 } catch(Exception e){
234 //TODO:EELF Cleanup - Remove logger
235 //logger.error("Caught Exception on: getPolicyDBDaoEntityQuery.getResultList()",e);
236 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception querying for other registered PolicyDBDaos");
237 logger.warn("List of remote PolicyDBDaos will be empty");
240 em.getTransaction().commit();
241 } catch(Exception e){
243 em.getTransaction().rollback();
244 } catch(Exception e2){
249 return policyDBDaoEntityList;
252 public PolicyDBDaoTransaction getNewTransaction(){
253 logger.debug("getNewTransaction() as getNewTransaction() called");
254 return (PolicyDBDaoTransaction)(new PolicyDBDaoTransactionInstance());
258 * Because the normal transactions are not used in audits, we can use the same transaction
259 * mechanism to get a transaction and obtain the emlock and the DB lock. We just need to
260 * provide different transaction timeout values in ms because the audit will run longer
261 * than normal transactions.
263 public PolicyDBDaoTransaction getNewAuditTransaction(){
264 logger.debug("getNewAuditTransaction() as getNewAuditTransaction() called");
265 //Use the standard transaction wait time in ms
266 int auditWaitMs = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT));
267 //Use the (extended) audit timeout time in ms
268 int auditTimeoutMs = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_AUDIT_TIMEOUT));
269 return (PolicyDBDaoTransaction)(new PolicyDBDaoTransactionInstance(auditTimeoutMs, auditWaitMs));
274 * Checks if two strings are equal. Null strings ARE allowed.
275 * @param one A String or null to compare
276 * @param two A String or null to compare
278 private static boolean stringEquals(String one, String two){
279 logger.debug("stringEquals(String one, String two) as stringEquals("+one+", "+two+") called");
280 if(one == null && two == null){
283 if(one == null || two == null){
286 return one.equals(two);
290 * Computes the scope in dotted format based on an absolute path and a path that divides the scope.
291 * @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)
292 * @param pathToExclude The path that acts as a division between the scope and the other folders
293 * @return The scope in dotted format (org.openecomp)
295 private static String computeScope(String fullPath, String pathToExclude){
296 logger.debug("computeScope(String fullPath, String pathToExclude) as computeScope("+fullPath+", "+pathToExclude+") called");
297 int excludeIndex = fullPath.indexOf(pathToExclude);
298 String scopePath = fullPath.substring(excludeIndex+pathToExclude.length());
299 String scope = scopePath.replace('\\', '.');
300 scope = scope.replace('/', '.');
301 if(scope.charAt(0) == '.'){
302 scope = scope.substring(1);
304 if(scope.charAt(scope.length()-1) == '.'){
305 scope = scope.substring(0, scope.length()-1);
311 * Returns the url of this local pap server, removing the username and password, if they are present
312 * @return The url of this local pap server
314 private String[] getPapUrlUserPass(){
315 logger.debug("getPapUrl() as getPapUrl() called");
316 String url = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
320 return splitPapUrlUserPass(url);
324 private String[] splitPapUrlUserPass(String url){
325 String[] urlUserPass = new String[3];
326 String[] commaSplit = url.split(",");
327 urlUserPass[0] = commaSplit[0];
328 if(commaSplit.length > 2){
329 urlUserPass[1] = commaSplit[1];
330 urlUserPass[2] = commaSplit[2];
332 if(urlUserPass[1] == null || urlUserPass[1].equals("")){
333 String usernamePropertyValue = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
334 if(usernamePropertyValue != null){
335 urlUserPass[1] = usernamePropertyValue;
338 if(urlUserPass[2] == null || urlUserPass[2].equals("")){
339 String passwordPropertyValue = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS);
340 if(passwordPropertyValue != null){
341 urlUserPass[2] = passwordPropertyValue;
344 //if there is no comma, for some reason there is no username and password, so don't try to cut them off
348 private static String encryptPassword(String password) throws Exception{
349 Cipher cipher = Cipher.getInstance("AES");
350 cipher.init(Cipher.ENCRYPT_MODE, aesKey());
351 byte[] encryption = cipher.doFinal(password.getBytes("UTF-8"));
352 System.out.println(encryption);
353 return new String(Base64.getMimeEncoder().encode(encryption),"UTF-8");
356 private static String decryptPassword(String encryptedPassword) throws Exception{
357 Cipher cipher = Cipher.getInstance("AES");
358 cipher.init(Cipher.DECRYPT_MODE, aesKey());
359 byte[] password = cipher.doFinal(Base64.getDecoder().decode(encryptedPassword.getBytes("UTF-8")));
360 return new String(password,"UTF-8");
362 private static Key aesKey(){
363 byte[] aesValue = (new String("njrmbklcxtoplawf")).getBytes();
364 return new SecretKeySpec(aesValue,"AES");
367 * Register the PolicyDBDao instance in the PolicyDBDaoEntity table
368 * @return Boolean, were we able to register?
370 private boolean register(){
371 logger.debug("register() as register() called");
372 String[] url = getPapUrlUserPass();
373 EntityManager em = emf.createEntityManager();
375 startTransactionSynced(em, 1000);
376 } catch(IllegalStateException e){
377 logger.debug ("\nPolicyDBDao.register() caught an IllegalStateException: \n" +e + "\n");
378 DatabaseLockEntity lock;
379 lock = em.find(DatabaseLockEntity.class, 1);
381 lock = new DatabaseLockEntity();
386 em.getTransaction().commit();
388 } catch(Exception e2){
389 //TODO:EELF Cleanup - Remove logger
390 //logger.error("COULD NOT CREATE DATABASELOCK ROW. WILL TRY ONE MORE TIME \n\n Exception: \n" + e2);
391 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "COULD NOT CREATE DATABASELOCK ROW. WILL TRY ONE MORE TIME");
392 e2.printStackTrace();
395 em = emf.createEntityManager();
397 startTransactionSynced(em, 1000);
398 } catch(Exception e3){
400 String msg = "DATABASE LOCKING NOT WORKING. CONCURRENCY CONTROL NOT WORKING";
401 //TODO:EELF Cleanup - Remove logger
403 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e3, "PolicyDBDao", msg);
404 throw new IllegalStateException("msg" + "\n" + e3);
408 logger.debug("\nPolicyDBDao.register. Database locking and concurrency control is initialized\n");
409 PolicyDBDaoEntity foundPolicyDBDaoEntity = em.find(PolicyDBDaoEntity.class, url[0]);
410 Query getPolicyDBDaoEntityQuery = em.createQuery("SELECT e FROM PolicyDBDaoEntity e WHERE e.policyDBDaoUrl=:url");
411 getPolicyDBDaoEntityQuery.setParameter("url", url[0]);
412 if(foundPolicyDBDaoEntity == null){
413 //em.getTransaction().begin();
414 PolicyDBDaoEntity newPolicyDBDaoEntity = new PolicyDBDaoEntity();
415 em.persist(newPolicyDBDaoEntity);
416 newPolicyDBDaoEntity.setPolicyDBDaoUrl(url[0]);
417 newPolicyDBDaoEntity.setDescription("PAP server at "+url[0]);
418 newPolicyDBDaoEntity.setUsername(url[1]);
420 newPolicyDBDaoEntity.setPassword(encryptPassword(url[2]));
421 } catch(Exception e){
422 //TODO:EELF Cleanup - Remove logger
423 //logger.error("Could not encrypt PAP password",e);
424 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
428 em.getTransaction().commit();
429 } catch(RollbackException e){
430 logger.error("Caught RollbackException during PolicyDBDao Registration on: em.getTransaction().commit()",e);
433 } catch(Exception e2){
434 logger.error("Caught Exception during PolicyDBDao Registration on: em.getTransaction().commit()",e2);
440 em.getTransaction().commit();
441 } catch(Exception e){
443 em.getTransaction().rollback();
444 } catch(Exception e2){
445 //TODO:EELF Cleanup - Remove logger
446 //logger.error("Could not add new PolicyDBDao to the database",e);
447 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not add new PolicyDBDao to the database");
451 //em.getTransaction().begin();
452 //just want to update in order to change modified date
453 String encryptedPassword = null;
455 encryptedPassword = encryptPassword(url[2]);
456 } catch(Exception e){
457 //TODO:EELF Cleanup - Remove logger
458 //logger.error("Could not encrypt PAP password",e);
459 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not encrypt PAP password");
461 if(url[1] != null && !stringEquals(url[1], foundPolicyDBDaoEntity.getUsername())){
462 foundPolicyDBDaoEntity.setUsername(url[1]);
464 if(encryptedPassword != null && !stringEquals(encryptedPassword, foundPolicyDBDaoEntity.getPassword())){
465 foundPolicyDBDaoEntity.setPassword(encryptedPassword);
467 foundPolicyDBDaoEntity.preUpdate();
469 em.getTransaction().commit();
470 } catch(Exception e){
472 em.getTransaction().rollback();
473 } catch(Exception e2){
474 //TODO:EELF Cleanup - Remove logger
475 //logger.error("Could not update PolicyDBDao in the database",e);
476 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Could not update PolicyDBDao in the database");
481 em.getTransaction().commit();
482 } catch(RollbackException e){
483 logger.error("Caught RollbackException during PolicyDBDao Registration on: em.getTransaction().commit()",e);
486 } catch(Exception e2){
487 logger.error("Caught Exception during PolicyDBDao Registration on: em.getTransaction().commit()",e2);
488 em.getTransaction().rollback();
494 logger.debug("\nPolicyDBDao.register(). Success!!\n");
497 public void notifyOthers(long entityId,String entityType){
498 notifyOthers(entityId,entityType,null);
500 public void notifyOthers(long entityId, String entityType, String newGroupId){
501 logger.debug("notifyOthers(long entityId, String entityType, long newGroupId) as notifyOthers("+entityId+","+entityType+","+newGroupId+") called");
502 LinkedList<Thread> notifyThreads = new LinkedList<Thread>();
504 //we're going to run notiftions in parellel threads to speed things up
505 for(Object obj : otherServers){
507 Thread newNotifyThread = new Thread(new NotifyOtherThread(obj, entityId, entityType, newGroupId));
509 newNotifyThread.start();
511 notifyThreads.add(newNotifyThread);
514 //we want to wait for all notifications to complete or timeout before we unlock the interface and allow more changes
515 for(Thread t : notifyThreads){
518 } catch (Exception e) {
519 logger.warn("Could not join a notifcation thread");
526 private class NotifyOtherThread implements Runnable {
527 public NotifyOtherThread(Object obj, long entityId, String entityType, String newGroupId){
529 this.entityId = entityId;
530 this.entityType = entityType;
531 this.newGroupId = newGroupId;
534 private long entityId;
535 private String entityType;
536 private String newGroupId;
539 //naming of 'o' is for backwards compatibility with the rest of the function
540 PolicyDBDaoEntity dbdEntity = (PolicyDBDaoEntity)obj;
541 String o = dbdEntity.getPolicyDBDaoUrl();
542 String username = dbdEntity.getUsername();
545 password = decryptPassword(dbdEntity.getPassword());
546 } catch(Exception e){
547 //if we can't decrypt, might as well try it anyway
548 password = dbdEntity.getPassword();
550 Base64.Encoder encoder = Base64.getEncoder();
551 String encoding = encoder.encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));
552 HttpURLConnection connection = null;
553 UUID requestID = UUID.randomUUID();
554 //loggingContext.setRequestID(requestID.toString());
555 //loggingContext.transactionStarted();
558 String papUrl = getPapUrlUserPass()[0];
560 papUrl = "undefined";
562 logger.debug("We are going to try to notify "+o);
563 //is this our own url?
566 ourUrl = splitPapUrlUserPass((String)o)[0];
573 if(papUrl.equals(ourUrl)){
574 logger.debug(((String)o)+" is our url, skipping notify");
577 if(newGroupId == null){
578 url = new URL(((String)o)+"?policydbdaourl="+papUrl+"&entityid="+entityId+"&entitytype="+entityType);
580 url = new URL(((String)o)+"?policydbdaourl="+papUrl+"&entityid="+entityId+"&entitytype="+entityType+"&extradata="+newGroupId);
582 } catch (MalformedURLException e) {
583 logger.warn("Caught MalformedURLException on: new URL()", e);
587 // Open up the connection
589 logger.debug("Connecting with url: "+url);
591 connection = (HttpURLConnection)url.openConnection();
592 } catch (Exception e) {
593 logger.warn("Caught exception on: url.openConnection()",e);
597 // Setup our method and headers
600 connection.setRequestMethod("PUT");
601 } catch (ProtocolException e) {
602 //why would this error ever occur?
603 logger.warn("Caught ProtocolException on connection.setRequestMethod(\"PUT\");",e);
606 connection.setRequestProperty("Authorization", "Basic " + encoding);
607 connection.setRequestProperty("Accept", "text/x-java-properties");
608 connection.setRequestProperty("Content-Type", "text/x-java-properties");
609 connection.setRequestProperty("requestID", requestID.toString());
612 readTimeout = Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_NOTIFY_TIMEOUT));
614 } catch(Exception e){
615 logger.error("xacml.rest.pap.notify.timeoutms property not set, using a default.");
618 connection.setReadTimeout(readTimeout);
619 connection.setConnectTimeout(readTimeout);
620 connection.setUseCaches(false);
622 // Adding this in. It seems the HttpUrlConnection class does NOT
623 // properly forward our headers for POST re-direction. It does so
624 // for a GET re-direction.
626 // So we need to handle this ourselves.
628 connection.setInstanceFollowRedirects(false);
629 connection.setDoOutput(true);
630 connection.setDoInput(true);
632 connection.connect();
633 } catch (Exception e) {
634 logger.warn("Caught exception on: connection.connect()",e);
638 if (connection.getResponseCode() == 200) {
639 logger.info("Received response 200 from pap server on notify");
642 logger.warn("connection response code not 200, received: "+connection.getResponseCode());
644 } catch (Exception e) {
645 logger.warn("Caught Exception on: connection.getResponseCode() ", e);
649 connection.disconnect();
653 private static String getElementFromXMLString(String element, String xml) {
654 InputSource source = new InputSource(new StringReader(xml));
656 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
657 String description = "";
659 DocumentBuilder db = dbf.newDocumentBuilder();
660 Document document = db.parse(source);
662 XPathFactory xpathFactory = XPathFactory.newInstance();
663 XPath xpath = xpathFactory.newXPath();
665 if (element.endsWith("/")){
666 element = element.substring(0, element.length() -1);
669 description = xpath.evaluate("/Policy" + element + "/text()", document);
675 System.out.println("description_" + description);
678 private static String evaluateXPath(String expression, String xml) {
679 InputSource source = new InputSource(new StringReader(xml));
681 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
682 String description = "";
684 DocumentBuilder db = dbf.newDocumentBuilder();
685 Document document = db.parse(source);
687 XPathFactory xpathFactory = XPathFactory.newInstance();
688 XPath xpath = xpathFactory.newXPath();
691 description = xpath.evaluate(expression, document);
697 System.out.println("description_" + description);
701 private static String getDescriptionFromXacml(String xacmlData){
702 //FIXME completely untested. Probably not a good idea to use. UPDATE: kind of tested
703 String openTag = "<Description>";
704 String closeTag = "</Description>";
705 int descIndex = xacmlData.indexOf(openTag);
706 int endDescIndex = xacmlData.indexOf(closeTag);
707 String desc = xacmlData.substring(descIndex+openTag.length(),endDescIndex);
710 private final String POLICY_NOTIFICATION = "policy";
711 private final String PDP_NOTIFICATION = "pdp";
712 private final String GROUP_NOTIFICATION = "group";
713 public void handleIncomingHttpNotification(String url, String entityId, String entityType, String extraData, XACMLPapServlet xacmlPapServlet){
714 logger.info("DBDao url: " + url + " has reported an update on "+entityType+" entity "+entityId);
715 PolicyDBDaoTransaction transaction = this.getNewTransaction();
718 case POLICY_NOTIFICATION:
720 handleIncomingPolicyChange(url, entityId,extraData);
721 } catch(Exception e){
722 //TODO:EELF Cleanup - Remove logger
723 //logger.error("Caught exception on handleIncomingPolicyChange("+url+", "+entityId+", "+extraData+")",e);
724 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPolicyChange("+url+", "+entityId+", "+extraData+")");
727 case PDP_NOTIFICATION:
729 handleIncomingPdpChange(url, entityId, transaction);
730 } catch(Exception e){
731 //TODO:EELF Cleanup - Remove logger
732 //logger.error("Caught exception on handleIncomingPdpChange("+url+", "+entityId+", "+transaction+")",e);
733 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingPdpChange("+url+", "+entityId+", "+transaction+")");
736 case GROUP_NOTIFICATION:
738 handleIncomingGroupChange(url, entityId, extraData, transaction, xacmlPapServlet);
740 //TODO:EELF Cleanup - Remove logger
741 //logger.error("Caught exception on handleIncomingGroupChange("+url+", "+entityId+", "+extraData+", "+transaction+", "+xacmlPapServlet+")",e);
742 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught exception on handleIncomingGroupChange("+url+", "+entityId+", "+extraData+", "+transaction+", "+xacmlPapServlet+")");
746 //no changes should be being made in this function, we still need to close
747 transaction.rollbackTransaction();
749 private void handleIncomingGroupChange(String url, String groupId, String extraData,PolicyDBDaoTransaction transaction,XACMLPapServlet xacmlPapServlet) throws PAPException{
751 GroupEntity groupRecord = null;
752 long groupIdLong = -1;
754 groupIdLong = Long.parseLong(groupId);
755 } catch(NumberFormatException e){
756 throw new IllegalArgumentException("groupId "+groupId+" cannot be parsed into a long");
759 groupRecord = transaction.getGroup(groupIdLong);
760 } catch(Exception e){
761 //TODO:EELF Cleanup - Remove logger
762 //logger.error("Caught Exception trying to get pdp group record with transaction.getGroup("+groupIdLong+");",e);
763 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp group record with transaction.getGroup("+groupIdLong+");");
764 throw new PAPException("Could not get local group "+groupIdLong);
766 if(groupRecord == null){
767 throw new PersistenceException("The group record returned is null");
769 //compare to local fs
770 //does group folder exist
771 EcompPDPGroup localGroup = null;
773 localGroup = papEngine.getGroup(groupRecord.getGroupId());
774 } catch (Exception e) {
775 logger.warn("Caught PAPException trying to get local pdp group with papEngine.getGroup("+groupId+");",e);
776 //throw new PAPException("Could not get local group "+groupId);
778 if(localGroup == null && extraData != null){
779 //here we can try to load an old group id from the extraData
781 localGroup = papEngine.getGroup(extraData);
783 logger.warn("Caught PAPException trying to get local pdp group with papEngine.getGroup("+extraData+");",e);
786 if(localGroup != null && groupRecord.isDeleted()){
787 EcompPDPGroup newLocalGroup = null;
788 if(extraData != null){
790 newLocalGroup = papEngine.getGroup(extraData);
791 } catch (PAPException e) {
792 //TODO:EELF Cleanup - Remove logger
793 //logger.error("Caught PAPException trying to get new pdp group with papEngine.getGroup("+extraData+");",e);
794 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get new pdp group with papEngine.getGroup("+extraData+");");
795 //throw new PAPException("Could not get new local group "+newGroupId);
800 papEngine.removeGroup(localGroup, newLocalGroup);
801 } catch (NullPointerException | PAPException e) {
802 //TODO:EELF Cleanup - Remove logger
803 //logger.error("Caught PAPException trying to get remove pdp group with papEngine.removeGroup("+localGroup+", "+newLocalGroup+");",e);
804 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get remove pdp group with papEngine.removeGroup("+localGroup+", "+newLocalGroup+");");
805 throw new PAPException("Could not remove group "+groupId);
808 else if(localGroup == null){
809 //creating a new group
811 papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());
812 } catch (NullPointerException | PAPException e) {
813 //TODO:EELF Cleanup - Remove logger
814 //logger.error("Caught PAPException trying to create pdp group with papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());",e);
815 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to create pdp group with papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());");
816 throw new PAPException("Could not create group "+groupRecord);
819 localGroup = papEngine.getGroup(groupRecord.getGroupId());
820 } catch (PAPException e1) {
821 //TODO:EELF Cleanup - Remove logger
822 //logger.error("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",e1);
823 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");
825 //throw new PAPException("Could not get group "+groupRecord);
827 //add possible pdps to group
828 List<?> pdpsInGroup = transaction.getPdpsInGroup(Long.parseLong(groupRecord.getGroupId()));
829 for(Object pdpO : pdpsInGroup){
830 PdpEntity pdp = (PdpEntity)pdpO;
832 papEngine.newPDP(pdp.getPdpId(), localGroup, pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());
833 } catch (NullPointerException | PAPException e) {
834 //TODO:EELF Cleanup - Remove logger
835 //logger.error("Caught PAPException trying to get create pdp with papEngine.newPDP(pdp.getPdpId(), localGroup, pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());",e);
836 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());");
837 throw new PAPException("Could not create pdp "+pdp);
840 //add possible policies to group (filesystem only, apparently)
842 if(!(localGroup instanceof StdPDPGroup)){
843 throw new PAPException("group is not a StdPDPGroup");
846 //because it will be comparing the new group to its own version
847 StdPDPGroup localGroupClone = new StdPDPGroup(localGroup.getId(),localGroup.isDefaultGroup(),localGroup.getName(),localGroup.getDescription(),((StdPDPGroup)localGroup).getDirectory());
848 localGroupClone.setEcompPdps(localGroup.getEcompPdps());
849 localGroupClone.setPipConfigs(localGroup.getPipConfigs());
850 localGroupClone.setStatus(localGroup.getStatus());
851 //we are updating a group or adding a policy or changing default
852 //set default if it should be
853 if(!localGroupClone.isDefaultGroup() && groupRecord.isDefaultGroup()){
855 papEngine.SetDefaultGroup(localGroup);
857 } catch (PAPException e) {
858 //TODO:EELF Cleanup - Remove logger
859 //logger.error("Caught PAPException trying to set default group with papEngine.SetDefaultGroup("+localGroupClone+");",e);
860 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to set default group with papEngine.SetDefaultGroup("+localGroupClone+");");
861 throw new PAPException("Could not set default group to "+localGroupClone);
864 boolean needToUpdate = false;
865 if(updateGroupPoliciesInFileSystem(localGroupClone,localGroup, groupRecord, transaction)){
868 if(!stringEquals(localGroupClone.getId(),groupRecord.getGroupId()) || !stringEquals(localGroupClone.getName(),groupRecord.getgroupName())){
870 //we do not want to change the id, the papEngine will do this for us, it needs to know the old id
871 localGroupClone.setName(groupRecord.getgroupName());
874 if(!stringEquals(localGroupClone.getDescription(),groupRecord.getDescription())){
875 localGroupClone.setDescription(groupRecord.getDescription());
881 papEngine.updateGroup(localGroupClone);
882 } catch (PAPException e) {
883 //TODO:EELF Cleanup - Remove logger
884 //logger.error("Caught PAPException trying to update group with papEngine.updateGroup("+localGroupClone+");",e);
885 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to update group with papEngine.updateGroup("+localGroupClone+");");
886 throw new PAPException("Could not update group "+localGroupClone);
891 //call command that corresponds to the change that was made
893 //this will also handle removes, since incoming pdpGroup has no policies internally, we are just going to add them all in from the db
894 private boolean updateGroupPoliciesInFileSystem(EcompPDPGroup pdpGroup,EcompPDPGroup oldPdpGroup, GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException{
895 if(!(pdpGroup instanceof StdPDPGroup)){
896 throw new PAPException("group is not a StdPDPGroup");
898 StdPDPGroup group = (StdPDPGroup)pdpGroup;
899 //this must always be true since we don't explicitly know when a delete is occuring
900 boolean didUpdate = true;
901 HashMap<String,PDPPolicy> currentPolicySet = new HashMap<String,PDPPolicy>(oldPdpGroup.getPolicies().size());
902 HashSet<PDPPolicy> newPolicySet = new HashSet<PDPPolicy>();
903 for(PDPPolicy pdpPolicy : oldPdpGroup.getPolicies()){
904 currentPolicySet.put(pdpPolicy.getId(), pdpPolicy);
906 for(PolicyEntity policy : groupRecord.getPolicies()){
907 String pdpPolicyName = getPdpPolicyName(policy.getPolicyName(), policy.getScope());
908 if(group.getPolicy(pdpPolicyName) == null){
910 if(currentPolicySet.containsKey(pdpPolicyName)){
911 newPolicySet.add(currentPolicySet.get(pdpPolicyName));
913 InputStream policyStream = new ByteArrayInputStream(policy.getPolicyData().getBytes());
914 group.copyPolicyToFile(pdpPolicyName,policyStream);
915 ((StdPDPPolicy)(group.getPolicy(pdpPolicyName))).setName(removeExtensionAndVersionFromPolicyName(policy.getPolicyName()));
917 policyStream.close();
918 } catch (IOException e) {
919 // TODO Auto-generated catch block
926 newPolicySet.addAll(group.getPolicies());
927 group.setPolicies(newPolicySet);
932 private String removeExtensionAndVersionFromPolicyName(String originalPolicyName){
933 String policyName = originalPolicyName;
935 policyName = removeFileExtension(policyName);
936 policyName = policyName.substring(0,policyName.lastIndexOf('.'));
937 if(isNullOrEmpty(policyName)){
938 throw new Exception();
940 } catch(Exception e){
941 policyName = originalPolicyName;
946 private void handleIncomingPdpChange(String url, String pdpId, PolicyDBDaoTransaction transaction) throws PAPException{
950 pdpIdLong = Long.parseLong(pdpId);
951 }catch(NumberFormatException e){
952 throw new IllegalArgumentException("pdpId "+pdpId+" cannot be parsed into a long");
954 PdpEntity pdpRecord = null;
956 pdpRecord = transaction.getPdp(pdpIdLong);
958 //TODO:EELF Cleanup - Remove logger
959 //logger.error("Caught Exception trying to get pdp record with transaction.getPdp("+pdpIdLong+");",e);
960 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp record with transaction.getPdp("+pdpIdLong+");");
961 throw new PAPException("Could not get local pdp "+pdpIdLong);
963 if(pdpRecord == null){
964 throw new PersistenceException("The pdpRecord returned is null");
968 localPdp = papEngine.getPDP(pdpRecord.getPdpId());
969 } catch (PAPException e) {
970 logger.warn("Caught PAPException trying to get local pdp with papEngine.getPDP("+pdpId+");",e);
972 if(localPdp != null && pdpRecord.isDeleted()){
974 papEngine.removePDP((EcompPDP) localPdp);
975 } catch (PAPException e) {
976 //TODO:EELF Cleanup - Remove logger
977 //logger.error("Caught PAPException trying to get remove pdp with papEngine.removePDP("+localPdp+");",e);
978 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to get remove pdp with papEngine.removePDP("+localPdp+");");
979 throw new PAPException("Could not remove pdp "+pdpId);
982 else if(localPdp == null){
986 EcompPDPGroup localGroup = null;
988 localGroup = papEngine.getGroup(pdpRecord.getGroup().getGroupId());
989 } catch (PAPException e1) {
990 //TODO:EELF Cleanup - Remove logger
991 //logger.error("Caught PAPException trying to get local group to add pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());",e1);
992 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Caught PAPException trying to get local group to add pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());");
993 throw new PAPException("Could not get local group");
996 papEngine.newPDP(pdpRecord.getPdpId(), localGroup, pdpRecord.getPdpName(), pdpRecord.getDescription(), pdpRecord.getJmxPort());
997 } catch (NullPointerException | PAPException e) {
998 //TODO:EELF Cleanup - Remove logger
999 //logger.error("Caught PAPException trying to create pdp with papEngine.newPDP("+pdpRecord.getPdpId()+", "+localGroup+", "+pdpRecord.getPdpName()+", "+pdpRecord.getDescription()+", "+pdpRecord.getJmxPort()+");",e);
1000 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()+");");
1001 throw new PAPException("Could not create pdp "+pdpRecord);
1004 boolean needToUpdate = false;
1005 if(!stringEquals(localPdp.getId(),pdpRecord.getPdpId()) || !stringEquals(localPdp.getName(),pdpRecord.getPdpName())){
1006 //again, we don't want to change the id, the papEngine will do this
1007 localPdp.setName(pdpRecord.getPdpName());
1008 needToUpdate = true;
1010 if(!stringEquals(localPdp.getDescription(),pdpRecord.getDescription())){
1011 localPdp.setDescription(pdpRecord.getDescription());
1012 needToUpdate = true;
1014 String localPdpGroupId = null;
1016 localPdpGroupId = papEngine.getPDPGroup((EcompPDP) localPdp).getId();
1017 } catch(PAPException e){
1018 //could be null or something, just warn at this point
1019 logger.warn("Caught PAPException trying to get id of local group that pdp is in with localPdpGroupId = papEngine.getPDPGroup(localPdp).getId();",e);
1020 //throw new PAPException("Could not get local group");
1022 if(!stringEquals(localPdpGroupId,pdpRecord.getGroup().getGroupId())){
1023 EcompPDPGroup newPdpGroup = null;
1025 newPdpGroup = papEngine.getGroup(pdpRecord.getGroup().getGroupId());
1026 }catch(PAPException e){
1027 //ok, now we have an issue. Time to stop things
1028 //TODO:EELF Cleanup - Remove logger
1029 //logger.error("Caught PAPException trying to get id of local group to move pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());",e);
1030 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());");
1031 throw new PAPException("Could not get local group");
1034 papEngine.movePDP((EcompPDP) localPdp, newPdpGroup);
1035 }catch(PAPException e){
1036 //TODO:EELF Cleanup - Remove logger
1037 //logger.error("Caught PAPException trying to move pdp with papEngine.movePDP(localPdp, newPdpGroup);",e);
1038 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to move pdp with papEngine.movePDP(localPdp, newPdpGroup);");
1039 throw new PAPException("Could not move pdp "+localPdp);
1042 if(((PdpEntity) localPdp).getJmxPort() != pdpRecord.getJmxPort()){
1043 ((PdpEntity) localPdp).setJmxPort(pdpRecord.getJmxPort());
1044 needToUpdate = true;
1048 papEngine.updatePDP((EcompPDP) localPdp);
1049 } catch (PAPException e) {
1050 //TODO:EELF Cleanup - Remove logger
1051 //logger.error("Caught PAPException trying to update pdp with papEngine.updatePdp("+localPdp+");",e);
1052 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PAPException trying to update pdp with papEngine.updatePdp("+localPdp+");");
1053 throw new PAPException("Could not update pdp "+localPdp);
1057 //compare to local situation
1058 //call command to update
1060 private void handleIncomingPolicyChange(String url, String policyId,String oldPathString){
1061 EntityManager em = emf.createEntityManager();
1062 Query getPolicyEntityQuery = em.createNamedQuery("PolicyEntity.FindById");
1063 getPolicyEntityQuery.setParameter("id", Long.valueOf(policyId));
1065 @SuppressWarnings("unchecked")
1066 List<PolicyEntity> policies = getPolicyEntityQuery.getResultList();
1067 PolicyEntity policy = null;
1068 if (policies.size() > 0){
1069 policy = policies.get(0);
1072 String policyRepo = buildPolicyScopeDirectory(policy);
1074 Path policyPath = Paths.get(policyRepo);
1075 String action = "unknown action";
1078 if(policy.isDeleted()){
1079 logger.debug("Deleting Policy: " + policy.getPolicyName());
1081 Path newPath = Paths.get(policyPath.toString(), policy.getPolicyName());
1082 Files.deleteIfExists(newPath);
1084 Path subFile = null;
1086 if (policy.getConfigurationData()!= null){
1087 subFile = getPolicySubFile(policy.getConfigurationData().getConfigurationName(), "Config");
1088 }else if(policy.getActionBodyEntity()!= null){
1089 subFile = getPolicySubFile(policy.getActionBodyEntity().getActionBodyName(), "Action");
1092 if(subFile != null){
1093 Files.deleteIfExists(subFile);
1097 logger.debug("Updating/Creating Policy: " + policy.getPolicyName());
1099 Files.createDirectories(policyPath);
1100 Path newPath = Paths.get(policyPath.toString(), policy.getPolicyName());
1101 Files.deleteIfExists(newPath);
1102 if(!isNullOrEmpty(oldPathString)){
1104 String[] scopeName = getScopeAndNameAndType(oldPathString);
1105 Path oldPath = Paths.get(buildPolicyScopeDirectory(scopeName[0]),scopeName[1]);
1106 Files.delete(oldPath.toAbsolutePath());
1107 }catch(Exception e){
1108 //TODO:EELF Cleanup - Remove logger
1109 //logger.error("Could not delete the old policy before rename: "+oldPathString,e);
1110 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy before rename: "+oldPathString);
1113 Object policyData = XACMLPolicyScanner.readPolicy(IOUtils.toInputStream(policy.getPolicyData()));
1114 XACMLPolicyWriter.writePolicyFile(newPath, (PolicyType) policyData);
1116 if (policy.getConfigurationData()!= null){
1117 if(!isNullOrEmpty(oldPathString)){
1119 String[] oldPolicyScopeName = getScopeAndNameAndType(oldPathString);
1120 String oldConfigFileName = getConfigFile(oldPolicyScopeName[1],oldPolicyScopeName[0],policy.getConfigurationData().getConfigType());
1121 Path oldConfigFilePath = getPolicySubFile(oldConfigFileName, "Config");
1122 logger.debug("Trying to delete: "+oldConfigFilePath.toString());
1123 Files.delete(oldConfigFilePath);
1124 }catch(Exception e){
1125 //TODO:EELF Cleanup - Remove logger
1126 //logger.error("Could not delete the old policy config before rename for policy: "+oldPathString,e);
1127 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy config before rename for policy: "+oldPathString);
1130 writePolicySubFile(policy, "Config");
1132 }else if(policy.getActionBodyEntity()!= null){
1133 if(!isNullOrEmpty(oldPathString)){
1135 String[] oldPolicyScopeName = getScopeAndNameAndType(oldPathString);
1136 String oldActionFileName = getConfigFile(oldPolicyScopeName[1],oldPolicyScopeName[0],ConfigPolicy.JSON_CONFIG);
1137 Path oldActionFilePath = getPolicySubFile(oldActionFileName, "Action");
1138 logger.debug("Trying to delete: "+oldActionFilePath.toString());
1139 Files.delete(oldActionFilePath);
1140 }catch(Exception e){
1141 //TODO:EELF Cleanup - Remove logger
1142 //logger.error("Could not delete the old policy action body before rename for policy: "+oldPathString,e);
1143 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not delete the old policy action body before rename for policy: "+oldPathString);
1146 writePolicySubFile(policy, "Action");
1150 } catch (IOException e1) {
1151 //TODO:EELF Cleanup - Remove logger
1152 //logger.error("Error occurred while performing [" + action + "] of Policy File: " + policy.getPolicyName(), e1);
1153 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while performing [" + action + "] of Policy File: " + policy.getPolicyName());
1157 //FIXME error correcting and logs
1158 private void createGroupsFromDatabase(){
1159 //get list of groups
1160 boolean foundDefault = false;
1161 //need to avoid infinite loop, just in case
1162 boolean alreadyRunAdd = false;
1163 while(!foundDefault){
1165 EntityManager em = emf.createEntityManager();
1166 Query getGroups = em.createQuery("SELECT g FROM GroupEntity g WHERE g.deleted=:deleted");
1167 getGroups.setParameter("deleted", false);
1168 List<?> groups = getGroups.getResultList();
1170 //make a folder for each group in pdps folders
1171 Path pdpsPath = Paths.get("pdps");
1173 FileUtils.forceDelete(pdpsPath.toFile());
1174 } catch (Exception e) {
1175 e.printStackTrace();
1178 FileUtils.forceMkdir(pdpsPath.toFile());
1179 } catch (Exception e) {
1180 e.printStackTrace();
1182 Properties propertyFileProperties = new Properties();
1183 String groupList = "";
1184 String defaultGroup = "";
1185 for(Object o : groups){
1186 GroupEntity group = (GroupEntity)o;
1187 Path groupPath = Paths.get(pdpsPath.toString(), group.getGroupId());
1189 FileUtils.forceMkdir(groupPath.toFile());
1190 } catch (IOException e) {
1191 // TODO Auto-generated catch block
1192 e.printStackTrace();
1194 Properties policyProperties = new Properties();
1195 String rootPolicies = "";
1196 for(PolicyEntity policy : group.getPolicies()){
1197 Path newPolicyPath = Paths.get(groupPath.toString(),getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
1198 File newPolicyFile = newPolicyPath.toFile();
1200 newPolicyFile.createNewFile();
1201 } catch (IOException e) {
1202 // TODO Auto-generated catch block
1203 e.printStackTrace();
1206 FileOutputStream policyFileStream = new FileOutputStream(newPolicyFile);
1207 policyFileStream.write(policy.getPolicyData().getBytes("UTF-8"));
1208 policyFileStream.close();
1209 } catch (IOException e) {
1210 // TODO Auto-generated catch block
1211 e.printStackTrace();
1213 policyProperties.setProperty(getPdpPolicyName(policy.getPolicyName(),policy.getScope())+".name",removeExtensionAndVersionFromPolicyName(policy.getPolicyName()));
1214 rootPolicies += ",".concat(getPdpPolicyName(policy.getPolicyName(),policy.getScope()));
1216 Path xacmlPolicyPropertiesPath = Paths.get(groupPath.toString(),"xacml.policy.properties");
1217 File xacmlPolicyPropertiesFile = xacmlPolicyPropertiesPath.toFile();
1218 if(rootPolicies.length() > 0){
1219 rootPolicies = rootPolicies.substring(1);
1221 policyProperties.setProperty("xacml.referencedPolicies", "");
1222 policyProperties.setProperty("xacml.rootPolicies", rootPolicies);
1225 xacmlPolicyPropertiesFile.createNewFile();
1226 } catch (IOException e) {
1227 // TODO Auto-generated catch block
1228 e.printStackTrace();
1231 FileOutputStream xacmlPolicyPropertiesFileStream = new FileOutputStream(xacmlPolicyPropertiesFile);
1232 //xacmlPolicyPropertiesFileStream.write(xacmlPolicyProperties.getBytes("UTF-8"));
1233 policyProperties.store(xacmlPolicyPropertiesFileStream, "");
1234 xacmlPolicyPropertiesFileStream.close();
1235 } catch (IOException e) {
1236 // TODO Auto-generated catch block
1237 e.printStackTrace();
1240 em = emf.createEntityManager();
1241 Query getPdpsQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group AND p.deleted=:deleted");
1242 getPdpsQuery.setParameter("group", group);
1243 getPdpsQuery.setParameter("deleted", false);
1244 List<?> pdps = getPdpsQuery.getResultList();
1246 String pdpLine = "";
1247 for(Object o2 : pdps){
1248 PdpEntity pdp = (PdpEntity)o2;
1249 pdpLine += ",".concat(pdp.getPdpId());
1250 propertyFileProperties.setProperty(pdp.getPdpId()+".description",pdp.getDescription());
1251 propertyFileProperties.setProperty(pdp.getPdpId()+".jmxport",String.valueOf(pdp.getJmxPort()));
1252 propertyFileProperties.setProperty(pdp.getPdpId()+".name",pdp.getPdpName());
1254 if(pdpLine.length() > 0){
1255 pdpLine = pdpLine.substring(1);
1257 propertyFileProperties.setProperty(group.getGroupId()+".description", group.getDescription());
1258 propertyFileProperties.setProperty(group.getGroupId()+".name", group.getgroupName());
1259 propertyFileProperties.setProperty(group.getGroupId()+".pdps",pdpLine);
1260 groupList += ",".concat(group.getGroupId());
1261 if(group.isDefaultGroup()){
1262 defaultGroup = group.getGroupId();
1263 foundDefault = true;
1266 if(!foundDefault && !alreadyRunAdd){
1267 alreadyRunAdd = true;
1268 //add default group to db
1270 em = emf.createEntityManager();
1271 em.getTransaction().begin();
1272 GroupEntity newDefaultGroup = new GroupEntity();
1273 em.persist(newDefaultGroup);
1274 newDefaultGroup.setDescription("The default group where new PDP's are put.");
1275 newDefaultGroup.setGroupId("default");
1276 newDefaultGroup.setGroupName("default");
1277 newDefaultGroup.setDefaultGroup(true);
1278 newDefaultGroup.setCreatedBy("automaticallyAdded");
1279 newDefaultGroup.setModifiedBy("automaticallyAdded");
1281 em.getTransaction().commit();
1283 } catch(Exception e){
1284 //TODO:EELF Cleanup - Remove logger
1285 //logger.error("Could not add a new default group to the database",e);
1286 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not add a new default group to the database");
1290 Path xacmlPropertiesPath = Paths.get(pdpsPath.toString(),"xacml.properties");
1291 File xacmlPropertiesFile = xacmlPropertiesPath.toFile();
1292 if(groupList.length()>0){
1293 groupList = groupList.substring(1);
1295 propertyFileProperties.setProperty("xacml.pap.groups",groupList);
1296 propertyFileProperties.setProperty("xacml.pap.groups.default",defaultGroup);
1298 xacmlPropertiesFile.createNewFile();
1299 } catch (IOException e) {
1300 // TODO Auto-generated catch block
1301 e.printStackTrace();
1304 FileOutputStream xacmlPropertiesFileStream = new FileOutputStream(xacmlPropertiesFile);
1305 //xacmlPropertiesFileStream.write(fileContents.getBytes("UTF-8"));
1306 propertyFileProperties.store(xacmlPropertiesFileStream, "");
1307 xacmlPropertiesFileStream.close();
1308 } catch (IOException e) {
1309 // TODO Auto-generated catch block
1310 e.printStackTrace();
1312 //if we get this far down, something went wrong and we don't want to get stuck in the loop
1313 foundDefault = true;
1315 //put policies in group folder
1316 //create xacml.policy.properties in each folder with list of policies in that folder
1318 //create xacml.properties with list of groups and pdps and other info
1322 //FIXME error checking and logging
1323 private String getPdpPolicyName(String name, String scope){
1324 String finalName = "";
1327 finalName += removeFileExtension(name);
1328 finalName += ".xml";
1331 private String removeFileExtension(String fileName){
1332 return fileName.substring(0, fileName.lastIndexOf('.'));
1335 private String buildPolicyScopeDirectory(PolicyEntity policy){
1336 String repo = buildPolicyDirectory();
1338 String policyScope = policy.getScope();
1339 if(policyScope == null){
1341 //TODO:EELF Cleanup - Remove logger
1342 //logger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1343 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1345 policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());
1347 if(policyScope == null){
1349 //TODO:EELF Cleanup - Remove logger
1350 //logger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1351 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") computed null policyScope. Using blank.");
1354 //TODO:EELF Cleanup - Remove logger
1355 //logger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
1356 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
1359 Path returnPath = Paths.get(repo + FileSystems.getDefault().getSeparator() + policyScope);
1360 if(returnPath != null){
1361 return returnPath.toString();
1363 //TODO:EELF Cleanup - Remove logger
1364 //logger.error("buildPolicyScopeDirectory("+policy+") computed null path");
1365 PolicyLogger.error("buildPolicyScopeDirectory("+policy+") received null repo. Using blank.");
1371 private String buildPolicyScopeDirectory(String policyScope){
1372 String repo = buildPolicyDirectory();
1373 policyScope = policyScope.replace(".", FileSystems.getDefault().getSeparator());
1374 return repo + FileSystems.getDefault().getSeparator() + policyScope;
1378 private static String buildPolicyDirectory(){
1379 Path workspacePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE), getDefaultWorkspace());
1380 Path repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY));
1381 Path gitPath = Paths.get(workspacePath.toString(), repositoryPath.getFileName().toString());
1384 * Getting and Setting the parent path for Admin Console use when reading the policy files
1386 //domain chosen by the client to store the policy action files
1387 //String domain = policy.getDomainDir();
1391 //getting the fullpath of the gitPath and convert to string
1392 String policyDir = gitPath.toAbsolutePath().toString();
1395 if(policyDir.contains("\\")){
1396 policyDir = policyDir.replace("XACML-PAP-REST", "XACML-PAP-ADMIN");
1398 if (policyDir.contains("pap")){
1399 policyDir = policyDir.replace("pap", "console");
1402 logger.debug("policyDir: " + policyDir);
1406 private Path getPolicySubFile(String filename, String subFileType){
1407 logger.debug("getPolicySubFile(" + filename + ", " + subFileType + ")");
1408 Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), subFileType);
1411 filename = FilenameUtils.removeExtension(filename);
1413 for(File tmpFile : filePath.toFile().listFiles()){
1414 if (FilenameUtils.removeExtension(tmpFile.getName()).equals(filename)){
1419 Path finalPath = null;
1421 finalPath = Paths.get(file.getAbsolutePath());
1424 logger.debug("end of getPolicySubFile: " + finalPath);
1428 private boolean writePolicySubFile(PolicyEntity policy, String policyType){
1429 logger.info("writePolicySubFile with policyName[" + policy.getPolicyName() + "] and policyType[" + policyType + "]");
1431 String subTypeName = null;
1432 String subTypeBody = null;
1433 if (policyType.equalsIgnoreCase("config")){
1435 subTypeName = FilenameUtils.removeExtension(policy.getConfigurationData().getConfigurationName());
1436 subTypeBody = policy.getConfigurationData().getConfigBody();
1438 String configType = policy.getConfigurationData().getConfigType();
1441 if (configType != null) {
1442 if (configType.equals(JSON_CONFIG)) {
1443 subTypeName = subTypeName + ".json";
1445 if (configType.equals(XML_CONFIG)) {
1446 subTypeName = subTypeName + ".xml";
1448 if (configType.equals(PROPERTIES_CONFIG)) {
1449 subTypeName = subTypeName + ".properties";
1451 if (configType.equals(OTHER_CONFIG)) {
1452 subTypeName = subTypeName + ".txt";
1456 }else if (policyType.equalsIgnoreCase("action")){
1458 subTypeName = policy.getActionBodyEntity().getActionBodyName();
1459 subTypeBody = policy.getActionBodyEntity().getActionBody();
1463 Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), type);
1465 if(subTypeBody == null){
1468 boolean success = false;
1470 Files.deleteIfExists(Paths.get(filePath.toString(), subTypeName));
1471 File file = Paths.get(filePath.toString(),subTypeName).toFile();
1472 file.createNewFile();
1473 FileWriter fileWriter = new FileWriter(file, false); // false to overwrite
1474 fileWriter.write(subTypeBody);
1478 } catch (Exception e) {
1479 //TODO:EELF Cleanup - Remove logger
1480 //logger.error("Exception occured while creating Configuration File for Policy : " + policy.getPolicyName(), e);
1481 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception occured while creating Configuration File for Policy : " + policy.getPolicyName());
1488 private String getPolicySubType(String filename){
1491 if (filename != null) {
1492 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("json")) {
1493 type = ConfigPolicy.JSON_CONFIG;
1495 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("xml")) {
1496 type = ConfigPolicy.XML_CONFIG;
1498 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("properties")) {
1499 type = ConfigPolicy.PROPERTIES_CONFIG;
1501 if (FilenameUtils.getExtension(filename).equalsIgnoreCase("txt")) {
1502 type = ConfigPolicy.OTHER_CONFIG;
1511 private void convertFileToDBEntry(Path path){
1512 logger.info("convertFileToDBEntry");
1514 if(path.toString().contains(".git")){
1518 String filename = path.getFileName().toString();
1519 if (filename.contains(".svnignore")){
1523 String[] scopeAndName = getScopeAndNameAndType(path.toString());
1525 if(scopeAndName == null){
1526 //TODO:EELF Cleanup - Remove logger
1527 //logger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + path.toString() + " is null!");
1528 PolicyLogger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + path.toString() + " is null!");
1532 EntityManager em = emf.createEntityManager();
1533 em.getTransaction().begin();
1535 PolicyEntity policy = new PolicyEntity();
1537 String policyScope = scopeAndName[0];
1538 String policyName = scopeAndName[1];
1539 policy.setScope(policyScope);
1540 policy.setPolicyName(policyName);
1541 policy.setCreatedBy(AUDIT_USER);
1542 policy.setModifiedBy(AUDIT_USER);
1544 String newScope = policyScope.replace(".", File.separator);
1545 String newName = FilenameUtils.removeExtension(policyName);
1548 //we want the last index +1 because we don't want the dot
1549 version = Integer.parseInt(newName.substring(newName.lastIndexOf(".")+1));
1550 } catch(Exception e){
1551 //TODO:EELF Cleanup - Remove logger
1552 //logger.error("Could not get the policy version number from "+newName);
1553 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get the policy version number from "+newName);
1555 newName = newScope + File.separator + newName.substring(0, newName.lastIndexOf("."));
1557 Query query = em.createNamedQuery("PolicyVersion.findByPolicyName");
1558 query.setParameter("pname", newName);
1560 List<?> result = query.getResultList();
1561 PolicyVersion versionEntity = null;
1563 if (!result.isEmpty()) {
1564 logger.info("Result is not empty");
1565 versionEntity = (PolicyVersion) result.get(0);
1566 int highestVersion = Math.max(versionEntity.getHigherVersion(),version);
1567 versionEntity.setHigherVersion(highestVersion);
1568 versionEntity.setActiveVersion(highestVersion);
1570 logger.info("result is empty");
1571 Calendar calendar = Calendar.getInstance();
1572 Timestamp createdDate = new Timestamp(calendar.getTime().getTime());
1574 versionEntity = new PolicyVersion();
1575 em.persist(versionEntity);
1576 versionEntity.setPolicyName(newName);
1577 versionEntity.setHigherVersion(version);
1578 versionEntity.setActiveVersion(version);
1579 versionEntity.setCreatedBy(AUDIT_USER);
1580 versionEntity.setModifiedBy(AUDIT_USER);
1581 versionEntity.setCreatedDate(createdDate);
1582 versionEntity.setModifiedDate(createdDate);
1587 String policyContent = new String(Files.readAllBytes(path));
1588 policy.setDescription(getElementFromXMLString("/Description", policyContent));
1589 policy.setPolicyData(policyContent);
1590 } catch (IOException e1) {
1591 //TODO:EELF Cleanup - Remove logger
1592 //logger.error("convertFileToDBEntry error settingPolicyData: " + e1.getMessage());
1593 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "convertFileToDBEntry error settingPolicyData");
1594 em.getTransaction().rollback();
1599 if((scopeAndName[2].equalsIgnoreCase("Config"))){
1600 String scopeName = scopeAndName[0] + "." + scopeAndName[1];
1601 Path subFilePath = getPolicySubFile(scopeName, scopeAndName[2]);
1603 String content = new String(Files.readAllBytes(subFilePath));
1604 String configName = subFilePath.getFileName().toString();
1605 ConfigurationDataEntity configData = new ConfigurationDataEntity();
1606 em.persist(configData);
1607 configData.setConfigurationName(subFilePath.getFileName().toString());
1608 configData.setConfigBody(content);
1609 configData.setConfigType(getPolicySubType(configName));
1610 configData.setCreatedBy(AUDIT_USER);
1611 configData.setModifiedBy(AUDIT_USER);
1612 policy.setConfigurationData(configData);
1614 } catch (Exception e) {
1615 //TODO:EELF Cleanup - Remove logger
1616 //logger.error("convertFileToDBEntry error for Config policy: " + e.getMessage());
1617 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "convertFileToDBEntry error for Config policy");
1618 em.getTransaction().rollback();
1622 }else if(scopeAndName[2].equalsIgnoreCase("Action")){
1623 String scopeName = scopeAndName[0] + "." + scopeAndName[1];
1624 Path subFilePath = getPolicySubFile(scopeName, scopeAndName[2]);
1626 String content = new String(Files.readAllBytes(subFilePath));
1627 ActionBodyEntity actionBody = new ActionBodyEntity();
1628 em.persist(actionBody);
1629 actionBody.setActionBodyName(subFilePath.getFileName().toString());
1630 actionBody.setActionBody(content);
1631 actionBody.setCreatedBy(AUDIT_USER);
1632 actionBody.setModifiedBy(AUDIT_USER);
1633 policy.setActionBodyEntity(actionBody);
1635 } catch (Exception e) {
1636 //TODO:EELF Cleanup - Remove logger
1637 //logger.error("convertFileToDBEntry error for Action policy: " + e.getMessage());
1638 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "convertFileToDBEntry error for Action policy");
1639 em.getTransaction().rollback();
1644 logger.debug("convertFileToDBEntry commit transaction");
1645 em.getTransaction().commit();
1649 private void deleteAllPolicyTables(){
1650 EntityManager em = emf.createEntityManager();
1651 em.getTransaction().begin();
1652 Query deletePolicyEntityTableUpdate = em.createNamedQuery("PolicyEntity.deleteAll");
1653 Query deleteActionBodyEntityTableUpdate = em.createNamedQuery("ActionBodyEntity.deleteAll");
1654 Query deleteConfigurationDataEntityTableUpdate = em.createNamedQuery("ConfigurationDataEntity.deleteAll");
1655 Query deletePolicyVersionEntityTableUpdate = em.createNamedQuery("PolicyVersion.deleteAll");
1656 deletePolicyEntityTableUpdate.executeUpdate();
1657 deleteActionBodyEntityTableUpdate.executeUpdate();
1658 deleteConfigurationDataEntityTableUpdate.executeUpdate();
1659 deletePolicyVersionEntityTableUpdate.executeUpdate();
1660 em.getTransaction().commit();
1665 public void auditLocalDatabase(PAPPolicyEngine papEngine2){
1666 logger.debug("PolicyDBDao.auditLocalDatabase() is called");
1667 Path webappsPath = Paths.get(buildPolicyDirectory());
1669 deleteAllGroupTables();
1670 deleteAllPolicyTables();
1671 Files.createDirectories(webappsPath);
1672 Files.walk(webappsPath).filter(Files::isRegularFile).forEach(this::convertFileToDBEntry);
1673 auditGroups(papEngine2);
1674 } catch(Exception e){
1675 //TODO:EELF Cleanup - Remove logger
1676 //logger.error("auditLocalDatabase() error: " + e.getMessage());
1677 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "auditLocalDatabase() error");
1678 e.printStackTrace();
1683 * Audits and loads the local file system to match the database version.
1685 @SuppressWarnings("unchecked")
1686 public void auditLocalFileSystem(){
1687 logger.debug("PolicyDBDau.auditLocalFileSystem() is called");
1689 Path webappsPath = Paths.get(buildPolicyDirectory());
1690 Path configFilesPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), "Config");
1691 Path actionFilesPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), "Action");
1693 Files.createDirectories(configFilesPath);
1694 Files.createDirectories(actionFilesPath);
1695 FileUtils.cleanDirectory(actionFilesPath.toFile());
1696 FileUtils.cleanDirectory(configFilesPath.toFile());
1697 if (webappsPath.toFile().exists()){
1698 FileUtils.cleanDirectory(webappsPath.toFile());
1700 Path repoWithScope = Paths.get(webappsPath.toString(), XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DOMAIN));
1701 Files.createDirectories(repoWithScope);
1702 } catch (IOException e2) {
1703 //TODO:EELF Cleanup - Remove logger
1704 //logger.error("Error occurred while creating / clearing Config and Policy filesystem directories", e2);
1705 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Error occurred while creating / clearing Config and Policy filesystem directories");
1708 List<PolicyEntity> policyEntityList;
1710 EntityManager em = emf.createEntityManager();
1711 Query getPolicyEntitiesQuery = em.createNamedQuery("PolicyEntity.findAllByDeletedFlag");
1712 getPolicyEntitiesQuery.setParameter("deleted", false);
1713 policyEntityList = getPolicyEntitiesQuery.getResultList();
1714 } catch(Exception e){
1715 policyEntityList = new LinkedList<PolicyEntity>();
1718 for (PolicyEntity policy: policyEntityList){
1721 if (!policy.isDeleted()){
1722 name = policy.getPolicyName();
1723 String scope = policy.getScope();
1725 scope = scope.replace(".", "//");
1726 if (policy.getConfigurationData()!=null){
1727 writePolicySubFile(policy, "Config");
1729 else if(policy.getActionBodyEntity()!=null){
1730 writePolicySubFile(policy, "Action");
1734 Path fileLocation = Paths.get(webappsPath.toString(), scope);
1736 Files.createDirectories(fileLocation);
1737 Path newPath = Paths.get(fileLocation.toString(), name);
1738 Object policyData = XACMLPolicyScanner.readPolicy(IOUtils.toInputStream(policy.getPolicyData()));
1739 XACMLPolicyWriter.writePolicyFile(newPath, (PolicyType) policyData);
1741 } catch (Exception e1) {
1742 //TODO:EELF Cleanup - Remove logger
1743 //logger.error("Error occurred while creating Policy File: " + name, e1);
1744 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Error occurred while creating Policy File: " + name);
1747 createGroupsFromDatabase();
1750 public void deleteAllGroupTables(){
1751 logger.debug("PolicyDBDao.deleteAllGroupTables() called");
1752 EntityManager em = emf.createEntityManager();
1753 em.getTransaction().begin();
1755 Query deletePdpEntityEntityTableUpdate = em.createNamedQuery("PdpEntity.deleteAll");
1756 deletePdpEntityEntityTableUpdate.executeUpdate();
1758 Query deleteGroupEntityTableUpdate = em.createNamedQuery("GroupEntity.deleteAll");
1759 deleteGroupEntityTableUpdate.executeUpdate();
1761 em.getTransaction().commit();
1765 @SuppressWarnings("unchecked")
1766 public void auditGroups(PAPPolicyEngine papEngine2){
1767 logger.debug("PolicyDBDao.auditGroups() called");
1769 EntityManager em = emf.createEntityManager();
1770 em.getTransaction().begin();
1771 final String AUDIT_STR = "Audit";
1774 Set<EcompPDPGroup> groups = papEngine2.getEcompPDPGroups();
1776 for (EcompPDPGroup grp : groups){
1778 GroupEntity groupEntity = new GroupEntity();
1779 em.persist(groupEntity);
1780 groupEntity.setGroupName(grp.getName());
1781 groupEntity.setDescription(grp.getDescription());
1782 groupEntity.setDefaultGroup(grp.isDefaultGroup());
1783 groupEntity.setCreatedBy(AUDIT_STR);
1784 groupEntity.setGroupId(createNewPDPGroupId(grp.getId()));
1785 groupEntity.setModifiedBy(AUDIT_STR);
1786 Set<EcompPDP> pdps = grp.getEcompPdps();
1788 for(EcompPDP pdp : pdps){
1789 PdpEntity pdpEntity = new PdpEntity();
1790 em.persist(pdpEntity);
1791 pdpEntity.setGroup(groupEntity);
1792 pdpEntity.setJmxPort(pdp.getJmxPort());
1793 pdpEntity.setPdpId(pdp.getId());
1794 pdpEntity.setPdpName(pdp.getName());
1795 pdpEntity.setModifiedBy(AUDIT_STR);
1796 pdpEntity.setCreatedBy(AUDIT_STR);
1800 Set<PDPPolicy> policies = grp.getPolicies();
1802 for(PDPPolicy policy : policies){
1804 String[] stringArray = getNameScopeAndVersionFromPdpPolicy(policy.getId());
1805 List<PolicyEntity> policyEntityList;
1806 Query getPolicyEntitiesQuery = em.createNamedQuery("PolicyEntity.findByNameAndScope");
1807 getPolicyEntitiesQuery.setParameter("name", stringArray[0]);
1808 getPolicyEntitiesQuery.setParameter("scope", stringArray[1]);
1810 policyEntityList = getPolicyEntitiesQuery.getResultList();
1811 PolicyEntity policyEntity = null;
1812 if(policyEntityList.size() < 1){
1813 policyEntity = addPolicyThatOnlyExistsInPdpGroup(policy.getId(),Paths.get("pdps",grp.getId(),policy.getId()),em);
1815 policyEntity = policyEntityList.get(0);
1817 if(policyEntity != null){
1818 groupEntity.addPolicyToGroup(policyEntity);
1820 }catch(Exception e2){
1821 //TODO:EELF Cleanup - Remove logger
1822 //logger.error("ERROR: " + e2);
1823 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Exception auditGroups inner catch");
1826 }catch(Exception e1){
1827 //TODO:EELF Cleanup - Remove logger
1828 //logger.error("ERROR: " + e1);
1829 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "Exception auditGroups middle catch");
1832 }catch(Exception e){
1833 em.getTransaction().rollback();
1834 //TODO:EELF Cleanup - Remove logger
1835 //logger.error("ERROR: " + e);
1836 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception auditGroups outer catch");
1841 em.getTransaction().commit();
1846 private PolicyEntity addPolicyThatOnlyExistsInPdpGroup(String polId, Path path,EntityManager em){
1847 String filename = path.getFileName().toString();
1848 if (filename.contains(".svnignore")){
1852 String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(polId);
1854 if(scopeAndName == null){
1855 //TODO:EELF Cleanup - Remove logger
1856 //logger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + polId.toString() + " is null!");
1857 PolicyLogger.error("convertFileToDBEntry error: getScopeAndNameAndType(" + polId.toString() + " is null!");
1862 PolicyEntity policy = new PolicyEntity();
1864 String policyScope = scopeAndName[1];
1865 String policyName = scopeAndName[0];
1866 policy.setScope(policyScope);
1867 policy.setPolicyName(policyName);
1868 policy.setCreatedBy(AUDIT_USER);
1869 policy.setModifiedBy(AUDIT_USER);
1870 policy.setDeleted(true);
1873 String policyContent = new String(Files.readAllBytes(path));
1874 policy.setDescription(getElementFromXMLString("/Description", policyContent));
1875 policy.setPolicyData(policyContent);
1877 //em.getTransaction().commit();
1878 } catch (IOException e1) {
1879 // TODO Auto-generated catch block
1880 //TODO:EELF Cleanup - Remove logger
1881 //logger.error("convertFileToDBEntry error settingPolicyData: " + e1.getMessage());
1882 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "convertFileToDBEntry error settingPolicyData");
1889 private String getConfigFile(String filename, String scope, PolicyRestAdapter policy){
1891 return getConfigFile(filename, scope, (String)null);
1893 return getConfigFile(filename, scope, policy.getConfigType());
1895 //copied from ConfigPolicy.java and modified
1896 // Here we are adding the extension for the configurations file based on the
1897 // config type selection for saving.
1898 private String getConfigFile(String filename, String scope, String configType) {
1899 logger.debug("getConfigFile(String filename, String scope, String configType) as getConfigFile("+filename+", "+scope+", "+configType+") called");
1900 filename = FilenameUtils.removeExtension(filename);
1901 // if (filename.endsWith(".xml")) {
1902 // filename = filename.substring(0, filename.length() - 4);
1904 String id = configType;
1907 if (id.equals(ConfigPolicy.JSON_CONFIG) || id.contains("Firewall")) {
1908 filename = filename + ".json";
1910 if (id.equals(ConfigPolicy.XML_CONFIG)) {
1911 filename = filename + ".xml";
1913 if (id.equals(ConfigPolicy.PROPERTIES_CONFIG)) {
1914 filename = filename + ".properties";
1916 if (id.equals(ConfigPolicy.OTHER_CONFIG)) {
1917 filename = filename + ".txt";
1920 return scope + "." + filename;
1924 * Constructs the file name of a policy.
1925 * @param policy The name of a policy (ex: mypolicy1)
1926 * @return The file name of the policy (ex: Config_mypolicy1.xml)
1929 @SuppressWarnings("unused")
1930 private String getName(PolicyRestAdapter policy){
1931 logger.debug("getName(PolicyRestAdapter policy) as getName("+policy+") called");
1932 String namePrefix = "";
1933 if(policy.getPolicyType().contains("Config")){
1934 namePrefix = namePrefix.concat(policy.getPolicyType());
1935 if(policy.getConfigType().contains("Firewall")){
1936 namePrefix = namePrefix.concat("_FW");
1939 String concats = namePrefix + "_" +policy.getPolicyName() + ".xml";
1943 private String stripPolicyName(String policyFileName){
1944 String policyName = policyFileName;
1946 policyName = policyName.substring(policyName.indexOf('_')+1);
1947 policyName = removeFileExtension(policyName);
1948 }catch(Exception e){
1949 throw new IllegalArgumentException("Could not get name out of policy file name: "+policyName);
1953 //FIXME error check, logs
1954 private String[] getNameScopeAndVersionFromPdpPolicy(String fileName){
1955 String[] splitByDots = fileName.split("\\.");
1956 if(splitByDots.length < 3){
1960 String policyName = splitByDots[splitByDots.length-3];
1961 String version = splitByDots[splitByDots.length-2];
1962 //policy names now include version
1963 policyName += "."+version +".xml";
1965 for(int i=0;i<splitByDots.length-3;i++){
1966 scope += ".".concat(splitByDots[i]);
1968 //remove the first dot
1969 if(scope.length() > 0){
1970 scope = scope.substring(1);
1972 String[] returnArray = new String[3];
1973 returnArray[0] = policyName;
1974 returnArray[2] = version;
1975 returnArray[1] = scope;
1980 * Constructs the complete repository path based on the properties files
1981 * @return The repository path
1983 public static String getGitPath(){
1984 logger.debug("getGitPath() as getGitPath() called");
1985 Path workspacePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE), "admin");
1986 Path repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY));
1987 Path gitPath = Paths.get(workspacePath.toString(), repositoryPath.getFileName().toString());
1988 logger.debug("after gitPath: " + gitPath);
1989 return gitPath.toString();
1992 //copied from StdEngine.java
1993 public static String createNewPDPGroupId(String name) {
1995 // replace "bad" characters with sequences that will be ok for file names and properties keys.
1996 id = id.replace(" ", "_sp_");
1997 id = id.replace("\t", "_tab_");
1998 id = id.replace("\\", "_bksl_");
1999 id = id.replace("/", "_sl_");
2000 id = id.replace(":", "_col_");
2001 id = id.replace("*", "_ast_");
2002 id = id.replace("?", "_q_");
2003 id = id.replace("\"", "_quo_");
2004 id = id.replace("<", "_lt_");
2005 id = id.replace(">", "_gt_");
2006 id = id.replace("|", "_bar_");
2007 id = id.replace("=", "_eq_");
2008 id = id.replace(",", "_com_");
2009 id = id.replace(";", "_scom_");
2015 * Checks if any of the given strings are empty or null
2016 * @param strings One or more Strings (or nulls) to check if they are null or empty
2017 * @return true if one or more of the given strings are empty or null
2019 private static boolean isNullOrEmpty(String... strings){
2020 for(String s : strings){
2021 if(!(s instanceof String)){
2032 * Computes the scope, name, and type of a policy based on its file path
2033 * @param path The file path of the policy (including the xml policy file)
2034 * @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.
2036 private static String[] getScopeAndNameAndType(String path){
2037 logger.debug("getScopeAndNameAndType(String path) as getScopeAndNameAndType("+path+") called");
2041 String gitPath = getGitPath();
2043 ArrayList<String> gitPathParts = new ArrayList<String>();
2044 Iterator<?> gitPathIterator = Paths.get(gitPath).iterator();
2045 while(gitPathIterator.hasNext()){
2046 gitPathParts.add(gitPathIterator.next().toString());
2048 for(int i=0;i<gitPathParts.size();i++){
2049 Path testGitPath = Paths.get("");
2050 for(int j=i;j<gitPathParts.size();j++){
2051 testGitPath = Paths.get(testGitPath.toString(),gitPathParts.get(j));
2053 if(path.contains(testGitPath.toString())){
2054 gitPath = testGitPath.toString();
2058 if(gitPath == null){
2059 logger.debug("gitPath is null. Returning");
2062 if(gitPath.length() >= path.length()){
2063 logger.debug("gitPath length(): " + gitPath.length() + ">= path.length(): " + path.length() + ". Returning null");
2066 String scopeAndName = path.substring(path.indexOf(gitPath)+gitPath.length());
2068 logger.debug("scopeAndName: " + scopeAndName);
2069 String policyType = null;
2070 String[] policyTypes = {"Config_","Action_","Decision_"};
2071 for(String pType : policyTypes){
2072 if(scopeAndName.contains(pType)){
2076 if(policyType == null){
2079 String scope = scopeAndName.substring(0,scopeAndName.indexOf(policyType));
2080 String name = scopeAndName.substring(scopeAndName.indexOf(policyType), scopeAndName.length());
2081 scope = scope.replace('\\', '.');
2082 scope = scope.replace('/', '.');
2083 if(scope.length()<1){
2086 if(scope.charAt(0) == '.'){
2087 if(scope.length() < 2){
2088 logger.debug("getScopeAndNameAndType error: " + scope.length() + " < 2. " + "| scope.charAt(0)==.");
2091 scope = scope.substring(1);
2093 if(scope.charAt(scope.length()-1) == '.'){
2094 if(scope.length() < 2){
2095 logger.debug("getScopeAndNameAndType error: " + scope.length() + " < 2" + "| scope.charAt(scope.length()-1)==.");
2098 scope = scope.substring(0,scope.length()-1);
2100 if(name.length()<1){
2101 logger.debug("getScopeAndNameAndType error: name.length()<1");
2104 if(name.charAt(0) == '.'){
2105 if(name.length() < 2){
2106 logger.debug("getScopeAndNameAndType error: " + name.length() + " < 2. " + "| scope.charAt(0)==.");
2109 name = name.substring(1);
2111 String[] returnArray = new String[3];
2112 returnArray[0] = scope;
2113 returnArray[1] = name;
2114 //remove the underscore and return it
2115 returnArray[2] = policyType.substring(0, policyType.length()-1);
2120 private class PolicyDBDaoTransactionInstance implements PolicyDBDaoTransaction {
2121 private EntityManager em;
2122 private final Object emLock = new Object();
2127 private boolean operationRun = false;
2128 private final Thread transactionTimer;
2130 private PolicyDBDaoTransactionInstance(){
2131 //call the constructor with arguments
2132 this(Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_TIMEOUT)),
2133 Integer.parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT)));
2135 //timeout is how long the transaction can sit before rolling back
2136 //wait time is how long to wait for the transaction to start before throwing an exception
2137 private PolicyDBDaoTransactionInstance(int transactionTimeout, int transactionWaitTime){
2138 if(logger.isDebugEnabled()){
2139 logger.debug("\n\nPolicyDBDaoTransactionInstance() as PolicyDBDaoTransactionInstance() called:"
2140 + "\n transactionTimeout = " + transactionTimeout
2141 + "\n transactionWaitTime = " + transactionWaitTime + "\n\n");
2143 this.em = emf.createEntityManager();
2148 synchronized(emLock){
2150 startTransactionSynced(this.em,transactionWaitTime);
2151 } catch(Exception e){
2152 throw new PersistenceException("Could not lock transaction within "+transactionWaitTime+" milliseconds");
2155 class TransactionTimer implements Runnable {
2157 private int sleepTime;
2158 public TransactionTimer(int timeout){
2159 this.sleepTime = timeout;
2163 if(logger.isDebugEnabled()){
2164 Date date= new java.util.Date();
2165 logger.debug("\n\nTransactionTimer.run() - SLEEPING: "
2166 + "\n sleepTime (ms) = " + sleepTime
2167 + "\n TimeStamp = " + date.getTime()
2171 Thread.sleep(sleepTime);
2172 } catch (InterruptedException e) {
2173 //probably, the transaction was completed, the last thing we want to do is roll back
2174 if(logger.isDebugEnabled()){
2175 Date date= new java.util.Date();
2176 logger.debug("\n\nTransactionTimer.run() - WAKE Interrupt: "
2177 + "\n TimeStamp = " + date.getTime()
2182 if(logger.isDebugEnabled()){
2183 Date date= new java.util.Date();
2184 logger.debug("\n\nTransactionTimer.run() - WAKE Timeout: "
2185 + "\n TimeStamp = " + date.getTime()
2188 rollbackTransaction();
2193 transactionTimer = new Thread(new TransactionTimer(transactionTimeout),"transactionTimerThread");
2194 transactionTimer.start();
2199 private void checkBeforeOperationRun(){
2200 checkBeforeOperationRun(false);
2202 private void checkBeforeOperationRun(boolean justCheckOpen){
2203 if(!isTransactionOpen()){
2204 //TODO:EELF Cleanup - Remove logger
2205 //logger.error("There is no transaction currently open");
2206 PolicyLogger.error("There is no transaction currently open");
2207 throw new IllegalStateException("There is no transaction currently open");
2209 if(operationRun && !justCheckOpen){
2210 //TODO:EELF Cleanup - Remove logger
2211 //logger.error("An operation has already been performed and the current transaction should be committed");
2212 PolicyLogger.error("An operation has already been performed and the current transaction should be committed");
2213 throw new IllegalStateException("An operation has already been performed and the current transaction should be committed");
2215 operationRun = true;
2218 public void commitTransaction() {
2219 synchronized(emLock){
2220 logger.debug("commitTransaction() as commitTransaction() called");
2221 if(!isTransactionOpen()){
2222 logger.warn("There is no open transaction to commit");
2223 //throw new IllegalStateException("There is no open transaction to commit");
2226 } catch(Exception e){
2227 e.printStackTrace();
2232 em.getTransaction().commit();
2233 } catch(RollbackException e){
2234 //TODO:EELF Cleanup - Remove logger
2235 //logger.error("Caught RollbackException on em.getTransaction().commit()",e);
2236 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught RollbackException on em.getTransaction().commit()");
2237 throw new PersistenceException("The commit failed. Message:\n"+e.getMessage());
2240 //FIXME need to revisit
2243 if(newGroupId != null){
2245 notifyOthers(policyId,POLICY_NOTIFICATION,newGroupId);
2246 } catch(Exception e){
2247 //TODO:EELF Cleanup - Remove logger
2248 //logger.error("Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+","+newGroupId+")",e);
2249 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+","+newGroupId+")");
2253 notifyOthers(policyId,POLICY_NOTIFICATION);
2254 } catch(Exception e){
2255 //TODO:EELF Cleanup - Remove logger
2256 //logger.error("Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+")",e);
2257 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+policyId+","+POLICY_NOTIFICATION+")");
2262 //we don't want commit to fail just because this does
2263 if(newGroupId != null){
2265 notifyOthers(groupId,GROUP_NOTIFICATION,newGroupId);
2266 } catch(Exception e){
2267 //TODO:EELF Cleanup - Remove logger
2268 //logger.error("Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+","+newGroupId+")",e);
2269 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+","+newGroupId+")");
2273 notifyOthers(groupId,GROUP_NOTIFICATION);
2274 } catch(Exception e){
2275 //TODO:EELF Cleanup - Remove logger
2276 //logger.error("Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+")",e);
2277 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+groupId+","+GROUP_NOTIFICATION+")");
2282 //we don't want commit to fail just because this does
2284 notifyOthers(pdpId,PDP_NOTIFICATION);
2285 } catch(Exception e){
2286 //TODO:EELF Cleanup - Remove logger
2287 //logger.error("Caught Exception on notifyOthers("+pdpId+","+PDP_NOTIFICATION+")",e);
2288 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on notifyOthers("+pdpId+","+PDP_NOTIFICATION+")");
2292 if(transactionTimer instanceof Thread){
2293 transactionTimer.interrupt();
2298 public void rollbackTransaction() {
2299 logger.debug("rollbackTransaction() as rollbackTransaction() called");
2300 synchronized(emLock){
2301 if(isTransactionOpen()){
2304 em.getTransaction().rollback();
2305 } catch(Exception e){
2306 //TODO:EELF Cleanup - Remove logger
2307 //logger.error("Could not rollback transaction");
2308 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not rollback transaction");
2312 }catch(Exception e){
2313 //TODO:EELF Cleanup - Remove logger
2314 //logger.error("Could not close EntityManager");
2315 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not close EntityManager");
2321 }catch(Exception e){
2322 logger.warn("Could not close already closed transaction");
2327 if(transactionTimer instanceof Thread){
2328 transactionTimer.interrupt();
2334 private void createPolicy(PolicyRestAdapter policy, String username, String policyScope, String policyName, String policyDataString) {
2335 logger.debug("createPolicy(PolicyRestAdapter policy, String username, String policyScope, String policyName, String policyDataString) as createPolicy("+policy+", "+username+", "+policyScope+", "+policyName+", "+policyDataString+") called");
2336 synchronized(emLock){
2337 checkBeforeOperationRun();
2338 //em.getTransaction().begin();
2339 //FIXME if the policy is already found but deleted, when we update it should we reset the created by and version number?
2340 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName");
2341 createPolicyQuery.setParameter("scope", policyScope);
2342 createPolicyQuery.setParameter("policyName", policyName);
2343 //createPolicyQuery.setParameter("deleted", false);
2344 List<?> createPolicyQueryList = createPolicyQuery.getResultList();
2345 PolicyEntity newPolicyEntity;
2347 if(createPolicyQueryList.size() < 1){
2348 newPolicyEntity = new PolicyEntity();
2350 } else if(createPolicyQueryList.size() > 1){
2351 //something went wrong
2352 //TODO:EELF Cleanup - Remove logger
2353 //logger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2354 PolicyLogger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2355 throw new PersistenceException("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2357 newPolicyEntity = (PolicyEntity)createPolicyQueryList.get(0);
2361 ActionBodyEntity newActionBodyEntity = null;
2362 if(policy.getPolicyType().equals("Action")){
2363 boolean abupdate = false;
2364 if(newPolicyEntity.getActionBodyEntity() == null){
2365 newActionBodyEntity = new ActionBodyEntity();
2367 newActionBodyEntity = em.find(ActionBodyEntity.class, newPolicyEntity.getActionBodyEntity().getActionBodyId());
2371 if(newActionBodyEntity != null){
2373 em.persist(newActionBodyEntity);
2375 //build the file path
2376 //trim the .xml off the end
2377 String policyNameClean = FilenameUtils.removeExtension(policyName);
2378 String actionBodyName = policyScope + "." + policyNameClean + ".json";
2379 Path actionBodyPath = Paths.get(Webapps.getActionHome(), actionBodyName);
2380 if(logger.isDebugEnabled()){
2381 logger.debug("\nPolicyDBDao.createPolicy"
2382 + "\n actionBodyPath = " + actionBodyPath);
2384 //get the action body
2385 String actionBodyString = null;
2386 String actionBodyPathStr = null;
2387 InputStream fileContentStream = null;
2389 if (Files.exists(actionBodyPath)) {
2391 actionBodyPathStr = (actionBodyPath != null ? actionBodyPath.toString() : null);
2392 fileContentStream = new FileInputStream(actionBodyPathStr);
2393 actionBodyString = IOUtils.toString(fileContentStream);
2394 if(logger.isDebugEnabled()){
2395 logger.debug("\nPolicyDBDao.createPolicy"
2396 + "\n actionBodyPathStr = " + actionBodyPathStr
2397 + "\n actionBodyString = " + actionBodyString);
2399 } catch (FileNotFoundException e) {
2400 //TODO:EELF Cleanup - Remove logger
2401 //logger.error("Caught FileNotFoundException on new actionBodyPathStr FileInputStream("+actionBodyPathStr+")",e);
2402 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new actionBodyPathStr FileInputStream("+actionBodyPathStr+")");
2403 throw new IllegalArgumentException("The actionBodyPathStr file path " + actionBodyPathStr + " does not exist"
2404 + "\nEXCEPTION: " + e);
2405 } catch(IOException e2){
2406 //TODO:EELF Cleanup - Remove logger
2407 //logger.error("Caught IOException on actionBodyPath newIOUtils.toString("+fileContentStream+")",e2);
2408 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on actionBodyPath newIOUtils.toString("+fileContentStream+")");
2409 throw new IllegalArgumentException("The actionBodyPath file path cannot be read" + fileContentStream
2410 + "\nEXCEPTION: " + e2);
2412 IOUtils.closeQuietly(fileContentStream);
2415 if(actionBodyString == null){
2416 throw new IllegalArgumentException("The file path (" + actionBodyPathStr + ") cannot be read");
2420 actionBodyString = "{}";
2423 newActionBodyEntity.setActionBody(actionBodyString);
2424 newActionBodyEntity.setActionBodyName(actionBodyName);
2425 newActionBodyEntity.setModifiedBy("PolicyDBDao.createPolicy()");
2426 newActionBodyEntity.setDeleted(false);
2428 newActionBodyEntity.setCreatedBy("PolicyDBDao.createPolicy()");
2430 if(logger.isDebugEnabled()){
2431 logger.debug("\nPolicyDBDao.createPolicy"
2432 + "\n newActionBodyEntity.getActionBody() = " + newActionBodyEntity.getActionBody()
2433 + "\n newActionBodyEntity.getActionBodyName() = " + newActionBodyEntity.getActionBodyName()
2434 + "\n newActionBodyEntity.getModifiedBy() = " + newActionBodyEntity.getModifiedBy()
2435 + "\n newActionBodyEntity.getCreatedBy() = " + newActionBodyEntity.getCreatedBy()
2436 + "\n newActionBodyEntity.isDeleted() = " + newActionBodyEntity.isDeleted()
2437 + "\n FLUSHING to DB");
2439 //push the actionBodyEntity to the DB
2442 //newActionBodyEntity == null
2443 //We have a actionBody in the policy but we found no actionBody in the DB
2444 String msg = "\n\nPolicyDBDao.createPolicy - Incoming Action policy had an "
2445 + "actionBody, but it could not be found in the DB for update."
2446 + "\n policyScope = " + policyScope
2447 + "\n policyName = " + policyName + "\n\n";
2448 //TODO:EELF Cleanup - Remove logger
2449 //logger.error(msg);
2450 PolicyLogger.error("PolicyDBDao.createPolicy - Incoming Action policy had an actionBody, but it could not be found in the DB for update: policyName = " + policyName);
2451 throw new IllegalArgumentException(msg);
2455 ConfigurationDataEntity newConfigurationDataEntity;
2456 if(policy.getPolicyType().equals("Config")){
2457 boolean configUpdate;
2458 if(newPolicyEntity.getConfigurationData() == null){
2459 newConfigurationDataEntity = new ConfigurationDataEntity();
2460 configUpdate = false;
2462 newConfigurationDataEntity = em.find(ConfigurationDataEntity.class, newPolicyEntity.getConfigurationData().getConfigurationDataId());
2463 configUpdate = true;
2466 if(newConfigurationDataEntity != null){
2468 em.persist(newConfigurationDataEntity);
2470 //ConfigPolicy configPolicy = (ConfigPolicy)policy;
2471 if(!stringEquals(newConfigurationDataEntity.getConfigurationName(),getConfigFile(policyName,policyScope,policy))){
2472 newConfigurationDataEntity.setConfigurationName(getConfigFile(policyName,policyScope,policy));
2474 if(newConfigurationDataEntity.getConfigType() == null || !newConfigurationDataEntity.getConfigType().equals(policy.getConfigType())){
2475 newConfigurationDataEntity.setConfigType(policy.getConfigType());
2478 newConfigurationDataEntity.setCreatedBy(username);
2480 if(newConfigurationDataEntity.getModifiedBy() == null || !newConfigurationDataEntity.getModifiedBy().equals(username)){
2481 newConfigurationDataEntity.setModifiedBy(username);
2483 if(newConfigurationDataEntity.getDescription() == null || !newConfigurationDataEntity.getDescription().equals("")){
2484 newConfigurationDataEntity.setDescription("");
2486 if(newConfigurationDataEntity.getConfigBody() == null || newConfigurationDataEntity.getConfigBody().isEmpty() ||
2487 (!newConfigurationDataEntity.getConfigBody().equals(policy.getConfigBodyData()))){
2488 //hopefully one of these won't be null
2489 if(policy.getConfigBodyData() == null){
2490 newConfigurationDataEntity.setConfigBody(policy.getJsonBody());
2492 newConfigurationDataEntity.setConfigBody(policy.getConfigBodyData());
2495 if(newConfigurationDataEntity.isDeleted() == true){
2496 newConfigurationDataEntity.setDeleted(false);
2500 }else{//newConfigurationDataEntity == null
2501 //We have a configurationData body in the policy but we found no configurationData body
2503 String msg = "\n\nPolicyDBDao.createPolicy - Incoming Config policy had a "
2504 + "configurationData body, but it could not be found in the DB for update."
2505 + "\n policyScope = " + policyScope
2506 + "\n policyName = " + policyName + "\n\n";
2507 //TODO:EELF Cleanup - Remove logger
2508 //logger.error(msg);
2509 PolicyLogger.error("PolicyDBDao.createPolicy - Incoming Config policy had a configurationData body, but it could not be found in the DB for update: policyName = " + policyName);
2510 throw new IllegalArgumentException(msg);
2514 newConfigurationDataEntity = null;
2517 em.persist(newPolicyEntity);
2520 policyId = newPolicyEntity.getPolicyId();
2521 //policy version is now part of policy name
2525 String versionString = evaluateXPath("Policy/@Version", policyDataString);
2526 int versionNum = Integer.parseInt(versionString);
2528 throw new NumberFormatException();
2530 newPolicyEntity.setPolicyVersion(versionNum);
2531 } catch(Exception e){
2532 if(newPolicyEntity.isDeleted()){
2533 newPolicyEntity.resetPolicyVersion();
2535 newPolicyEntity.advancePolicyVersion();
2542 if(!stringEquals(newPolicyEntity.getPolicyName(),policyName)){
2543 newPolicyEntity.setPolicyName(policyName);
2545 if(!stringEquals(newPolicyEntity.getCreatedBy(),username)){
2546 newPolicyEntity.setCreatedBy(username);
2548 if(!stringEquals(newPolicyEntity.getDescription(),policy.getPolicyDescription())){
2549 newPolicyEntity.setDescription(policy.getPolicyDescription());
2551 if(!stringEquals(newPolicyEntity.getModifiedBy(),username)){
2552 newPolicyEntity.setModifiedBy(username);
2554 if(!stringEquals(newPolicyEntity.getPolicyData(),policyDataString)){
2555 newPolicyEntity.setPolicyData(policyDataString);
2557 if(!stringEquals(newPolicyEntity.getScope(),policyScope)){
2558 newPolicyEntity.setScope(policyScope);
2560 if(newPolicyEntity.isDeleted() == true){
2561 newPolicyEntity.setDeleted(false);
2563 newPolicyEntity.setConfigurationData(newConfigurationDataEntity);
2564 newPolicyEntity.setActionBodyEntity(newActionBodyEntity);
2568 this.policyId = newPolicyEntity.getPolicyId();
2574 @SuppressWarnings("unused")
2575 public PolicyEntity getPolicy(int policyID){
2576 return getPolicy(policyID,null,null);
2578 public PolicyEntity getPolicy(String policyName,String scope){
2579 return getPolicy(-1,policyName,scope);
2581 private PolicyEntity getPolicy(int policyID, String policyName,String scope){
2582 logger.debug("getPolicy(int policyId, String policyName) as getPolicy("+policyID+","+policyName+") called");
2583 if(policyID < 0 && isNullOrEmpty(policyName,scope)){
2584 throw new IllegalArgumentException("policyID must be at least 0 or policyName must be not null or blank");
2587 synchronized(emLock){
2588 checkBeforeOperationRun(true);
2589 //check if group exists
2592 if(!isNullOrEmpty(policyName,scope)){
2593 policyId = policyName;
2594 policyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:name AND p.scope=:scope");
2595 policyQuery.setParameter("name", policyId);
2596 policyQuery.setParameter("scope", scope);
2598 policyId = String.valueOf(policyID);
2599 policyQuery = em.createNamedQuery("PolicyEntity.FindById");
2600 policyQuery.setParameter("id", policyId);
2602 List<?> policyQueryList;
2604 policyQueryList = policyQuery.getResultList();
2605 }catch(Exception e){
2606 //TODO:EELF Cleanup - Remove logger
2607 //logger.error("Caught Exception trying to get policy with policyQuery.getResultList()",e);
2608 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get policy with policyQuery.getResultList()");
2609 throw new PersistenceException("Query failed trying to get policy "+policyId);
2611 if(policyQueryList.size() < 1){
2612 //TODO:EELF Cleanup - Remove logger
2613 //logger.error("Policy does not exist with id "+policyId);
2614 PolicyLogger.error("Policy does not exist with id "+policyId);
2615 throw new PersistenceException("Group policy is being added to does not exist with id "+policyId);
2616 } else if(policyQueryList.size() > 1){
2617 //TODO:EELF Cleanup - Remove logger
2618 //logger.error("Somehow, more than one policy with the id "+policyId+" were found in the database");
2619 PolicyLogger.error("Somehow, more than one policy with the id "+policyId+" were found in the database");
2620 throw new PersistenceException("Somehow, more than one policy with the id "+policyId+" were found in the database");
2622 return (PolicyEntity)policyQueryList.get(0);
2627 public void renamePolicy(String oldPath, String newPath,String username){
2628 String[] oldPolicy = getScopeAndNameAndType(oldPath);
2629 String[] newPolicy = getScopeAndNameAndType(newPath);
2630 if(oldPolicy == null || newPolicy == null){
2631 //TODO:EELF Cleanup - Remove logger
2632 //logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2633 //+oldPath+", "+newPath);
2634 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2635 +oldPath+", "+newPath);
2636 throw new IllegalArgumentException("Could not parse one or more of the path names");
2638 synchronized (emLock) {
2639 checkBeforeOperationRun();
2641 PolicyEntity existingPolicy;
2642 boolean existingPolicyDeleted = false;
2643 List<?> groups = null;
2645 existingPolicy = getPolicy(newPolicy[1],newPolicy[0]);
2646 } catch(Exception e){
2647 existingPolicy = null;
2649 if(existingPolicy != null && !existingPolicy.isDeleted()){
2650 logger.error("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
2651 throw new IllegalArgumentException("The policy named "+existingPolicy.getPolicyName()+" already exists, cannot rename policy: "+newPolicy);
2652 } else if(existingPolicy != null && existingPolicy.isDeleted()){
2654 Query getGroups = em.createQuery("SELECT g FROM GroupEntity g JOIN g.policies p WHERE p.policyId=:pid");
2656 getGroups.setParameter("pid", existingPolicy.getPolicyId());
2657 groups = getGroups.getResultList();
2658 }catch(Exception e){
2659 groups = new LinkedList<GroupEntity>();
2661 for(Object o : groups){
2663 GroupEntity group = (GroupEntity)o;
2664 group.removePolicyFromGroup(existingPolicy);
2668 }catch(Exception e){
2669 logger.error("Error while removing the policy from groups: "+existingPolicy.getPolicyName());
2672 em.remove(existingPolicy);
2674 }catch(Exception e){
2675 logger.error("Could not remove the existing deleted policy: "+existingPolicy.getPolicyName());
2677 existingPolicyDeleted = true;
2678 //create the new policy
2679 //for each of the groups, add the new policy
2682 PolicyEntity policyToRename;
2684 policyToRename = getPolicy(oldPolicy[1],oldPolicy[0]);
2685 } catch(Exception e){
2686 //TODO:EELF Cleanup - Remove logger
2687 //logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Could not get policy record to rename: "
2689 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "PolicyDBDao", "Could not get policy record to rename: "
2691 throw new PersistenceException("Could not get policy record to rename");
2693 String policyDataString = null;
2694 InputStream fileContentStream = null;
2695 String policyFilePath = Paths.get(oldPath).toAbsolutePath().toString();
2696 //I want to try the old path first, then if it doesn't work, try the new path
2697 for(int i=0;i<2;i++){
2699 fileContentStream = new FileInputStream(policyFilePath);
2700 policyDataString = IOUtils.toString(fileContentStream);
2701 } catch (FileNotFoundException e) {
2702 //TODO:EELF Cleanup - Remove logger
2703 //logger.error("Caught FileNotFoundException on new FileInputStream("+policyFilePath+")",e);
2704 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new FileInputStream("+policyFilePath+")");
2705 //if we can't find the oldPath, we'll try the new path
2707 policyFilePath = Paths.get(newPath).toAbsolutePath().toString();
2710 throw new IllegalArgumentException("The file path does not exist");
2711 } catch(IOException e2){
2712 //TODO:EELF Cleanup - Remove logger
2713 //logger.error("Caught IOException on newIOUtils.toString("+fileContentStream+")",e2);
2714 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on newIOUtils.toString("+fileContentStream+")");
2715 throw new IllegalArgumentException("The file path cannot be read");
2717 IOUtils.closeQuietly(fileContentStream);
2719 if(policyDataString == null){
2720 throw new IllegalArgumentException("The file path cannot be read");
2725 policyToRename.setPolicyName(newPolicy[1]);
2726 policyToRename.setPolicyData(policyDataString);
2727 policyToRename.setScope(newPolicy[0]);
2728 policyToRename.setModifiedBy(username);
2729 if(policyToRename.getConfigurationData() != null){
2730 //String configType = getPolicySubType(policyToRename.getConfigurationData().getConfigurationName());
2731 String configType = policyToRename.getConfigurationData().getConfigType();
2732 policyToRename.getConfigurationData().setConfigurationName(getConfigFile(newPolicy[1], newPolicy[0], configType));
2733 policyToRename.getConfigurationData().setModifiedBy(username);
2735 if(policyToRename.getActionBodyEntity() != null){
2736 String newActionName = newPolicy[0]+"."+removeFileExtension(newPolicy[1])+".json";
2737 policyToRename.getActionBodyEntity().setActionBodyName(newActionName);
2738 policyToRename.getActionBodyEntity().setModifiedBy(username);
2740 if(existingPolicyDeleted){
2741 for(Object o : groups){
2743 GroupEntity group = (GroupEntity)o;
2744 group.addPolicyToGroup(policyToRename);
2748 this.policyId = policyToRename.getPolicyId();
2749 this.newGroupId = oldPath;
2754 public GroupEntity getGroup(long groupKey){
2755 logger.debug("getGroup(int groupKey) as getGroup("+groupKey+") called");
2757 throw new IllegalArgumentException("groupKey must be at least 0");
2759 synchronized(emLock){
2760 checkBeforeOperationRun(true);
2761 //check if group exists
2762 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupKey=:groupKey");
2763 groupQuery.setParameter("groupKey", groupKey);
2764 List<?> groupQueryList;
2766 groupQueryList = groupQuery.getResultList();
2767 }catch(Exception e){
2768 //TODO:EELF Cleanup - Remove logger
2769 //logger.error("Caught Exception trying to get group with groupQuery.getResultList()",e);
2770 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group with groupQuery.getResultList()");
2771 throw new PersistenceException("Query failed trying to get group "+groupKey);
2773 if(groupQueryList.size() < 1){
2774 //TODO:EELF Cleanup - Remove logger
2775 //logger.error("Group does not exist with groupKey "+groupKey);
2776 PolicyLogger.error("Group does not exist with groupKey "+groupKey);
2777 throw new PersistenceException("Group does not exist with groupKey "+groupKey);
2778 } else if(groupQueryList.size() > 1){
2779 //TODO:EELF Cleanup - Remove logger
2780 //logger.error("Somehow, more than one group with the groupKey "+groupKey+" were found in the database");
2781 PolicyLogger.error("Somehow, more than one group with the groupKey "+groupKey+" were found in the database");
2782 throw new PersistenceException("Somehow, more than one group with the groupKey "+groupKey+" were found in the database");
2784 return (GroupEntity)groupQueryList.get(0);
2789 public GroupEntity getGroup(String groupId){
2790 logger.debug("getGroup(String groupId) as getGroup("+groupId+") called");
2791 if(isNullOrEmpty(groupId)){
2792 throw new IllegalArgumentException("groupId must not be null or empty");
2794 synchronized(emLock){
2795 checkBeforeOperationRun(true);
2796 //check if group exists
2797 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId");
2798 groupQuery.setParameter("groupId", groupId);
2799 List<?> groupQueryList;
2801 groupQueryList = groupQuery.getResultList();
2802 }catch(Exception e){
2803 //TODO:EELF Cleanup - Remove logger
2804 //logger.error("Caught Exception trying to get group with groupQuery.getResultList()",e);
2805 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group with groupQuery.getResultList()");
2806 throw new PersistenceException("Query failed trying to get group "+groupId);
2808 if(groupQueryList.size() < 1){
2809 //TODO:EELF Cleanup - Remove logger
2810 //logger.error("Group does not exist with id "+groupId);
2811 PolicyLogger.error("Group does not exist with id "+groupId);
2812 throw new PersistenceException("Group does not exist with id "+groupId);
2813 } else if(groupQueryList.size() > 1){
2814 //TODO:EELF Cleanup - Remove logger
2815 //logger.error("Somehow, more than one group with the id "+groupId+" were found in the database");
2816 PolicyLogger.error("Somehow, more than one group with the id "+groupId+" were found in the database");
2817 throw new PersistenceException("Somehow, more than one group with the id "+groupId+" were found in the database");
2819 return (GroupEntity)groupQueryList.get(0);
2823 public List<?> getPdpsInGroup(long groupKey){
2824 logger.debug("getPdpsInGroup(int groupKey) as getPdpsInGroup("+groupKey+") called");
2826 throw new IllegalArgumentException("groupId must not be < 0");
2828 synchronized(emLock){
2829 checkBeforeOperationRun(true);
2830 Query pdpsQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group");
2831 pdpsQuery.setParameter("group", getGroup(groupKey));
2832 return pdpsQuery.getResultList();
2836 public PdpEntity getPdp(long pdpKey){
2837 logger.debug("getPdp(int pdpKey) as getPdp("+pdpKey+") called");
2839 throw new IllegalArgumentException("pdpKey must be at least 0");
2841 synchronized(emLock){
2842 checkBeforeOperationRun(true);
2843 //check if group exists
2844 Query pdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpKey=:pdpKey");
2845 pdpQuery.setParameter("pdpKey", pdpKey);
2846 List<?> pdpQueryList;
2848 pdpQueryList = pdpQuery.getResultList();
2849 }catch(Exception e){
2850 //TODO:EELF Cleanup - Remove logger
2851 //logger.error("Caught Exception trying to get pdp with pdpQuery.getResultList()",e);
2852 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get pdp with pdpQuery.getResultList()");
2853 throw new PersistenceException("Query failed trying to get pdp "+pdpKey);
2855 if(pdpQueryList.size() < 1){
2856 //TODO:EELF Cleanup - Remove logger
2857 //logger.error("Pdp does not exist with pdpKey "+pdpKey);
2858 PolicyLogger.error("Pdp does not exist with pdpKey "+pdpKey);
2859 throw new PersistenceException("Pdp does not exist with pdpKey "+pdpKey);
2860 } else if(pdpQueryList.size() > 1){
2861 //TODO:EELF Cleanup - Remove logger
2862 //logger.error("Somehow, more than one pdp with the pdpKey "+pdpKey+" were found in the database");
2863 PolicyLogger.error("Somehow, more than one pdp with the pdpKey "+pdpKey+" were found in the database");
2864 throw new PersistenceException("Somehow, more than one pdp with the pdpKey "+pdpKey+" were found in the database");
2866 return (PdpEntity)pdpQueryList.get(0);
2870 //FIXME: maybe this should be boolean
2871 public void deletePolicy(String policyToDeletes){
2872 synchronized(emLock){
2873 // if(isTransactionOpen()){
2874 // logger.error("A transaction is already open which has not been committed");
2875 // throw new IllegalStateException("A transaction is already open which has not been committed");
2877 checkBeforeOperationRun();
2878 logger.debug("deletePolicy(String policyToDeletes) as deletePolicy("+policyToDeletes+") called");
2879 String[] scopeNameAndType = getScopeAndNameAndType(policyToDeletes);
2880 if(scopeNameAndType == null){
2881 throw new IllegalArgumentException("Could not parse file path");
2883 String realScope = scopeNameAndType[0];
2884 String realName = scopeNameAndType[1];
2885 // if(isTransactionOpen()){
2886 // throw new IllegalStateException("A transaction is already open which has not been committed");
2888 Query deletePolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName AND p.deleted=:deleted");
2889 deletePolicyQuery.setParameter("scope",realScope);
2890 deletePolicyQuery.setParameter("policyName", realName);
2891 deletePolicyQuery.setParameter("deleted", false);
2892 List<?> deletePolicyQueryList = deletePolicyQuery.getResultList();
2893 if(deletePolicyQueryList.size() < 1){
2894 logger.warn("The policy being deleted could not be found.");
2896 } else if(deletePolicyQueryList.size() > 1){
2897 //TODO:EELF Cleanup - Remove logger
2898 //logger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2899 PolicyLogger.error("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2900 throw new PersistenceException("Somehow, more than one policy with the same scope, name, and deleted status were found in the database");
2902 //em.getTransaction().begin();
2903 PolicyEntity policyToDelete = (PolicyEntity)deletePolicyQueryList.get(0);
2904 policyToDelete.setDeleted(true);
2905 if(policyToDelete.getConfigurationData() != null){
2906 ConfigurationDataEntity cde = em.find(ConfigurationDataEntity.class,policyToDelete.getConfigurationData().getConfigurationDataId());
2908 cde.setDeleted(true);
2911 if(policyToDelete.getActionBodyEntity() != null){
2912 ActionBodyEntity abe = em.find(ActionBodyEntity.class,policyToDelete.getActionBodyEntity().getActionBodyId());
2914 abe.setDeleted(true);
2919 this.policyId = policyToDelete.getPolicyId();
2928 public boolean isTransactionOpen() {
2929 logger.debug("isTransactionOpen() as isTransactionOpen() called");
2930 synchronized(emLock){
2931 return em.isOpen() && em.getTransaction().isActive();
2937 public void clonePolicy(String oldPolicyPath, String newPolicyPath, String username){
2938 String[] oldPolicyData = getScopeAndNameAndType(oldPolicyPath);
2939 String[] newPolicyData = getScopeAndNameAndType(newPolicyPath);
2940 if(oldPolicyData == null || newPolicyData == null){
2941 //TODO:EELF Cleanup - Remove logger
2942 //logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2943 //+oldPolicyPath+", "+newPolicyPath);
2944 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW+"Could not parse one or more of the path names: "
2945 +oldPolicyPath+", "+newPolicyPath);
2946 throw new IllegalArgumentException("Could not parse the oldPolicyPath or newPolicyPath");
2948 PolicyEntity oldPolicy;
2950 oldPolicy = getPolicy(oldPolicyData[1],oldPolicyData[0]);
2951 }catch(Exception e){
2952 //TODO:EELF Cleanup - Remove logger
2953 //logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Could not get policy record to clone: "
2954 //+oldPolicyData[1],e);
2955 PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "PolicyDBDao", "Could not get policy record to clone: "
2957 throw new PersistenceException("Could not get policy record to clone");
2959 ConfigurationDataEntity clonedConfig = null;
2960 if(oldPolicy.getConfigurationData() != null){
2961 clonedConfig = new ConfigurationDataEntity();
2962 em.persist(clonedConfig);
2963 clonedConfig.setConfigBody(oldPolicy.getConfigurationData().getConfigBody());
2964 clonedConfig.setConfigType(oldPolicy.getConfigurationData().getConfigType());
2965 clonedConfig.setCreatedBy(username);
2966 clonedConfig.setConfigurationName(getConfigFile(newPolicyData[1], newPolicyData[0], oldPolicy.getConfigurationData().getConfigType()));
2967 clonedConfig.setDescription(oldPolicy.getConfigurationData().getDescription());
2968 clonedConfig.setModifiedBy(username);
2971 ActionBodyEntity clonedAction = null;
2972 if(oldPolicy.getActionBodyEntity() != null){
2973 clonedAction = new ActionBodyEntity();
2974 em.persist(clonedAction);
2975 clonedAction.setActionBody(oldPolicy.getActionBodyEntity().getActionBody());
2976 clonedAction.setActionBodyName(newPolicyData[0]+"."+newPolicyData[1]+".json");
2977 clonedAction.setCreatedBy(username);
2978 clonedAction.setModifiedBy(username);
2986 public void createPolicy(String filePath, String username) {
2987 logger.debug("createPolicy(String filePath, String username) as createPolicy("+filePath+","+username+") called");
2988 //get just the scope and file name
2989 //its actually scope, name, and type now
2990 String[] scopeAndName = getScopeAndNameAndType(filePath);
2991 if(scopeAndName == null){
2992 throw new IllegalArgumentException("The file path could not be parsed");
2994 PolicyRestAdapter policy = new PolicyRestAdapter();
2996 policy.setPolicyType(scopeAndName[2]);
2997 policy.setPolicyDescription("");
2999 String policyName = scopeAndName[1];
3001 policyName = stripPolicyName(policyName);
3002 }catch(IllegalArgumentException e){
3003 if(scopeAndName[2].equals("Config")){
3004 //TODO:EELF Cleanup - Remove logger
3005 //logger.error(e.getMessage());
3006 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Exception calling stripPolicyName with policy name: "+policyName);
3007 throw new IllegalArgumentException(e.getMessage(),e);
3009 logger.warn(e.getMessage());
3012 policy.setPolicyName(policyName);
3013 String policyDataString = null;
3014 InputStream fileContentStream = null;
3016 fileContentStream = new FileInputStream(filePath);
3017 policyDataString = IOUtils.toString(fileContentStream);
3018 } catch (FileNotFoundException e) {
3019 //TODO:EELF Cleanup - Remove logger
3020 //logger.error("Caught FileNotFoundException on new FileInputStream("+filePath+")",e);
3021 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught FileNotFoundException on new FileInputStream("+filePath+")");
3022 throw new IllegalArgumentException("The file path does not exist");
3023 } catch(IOException e2){
3024 //TODO:EELF Cleanup - Remove logger
3025 //logger.error("Caught IOException on newIOUtils.toString("+fileContentStream+")",e2);
3026 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, "PolicyDBDao", "Caught IOException on newIOUtils.toString("+fileContentStream+")");
3027 throw new IllegalArgumentException("The file path cannot be read");
3029 IOUtils.closeQuietly(fileContentStream);
3031 if(policyDataString == null){
3032 throw new IllegalArgumentException("The file path cannot be read");
3035 String policyDescription = getElementFromXMLString("/Description", policyDataString);
3036 if(policyDescription != null){
3037 policy.setPolicyDescription(policyDescription);
3039 } catch(Exception e){
3040 logger.warn("Could not get description from the policy file");
3042 if(scopeAndName[2].equals("Config")){
3043 //this method is not used for config, since there is no way to get config info (could be modified to)
3046 configPath = evaluateXPath("/Policy/Rule/AdviceExpressions/AdviceExpression[contains(@AdviceId,'ID')]/AttributeAssignmentExpression[@AttributeId='URLID']/AttributeValue/text()", policyDataString);
3047 if(configPath == null){
3048 throw new NullPointerException("configPath is null");
3050 } catch(Exception e){
3051 //TODO:EELF Cleanup - Remove logger
3052 //logger.error("Could not get config file path from policy file",e);
3053 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get config file path from policy file");
3054 throw new IllegalArgumentException("Could not get config file path from policy file");
3056 configPath = processConfigPath(configPath);
3057 logger.debug("The location of our config file is: "+configPath);
3058 policy.setConfigType(getPolicySubType(configPath));
3059 logger.debug("Config type is: "+policy.getConfigType());
3061 String configDataString = readConfigFile(configPath);
3062 policy.setConfigBodyData(configDataString);
3064 createPolicy(policy,username,scopeAndName[0],scopeAndName[1],policyDataString);
3066 private String processConfigPath(String configPath){
3067 String webappsPath = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS);
3068 if(webappsPath == null){
3069 logger.error("Webapps property does not exist");
3070 throw new IllegalArgumentException("Webapps property does not exist");
3072 configPath = configPath.replace("$URL", webappsPath);
3073 //make sure the correct slashes are in
3075 configPath = Paths.get(configPath).toString();
3076 } catch(InvalidPathException e){
3077 logger.error("Invalid config path: "+configPath);
3078 throw new IllegalArgumentException("Invalid config path: "+configPath);
3082 private String readConfigFile(String configPath){
3083 String configDataString = null;
3084 InputStream configContentStream = null;
3086 configContentStream = new FileInputStream(configPath);
3087 configDataString = IOUtils.toString(configContentStream);
3088 } catch (FileNotFoundException e) {
3089 logger.error("Caught FileNotFoundException on new FileInputStream("+configPath+")",e);
3090 throw new IllegalArgumentException("The config file path does not exist");
3091 } catch(IOException e2){
3092 logger.error("Caught IOException on newIOUtils.toString("+configContentStream+")",e2);
3093 throw new IllegalArgumentException("The config file path cannot be read");
3095 IOUtils.closeQuietly(configContentStream);
3097 if(configDataString == null){
3098 throw new IllegalArgumentException("The config file path cannot be read");
3100 return configDataString;
3104 public void createPolicy(Policy policy, String username){
3105 logger.debug("createPolicy(PolicyRestAdapter policy, String username) as createPolicy("+policy+","+username+") called");
3106 String policyScope = computeScope(policy.policyAdapter.getParentPath(),policy.policyAdapter.getUserGitPath());
3108 //Does not need to be XACMLPolicyWriterWithPapNotify since it is already in the PAP
3109 //and this transaction is intercepted up stream.
3110 InputStream policyXmlStream = XACMLPolicyWriter.getXmlAsInputStream((PolicyType)policy.getCorrectPolicyDataObject());
3111 String policyDataString;
3113 policyDataString = IOUtils.toString(policyXmlStream);
3114 } catch (IOException e) {
3115 policyDataString = "could not read";
3116 //TODO:EELF Cleanup - Remove logger
3117 //logger.error("Caught IOException on IOUtils.toString("+policyXmlStream+")",e);
3118 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught IOException on IOUtils.toString("+policyXmlStream+")");
3119 throw new IllegalArgumentException("Cannot parse the policy xml from the PolicyRestAdapter.");
3121 IOUtils.closeQuietly(policyXmlStream);
3122 String configPath = "";
3123 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Config")) {
3124 configPath = evaluateXPath("/Policy/Rule/AdviceExpressions/AdviceExpression[contains(@AdviceId,'ID')]/AttributeAssignmentExpression[@AttributeId='URLID']/AttributeValue/text()", policyDataString);
3125 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")) {
3126 configPath = evaluateXPath("/Policy/Rule/ObligationExpressions/ObligationExpression[contains(@ObligationId, " +policy.policyAdapter.getActionAttribute()+ ")]/AttributeAssignmentExpression[@AttributeId='body']/AttributeValue/text()", policyDataString);
3129 String prefix = null;
3130 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Config")) {
3132 prefix = configPath.substring(configPath.indexOf(policyScope+".")+policyScope.concat(".").length(), configPath.indexOf(policy.policyAdapter.getPolicyName()));
3133 if(isNullOrEmpty(policy.policyAdapter.getConfigBodyData())){
3134 String configData = "";
3136 String newConfigPath = configPath;
3138 newConfigPath = processConfigPath(newConfigPath);
3139 }catch(Exception e2){
3140 logger.error("Could not process config path: "+newConfigPath,e2);
3142 configData = readConfigFile(newConfigPath);
3143 }catch(Exception e){
3144 logger.error("Could not read config body data for "+configPath,e);
3146 policy.policyAdapter.setConfigBodyData(configData);
3149 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")) {
3153 } else if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Decision")) {
3155 prefix = "Decision_";
3158 if(!(policy.policyAdapter.getData() instanceof PolicyType)){
3159 //TODO:EELF Cleanup - Remove logger
3160 //logger.error("The data field is not an instance of PolicyType");
3161 PolicyLogger.error("The data field is not an instance of PolicyType");
3162 throw new IllegalArgumentException("The data field is not an instance of PolicyType");
3164 String finalName = prefix+policy.policyAdapter.getPolicyName()+"."+((PolicyType)policy.policyAdapter.getData()).getVersion()+".xml";
3165 if(policy.policyAdapter.getConfigType() == null || policy.policyAdapter.getConfigType().equals("")){
3166 //we need to make it
3167 //get the config file extension
3169 if (configPath != null) {
3170 if (!configPath.equalsIgnoreCase("")) {
3171 ext = configPath.substring(configPath.lastIndexOf('.'), configPath.length());;
3175 if(ext.contains("txt")){
3176 policy.policyAdapter.setConfigType(OTHER_CONFIG);
3177 } else if(ext.contains("json")){
3178 policy.policyAdapter.setConfigType(JSON_CONFIG);
3179 } else if(ext.contains("xml")){
3180 policy.policyAdapter.setConfigType(XML_CONFIG);
3181 } else if(ext.contains("properties")){
3182 policy.policyAdapter.setConfigType(PROPERTIES_CONFIG);
3184 if (policy.policyAdapter.getPolicyType().equalsIgnoreCase("Action")){
3185 policy.policyAdapter.setConfigType(JSON_CONFIG);
3189 createPolicy(policy.policyAdapter, username, policyScope,finalName,policyDataString);
3194 public void close(){
3195 synchronized(emLock){
3197 if(em.getTransaction().isActive()){
3198 em.getTransaction().rollback();
3202 if(transactionTimer instanceof Thread){
3203 transactionTimer.interrupt();
3211 public void createGroup(String groupId, String groupName, String groupDescription, String username) {
3212 logger.debug("deletePolicy(String policyToDeletes) as createGroup("+groupId+", "+groupName+", "+groupDescription+") called");
3213 // if(isTransactionOpen()){
3214 // logger.error("A transaction is already open which has not been committed");
3215 // throw new IllegalStateException("A transaction is already open which has not been committed");
3218 if(isNullOrEmpty(groupId, groupName, username)){
3219 throw new IllegalArgumentException("groupId, groupName, and username must not be null or empty");
3221 if(!(groupDescription instanceof String)){
3222 groupDescription = "";
3225 synchronized(emLock){
3226 checkBeforeOperationRun();
3227 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3228 checkGroupQuery.setParameter("groupId", groupId);
3229 checkGroupQuery.setParameter("deleted", false);
3230 List<?> checkGroupQueryList;
3232 checkGroupQueryList = checkGroupQuery.getResultList();
3233 } catch(Exception e){
3234 //TODO:EELF Cleanup - Remove logger
3235 //logger.error("Caught Exception on checkGroupQuery.getResultList()",e);
3236 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on checkGroupQuery.getResultList()");
3237 throw new PersistenceException("Query failed trying to check for existing group");
3239 if(checkGroupQueryList.size() > 0){
3240 //TODO:EELF Cleanup - Remove logger
3241 //logger.error("The group being added already exists with id "+groupId);
3242 PolicyLogger.error("The group being added already exists with id "+groupId);
3243 throw new PersistenceException("The group being added already exists with id "+groupId);
3245 //em.getTransaction().begin();
3246 GroupEntity newGroup = new GroupEntity();
3247 em.persist(newGroup);
3248 newGroup.setCreatedBy(username);
3249 newGroup.setModifiedBy(username);
3250 newGroup.setGroupName(groupName);
3251 newGroup.setGroupId(groupId);
3252 newGroup.setDescription(groupDescription);
3255 this.groupId = newGroup.getGroupKey();
3260 public void updateGroup(EcompPDPGroup group, String username){
3261 logger.debug("updateGroup(PDPGroup group) as updateGroup("+group+","+username+") called");
3262 // if(isTransactionOpen()){
3263 // logger.error("A transaction is already open which has not been committed");
3264 // throw new IllegalStateException("A transaction is already open which has not been committed");
3269 throw new IllegalArgumentException("PDPGroup group must not be null");
3271 if(isNullOrEmpty(group.getId(), username)){
3272 throw new IllegalArgumentException("group.getId() and username must not be null or empty");
3275 synchronized(emLock){
3276 checkBeforeOperationRun();
3277 Query getGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3278 getGroupQuery.setParameter("groupId", group.getId());
3279 getGroupQuery.setParameter("deleted", false);
3280 List<?> getGroupQueryList;
3282 getGroupQueryList = getGroupQuery.getResultList();
3283 } catch(Exception e){
3284 //TODO:EELF Cleanup - Remove logger
3285 //logger.error("Caught Exception on getGroupQuery.getResultList()",e);
3286 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getGroupQuery.getResultList()");
3287 throw new PersistenceException("Query failed trying to get group "+group.getId()+" for editing");
3289 if(getGroupQueryList.size() < 1){
3290 //TODO:EELF Cleanup - Remove logger
3291 //logger.error("The group cannot be found to update with id "+group.getId());
3292 PolicyLogger.error("The group cannot be found to update with id "+group.getId());
3293 throw new PersistenceException("The group cannot be found to update with id "+group.getId());
3294 } else if(getGroupQueryList.size() > 1){
3295 //TODO:EELF Cleanup - Remove logger
3296 //logger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3297 PolicyLogger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3298 throw new PersistenceException("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3300 //em.getTransaction().begin();
3301 GroupEntity groupToUpdate = (GroupEntity)getGroupQueryList.get(0);
3302 if(!stringEquals(groupToUpdate.getModifiedBy(), username)){
3303 groupToUpdate.setModifiedBy(username);
3305 if(group.getDescription() != null && !stringEquals(group.getDescription(),groupToUpdate.getDescription())){
3306 groupToUpdate.setDescription(group.getDescription());
3308 //let's find out what policies have been deleted
3309 StdPDPGroup oldGroup = null;
3311 oldGroup = (StdPDPGroup) papEngine.getGroup(group.getId());
3312 } catch (PAPException e1) {
3313 //TODO:EELF Cleanup - Remove logger
3314 //logger.error("We cannot get the group from the papEngine to delete policies",e1);
3315 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "We cannot get the group from the papEngine to delete policies");
3317 if(oldGroup == null){
3318 //TODO:EELF Cleanup - Remove logger
3319 //logger.error("We cannot get the group from the papEngine to delete policies");
3320 PolicyLogger.error("We cannot get the group from the papEngine to delete policies");
3323 Set<String> newPolicySet = new HashSet<String>(group.getPolicies().size());
3324 //a multiple of n runtime is faster than n^2, so I am using a hashset to do the comparison
3325 for(PDPPolicy pol: group.getPolicies()){
3326 newPolicySet.add(pol.getId());
3328 for(PDPPolicy pol : oldGroup.getPolicies()){
3329 //should be fast since getPolicies uses a HashSet in StdPDPGroup
3330 if(!newPolicySet.contains(pol.getId())){
3331 String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(pol.getId());
3332 PolicyEntity policyToDelete;
3334 policyToDelete = getPolicy(scopeAndName[0],scopeAndName[1]);
3335 }catch(Exception e){
3336 //TODO:EELF Cleanup - Remove logger
3337 //logger.error("Could not get policy to remove: "+pol.getId(),e);
3338 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get policy to remove: "+pol.getId());
3339 throw new PersistenceException("Could not get policy to remove: "+pol.getId());
3341 groupToUpdate.getPolicies().remove(policyToDelete);
3346 if(group.getName() != null && !stringEquals(group.getName(),groupToUpdate.getgroupName())){
3347 //we need to check if the new id exists in the database
3348 String newGroupId = createNewPDPGroupId(group.getName());
3349 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3350 checkGroupQuery.setParameter("groupId", newGroupId);
3351 checkGroupQuery.setParameter("deleted", false);
3352 List<?> checkGroupQueryList;
3354 checkGroupQueryList = checkGroupQuery.getResultList();
3355 } catch(Exception e){
3356 //TODO:EELF Cleanup - Remove logger
3357 //logger.error("Caught Exception on checkGroupQuery.getResultList()",e);
3358 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on checkGroupQuery.getResultList()");
3359 throw new PersistenceException("Query failed trying to check for existing group");
3361 if(checkGroupQueryList.size() != 0){
3362 //TODO:EELF Cleanup - Remove logger
3363 //logger.error("The new group name already exists, group id "+newGroupId);
3364 PolicyLogger.error("The new group name already exists, group id "+newGroupId);
3365 throw new PersistenceException("The new group name already exists, group id "+newGroupId);
3367 groupToUpdate.setGroupId(newGroupId);
3368 groupToUpdate.setGroupName(group.getName());
3369 this.newGroupId = group.getId();
3373 this.groupId = groupToUpdate.getGroupKey();
3378 public void addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) {
3379 logger.debug("addPdpToGroup(String pdpID, String groupID, String pdpName, String pdpDescription, int pdpJmxPort, String username) as addPdpToGroup("+pdpID+", "+groupID+", "+pdpName+", "+pdpDescription+", "+pdpJmxPort+", "+username+") called");
3380 if(isNullOrEmpty(pdpID, groupID,pdpName,username)){
3381 throw new IllegalArgumentException("pdpID, groupID, pdpName, and username must not be null or empty");
3383 if(!(pdpDescription instanceof String)){
3384 pdpDescription = "";
3386 // if(isTransactionOpen()){
3387 // logger.error("A transaction is already open which has not been committed");
3388 // throw new IllegalStateException("A transaction is already open which has not been committed");
3391 synchronized(emLock){
3392 checkBeforeOperationRun();
3393 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3394 checkGroupQuery.setParameter("groupId", groupID);
3395 checkGroupQuery.setParameter("deleted", false);
3396 List<?> checkGroupQueryList;
3398 checkGroupQueryList = checkGroupQuery.getResultList();
3399 } catch(Exception e){
3400 //TODO:EELF Cleanup - Remove logger
3401 //logger.error("Caught Exception trying to check for existing group on checkGroupQuery.getResultList()",e);
3402 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check for existing group on checkGroupQuery.getResultList()");
3403 throw new PersistenceException("Query failed trying to check for existing group");
3405 if(checkGroupQueryList.size() != 1){
3406 //TODO:EELF Cleanup - Remove logger
3407 //logger.error("The group does not exist");
3408 PolicyLogger.error("The group does not exist");
3409 throw new PersistenceException("The group does not exist");
3411 Query checkDuplicateQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3412 checkDuplicateQuery.setParameter("pdpId", pdpID);
3413 checkDuplicateQuery.setParameter("deleted", false);
3414 List<?> checkDuplicateList;
3416 checkDuplicateList = checkDuplicateQuery.getResultList();
3417 } catch(Exception e){
3418 //TODO:EELF Cleanup - Remove logger
3419 //logger.error("Caught Exception trying to check for duplicate PDP "+pdpID+" on checkDuplicateQuery.getResultList()",e);
3420 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check for duplicate PDP "+pdpID+" on checkDuplicateQuery.getResultList()");
3421 throw new PersistenceException("Query failed trying to check for duplicate PDP "+pdpID);
3424 if(checkDuplicateList.size() > 0){
3425 logger.warn("PDP already exists with id "+pdpID);
3426 newPdp = (PdpEntity)checkDuplicateList.get(0);
3428 newPdp = new PdpEntity();
3432 newPdp.setCreatedBy(username);
3433 newPdp.setDeleted(false);
3434 newPdp.setDescription(pdpDescription);
3435 newPdp.setGroup((GroupEntity)checkGroupQueryList.get(0));
3436 newPdp.setJmxPort(pdpJmxPort);
3437 newPdp.setModifiedBy(username);
3438 newPdp.setPdpId(pdpID);
3439 newPdp.setPdpName(pdpName);
3442 this.pdpId = newPdp.getPdpKey();
3449 public void updatePdp(EcompPDP pdp, String username){
3450 logger.debug("updatePdp(PDP pdp, String username) as updatePdp("+pdp+","+username+") called");
3451 // if(isTransactionOpen()){
3452 // logger.error("A transaction is already open which has not been committed");
3453 // throw new IllegalStateException("A transaction is already open which has not been committed");
3457 throw new IllegalArgumentException("PDP pdp must not be null");
3459 if(isNullOrEmpty(pdp.getId(),username)){
3460 throw new IllegalArgumentException("pdp.getId() and username must not be null or empty");
3463 synchronized(emLock){
3464 checkBeforeOperationRun();
3465 Query getPdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3466 getPdpQuery.setParameter("pdpId", pdp.getId());
3467 getPdpQuery.setParameter("deleted", false);
3468 List<?> getPdpQueryList;
3470 getPdpQueryList = getPdpQuery.getResultList();
3471 } catch(Exception e){
3472 //TODO:EELF Cleanup - Remove logger
3473 //logger.error("Caught Exception on getPdpQuery.getResultList()",e);
3474 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getPdpQuery.getResultList()");
3475 throw new PersistenceException("Query failed trying to get PDP "+pdp.getId());
3477 if(getPdpQueryList.size() < 1){
3478 //TODO:EELF Cleanup - Remove logger
3479 //logger.error("The pdp cannot be found to update with id "+pdp.getId());
3480 PolicyLogger.error("The pdp cannot be found to update with id "+pdp.getId());
3481 throw new PersistenceException("The pdp cannot be found to update with id "+pdp.getId());
3482 } else if(getPdpQueryList.size() > 1){
3483 //TODO:EELF Cleanup - Remove logger
3484 //logger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3485 PolicyLogger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3486 throw new PersistenceException("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3488 //em.getTransaction().begin();
3489 PdpEntity pdpToUpdate = (PdpEntity)getPdpQueryList.get(0);
3490 if(!stringEquals(pdpToUpdate.getModifiedBy(), username)){
3491 pdpToUpdate.setModifiedBy(username);
3493 if(pdp.getDescription() != null && !stringEquals(pdp.getDescription(),pdpToUpdate.getDescription())){
3494 pdpToUpdate.setDescription(pdp.getDescription());
3496 if(pdp.getName() != null && !stringEquals(pdp.getName(),pdpToUpdate.getPdpName())){
3497 pdpToUpdate.setPdpName(pdp.getName());
3499 if(pdp.getJmxPort() != null && !pdp.getJmxPort().equals(pdpToUpdate.getJmxPort())){
3500 pdpToUpdate.setJmxPort(pdp.getJmxPort());
3504 this.pdpId = pdpToUpdate.getPdpKey();
3509 public void movePdp(EcompPDP pdp, EcompPDPGroup group, String username){
3510 logger.debug("movePdp(PDP pdp, PDPGroup group, String username) as movePdp("+pdp+","+group+","+username+") called");
3511 // if(isTransactionOpen()){
3512 // logger.error("A transaction is already open which has not been committed");
3513 // throw new IllegalStateException("A transaction is already open which has not been committed");
3515 if(pdp == null || group == null){
3516 throw new IllegalArgumentException("PDP pdp and PDPGroup group must not be null");
3518 if(isNullOrEmpty(username,pdp.getId(),group.getId())){
3519 throw new IllegalArgumentException("pdp.getId(), group.getId(), and username must not be null or empty");
3522 synchronized(emLock){
3523 checkBeforeOperationRun();
3524 //check if pdp exists
3525 Query getPdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3526 getPdpQuery.setParameter("pdpId", pdp.getId());
3527 getPdpQuery.setParameter("deleted", false);
3528 List<?> getPdpQueryList;
3530 getPdpQueryList = getPdpQuery.getResultList();
3531 } catch(Exception e){
3532 //TODO:EELF Cleanup - Remove logger
3533 //logger.error("Caught Exception on getPdpQuery.getResultList()",e);
3534 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getPdpQuery.getResultList()");
3535 throw new PersistenceException("Query failed trying to get pdp to move with id "+pdp.getId());
3537 if(getPdpQueryList.size() < 1){
3538 //TODO:EELF Cleanup - Remove logger
3539 //logger.error("The pdp cannot be found to move with id "+pdp.getId());
3540 PolicyLogger.error("The pdp cannot be found to move with id "+pdp.getId());
3541 throw new PersistenceException("The pdp cannot be found to move with id "+pdp.getId());
3542 } else if(getPdpQueryList.size() > 1){
3543 //TODO:EELF Cleanup - Remove logger
3544 //logger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3545 PolicyLogger.error("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3546 throw new PersistenceException("Somehow, more than one pdp with the same id "+pdp.getId()+" and deleted status were found in the database");
3549 //check if new group exists
3550 Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3551 checkGroupQuery.setParameter("groupId", group.getId());
3552 checkGroupQuery.setParameter("deleted", false);
3553 List<?> checkGroupQueryList;
3555 checkGroupQueryList = checkGroupQuery.getResultList();
3556 } catch(Exception e){
3557 //TODO:EELF Cleanup - Remove logger
3558 //logger.error("Caught Exception trying to get group on checkGroupQuery.getResultList()",e);
3559 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get group on checkGroupQuery.getResultList()");
3560 throw new PersistenceException("Query failed trying to get new group "+group.getId());
3562 if(checkGroupQueryList.size() != 1){
3563 //TODO:EELF Cleanup - Remove logger
3564 //logger.error("The group "+group.getId()+" does not exist");
3565 PolicyLogger.error("The group "+group.getId()+" does not exist");
3566 throw new PersistenceException("The group "+group.getId()+" does not exist");
3568 GroupEntity groupToMoveInto = (GroupEntity)checkGroupQueryList.get(0);
3570 //em.getTransaction().begin();
3571 PdpEntity pdpToUpdate = (PdpEntity)getPdpQueryList.get(0);
3572 pdpToUpdate.setGroup(groupToMoveInto);
3573 if(!stringEquals(pdpToUpdate.getModifiedBy(), username)){
3574 pdpToUpdate.setModifiedBy(username);
3578 this.pdpId = pdpToUpdate.getPdpKey();
3583 public void changeDefaultGroup(EcompPDPGroup group, String username){
3584 logger.debug("changeDefaultGroup(PDPGroup group, String username) as changeDefaultGroup("+group+","+username+") called");
3585 // if(isTransactionOpen()){
3586 // logger.error("A transaction is already open which has not been committed");
3587 // throw new IllegalStateException("A transaction is already open which has not been committed");
3591 throw new IllegalArgumentException("PDPGroup group must not be null");
3593 if(isNullOrEmpty(group.getId(),username)){
3594 throw new IllegalArgumentException("group.getId() and username must not be null or empty");
3597 synchronized(emLock){
3598 checkBeforeOperationRun();
3599 Query getGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3600 getGroupQuery.setParameter("groupId", group.getId());
3601 getGroupQuery.setParameter("deleted", false);
3602 List<?> getGroupQueryList;
3604 getGroupQueryList = getGroupQuery.getResultList();
3605 } catch(Exception e){
3606 //TODO:EELF Cleanup - Remove logger
3607 //logger.error("Caught Exception on getGroupQuery.getResultList()",e);
3608 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getGroupQuery.getResultList()");
3609 throw new PersistenceException("Query failed trying to get group "+group.getId());
3611 if(getGroupQueryList.size() < 1){
3612 //TODO:EELF Cleanup - Remove logger
3613 //logger.error("The group cannot be found to set default with id "+group.getId());
3614 PolicyLogger.error("The group cannot be found to set default with id "+group.getId());
3615 throw new PersistenceException("The group cannot be found to set default with id "+group.getId());
3616 } else if(getGroupQueryList.size() > 1){
3617 //TODO:EELF Cleanup - Remove logger
3618 //logger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3619 PolicyLogger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3620 throw new PersistenceException("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
3622 //em.getTransaction().begin();
3623 GroupEntity newDefaultGroup = (GroupEntity)getGroupQueryList.get(0);
3624 newDefaultGroup.setDefaultGroup(true);
3625 if(!stringEquals(newDefaultGroup.getModifiedBy(), username)){
3626 newDefaultGroup.setModifiedBy(username);
3630 this.groupId = newDefaultGroup.getGroupKey();
3631 Query setAllGroupsNotDefault = em.createQuery("UPDATE GroupEntity g SET g.defaultGroup=:defaultGroup WHERE g.deleted=:deleted AND g.groupKey<>:groupKey");
3632 //not going to set modified by for all groups
3633 setAllGroupsNotDefault.setParameter("defaultGroup", false);
3634 setAllGroupsNotDefault.setParameter("deleted", false);
3635 setAllGroupsNotDefault.setParameter("groupKey", newDefaultGroup.getGroupKey());
3637 logger.info("set " + setAllGroupsNotDefault.executeUpdate() + " groups as not default");
3638 } catch(Exception e){
3639 //TODO:EELF Cleanup - Remove logger
3640 //logger.error("Caught Exception on setAllGroupsNotDefault.executeUpdate()",e);
3641 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on setAllGroupsNotDefault.executeUpdate()");
3642 throw new PersistenceException("Could not set all other groups default to false");
3651 public void deleteGroup(EcompPDPGroup group, EcompPDPGroup moveToGroup, String username) throws PAPException {
3652 logger.debug("deleteGroup(PDPGroup group, PDPGroup moveToGroup, String username) as deleteGroup("+group+", "+moveToGroup+","+username+") called");
3653 // if(isTransactionOpen()){
3654 // logger.error("A transaction is already open which has not been committed");
3655 // throw new IllegalStateException("A transaction is already open which has not been committed");
3658 throw new IllegalArgumentException("PDPGroup group cannot be null");
3660 if(isNullOrEmpty(username,group.getId())){
3661 throw new IllegalArgumentException("group.getId() and and username must not be null or empty");
3664 if(group.isDefaultGroup()){
3665 //TODO:EELF Cleanup - Remove logger
3666 //logger.error("The default group "+group.getId()+" was attempted to be deleted. It cannot be.");
3667 PolicyLogger.error("The default group "+group.getId()+" was attempted to be deleted. It cannot be.");
3668 throw new PAPException("You cannot delete the default group.");
3670 synchronized(emLock){
3671 checkBeforeOperationRun();
3672 Query deleteGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3673 deleteGroupQuery.setParameter("groupId", group.getId());
3674 deleteGroupQuery.setParameter("deleted", false);
3675 List<?> deleteGroupQueryList;
3677 deleteGroupQueryList = deleteGroupQuery.getResultList();
3678 } catch(Exception e){
3679 //TODO:EELF Cleanup - Remove logger
3680 //logger.error("Caught Exception trying to check if group exists deleteGroupQuery.getResultList()",e);
3681 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists deleteGroupQuery.getResultList()");
3682 throw new PersistenceException("Query failed trying to check if group exists");
3684 if(deleteGroupQueryList.size() < 1){
3685 logger.warn("The group could not be found with id " + group.getId());
3687 } else if(deleteGroupQueryList.size() > 1){
3688 //TODO:EELF Cleanup - Remove logger
3689 //logger.error("Somehow, more than one group with the id "+group.getId()+" were found in the database that are not deleted");
3690 PolicyLogger.error("Somehow, more than one group with the id "+group.getId()+" were found in the database that are not deleted");
3691 throw new PersistenceException("Somehow, more than one group with the id "+group.getId()+" were found in the database that are not deleted");
3694 Query pdpsInGroupQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.groupEntity=:group and p.deleted=:deleted");
3695 pdpsInGroupQuery.setParameter("group", ((GroupEntity)deleteGroupQueryList.get(0)));
3696 pdpsInGroupQuery.setParameter("deleted", false);
3697 List<?> pdpsInGroupList;
3699 pdpsInGroupList = pdpsInGroupQuery.getResultList();
3700 } catch(Exception e){
3701 //TODO:EELF Cleanup - Remove logger
3702 //logger.error("Caught Exception trying to get PDPs in group on pdpsInGroupQuery.getResultList()",e);
3703 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to get PDPs in group on pdpsInGroupQuery.getResultList()");
3704 throw new PersistenceException("Query failed trying to get PDPs in group");
3706 //em.getTransaction().begin();
3707 if(pdpsInGroupList.size() > 0){
3708 if(moveToGroup != null){
3709 Query checkMoveToGroupQuery = em.createQuery("SELECT o FROM GroupEntity o WHERE o.groupId=:groupId AND o.deleted=:deleted");
3710 checkMoveToGroupQuery.setParameter("groupId", moveToGroup.getId());
3711 checkMoveToGroupQuery.setParameter("deleted", false);
3712 List<?> checkMoveToGroupList;
3714 checkMoveToGroupList = checkMoveToGroupQuery.getResultList();
3715 } catch(Exception e){
3716 //TODO:EELF Cleanup - Remove logger
3717 //logger.error("Caught Exception trying to check if group exists checkMoveToGroupQuery.getResultList()",e);
3718 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists checkMoveToGroupQuery.getResultList()");
3719 throw new PersistenceException("Query failed trying to check if group exists");
3721 if(checkMoveToGroupList.size() < 1){
3722 //TODO:EELF Cleanup - Remove logger
3723 //logger.error("The group could not be found with id " + moveToGroup.getId());
3724 PolicyLogger.error("The group could not be found with id " + moveToGroup.getId());
3725 throw new PersistenceException("The group could not be found with id " + moveToGroup.getId());
3726 } else if(checkMoveToGroupList.size() > 1){
3727 //TODO:EELF Cleanup - Remove logger
3728 //logger.error("Somehow, more than one group with the id "+moveToGroup.getId()+" were found in the database that are not deleted");
3729 PolicyLogger.error("Somehow, more than one group with the id "+moveToGroup.getId()+" were found in the database that are not deleted");
3730 throw new PersistenceException("Somehow, more than one group with the id "+moveToGroup.getId()+" were found in the database that are not deleted");
3732 GroupEntity newGroup = (GroupEntity)checkMoveToGroupList.get(0);
3733 for(Object pdpObject : pdpsInGroupList){
3734 PdpEntity pdp = (PdpEntity)pdpObject;
3735 pdp.setGroup(newGroup);
3736 if(!stringEquals(pdp.getModifiedBy(),username)){
3737 pdp.setModifiedBy(username);
3742 this.newGroupId = newGroup.getGroupId();
3743 } catch(PersistenceException e){
3744 //TODO:EELF Cleanup - Remove logger
3745 //logger.error("Caught PersistenceException trying to set pdp group to null on em.flush()",e);
3746 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught PersistenceException trying to set pdp group to null on em.flush()");
3747 throw new PersistenceException("Query failed trying to set pdp group to ");
3752 //TODO:EELF Cleanup - Remove logger
3753 //logger.error("Group "+group.getId()+" is trying to be delted with PDPs. No group was provided to move them to");
3754 PolicyLogger.error("Group "+group.getId()+" is trying to be delted with PDPs. No group was provided to move them to");
3755 throw new PAPException("Group has PDPs. Must provide a group for them to move to");
3761 GroupEntity groupToDelete = (GroupEntity)deleteGroupQueryList.get(0);
3762 groupToDelete.setDeleted(true);
3763 if(!stringEquals(groupToDelete.getModifiedBy(), username)){
3764 groupToDelete.setModifiedBy(username);
3770 this.groupId = groupToDelete.getGroupKey();
3772 //} catch(PersistenceException pe){
3773 //logger.error("Database error while marking policy or config as deleted");
3774 //throw new PersistenceException("Database error while marking policy or config as deleted");
3780 public void addPolicyToGroup(String groupID, String policyID, String username) {
3781 logger.debug("addPolicyToGroup(String groupID, String policyID, String username) as addPolicyToGroup("+groupID+", "+policyID+","+username+") called");
3782 // if(isTransactionOpen()){
3783 // logger.error("A transaction is already open which has not been committed");
3784 // throw new IllegalStateException("A transaction is already open which has not been committed");
3786 if(isNullOrEmpty(groupID, policyID, username)){
3787 throw new IllegalArgumentException("groupID, policyID, and username must not be null or empty");
3789 synchronized(emLock){
3790 checkBeforeOperationRun();
3791 //check if group exists
3792 Query groupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
3793 groupQuery.setParameter("groupId", groupID);
3794 groupQuery.setParameter("deleted", false);
3795 List<?> groupQueryList;
3797 groupQueryList = groupQuery.getResultList();
3798 }catch(Exception e){
3799 //TODO:EELF Cleanup - Remove logger
3800 //logger.error("Caught Exception trying to check if group exists groupQuery.getResultList()",e);
3801 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if group exists groupQuery.getResultList()");
3802 throw new PersistenceException("Query failed trying to check if group "+groupID+" exists");
3804 if(groupQueryList.size() < 1){
3805 //TODO:EELF Cleanup - Remove logger
3806 //logger.error("Group policy is being added to does not exist with id "+groupID);
3807 PolicyLogger.error("Group policy is being added to does not exist with id "+groupID);
3808 throw new PersistenceException("Group policy is being added to does not exist with id "+groupID);
3809 } else if(groupQueryList.size() > 1){
3810 //TODO:EELF Cleanup - Remove logger
3811 //logger.error("Somehow, more than one group with the id "+groupID+" were found in the database that are not deleted");
3812 PolicyLogger.error("Somehow, more than one group with the id "+groupID+" were found in the database that are not deleted");
3813 throw new PersistenceException("Somehow, more than one group with the id "+groupID+" were found in the database that are not deleted");
3815 //we need to convert the form of the policy id that is used groups into the form that is used
3816 //for the database. (com.Config_mypol.1.xml) to (Config_mypol.xml)
3817 String[] policyNameScopeAndVersion = getNameScopeAndVersionFromPdpPolicy(policyID);
3818 Query policyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:policyName AND p.scope=:scope AND p.deleted=:deleted");
3819 policyQuery.setParameter("policyName", policyNameScopeAndVersion[0]);
3820 policyQuery.setParameter("scope", policyNameScopeAndVersion[1]);
3821 policyQuery.setParameter("deleted", false);
3822 List<?> policyQueryList;
3824 policyQueryList = policyQuery.getResultList();
3825 } catch(Exception e){
3826 //TODO:EELF Cleanup - Remove logger
3827 //logger.error("Caught Exception trying to check if policy exists policyQuery.getResultList()",e);
3828 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if policy exists policyQuery.getResultList()");
3829 throw new PersistenceException("Query failed trying to check if policy "+policyNameScopeAndVersion[0]+" exists");
3831 if(policyQueryList.size() < 1){
3832 //TODO:EELF Cleanup - Remove logger
3833 //logger.error("Policy being added to the group does not exist with policy id "+policyNameScopeAndVersion[0]);
3834 PolicyLogger.error("Policy being added to the group does not exist with policy id "+policyNameScopeAndVersion[0]);
3835 throw new PersistenceException("Policy being added to the group does not exist with policy id "+policyNameScopeAndVersion[0]);
3836 } else if(policyQueryList.size() > 1){
3837 //TODO:EELF Cleanup - Remove logger
3838 //logger.error("Somehow, more than one policy with the id "+policyNameScopeAndVersion[0]+" were found in the database that are not deleted");
3839 PolicyLogger.error("Somehow, more than one policy with the id "+policyNameScopeAndVersion[0]+" were found in the database that are not deleted");
3840 throw new PersistenceException("Somehow, more than one group with the id "+policyNameScopeAndVersion[0]+" were found in the database that are not deleted");
3842 //em.getTransaction().begin();
3843 GroupEntity group = (GroupEntity)groupQueryList.get(0);
3844 PolicyEntity policy = (PolicyEntity)policyQueryList.get(0);
3845 group.addPolicyToGroup(policy);
3850 //this means delete pdp not just remove from group
3852 public void removePdpFromGroup(String pdpID, String username) {
3853 logger.debug("removePdpFromGroup(String pdpID, String username) as removePdpFromGroup("+pdpID+","+username+") called");
3854 // if(isTransactionOpen()){
3855 // logger.error("A transaction is already open which has not been committed");
3856 // throw new IllegalStateException("A transaction is already open which has not been committed");
3858 if(isNullOrEmpty(pdpID,username)){
3859 throw new IllegalArgumentException("pdpID and username must not be null or empty");
3861 synchronized(emLock){
3862 checkBeforeOperationRun();
3863 Query pdpQuery = em.createQuery("SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted");
3864 pdpQuery.setParameter("pdpId", pdpID);
3865 pdpQuery.setParameter("deleted", false);
3868 pdpList = pdpQuery.getResultList();
3869 } catch(Exception e){
3870 //TODO:EELF Cleanup - Remove logger
3871 //logger.error("Caught Exception trying to check if pdp exists pdpQuery.getResultList()",e);
3872 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception trying to check if pdp exists pdpQuery.getResultList()");
3873 throw new PersistenceException("Query failed trying to check if pdp "+pdpID+" exists");
3875 if(pdpList.size() > 1){
3876 //TODO:EELF Cleanup - Remove logger
3877 //logger.error("Somehow, more than one pdp with the id "+pdpID+" were found in the database that are not deleted");
3878 PolicyLogger.error("Somehow, more than one pdp with the id "+pdpID+" were found in the database that are not deleted");
3879 throw new PersistenceException("Somehow, more than one pdp with the id "+pdpID+" were found in the database that are not deleted");
3880 } else if(pdpList.size() < 1){
3881 //logger.warn("Pdp being removed does not exist with id "+pdpID);
3882 PolicyLogger.error("Pdp being removed does not exist with id "+pdpID);
3885 //em.getTransaction().begin();
3886 PdpEntity pdp = (PdpEntity)pdpList.get(0);
3888 if(!stringEquals(pdp.getModifiedBy(),username)){
3889 pdp.setModifiedBy(username);
3891 pdp.setDeleted(true);
3894 this.pdpId = pdp.getPdpKey();
3901 private static String getDefaultWorkspace(){
3905 private PolicyDBDao(){
3908 public static PolicyDBDaoTestClass getPolicyDBDaoTestClass(){
3909 return new PolicyDBDao().new PolicyDBDaoTestClass();
3911 final class PolicyDBDaoTestClass {
3912 String[] getScopeAndNameAndType(final String path){
3913 return PolicyDBDao.getScopeAndNameAndType(path);
3915 String getGitPath(){
3916 return PolicyDBDao.getGitPath();
3918 String getConfigFile(String filename, String scope, PolicyRestAdapter policy){
3919 return PolicyDBDao.this.getConfigFile(filename, scope, policy);
3921 String computeScope(String fullPath, String pathToExclude){
3922 return PolicyDBDao.computeScope(fullPath, pathToExclude);
3924 String encryptPassword(String password) throws Exception{
3925 return PolicyDBDao.encryptPassword(password);
3927 String decryptPassword(String password) throws Exception{
3928 return PolicyDBDao.decryptPassword(password);
3930 String getDescriptionFromXacml(String xacmlData){
3931 return PolicyDBDao.getDescriptionFromXacml(xacmlData);