aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/HandleIncomingNotifications.java
blob: fc08b643e630d703ef93efe5dc80bd3f68050d88 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PAP-REST
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
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.List;
import javax.persistence.PersistenceException;
import org.apache.commons.io.FilenameUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
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.pap.xacml.rest.XACMLPapServlet;
import org.onap.policy.rest.XACMLRestProperties;
import org.onap.policy.rest.dao.PolicyDBException;
import org.onap.policy.rest.jpa.GroupEntity;
import org.onap.policy.rest.jpa.PdpEntity;
import org.onap.policy.rest.jpa.PolicyEntity;
import org.onap.policy.xacml.api.pap.OnapPDP;
import org.onap.policy.xacml.api.pap.OnapPDPGroup;
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 HandleIncomingNotifications {

    private static final Logger logger = FlexLogger.getLogger(HandleIncomingNotifications.class);

    private static final String POLICY_NOTIFICATION = "policy";
    private static final String PDP_NOTIFICATION = "pdp";
    private static final String GROUP_NOTIFICATION = "group";
    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";


    private static SessionFactory sessionfactory;

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

    public HandleIncomingNotifications() {
        // Default Constructor
    }

    public void handleIncomingHttpNotification(String url, String entityId, String entityType, String extraData,
            XACMLPapServlet xacmlPapServlet) {
        logger.info("DBDao url: " + url + " has reported an update on " + entityType + " entity " + entityId);
        PolicyDBDaoTransaction transaction = PolicyDBDao.getPolicyDBDaoInstance().getNewTransaction();
        // although its named retries, this is the total number of tries
        int retries;
        try {
            retries = Integer
                    .parseInt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_INCOMINGNOTIFICATION_TRIES));
        } catch (Exception e) {
            logger.error("xacml.rest.pap.incomingnotification.tries property not set, using a default of 3." + e);
            retries = 3;
        }
        // if someone sets it to some dumb value, we need to make sure it will
        // try at least once
        if (retries < 1) {
            retries = 1;
        }
        int pauseBetweenRetries = 1000;
        switch (entityType) {

            case POLICY_NOTIFICATION:
                for (int i = 0; i < retries; i++) {
                    try {
                        handleIncomingPolicyChange(entityId);
                        break;
                    } catch (Exception e) {
                        logger.debug(e);
                        PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                                "Caught exception on handleIncomingPolicyChange(" + url + ", " + entityId + ", "
                                        + extraData + ")");
                    }
                    try {
                        Thread.sleep(pauseBetweenRetries);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
                break;
            case PDP_NOTIFICATION:
                for (int i = 0; i < retries; i++) {
                    try {
                        handleIncomingPdpChange(entityId, transaction);
                        break;
                    } catch (Exception e) {
                        logger.debug(e);
                        PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                                "Caught exception on handleIncomingPdpChange(" + url + ", " + entityId + ", "
                                        + transaction + ")");
                    }
                    try {
                        Thread.sleep(pauseBetweenRetries);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
                break;
            case GROUP_NOTIFICATION:
                for (int i = 0; i < retries; i++) {
                    try {
                        handleIncomingGroupChange(entityId, extraData, transaction);
                        break;
                    } catch (Exception e) {
                        logger.debug(e);
                        PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                                "Caught exception on handleIncomingGroupChange(" + url + ", " + entityId + ", "
                                        + extraData + ", " + transaction + ", " + xacmlPapServlet + ")");
                    }
                    try {
                        Thread.sleep(pauseBetweenRetries);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }
                break;
        }
        // no changes should be being made in this function, we still need to
        // close
        transaction.rollbackTransaction();
    }

    private void handleIncomingGroupChange(String groupId, String extraData, PolicyDBDaoTransaction transaction)
            throws PAPException, PolicyDBException {
        GroupEntity groupRecord = null;
        long groupIdLong = -1;
        try {
            groupIdLong = Long.parseLong(groupId);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("groupId " + groupId + " cannot be parsed into a long");
        }
        try {
            groupRecord = transaction.getGroup(groupIdLong);
        } catch (Exception e) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                    "Caught Exception trying to get pdp group record with transaction.getGroup(" + groupIdLong + ");");
            throw new PAPException("Could not get local group " + groupIdLong);
        }
        if (groupRecord == null) {
            throw new PersistenceException("The group record returned is null");
        }
        // compare to local fs
        // does group folder exist
        OnapPDPGroup localGroup = null;
        try {
            localGroup = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getGroup(groupRecord.getGroupId());
        } catch (Exception e) {
            logger.warn("Caught PAPException trying to get local pdp group with papEngine.getGroup(" + groupId + ");",
                    e);
        }
        if (localGroup == null && extraData != null) {
            // here we can try to load an old group id from the extraData
            try {
                localGroup = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getGroup(extraData);
            } catch (Exception e) {
                logger.warn(
                        "Caught PAPException trying to get local pdp group with papEngine.getGroup(" + extraData + ");",
                        e);
            }
        }
        if (localGroup != null && groupRecord.isDeleted()) {
            OnapPDPGroup newLocalGroup = null;
            if (extraData != null) {
                try {
                    newLocalGroup = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getGroup(extraData);
                } catch (PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to get new pdp group with papEngine.getGroup(" + extraData
                                    + ");");
                }
            }
            try {
                PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().removeGroup(localGroup, newLocalGroup);
            } catch (NullPointerException | PAPException e) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                        "Caught PAPException trying to get remove pdp group with papEngine.removeGroup(" + localGroup
                                + ", " + newLocalGroup + ");");
                throw new PAPException("Could not remove group " + groupId);
            }
        } else if (localGroup == null) {
            // creating a new group
            try {
                PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().newGroup(groupRecord.getgroupName(),
                        groupRecord.getDescription());
            } catch (NullPointerException | PAPException e) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                        "Caught PAPException trying to create pdp group with papEngine.newGroup(groupRecord.getgroupName(), groupRecord.getDescription());");
                throw new PAPException("Could not create group " + groupRecord);
            }
            try {
                localGroup = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getGroup(groupRecord.getGroupId());
            } catch (PAPException e1) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, PolicyDBDao.POLICYDBDAO_VAR,
                        "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");
                return;
            }
            // add possible pdps to group
            List<?> pdpsInGroup = transaction.getPdpsInGroup(Long.parseLong(groupRecord.getGroupId()));
            for (Object pdpO : pdpsInGroup) {
                PdpEntity pdp = (PdpEntity) pdpO;
                try {
                    PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().newPDP(pdp.getPdpId(), localGroup,
                            pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());
                } catch (NullPointerException | PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to get create pdp with papEngine.newPDP(pdp.getPdpId(), localGroup, pdp.getPdpName(), pdp.getDescription(), pdp.getJmxPort());");
                    throw new PAPException("Could not create pdp " + pdp);
                }
            }
            // add possible policies to group (file system only, apparently)
        } else {
            if (!(localGroup instanceof StdPDPGroup)) {
                throw new PAPException("group is not a StdPDPGroup");
            }
            // clone the object
            // because it will be comparing the new group to its own version
            StdPDPGroup localGroupClone = new StdPDPGroup(localGroup.getId(), localGroup.isDefaultGroup(),
                    localGroup.getName(), localGroup.getDescription(), ((StdPDPGroup) localGroup).getDirectory());
            localGroupClone.setOnapPdps(localGroup.getOnapPdps());
            localGroupClone.setPipConfigs(localGroup.getPipConfigs());
            localGroupClone.setStatus(localGroup.getStatus());
            // we are updating a group or adding a policy or changing default
            // set default if it should be
            if (!localGroupClone.isDefaultGroup() && groupRecord.isDefaultGroup()) {
                try {
                    PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().setDefaultGroup(localGroup);
                    return;
                } catch (PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to set default group with papEngine.SetDefaultGroup("
                                    + localGroupClone + ");");
                    throw new PAPException("Could not set default group to " + localGroupClone);
                }
            }
            boolean needToUpdate = false;
            if (updateGroupPoliciesInFileSystem(localGroupClone, localGroup, groupRecord, transaction)) {
                needToUpdate = true;
            }
            if (!PolicyDBDao.stringEquals(localGroupClone.getId(), groupRecord.getGroupId())
                    || !PolicyDBDao.stringEquals(localGroupClone.getName(), groupRecord.getgroupName())) {
                // changing ids
                // we do not want to change the id, the papEngine will do this
                // for us, it needs to know the old id
                localGroupClone.setName(groupRecord.getgroupName());
                needToUpdate = true;
            }
            if (!PolicyDBDao.stringEquals(localGroupClone.getDescription(), groupRecord.getDescription())) {
                localGroupClone.setDescription(groupRecord.getDescription());
                needToUpdate = true;
            }
            if (needToUpdate) {
                try {
                    PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().updateGroup(localGroupClone);
                } catch (PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to update group with papEngine.updateGroup(" + localGroupClone
                                    + ");");
                    throw new PAPException("Could not update group " + localGroupClone);
                }
            }
        }
    }

    // this will also handle removes, since incoming pdpGroup has no policies
    // internally, we are just going to add them all in from the db
    private boolean updateGroupPoliciesInFileSystem(OnapPDPGroup pdpGroup, OnapPDPGroup oldPdpGroup,
            GroupEntity groupRecord, PolicyDBDaoTransaction transaction) throws PAPException, PolicyDBException {
        if (!(pdpGroup instanceof StdPDPGroup)) {
            throw new PAPException("group is not a StdPDPGroup");
        }
        StdPDPGroup group = (StdPDPGroup) pdpGroup;
        // this must always be true since we don't explicitly know when a delete
        // is occuring
        boolean didUpdate = true;
        HashMap<String, PDPPolicy> currentPolicySet = new HashMap<>(oldPdpGroup.getPolicies().size());
        HashSet<PDPPolicy> newPolicySet = new HashSet<>();
        for (PDPPolicy pdpPolicy : oldPdpGroup.getPolicies()) {
            currentPolicySet.put(pdpPolicy.getId(), pdpPolicy);
        }
        for (PolicyEntity policy : groupRecord.getPolicies()) {
            String pdpPolicyName =
                    PolicyDBDao.getPolicyDBDaoInstance().getPdpPolicyName(policy.getPolicyName(), policy.getScope());
            if (group.getPolicy(pdpPolicyName) == null) {
                didUpdate = true;
                if (currentPolicySet.containsKey(pdpPolicyName)) {
                    newPolicySet.add(currentPolicySet.get(pdpPolicyName));
                } else {
                    logger.info(
                            "PolicyDBDao: Adding the new policy to the PDP group after notification: " + pdpPolicyName);
                    InputStream policyStream = new ByteArrayInputStream(policy.getPolicyData().getBytes());
                    group.copyPolicyToFile(pdpPolicyName, policyStream);
                    ((StdPDPPolicy) (group.getPolicy(pdpPolicyName))).setName(PolicyDBDao.getPolicyDBDaoInstance()
                            .removeExtensionAndVersionFromPolicyName(pdpPolicyName));
                    try {
                        policyStream.close();
                    } catch (IOException e) {
                        didUpdate = false;
                        PolicyLogger.error(e.getMessage() + e);
                    }
                }
            }
        }
        logger.info("PolicyDBDao: Adding updated policies to group after notification.");
        if (didUpdate) {
            newPolicySet.addAll(group.getPolicies());
            group.setPolicies(newPolicySet);
        }
        return didUpdate;
    }

    private void handleIncomingPdpChange(String pdpId, PolicyDBDaoTransaction transaction) throws PAPException {
        // get pdp
        long pdpIdLong = -1;
        try {
            pdpIdLong = Long.parseLong(pdpId);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("pdpId " + pdpId + " cannot be parsed into a long");
        }
        PdpEntity pdpRecord = null;
        try {
            pdpRecord = transaction.getPdp(pdpIdLong);
        } catch (Exception e) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                    "Caught Exception trying to get pdp record with transaction.getPdp(" + pdpIdLong + ");");
            throw new PAPException("Could not get local pdp " + pdpIdLong);
        }
        if (pdpRecord == null) {
            throw new PersistenceException("The pdpRecord returned is null");
        }
        OnapPDP localPdp = null;
        try {
            localPdp = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getPDP(pdpRecord.getPdpId());
        } catch (PAPException e) {
            logger.warn("Caught PAPException trying to get local pdp  with papEngine.getPDP(" + pdpId + ");", e);
        }
        if (localPdp != null && pdpRecord.isDeleted()) {
            try {
                PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().removePDP(localPdp);
            } catch (PAPException e) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                        "Caught PAPException trying to get remove pdp with papEngine.removePDP(" + localPdp + ");");
                throw new PAPException("Could not remove pdp " + pdpId);
            }
        } else if (localPdp == null) {
            // add new pdp
            // get group
            OnapPDPGroup localGroup = null;
            try {
                localGroup =
                        PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getGroup(pdpRecord.getGroup().getGroupId());
            } catch (PAPException e1) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, PolicyDBDao.POLICYDBDAO_VAR,
                        "Caught PAPException trying to get local group to add pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());");
                throw new PAPException("Could not get local group");
            }
            try {
                PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().newPDP(pdpRecord.getPdpId(), localGroup,
                        pdpRecord.getPdpName(), pdpRecord.getDescription(), pdpRecord.getJmxPort());
            } catch (NullPointerException | PAPException e) {
                PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                        "Caught PAPException trying to create pdp with papEngine.newPDP(" + pdpRecord.getPdpId() + ", "
                                + localGroup + ", " + pdpRecord.getPdpName() + ", " + pdpRecord.getDescription() + ", "
                                + pdpRecord.getJmxPort() + ");");
                throw new PAPException("Could not create pdp " + pdpRecord);
            }
        } else {
            boolean needToUpdate = false;
            if (!PolicyDBDao.stringEquals(localPdp.getId(), pdpRecord.getPdpId())
                    || !PolicyDBDao.stringEquals(localPdp.getName(), pdpRecord.getPdpName())) {
                // again, we don't want to change the id, the papEngine will do
                // this
                localPdp.setName(pdpRecord.getPdpName());
                needToUpdate = true;
            }
            if (!PolicyDBDao.stringEquals(localPdp.getDescription(), pdpRecord.getDescription())) {
                localPdp.setDescription(pdpRecord.getDescription());
                needToUpdate = true;
            }
            String localPdpGroupId = null;
            try {
                localPdpGroupId = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().getPDPGroup(localPdp).getId();
            } catch (PAPException e) {
                // could be null or something, just warn at this point
                logger.warn(
                        "Caught PAPException trying to get id of local group that pdp is in with localPdpGroupId = papEngine.getPDPGroup(localPdp).getId();",
                        e);
            }
            if (!PolicyDBDao.stringEquals(localPdpGroupId, pdpRecord.getGroup().getGroupId())) {
                OnapPDPGroup newPdpGroup = null;
                try {
                    newPdpGroup = PolicyDBDao.getPolicyDBDaoInstance().getPapEngine()
                            .getGroup(pdpRecord.getGroup().getGroupId());
                } catch (PAPException e) {
                    // ok, now we have an issue. Time to stop things
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to get id of local group to move pdp to with papEngine.getGroup(pdpRecord.getGroup().getGroupId());");
                    throw new PAPException("Could not get local group");
                }
                try {
                    PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().movePDP(localPdp, newPdpGroup);
                } catch (PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to move pdp with papEngine.movePDP(localPdp, newPdpGroup);");
                    throw new PAPException("Could not move pdp " + localPdp);
                }
            }
            if (localPdp.getJmxPort() != pdpRecord.getJmxPort()) {
                localPdp.setJmxPort(pdpRecord.getJmxPort());
                needToUpdate = true;
            }
            if (needToUpdate) {
                try {
                    PolicyDBDao.getPolicyDBDaoInstance().getPapEngine().updatePDP(localPdp);
                } catch (PAPException e) {
                    PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                            "Caught PAPException trying to update pdp with papEngine.updatePdp(" + localPdp + ");");
                    throw new PAPException("Could not update pdp " + localPdp);
                }
            }
        }
        // compare to local situation
        // call command to update
    }

    private void handleIncomingPolicyChange(String policyId) {
        String policyName = null;
        Session session = sessionfactory.openSession();
        Query getPolicyEntityQuery = session.getNamedQuery("PolicyEntity.FindById");
        getPolicyEntityQuery.setParameter("id", Long.valueOf(policyId));

        @SuppressWarnings("unchecked")
        List<PolicyEntity> policies = getPolicyEntityQuery.list();
        PolicyEntity policy = null;
        if (!policies.isEmpty()) {
            policy = policies.get(0);
        }
        String action = "unknown action";
        try {
            if (policy != null) {
                policyName = policy.getPolicyName();
                logger.info("Deleting old Policy Config File for " + policy.getPolicyName());
                action = "delete";
                Path subFile = null;

                if (policy.getConfigurationData() != null) {
                    subFile =
                            getPolicySubFile(policy.getConfigurationData().getConfigurationName(), PolicyDBDao.CONFIG);
                } else if (policy.getActionBodyEntity() != null) {
                    subFile = getPolicySubFile(policy.getActionBodyEntity().getActionBodyName(), PolicyDBDao.ACTION);
                }

                if (subFile != null) {
                    Files.deleteIfExists(subFile);
                }
                if (policy.getConfigurationData() != null) {
                    writePolicySubFile(policy, PolicyDBDao.CONFIG);
                } else if (policy.getActionBodyEntity() != null) {
                    writePolicySubFile(policy, action);
                }
            }
        } catch (IOException e1) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, PolicyDBDao.POLICYDBDAO_VAR,
                    "Error occurred while performing [" + action + "] of Policy File: " + policyName);
        } finally {
            session.close();
        }
    }

    private boolean writePolicySubFile(PolicyEntity policy, String policyType) {
        logger.info("writePolicySubFile with policyName[" + policy.getPolicyName() + "] and policyType[" + policyType
                + "]");
        String type = null;
        String subTypeName = null;
        String subTypeBody = null;
        if (PolicyDBDao.CONFIG.equalsIgnoreCase(policyType)) {
            type = PolicyDBDao.CONFIG;
            subTypeName = FilenameUtils.removeExtension(policy.getConfigurationData().getConfigurationName());
            subTypeBody = policy.getConfigurationData().getConfigBody();

            String configType = policy.getConfigurationData().getConfigType();

            if (configType != null) {
                if (configType.equals(JSON_CONFIG)) {
                    subTypeName = subTypeName + ".json";
                }
                if (configType.equals(XML_CONFIG)) {
                    subTypeName = subTypeName + ".xml";
                }
                if (configType.equals(PROPERTIES_CONFIG)) {
                    subTypeName = subTypeName + ".properties";
                }
                if (configType.equals(OTHER_CONFIG)) {
                    subTypeName = subTypeName + ".txt";
                }
            }
        } else if (PolicyDBDao.ACTION.equalsIgnoreCase(policyType)) {
            type = PolicyDBDao.ACTION;
            subTypeName = policy.getActionBodyEntity().getActionBodyName();
            subTypeBody = policy.getActionBodyEntity().getActionBody();
        }
        Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS).toString(), type);

        if (subTypeBody == null) {
            subTypeBody = "";
        }
        boolean success = false;
        try {
            Files.deleteIfExists(Paths.get(filePath.toString(), subTypeName));
            File file = Paths.get(filePath.toString(), subTypeName).toFile();
            boolean value = file.createNewFile();
            logger.debug("New file created successfully" + value);
            try (FileWriter fileWriter = new FileWriter(file, false)) {
                // false to overwrite
                fileWriter.write(subTypeBody);
                fileWriter.close();
                success = true;
            }
        } catch (Exception e) {
            PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, PolicyDBDao.POLICYDBDAO_VAR,
                    "Exception occured while creating Configuration File for Policy : " + policy.getPolicyName());
        }
        return success;
    }

    Path getPolicySubFile(String inputFileName, String subFileType) {
        String filename = inputFileName;
        logger.info("getPolicySubFile(" + filename + ", " + subFileType + ")");
        Path filePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS), subFileType);
        File file = null;

        filename = FilenameUtils.removeExtension(filename);

        for (File tmpFile : filePath.toFile().listFiles()) {
            if (FilenameUtils.removeExtension(tmpFile.getName()).equals(filename)) {
                file = tmpFile;
            }
        }

        Path finalPath = null;
        if (file != null) {
            finalPath = Paths.get(file.getAbsolutePath());
        }

        logger.info("end of getPolicySubFile: " + finalPath);
        return finalPath;
    }
}