aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDbDao.java
blob: 9af380b05b5cb47dde57bf4823dc816425efe371 (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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/*-
 * ============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;

/**
 * The Class PolicyDbDao.
 */
@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 final String AUDIT_STR = "Audit";

    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;
    }

    /**
     * Sets the pap engine.
     *
     * @param papEngine2 the new pap engine
     */
    public void setPapEngine(PAPPolicyEngine papEngine2) {
        this.papEngine = papEngine2;
    }

    /**
     * Instantiates a new policy db dao.
     *
     * @param sessionFactory the session factory
     */
    @Autowired
    public PolicyDbDao(SessionFactory sessionFactory) {
        PolicyDbDao.sessionfactory = sessionFactory;
    }

    /**
     * Instantiates a new policy db dao.
     */
    public PolicyDbDao() {
        // Default Constructor
    }

    /**
     * Initialize the DAO.
     *
     * @param init initiation parameters
     */
    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");
        }
    }

    /**
     * Start a synchronized transaction.
     *
     * <p>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)
     *
     * @param session the session
     * @param waitTime the wait time
     */
    @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;
    }

    /**
     * Gets the new transaction.
     *
     * @return the new transaction
     */
    public PolicyDbDaoTransaction getNewTransaction() {
        logger.debug("getNewTransaction() as getNewTransaction() called");
        return new PolicyDbDaoTransactionInstance("init");
    }

    /**
     * Get a new audit transaction.
     *
     * <p>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.
     *
     * @return the new audit transaction
     */
    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
     * @return true, if successful
     */
    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);
    }

    /**
     * Split the user and password of a PAP URL.
     *
     * @param url the URL
     * @return the user and password
     */
    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;
    }

    /**
     * Synchronize group policies in the fuile system.
     *
     * <p>This method is called during all pushPolicy transactions and makes sure the file system group is in sync with
     * the database group entity
     *
     * @param pdpGroup the pdp group
     * @param groupentity the groupentity
     * @return the std PDP group
     * @throws PAPException the PAP exception
     * @throws PolicyDbException the policy db exception
     */
    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;
    }

    /**
     * Removes the extension and version from policy name.
     *
     * @param originalPolicyName the original policy name
     * @return the string
     * @throws PolicyDbException the policy db exception
     */
    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 the original policy name
     * @return An array [0]: The policy name, [1]: the policy version, as a string
     * @throws PolicyDbException the policy db exception
     */
    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;
    }

    /**
     * Get the PDP policy by name.
     *
     * @param name The name to get
     * @param scope The scope to use
     * @return the policy.
     */
    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('.'));
    }

    /**
     * Audit local database.
     *
     * @param papEngine2 the pap engine 2
     */
    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);
        }
    }

    /**
     * Audit local file system.
     *
     * @param group the group
     * @return the std PDP group
     */
    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;
    }

    /**
     * Synchronize config data in 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();
    }

    /**
     * Delete all group tables.
     */
    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();
    }

    /**
     * Audit groups.
     *
     * @param papEngine2 the pap engine 2
     */
    @SuppressWarnings("unchecked")
    public void auditGroups(PAPPolicyEngine papEngine2) {
        logger.debug("PolicyDBDao.auditGroups() called");

        Session session = sessionfactory.openSession();
        session.getTransaction().begin();
        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();

    }

    /**
     * Gets the config file.
     *
     * @param filename the filename
     * @param policy the policy
     * @return the config file
     */
    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
    /**
     * Gets the config file.
     *
     * @param inputFilename the input filename
     * @param configType the config type
     * @return the config file
     */
    // 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;
    }

    /**
     * Gets the name scope and version from pdp policy.
     *
     * @param fileName the file name
     * @return the name scope and version from pdp policy
     */
    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;
    }

    /**
     * Creates the new PDP group id.
     *
     * @param name the name
     * @return the string
     */
    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;
    }

    /**
     * Gets the other servers.
     *
     * @return the other servers
     */
    public List<?> getOtherServers() {
        return otherServers;
    }

    /**
     * Sets the other servers.
     *
     * @param otherServers the new other servers
     */
    public void setOtherServers(List<?> otherServers) {
        this.otherServers = otherServers;
    }

    /**
     * Gets the pap engine.
     *
     * @return the pap engine
     */
    public PAPPolicyEngine getPapEngine() {
        return papEngine;
    }

    /**
     * Checks if is junit.
     *
     * @return true, if is junit
     */
    public static boolean isJunit() {
        return isJunit;
    }

    /**
     * Sets the junit.
     *
     * @param isJunit the new junit
     */
    public static void setJunit(boolean isJunit) {
        PolicyDbDao.isJunit = isJunit;
    }

    /**
     * Gets the policy DB dao test class.
     *
     * @return the policy DB dao test class
     */
    public static PolicyDbDaoTestClass getPolicyDbDaoTestClass() {
        return new PolicyDbDao().new PolicyDbDaoTestClass();
    }

    /**
     * The Class PolicyDBDaoTestClass.
     */
    final class PolicyDbDaoTestClass {

        /**
         * Gets the config file.
         *
         * @param filename the filename
         * @param scope the scope
         * @param policy the policy
         * @return the config file
         */
        String getConfigFile(String filename, String scope, PolicyRestAdapter policy) {
            return scope + "." + PolicyDbDao.this.getConfigFile(filename, policy);
        }

        /**
         * Gets the policy name and version from policy file name.
         *
         * @param originalPolicyName the original policy name
         * @return the policy name and version from policy file name
         * @throws PolicyDbException the policy db exception
         */
        String[] getPolicyNameAndVersionFromPolicyFileName(String originalPolicyName) throws PolicyDbException {
            return PolicyDbDao.this.getPolicyNameAndVersionFromPolicyFileName(originalPolicyName);
        }

        /**
         * Gets the name scope and version from pdp policy.
         *
         * @param fileName the file name
         * @return the name scope and version from pdp policy
         */
        String[] getNameScopeAndVersionFromPdpPolicy(String fileName) {
            return PolicyDbDao.this.getNameScopeAndVersionFromPdpPolicy(fileName);
        }

        /**
         * Gets the pdp policy name.
         *
         * @param name the name
         * @param scope the scope
         * @return the pdp policy name
         */
        String getPdpPolicyName(String name, String scope) {
            return PolicyDbDao.this.getPdpPolicyName(name, scope);
        }
    }

}