aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java
blob: 9a39b6ed1ee2bba11ebaf77ff9af98f2fee04a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PAP-REST
 * ================================================================================
 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.xacml.rest.components;

import com.att.research.xacml.api.pap.PAPException;
import com.att.research.xacml.api.pap.PDPPolicy;
import com.att.research.xacml.util.XACMLProperties;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.persistence.PersistenceException;
import org.apache.commons.io.FilenameUtils;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.onap.policy.common.logging.eelf.MessageCodes;
import org.onap.policy.common.logging.eelf.PolicyLogger;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.rest.XACMLRestProperties;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.dao.PolicyDBException;
import org.onap.policy.rest.jpa.ActionBodyEntity;
import org.onap.policy.rest.jpa.ConfigurationDataEntity;
import org.onap.policy.rest.jpa.DatabaseLockEntity;
import org.onap.policy.rest.jpa.GroupEntity;
import org.onap.policy.rest.jpa.PdpEntity;
import org.onap.policy.rest.jpa.PolicyDBDaoEntity;
import org.onap.policy.rest.jpa.PolicyEntity;
import org.onap.policy.utils.PeCryptoUtils;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.api.pap.OnapPDP;
import org.onap.policy.xacml.api.pap.OnapPDPGroup;
import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
import org.onap.policy.xacml.std.pap.StdPDPGroup;
import org.onap.policy.xacml.std.pap.StdPDPPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PolicyDBDao {
    private static final Logger logger = FlexLogger.getLogger(PolicyDBDao.class);
    public static final String JSON_CONFIG = "JSON";
    public static final String XML_CONFIG = "XML";
    public static final String PROPERTIES_CONFIG = "PROPERTIES";
    public static final String OTHER_CONFIG = "OTHER";
    public static final String AUDIT_USER = "audit";

    public static final String CONFIG = "Config";
    public static final String ACTION = "Action";
    public static final String GROUP_ID = "groupId";
    public static final String DELETED = "deleted";
    public static final String GROUPENTITY_SELECT =
            "SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted";
    public static final String PDPENTITY_SELECT =
            "SELECT p FROM PdpEntity p WHERE p.pdpId=:pdpId AND p.deleted=:deleted";
    public static final String GROUP_NOT_FOUND = "The group could not be found with id ";
    public static final String FOUND_IN_DB_NOT_DEL =
            " were found in the database that are not deleted";
    public static final String MORE_THAN_ONE_PDP = "Somehow, more than one pdp with the same id ";
    public static final String DELETED_STATUS_FOUND =
            " and deleted status were found in the database";
    public static final String DUPLICATE_GROUPID = "Somehow, more than one group with the same id ";
    public static final String PDP_ID = "pdpId";
    public static final String QUERY_FAILED_FOR_GROUP =
            "Query failed trying to check for existing group";
    public static final String QUERY_FAILED_GET_GROUP = "Query failed trying to get group ";
    public static final String SCOPE = "scope";
    public static final String POLICYDBDAO_VAR = "PolicyDBDao";
    public static final String DUP_POLICYID = "Somehow, more than one policy with the id ";
    public static final String FOUND_IN_DB = " were found in the database";
    private static PolicyDBDao currentInstance = null;
    private static boolean isJunit = false;
    private static SessionFactory sessionfactory;
    private List<?> otherServers;
    private PAPPolicyEngine papEngine;


    /**
     * Gets the current instance of PolicyDBDao.
     *
     * @return The instance of PolicyDBDao or throws exception if the given instance is null.
     * @throws IllegalStateException if a PolicyDBDao instance is null. Call
     *         createPolicyDBDaoInstance (EntityManagerFactory emf) to get this.
     */
    public static PolicyDBDao getPolicyDBDaoInstance() {
        logger.debug("getPolicyDBDaoInstance() as getPolicyDBDaoInstance() called");
        if (currentInstance != null) {
            return currentInstance;
        } else {
            currentInstance = new PolicyDBDao("init");
        }
        return currentInstance;
    }

    public void setPapEngine(PAPPolicyEngine papEngine2) {
        this.papEngine = papEngine2;
    }

    @Autowired
    public PolicyDBDao(SessionFactory sessionFactory) {
        PolicyDBDao.sessionfactory = sessionFactory;
    }

    public PolicyDBDao() {
        // Default Constructor
    }

    public PolicyDBDao(String init) {
        // not needed in this release
        if (!register()) {
            PolicyLogger.error(
                    "This server's PolicyDBDao instance could not be registered and may not reveive updates");
        }

        otherServers = getRemotePolicyDBDaoList();
        if (logger.isDebugEnabled()) {
            logger.debug("Number of remote PolicyDBDao instances: " + otherServers.size());
        }
        if (otherServers.isEmpty()) {
            logger.warn("List of PolicyDBDao servers is empty or could not be retrieved");
        }
    }

    // not static because we are going to be using the instance's emf
    // waitTime in ms to wait for lock, or -1 to wait forever (no)
    @SuppressWarnings("deprecation")
    public void startTransactionSynced(Session session, int waitTime) {
        logger.debug("\n\nstartTransactionSynced(Hibernate Session,int waitTime) as "
                + "\n   startTransactionSynced(" + session + "," + waitTime + ") called\n\n");
        DatabaseLockEntity lock = null;
        session.beginTransaction();
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("\n\nstartTransactionSynced():" + "\n   ATTEMPT to get the DB lock"
                        + "\n\n");
            }
            lock = (DatabaseLockEntity) session.get(DatabaseLockEntity.class, 1,
                    LockMode.PESSIMISTIC_WRITE);
            if (logger.isDebugEnabled()) {
                logger.debug("\n\nstartTransactionSynced():" + "\n   GOT the DB lock" + "\n\n");
            }
        } catch (Exception e) {
            logger.error("Exception Occured" + e);
        }
        if (lock == null) {
            throw new IllegalStateException(
                    "The lock row does not exist in the table. Please create a primary key with value = 1.");
        }

    }

    /**
     * Gets the list of other registered PolicyDBDaos from the database
     *
     * @return List (type PolicyDBDaoEntity) of other PolicyDBDaos
     */
    private List<?> getRemotePolicyDBDaoList() {
        logger.debug("getRemotePolicyDBDaoList() as getRemotePolicyDBDaoList() called");
        List<?> policyDBDaoEntityList = new LinkedList<>();
        Session session = sessionfactory.openSession();
        try {
            Criteria cr = session.createCriteria(PolicyDBDaoEntity.class);
            policyDBDaoEntityList = cr.list();
        } catch (Exception e) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR,
                    "Exception querying for other registered PolicyDBDaos");
            logger.warn("List of remote PolicyDBDaos will be empty", e);
        } finally {
            try {
                session.close();
            } catch (Exception e) {
                logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
                        + "Error While Closing Connection/Statement" + e);
            }
        }
        return policyDBDaoEntityList;
    }

    public PolicyDBDaoTransaction getNewTransaction() {
        logger.debug("getNewTransaction() as getNewTransaction() called");
        return new PolicyDbDaoTransactionInstance("init");
    }

    /*
     * Because the normal transactions are not used in audits, we can use the same transaction
     * mechanism to get a transaction and obtain the emlock and the DB lock. We just need to provide
     * different transaction timeout values in ms because the audit will run longer than normal
     * transactions.
     */
    public PolicyDBDaoTransaction getNewAuditTransaction() {
        logger.debug("getNewAuditTransaction() as getNewAuditTransaction() called");
        // Use the standard transaction wait time in ms
        int auditWaitMs = Integer
                .parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_TRANS_WAIT));
        // Use the (extended) audit timeout time in ms
        int auditTimeoutMs = Integer
                .parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_AUDIT_TIMEOUT));
        return new PolicyDbDaoTransactionInstance(auditTimeoutMs, auditWaitMs);
    }

    /**
     * Checks if two strings are equal. Null strings ARE allowed.
     *
     * @param one A String or null to compare
     * @param two A String or null to compare
     */
    public static boolean stringEquals(String one, String two) {
        logger.debug("stringEquals(String one, String two) as stringEquals(" + one + ", " + two
                + ") called");
        if (one == null && two == null) {
            return true;
        }
        if (one == null || two == null) {
            return false;
        }
        return one.equals(two);
    }

    /**
     * Returns the url of this local pap server, removing the username and password, if they are
     * present
     *
     * @return The url of this local pap server
     */
    public String[] getPapUrlUserPass() {
        logger.debug("getPapUrl() as getPapUrl() called");
        String url = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
        if (url == null) {
            return null;
        }
        return splitPapUrlUserPass(url);
    }

    public String[] splitPapUrlUserPass(String url) {
        String[] urlUserPass = new String[3];
        String[] commaSplit = url.split(",");
        urlUserPass[0] = commaSplit[0];
        if (commaSplit.length > 2) {
            urlUserPass[1] = commaSplit[1];
            urlUserPass[2] = commaSplit[2];
        }
        if (urlUserPass[1] == null || "".equals(urlUserPass[1])) {
            String usernamePropertyValue =
                    XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
            if (usernamePropertyValue != null) {
                urlUserPass[1] = usernamePropertyValue;
            }
        }
        if (urlUserPass[2] == null || "".equals(urlUserPass[2])) {
            String passwordPropertyValue =
                    PeCryptoUtils.decrypt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS));
            if (passwordPropertyValue != null) {
                urlUserPass[2] = passwordPropertyValue;
            }
        }
        // if there is no comma, for some reason there is no user name and
        // password, so don't try to cut them off
        return urlUserPass;
    }

    /**
     * Register the PolicyDBDao instance in the PolicyDBDaoEntity table
     *
     * @return Boolean, were we able to register?
     */
    private boolean register() {
        logger.debug("register() as register() called");
        String[] url = getPapUrlUserPass();
        // --- check URL length
        if (url == null || url.length < 3) {
            return false;
        }
        Session session = sessionfactory.openSession();
        try {
            startTransactionSynced(session, 1000);
        } catch (IllegalStateException e) {
            logger.debug("\nPolicyDBDao.register() caught an IllegalStateException: \n" + e + "\n");
            DatabaseLockEntity lock;
            lock = (DatabaseLockEntity) session.get(DatabaseLockEntity.class, 1);
            if (lock == null) {
                lock = new DatabaseLockEntity();
                lock.setKey(1);
                try {
                    session.persist(lock);
                    session.flush();
                    session.getTransaction().commit();
                    session.close();
                } catch (Exception e2) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, POLICYDBDAO_VAR,
                            "COULD NOT CREATE DATABASELOCK ROW.  WILL TRY ONE MORE TIME");
                }

                session = sessionfactory.openSession();
                try {
                    startTransactionSynced(session, 1000);
                } catch (Exception e3) {
                    String msg = "DATABASE LOCKING NOT WORKING. CONCURRENCY CONTROL NOT WORKING";
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e3, POLICYDBDAO_VAR, msg);
                    throw new IllegalStateException("msg" + "\n" + e3);
                }
            }
        }
        logger.debug(
                "\nPolicyDBDao.register. Database locking and concurrency control is initialized\n");
        PolicyDBDaoEntity foundPolicyDBDaoEntity = null;
        Criteria cr = session.createCriteria(PolicyDBDaoEntity.class);
        cr.add(Restrictions.eq("policyDBDaoUrl", url[0]));
        List<?> data = cr.list();
        if (!data.isEmpty()) {
            foundPolicyDBDaoEntity = (PolicyDBDaoEntity) data.get(0);
        }

        // encrypt the password
        String txt = PeCryptoUtils.encrypt(url[2]);
        if (foundPolicyDBDaoEntity == null) {
            PolicyDBDaoEntity newPolicyDBDaoEntity = new PolicyDBDaoEntity();
            newPolicyDBDaoEntity.setPolicyDBDaoUrl(url[0]);
            newPolicyDBDaoEntity.setDescription("PAP server at " + url[0]);
            newPolicyDBDaoEntity.setUsername(url[1]);
            newPolicyDBDaoEntity.setPassword(txt);
            try {
                session.persist(newPolicyDBDaoEntity);
                session.getTransaction().commit();
            } catch (Exception e) {
                logger.debug(e);
                try {
                    session.getTransaction().rollback();
                } catch (Exception e2) {
                    logger.debug(e2);
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, POLICYDBDAO_VAR,
                            "Could not add new PolicyDBDao to the database");
                }
            }
        } else {
            // just want to update in order to change modified date
            if (url[1] != null && !stringEquals(url[1], foundPolicyDBDaoEntity.getUsername())) {
                foundPolicyDBDaoEntity.setUsername(url[1]);
            }
            if (txt != null && !stringEquals(txt, foundPolicyDBDaoEntity.getPassword())) {
                foundPolicyDBDaoEntity.setPassword(txt);
            }
            foundPolicyDBDaoEntity.preUpdate();
            try {
                session.getTransaction().commit();
            } catch (Exception e) {
                logger.debug(e);
                try {
                    session.getTransaction().rollback();
                } catch (Exception e2) {
                    logger.debug(e2);
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, POLICYDBDAO_VAR,
                            "Could not update PolicyDBDao in the database");
                }
            }
        }
        session.close();
        logger.debug("\nPolicyDBDao.register(). Success!!\n");
        return true;
    }

    /*
     * This method is called during all pushPolicy transactions and makes sure the file system group
     * is in sync with the database groupentity
     */
    public StdPDPGroup synchronizeGroupPoliciesInFileSystem(StdPDPGroup pdpGroup,
            GroupEntity groupentity) throws PAPException, PolicyDBException {

        HashMap<String, PDPPolicy> currentPolicyMap = new HashMap<>();
        HashSet<String> newPolicyIdSet = new HashSet<>();
        HashSet<PDPPolicy> newPolicySet = new HashSet<>();

        for (PDPPolicy pdpPolicy : pdpGroup.getPolicies()) {
            currentPolicyMap.put(pdpPolicy.getId(), pdpPolicy);
        }

        for (PolicyEntity policy : groupentity.getPolicies()) {
            String pdpPolicyId = getPdpPolicyName(policy.getPolicyName(), policy.getScope());
            newPolicyIdSet.add(pdpPolicyId);

            if (currentPolicyMap.containsKey(pdpPolicyId)) {
                newPolicySet.add(currentPolicyMap.get(pdpPolicyId));
            } else {
                // convert PolicyEntity object to PDPPolicy
                String name = pdpPolicyId.replace(".xml", "");
                name = name.substring(0, name.lastIndexOf('.'));
                InputStream policyStream =
                        new ByteArrayInputStream(policy.getPolicyData().getBytes());
                pdpGroup.copyPolicyToFile(pdpPolicyId, name, policyStream);
                URI location =
                        Paths.get(pdpGroup.getDirectory().toAbsolutePath().toString(), pdpPolicyId)
                                .toUri();
                StdPDPPolicy newPolicy = null;
                try {
                    newPolicy = new StdPDPPolicy(pdpPolicyId, true,
                            removeExtensionAndVersionFromPolicyName(pdpPolicyId), location);
                    newPolicySet.add(newPolicy);
                    logger.info("Adding new policy to PDPGroup - " + newPolicy.getId()
                            + ", Location - " + location);
                } catch (Exception e) {
                    logger.debug(e);
                    PolicyLogger.error(
                            "PolicyDBDao: Exception occurred while creating the StdPDPPolicy newPolicy object "
                                    + e.getMessage());
                }
            }
        }

        for (String id : currentPolicyMap.keySet()) {
            if (!newPolicyIdSet.contains(id)) {
                try {
                    Files.delete(Paths.get(currentPolicyMap.get(id).getLocation()));
                } catch (Exception e) {
                    logger.debug(e);
                    PolicyLogger.error(
                            "PolicyDBDao: Exception occurred while attempting to delete the old version of the policy file from the group. "
                                    + e.getMessage());
                }
            }
        }

        logger.info(
                "PolicyDBDao: Adding new policy set to group to keep filesystem and DB in sync");
        pdpGroup.setPolicies(newPolicySet);

        return pdpGroup;
    }

    public String removeExtensionAndVersionFromPolicyName(String originalPolicyName)
            throws PolicyDBException {
        return getPolicyNameAndVersionFromPolicyFileName(originalPolicyName)[0];
    }

    /**
     * Splits apart the policy name and version from a policy file path
     *
     * @param originalPolicyName: a policy file name ex: Config_policy.2.xml
     * @return An array [0]: The policy name, [1]: the policy version, as a string
     */
    public String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName)
            throws PolicyDBException {
        String policyName = originalPolicyName;
        String[] nameAndVersion = new String[2];
        try {
            policyName = removeFileExtension(policyName);
            nameAndVersion[0] = policyName.substring(0, policyName.lastIndexOf('.'));
            if (isNullOrEmpty(nameAndVersion[0])) {
                throw new PolicyDBException();
            }
        } catch (Exception e) {
            nameAndVersion[0] = originalPolicyName;
            logger.debug(e);
        }
        try {
            nameAndVersion[1] = policyName.substring(policyName.lastIndexOf('.') + 1);
            if (isNullOrEmpty(nameAndVersion[1])) {
                throw new PolicyDBException();
            }
        } catch (Exception e) {
            nameAndVersion[1] = "1";
            logger.debug(e);
        }
        return nameAndVersion;
    }

    public String getPdpPolicyName(String name, String scope) {
        String finalName = "";
        finalName += scope;
        finalName += ".";
        finalName += removeFileExtension(name);
        finalName += ".xml";
        return finalName;
    }

    private String removeFileExtension(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf('.'));
    }

    public void auditLocalDatabase(PAPPolicyEngine papEngine2) {
        logger.debug("PolicyDBDao.auditLocalDatabase() is called");
        try {
            deleteAllGroupTables();
            auditGroups(papEngine2);
        } catch (Exception e) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR,
                    "auditLocalDatabase() error");
            logger.error("Exception Occured" + e);
        }
    }

    public StdPDPGroup auditLocalFileSystem(StdPDPGroup group) {

        logger.info("Starting Local File System group audit");
        Session session = sessionfactory.openSession();
        session.getTransaction().begin();

        StdPDPGroup updatedGroup = null;
        try {
            Query groupQuery = session.createQuery(GROUPENTITY_SELECT);
            groupQuery.setParameter(GROUP_ID, group.getId());
            groupQuery.setParameter(DELETED, false);
            List<?> groupQueryList = groupQuery.list();
            if (groupQueryList != null && !groupQueryList.isEmpty()) {
                GroupEntity dbgroup = (GroupEntity) groupQueryList.get(0);
                updatedGroup = synchronizeGroupPoliciesInFileSystem(group, dbgroup);
                logger.info(
                        "Group was updated during file system audit: " + updatedGroup.toString());
            }
        } catch (PAPException | PolicyDBException e) {
            logger.error(e);
        } catch (Exception e) {
            logger.error(e);
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR,
                    "Caught Exception trying to check if group exists groupQuery.getResultList()");
            throw new PersistenceException(
                    "Query failed trying to check if group " + group.getId() + " exists");
        }

        session.getTransaction().commit();
        session.close();

        return updatedGroup;
    }

    /*
     * This method is called at startup to recreate config data from DB to the file system.
     *
     */
    public void synchronizeConfigDataInFileSystem() {

        logger.info("Starting Local File System Config data Sync");
        // sync both web apps Config and Action
        syncConfigData(ConfigurationDataEntity.class, CONFIG);
        syncConfigData(ActionBodyEntity.class, ACTION);
    }

    private <T> void syncConfigData(Class<T> cl, String type) {
        Session session = sessionfactory.openSession();
        try {
            final Criteria configDataQuery = session.createCriteria(cl.getName());
            @SuppressWarnings("unchecked")
            final List<T> configDataResult = configDataQuery.list();
            Path webappsPath = Paths
                    .get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS), type);

            for (final T configData : configDataResult) {
                String configName = null;
                byte[] configBody;
                try {
                    if (CONFIG.equalsIgnoreCase(type)) {
                        configName = ((ConfigurationDataEntity) configData).getConfigurationName();
                        configBody =
                                (((ConfigurationDataEntity) configData).getConfigBody() != null)
                                        ? ((ConfigurationDataEntity) configData).getConfigBody()
                                                .getBytes(StandardCharsets.UTF_8)
                                        : "".getBytes();
                    } else {
                        configName = ((ActionBodyEntity) configData).getActionBodyName();
                        configBody = (((ActionBodyEntity) configData).getActionBody() != null)
                                ? ((ActionBodyEntity) configData).getActionBody()
                                        .getBytes(StandardCharsets.UTF_8)
                                : "".getBytes();
                    }
                    Path filePath = Paths.get(webappsPath.toString(), configName);
                    if (!filePath.toFile().exists()) {
                        Files.write(filePath, configBody);
                        logger.info("Created Config File from DB - " + filePath.toString());
                    }
                } catch (Exception e) {
                    // log and keep going
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR,
                            "Exception occured while creating Configuration File - " + configName);
                }
            }
        } catch (final Exception exception) {
            logger.error("Unable to synchronizeConfigDataInFileSystem", exception);
        }
        session.close();
    }

    public void deleteAllGroupTables() {
        logger.debug("PolicyDBDao.deleteAllGroupTables() called");
        Session session = sessionfactory.openSession();
        session.getTransaction().begin();

        Query deletePdpEntityEntityTableUpdate = session.getNamedQuery("PdpEntity.deleteAll");
        deletePdpEntityEntityTableUpdate.executeUpdate();

        Query deleteGroupEntityTableUpdate = session.getNamedQuery("GroupEntity.deleteAll");
        deleteGroupEntityTableUpdate.executeUpdate();

        session.getTransaction().commit();
        session.close();
    }

    @SuppressWarnings("unchecked")
    public void auditGroups(PAPPolicyEngine papEngine2) {
        logger.debug("PolicyDBDao.auditGroups() called");

        Session session = sessionfactory.openSession();
        session.getTransaction().begin();
        final String AUDIT_STR = "Audit";
        try {

            Set<OnapPDPGroup> groups = papEngine2.getOnapPDPGroups();

            for (OnapPDPGroup grp : groups) {
                try {
                    GroupEntity groupEntity = new GroupEntity();
                    groupEntity.setGroupName(grp.getName());
                    groupEntity.setDescription(grp.getDescription());
                    groupEntity.setDefaultGroup(grp.isDefaultGroup());
                    groupEntity.setCreatedBy(AUDIT_STR);
                    groupEntity.setGroupId(createNewPDPGroupId(grp.getId()));
                    groupEntity.setModifiedBy(AUDIT_STR);
                    session.persist(groupEntity);
                    Set<OnapPDP> pdps = grp.getOnapPdps();

                    for (OnapPDP pdp : pdps) {
                        PdpEntity pdpEntity = new PdpEntity();
                        pdpEntity.setGroup(groupEntity);
                        pdpEntity.setJmxPort(pdp.getJmxPort());
                        pdpEntity.setPdpId(pdp.getId());
                        pdpEntity.setPdpName(pdp.getName());
                        pdpEntity.setModifiedBy(AUDIT_STR);
                        pdpEntity.setCreatedBy(AUDIT_STR);
                        session.persist(pdpEntity);
                    }

                    Set<PDPPolicy> policies = grp.getPolicies();

                    for (PDPPolicy policy : policies) {
                        try {
                            String[] stringArray =
                                    getNameScopeAndVersionFromPdpPolicy(policy.getId());
                            if (stringArray == null) {
                                throw new IllegalArgumentException(
                                        "Invalid input - policyID must contain name, scope and version");
                            }
                            List<PolicyEntity> policyEntityList;
                            Query getPolicyEntitiesQuery =
                                    session.getNamedQuery("PolicyEntity.findByNameAndScope");
                            getPolicyEntitiesQuery.setParameter("name", stringArray[0]);
                            getPolicyEntitiesQuery.setParameter(SCOPE, stringArray[1]);

                            policyEntityList = getPolicyEntitiesQuery.list();
                            PolicyEntity policyEntity = null;
                            if (!policyEntityList.isEmpty()) {
                                policyEntity = policyEntityList.get(0);
                            }
                            if (policyEntity != null) {
                                groupEntity.addPolicyToGroup(policyEntity);
                            }
                        } catch (Exception e2) {
                            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e2, POLICYDBDAO_VAR,
                                    "Exception auditGroups inner catch");
                        }
                    }
                } catch (Exception e1) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, POLICYDBDAO_VAR,
                            "Exception auditGroups middle catch");
                }
            }
        } catch (Exception e) {
            session.getTransaction().rollback();
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, POLICYDBDAO_VAR,
                    "Exception auditGroups outer catch");
            session.close();
            return;
        }

        session.getTransaction().commit();
        session.close();

    }

    public String getConfigFile(String filename, PolicyRestAdapter policy) {
        if (policy == null) {
            return getConfigFile(filename, (String) null);
        }
        return getConfigFile(filename, policy.getConfigType());
    }

    // copied from ConfigPolicy.java and modified
    // Here we are adding the extension for the configurations file based on the
    // config type selection for saving.
    public String getConfigFile(String inputFilename, String configType) {
        String filename = inputFilename;
        logger.debug(
                "getConfigFile(String filename, String scope, String configType) as getConfigFile("
                        + filename + ", " + configType + ") called");
        filename = FilenameUtils.removeExtension(filename);
        String id = configType;

        if (id != null) {
            if (id.equals(ConfigPolicy.JSON_CONFIG) || id.contains("Firewall")) {
                filename = filename + ".json";
            }
            if (id.equals(ConfigPolicy.XML_CONFIG)) {
                filename = filename + ".xml";
            }
            if (id.equals(ConfigPolicy.PROPERTIES_CONFIG)) {
                filename = filename + ".properties";
            }
            if (id.equals(ConfigPolicy.OTHER_CONFIG)) {
                filename = filename + ".txt";
            }
        }
        return filename;
    }

    public String[] getNameScopeAndVersionFromPdpPolicy(String fileName) {
        String[] splitByDots = fileName.split("\\.");
        if (splitByDots.length < 3) {
            return null;
        }
        String policyName = splitByDots[splitByDots.length - 3];
        String version = splitByDots[splitByDots.length - 2];
        // policy names now include version
        String scope = "";
        for (int i = 0; i < splitByDots.length - 3; i++) {
            scope += ".".concat(splitByDots[i]);
        }
        // remove the first dot
        if (scope.length() > 0) {
            scope = scope.substring(1);
        }
        String[] returnArray = new String[3];
        returnArray[0] = policyName + "." + version + ".xml";
        returnArray[2] = version;
        returnArray[1] = scope;
        return returnArray;
    }

    public static String createNewPDPGroupId(String name) {
        String id = name;
        // replace "bad" characters with sequences that will be ok for file
        // names and properties keys.
        id = id.replace(" ", "_sp_");
        id = id.replace("\t", "_tab_");
        id = id.replace("\\", "_bksl_");
        id = id.replace("/", "_sl_");
        id = id.replace(":", "_col_");
        id = id.replace("*", "_ast_");
        id = id.replace("?", "_q_");
        id = id.replace("\"", "_quo_");
        id = id.replace("<", "_lt_");
        id = id.replace(">", "_gt_");
        id = id.replace("|", "_bar_");
        id = id.replace("=", "_eq_");
        id = id.replace(",", "_com_");
        id = id.replace(";", "_scom_");

        return id;
    }

    /**
     * Checks if any of the given strings are empty or null
     *
     * @param strings One or more Strings (or nulls) to check if they are null or empty
     * @return true if one or more of the given strings are empty or null
     */
    public static boolean isNullOrEmpty(String... strings) {
        for (String s : strings) {
            if (s == null || "".equals(s)) {
                return true;
            }
        }
        return false;
    }

    public List<?> getOtherServers() {
        return otherServers;
    }

    public void setOtherServers(List<?> otherServers) {
        this.otherServers = otherServers;
    }

    public PAPPolicyEngine getPapEngine() {
        return papEngine;
    }


    public static boolean isJunit() {
        return isJunit;
    }

    public static void setJunit(boolean isJunit) {
        PolicyDBDao.isJunit = isJunit;
    }

    public static PolicyDBDaoTestClass getPolicyDBDaoTestClass() {
        return new PolicyDBDao().new PolicyDBDaoTestClass();
    }

    final class PolicyDBDaoTestClass {
        String getConfigFile(String filename, String scope, PolicyRestAdapter policy) {
            return scope + "." + PolicyDBDao.this.getConfigFile(filename, policy);
        }

        String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName)
                throws PolicyDBException {
            return PolicyDBDao.this.getPolicyNameAndVersionFromPolicyFileName(originalPolicyName);
        }

        String[] getNameScopeAndVersionFromPdpPolicy(String fileName) {
            return PolicyDBDao.this.getNameScopeAndVersionFromPdpPolicy(fileName);
        }

        String getPdpPolicyName(String name, String scope) {
            return PolicyDBDao.this.getPdpPolicyName(name, scope);
        }
    }

}