aboutsummaryrefslogtreecommitdiffstats
path: root/PolicyEngineAPI/src/main/java/org/onap/policy/std/StdPolicyEngine.java
blob: a4c1e985298a1fed97be6916222d6022ccbddc31 (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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
/*-
 * ============LICENSE_START=======================================================
 * PolicyEngineAPI
 * ================================================================================
 * 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.std;

import static org.onap.policy.std.utils.PolicyConfigConstants.BAD_REQUEST_STATUS_CODE;
import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_ID_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.CLIENT_KEY_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.COMMA;
import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_DICTIONARY_ITEM_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.CREATE_POLICY_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.DATE_FORMAT;
import static org.onap.policy.std.utils.PolicyConfigConstants.DEFAULT_NOTIFICATION;
import static org.onap.policy.std.utils.PolicyConfigConstants.DELETE_POLICY_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.DMAAP;
import static org.onap.policy.std.utils.PolicyConfigConstants.ENVIRONMENT_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.ERROR_AUTH_GET_PERM;
import static org.onap.policy.std.utils.PolicyConfigConstants.ERROR_DATA_ISSUE;
import static org.onap.policy.std.utils.PolicyConfigConstants.ERROR_INVALID_PDPS;
import static org.onap.policy.std.utils.PolicyConfigConstants.ERROR_WHILE_CONNECTING;
import static org.onap.policy.std.utils.PolicyConfigConstants.GET_CONFIG_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DECISION_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.GET_DICTIONARY_ITEMS_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.GET_METRICS_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.HTTP;
import static org.onap.policy.std.utils.PolicyConfigConstants.JUNIT_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_SERVERS_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TOPIC_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.NOTIFICATION_TYPE_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.PDP_URL_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.PDP_VALUE_REGEX;
import static org.onap.policy.std.utils.PolicyConfigConstants.PE300;
import static org.onap.policy.std.utils.PolicyConfigConstants.POLICY_ENGINE_IMPORT_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.PUSH_POLICY_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.REGEX;
import static org.onap.policy.std.utils.PolicyConfigConstants.SEMICOLLON;
import static org.onap.policy.std.utils.PolicyConfigConstants.SEND_EVENT_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.TEST_POLICY_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.UEB;
import static org.onap.policy.std.utils.PolicyConfigConstants.UEB_API_KEY_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.UEB_API_SECRET_PROP_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.UNAUTHORIZED_STATUS_CODE;
import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_DICTIONARY_ITEM_RESOURCE_NAME;
import static org.onap.policy.std.utils.PolicyConfigConstants.UPDATE_POLICY_RESOURCE_NAME;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.onap.policy.api.AttributeType;
import org.onap.policy.api.ConfigNameRequest;
import org.onap.policy.api.ConfigRequestParameters;
import org.onap.policy.api.DecisionRequestParameters;
import org.onap.policy.api.DecisionResponse;
import org.onap.policy.api.DeletePolicyParameters;
import org.onap.policy.api.DictionaryParameters;
import org.onap.policy.api.DictionaryResponse;
import org.onap.policy.api.EventRequestParameters;
import org.onap.policy.api.ImportParameters;
import org.onap.policy.api.MetricsRequestParameters;
import org.onap.policy.api.MetricsResponse;
import org.onap.policy.api.NotificationHandler;
import org.onap.policy.api.NotificationScheme;
import org.onap.policy.api.PDPNotification;
import org.onap.policy.api.PolicyChangeResponse;
import org.onap.policy.api.PolicyClass;
import org.onap.policy.api.PolicyConfig;
import org.onap.policy.api.PolicyConfigException;
import org.onap.policy.api.PolicyConfigType;
import org.onap.policy.api.PolicyDecisionException;
import org.onap.policy.api.PolicyEngineException;
import org.onap.policy.api.PolicyEventException;
import org.onap.policy.api.PolicyException;
import org.onap.policy.api.PolicyParameters;
import org.onap.policy.api.PolicyResponse;
import org.onap.policy.api.PolicyType;
import org.onap.policy.api.PushPolicyParameters;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.models.APIDictionaryResponse;
import org.onap.policy.models.APIPolicyConfigResponse;
import org.onap.policy.std.utils.PolicyCommonConfigConstants;
import org.onap.policy.utils.AAFEnvironment;
import org.onap.policy.utils.PeCryptoUtils;
import org.onap.policy.utils.PolicyUtils;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.xml.sax.InputSource;

/**
 * PolicyEngine Implementation class
 *
 * @version 1.0
 */
public class StdPolicyEngine {

    private static String clientEncoding = null;
    private String contentType = null;
    private static List<String> pdps = null;
    private static String environment = null;
    private static String userName = null;
    private static String pass = null;
    private static List<String> encoding = null;
    private static boolean junit = false;
    private List<String> notificationType = new ArrayList<>();
    private List<String> notificationURLList = new ArrayList<>();
    private NotificationScheme scheme = null;
    private NotificationHandler handler = null;
    private AutoClientUEB uebClientThread = null;
    private Thread registerUEBThread = null;
    private boolean uebThread = false;
    private AutoClientDMAAP dmaapClientThread = null;
    private Thread registerDMAAPThread = null;
    private boolean dmaapThread = false;
    private String topic = null;
    private String apiKey = null;
    private String apiSecret = null;

    private static final String UNIQUEID = UUID.randomUUID().toString();
    private static final Logger LOGGER = FlexLogger.getLogger(StdPolicyConfig.class.getName());
    private static final String LIST_POLICY_RESOURCE_NAME = "listPolicy";

    /*
     * Taking the Property file even if it null.
     */
    public StdPolicyEngine(final String propertyFilePath, final String clientKey) throws PolicyEngineException {
        setProperty(propertyFilePath, clientKey);
    }

    /*
     * Taking the Property structure even if it null.
     */
    public StdPolicyEngine(final Properties properties, final String clientKey) throws PolicyEngineException {
        setProperty(properties, clientKey);
    }

    /*
     * Taking the Notification Constructor.
     */
    public StdPolicyEngine(final String propertyFilePath, final NotificationScheme scheme,
            final NotificationHandler handler) throws PolicyEngineException {
        setProperty(propertyFilePath, null);
        this.scheme = scheme;
        this.handler = handler;
        if ((!UEB.equals(notificationType.get(0))) || (!DMAAP.equals(notificationType.get(0)))) {
            AutoClientEnd.setAuto(scheme, handler);
        }
        notification(scheme, handler);
    }

    /*
     * Taking the Notification Constructor.
     */
    public StdPolicyEngine(final String propertyFilePath, final NotificationScheme scheme)
            throws PolicyEngineException {
        setProperty(propertyFilePath, null);
        this.scheme = scheme;
        setScheme(scheme);
    }

    /*
     * sendEvent API Implementation
     */
    public Collection<PolicyResponse> sendEvent(final Map<String, String> eventAttributes, final UUID requestID)
            throws PolicyEventException {
        return sendEventImpl(eventAttributes, requestID);
    }

    /*
     * sendEvent API Implementation for eventRequestParameters
     */
    public Collection<PolicyResponse> sendEvent(final EventRequestParameters eventRequestParameters)
            throws PolicyEventException {
        if (eventRequestParameters == null) {
            final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No event Request Parameters Given. ";
            LOGGER.error(message);
            throw new PolicyEventException(message);
        }
        return sendEventImpl(eventRequestParameters.getEventAttributes(), eventRequestParameters.getRequestID());
    }

    /*
     * getConfig using configRequestParameters Implementation
     */
    public Collection<PolicyConfig> getConfig(final ConfigRequestParameters configRequestParameters)
            throws PolicyConfigException {
        return getConfigImpl(configRequestParameters);
    }

    /*
     * listPolicies using configRequestParameters Implementation
     */
    public Collection<String> listConfig(final ConfigRequestParameters listPolicyRequestParameters)
            throws PolicyConfigException {
        return listConfigImpl(listPolicyRequestParameters);
    }

    /*
     * listPolicies from PDP using ConfigNameRequest Implementation
     */
    public Collection<String> listPolicy(final ConfigNameRequest listPolicyRequestParameters)
            throws PolicyConfigException {
        return listPolicyImpl(listPolicyRequestParameters);
    }

    /*
     * getDecision using the decision Attributes.
     */
    public DecisionResponse getDecision(final String onapName, final Map<String, String> decisionAttributes,
            final UUID requestID) throws PolicyDecisionException {
        return getDecisionImpl(onapName, decisionAttributes, requestID);
    }

    /*
     * getDecision Using decisionRequestParameters.
     */
    public DecisionResponse getDecision(final DecisionRequestParameters decisionRequestParameters)
            throws PolicyDecisionException {
        if (decisionRequestParameters == null) {
            final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Decision Request Parameters Given. ";
            LOGGER.error(message);
            throw new PolicyDecisionException(message);
        }
        return getDecisionImpl(decisionRequestParameters.getOnapName(),
                decisionRequestParameters.getDecisionAttributes(), decisionRequestParameters.getRequestID());
    }

    /*
     * getMetrics using metricsRequestParameters
     */
    public MetricsResponse getMetrics(final MetricsRequestParameters parameters) throws PolicyException {
        return getMetricsImpl(parameters);
    }

    public MetricsResponse getMetricsImpl(final MetricsRequestParameters parameters) throws PolicyException {
        String body = null;
        // Create the Request
        try {
            if (parameters != null) {
                body = PolicyUtils.objectToJsonString(parameters);
            }
        } catch (final JsonProcessingException jsonProcessingException) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + jsonProcessingException;
            LOGGER.error(message);
            throw new PolicyException(message, jsonProcessingException);
        }

        final StdMetricsResponse response = new StdMetricsResponse();
        // Get Response.
        try {
            final ResponseEntity<String> result =
                    callNewPDP(GET_METRICS_RESOURCE_NAME, HttpMethod.GET, body, String.class);
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
            return response;
        } catch (final PolicyException exception) {
            if (exception.getCause() != null && exception.getCause() instanceof HttpClientErrorException) {
                LOGGER.error(exception);
                final HttpClientErrorException ex = (HttpClientErrorException) exception.getCause();
                response.setResponseCode(ex.getRawStatusCode());
                response.setResponseMessage(exception.getMessage());
                return response;
            }
            final String message =
                    XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error while processing results. please check logs.";
            LOGGER.error(message, exception);
            throw new PolicyException(message, exception);
        }
    }

    /*
     * PushPolicy using pushPolicyParameters.
     */
    public PolicyChangeResponse pushPolicy(final PushPolicyParameters pushPolicyParameters) throws PolicyException {
        return pushPolicyImpl(pushPolicyParameters);
    }

    public PolicyChangeResponse pushPolicyImpl(final PushPolicyParameters pushPolicyParameters) throws PolicyException {
        final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(pushPolicyParameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<String> result =
                    callNewPDP(PUSH_POLICY_RESOURCE_NAME, HttpMethod.PUT, body, String.class);
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
            return response;
        } catch (final PolicyException exception) {
            return processException(exception);
        }
    }

    /*
     * Delete a Policy using deletePolicyParameters
     */
    public PolicyChangeResponse deletePolicy(final DeletePolicyParameters parameters) throws PolicyException {
        return deletePolicyImpl(parameters);
    }

    private PolicyChangeResponse deletePolicyImpl(final DeletePolicyParameters parameters) throws PolicyException {
        final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(parameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<String> result =
                    callNewPDP(DELETE_POLICY_RESOURCE_NAME, HttpMethod.DELETE, body, String.class);
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
        } catch (final PolicyException exception) {
            return processException(exception);
        }
        return response;
    }

    /*
     * getDictionaryItem Using dictionaryParameters
     */
    public DictionaryResponse getDictionaryItem(final DictionaryParameters parameters) throws PolicyException {
        return getDictionaryItemImpl(parameters);
    }

    private DictionaryResponse getDictionaryItemImpl(final DictionaryParameters parameters) throws PolicyException {
        final StdDictionaryResponse response = new StdDictionaryResponse();
        String body = "{}";
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(parameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<APIDictionaryResponse> result =
                    callNewPDP(GET_DICTIONARY_ITEMS_RESOURCE_NAME, HttpMethod.POST, body, APIDictionaryResponse.class);
            // Process response
            return dictionaryResult(result.getBody());
        } catch (final Exception exception) {
            if (exception.getCause().getMessage().contains(UNAUTHORIZED_STATUS_CODE)) {
                final String message = XACMLErrorConstants.ERROR_PERMISSIONS + ERROR_AUTH_GET_PERM
                        + GET_DICTIONARY_ITEMS_RESOURCE_NAME;
                LOGGER.error(message);
                response.setResponseMessage(message);
                response.setResponseCode(HttpStatus.UNAUTHORIZED.value());
                return response;
            }
            if (exception.getCause().getMessage().contains(BAD_REQUEST_STATUS_CODE)) {
                final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + ERROR_DATA_ISSUE;
                response.setResponseMessage(message);
                response.setResponseCode(HttpStatus.BAD_REQUEST.value());
                return response;
            }
            final String message = XACMLErrorConstants.ERROR_PERMISSIONS + ERROR_INVALID_PDPS + pdps;
            LOGGER.error(message, exception);
            response.setResponseMessage(message);
            response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return response;
        }
    }

    @SuppressWarnings("unchecked")
    private StdDictionaryResponse dictionaryResult(final APIDictionaryResponse body) {
        final StdDictionaryResponse response = new StdDictionaryResponse();
        response.setResponseCode(body.getResponseCode());
        response.setResponseMessage(body.getResponseMessage());
        response.setDictionaryData((Map<String, String>) body.getDictionaryData());
        if (body.getDictionaryJson() != null) {
            final Gson objGson = new GsonBuilder().create();
            final String mapToJson = objGson.toJson(body.getDictionaryJson());
            try (final JsonReader jsonReader = Json.createReader(new StringReader(mapToJson));) {
                final JsonObject object = jsonReader.readObject();
                response.setDictionaryJson(object);
            }
        }
        return response;
    }

    /*
     * createDictinaryItem Using dictionaryParameters.
     */
    public PolicyChangeResponse createDictionaryItem(final DictionaryParameters parameters) throws PolicyException {
        return createUpdateDictionaryItemImpl(parameters, false);
    }

    /*
     * updateDictinaryItem Using dictionaryParameters.
     */
    public PolicyChangeResponse updateDictionaryItem(final DictionaryParameters parameters) throws PolicyException {
        return createUpdateDictionaryItemImpl(parameters, true);
    }

    private PolicyChangeResponse createUpdateDictionaryItemImpl(final DictionaryParameters parameters,
            final boolean updateFlag) throws PolicyException {

        final String resource = getDictionaryResouceName(updateFlag);
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(parameters);
        } catch (final JsonProcessingException jsonProcessingException) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + jsonProcessingException;
            LOGGER.error(message);
            throw new PolicyException(message, jsonProcessingException);
        }
        // Get Response.
        try {
            final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
            final ResponseEntity<String> result = callNewPDP(resource, HttpMethod.PUT, body, String.class);
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
            return response;
        } catch (final PolicyException exception) {
            return processException(exception);
        }
    }

    private String getDictionaryResouceName(final boolean updateFlag) {
        return updateFlag ? UPDATE_DICTIONARY_ITEM_RESOURCE_NAME : CREATE_DICTIONARY_ITEM_RESOURCE_NAME;
    }

    /*
     * PolicyEngine Import
     */
    public PolicyChangeResponse policyEngineImport(final ImportParameters importParameters) throws PolicyException {
        return policyEngineImportImpl(importParameters);
    }

    private PolicyChangeResponse policyEngineImportImpl(final ImportParameters importParameters)
            throws PolicyException {
        final LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
        // Create Request.
        try {
            final String body = PolicyUtils.objectToJsonString(importParameters);
            parameters.set("importParametersJson", body);
            parameters.set("file", new FileSystemResource(importParameters.getFilePath()));
        } catch (final Exception e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyException(message, e);
        }
        contentType = MediaType.MULTIPART_FORM_DATA_VALUE;
        // Get Response.
        try {
            final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
            final ResponseEntity<String> result =
                    callNewPDP(POLICY_ENGINE_IMPORT_RESOURCE_NAME, HttpMethod.POST, parameters, String.class);
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
            return response;
        } catch (final PolicyException exception) {
            return processException(exception);
        } finally {
            contentType = null;
        }
    }

    /*
     * createPolicy Using policyParameters.
     */
    public PolicyChangeResponse createPolicy(final PolicyParameters policyParameters) throws PolicyException {
        return createUpdatePolicyImpl(policyParameters, false);
    }

    /*
     * updatePolicy using policyParameters.
     */
    public PolicyChangeResponse updatePolicy(final PolicyParameters policyParameters) throws PolicyException {
        return createUpdatePolicyImpl(policyParameters, true);
    }

    private PolicyChangeResponse createUpdatePolicyImpl(final PolicyParameters policyParameters,
            final boolean updateFlag) throws PolicyException {
        final String resource = getPolicyResourceName(updateFlag);
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(policyParameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<String> result = callNewPDP(resource, HttpMethod.PUT, body, String.class);
            final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
            // Process response
            response.setResponseMessage(result.getBody());
            response.setResponseCode(result.getStatusCode().value());
            return response;
        } catch (final PolicyException exception) {
            return processException(exception);
        }
    }

    private String getPolicyResourceName(final boolean updateFlag) {
        return updateFlag ? UPDATE_POLICY_RESOURCE_NAME : CREATE_POLICY_RESOURCE_NAME;
    }

    private PolicyChangeResponse processException(final PolicyException exception) throws PolicyException {
        final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
        if (exception.getCause() != null && exception.getCause() instanceof HttpClientErrorException) {
            LOGGER.error(exception);
            final HttpClientErrorException ex = (HttpClientErrorException) exception.getCause();
            response.setResponseCode(ex.getRawStatusCode());
            response.setResponseMessage(exception.getMessage());
            return response;
        } else {
            final String message =
                    XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Error while processing results. please check logs.";
            LOGGER.error(message, exception);
            throw new PolicyException(message, exception);
        }
    }

    private DecisionResponse getDecisionImpl(final String onapName, final Map<String, String> decisionAttributes,
            final UUID requestID) throws PolicyDecisionException {
        String body = null;
        // Create Request.
        try {
            final DecisionRequestParameters decisionRequestParameters = new DecisionRequestParameters();
            decisionRequestParameters.setDecisionAttributes(decisionAttributes);
            decisionRequestParameters.setOnapName(onapName);
            decisionRequestParameters.setRequestID(requestID);
            body = PolicyUtils.objectToJsonString(decisionRequestParameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyDecisionException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<StdDecisionResponse> result =
                    callNewPDP(GET_DECISION_RESOURCE_NAME, HttpMethod.POST, body, StdDecisionResponse.class);
            // Process response
            return result.getBody();
        } catch (final Exception exception) {
            final String defaulMessage = XACMLErrorConstants.ERROR_PERMISSIONS + ERROR_INVALID_PDPS + pdps;
            final String message = getErrorMessage(exception, defaulMessage, GET_DECISION_RESOURCE_NAME);
            LOGGER.error(message, exception);
            throw new PolicyDecisionException(message, exception);
        }
    }

    private Collection<PolicyConfig> getConfigImpl(final ConfigRequestParameters configRequestParameters)
            throws PolicyConfigException {
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(configRequestParameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyConfigException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<APIPolicyConfigResponse[]> result =
                    callNewPDP(GET_CONFIG_RESOURCE_NAME, HttpMethod.POST, body, APIPolicyConfigResponse[].class);
            // Process Response
            return configResult(result.getBody());
        } catch (final Exception exception) {
            final String defaulMessage = XACMLErrorConstants.ERROR_PROCESS_FLOW + ERROR_INVALID_PDPS + pdps;
            final String message = getErrorMessage(exception, defaulMessage, GET_CONFIG_RESOURCE_NAME);
            LOGGER.error(message, exception);
            throw new PolicyConfigException(message, exception);
        }
    }

    private ArrayList<PolicyConfig> configResult(final APIPolicyConfigResponse[] response)
            throws PolicyConfigException {
        final ArrayList<PolicyConfig> result = new ArrayList<>();
        if (response == null) {
            return result;
        }

        for (final APIPolicyConfigResponse policyConfigResponse : response) {
            final StdPolicyConfig policyConfig = new StdPolicyConfig();
            policyConfig.setConfigStatus(policyConfigResponse.getPolicyConfigMessage());
            policyConfig.setMatchingConditions(policyConfigResponse.getMatchingConditions());
            policyConfig.setPolicyConfigStatus(policyConfigResponse.getPolicyConfigStatus());
            policyConfig.setPolicyName(policyConfigResponse.getPolicyName());
            policyConfig.setPolicyType(policyConfigResponse.getType());
            policyConfig.setPolicyVersion(policyConfigResponse.getPolicyVersion());
            policyConfig.setPolicyType(policyConfigResponse.getPolicyType());
            policyConfig.setResponseAttributes(policyConfigResponse.getResponseAttributes());
            setMatches(policyConfig.getMatchingConditions());
            if (policyConfigResponse.getType() != null) {
                try {
                    switch (policyConfigResponse.getType()) {
                        case JSON:
                            final StringReader reader = new StringReader(policyConfigResponse.getConfig());
                            try (final JsonReader jsonReader = Json.createReader(reader)) {
                                final JsonObject object = jsonReader.readObject();
                                policyConfig.setJsonObject(object);
                            }
                            break;
                        case OTHER:
                            policyConfig.setOther(policyConfigResponse.getConfig());
                            break;
                        case PROPERTIES:
                            final Properties props = new Properties();
                            props.putAll(policyConfigResponse.getProperty());
                            policyConfig.setProperties(props);
                            break;
                        case XML:
                            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                            final DocumentBuilder builder = factory.newDocumentBuilder();
                            final StringReader stringReader = new StringReader(policyConfigResponse.getConfig());
                            policyConfig.setDocument(builder.parse(new InputSource(stringReader)));
                            break;
                    }
                } catch (final Exception exception) {
                    LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + exception);
                    throw new PolicyConfigException(
                            XACMLErrorConstants.ERROR_SCHEMA_INVALID + "Unable to parse the config", exception);
                }
            }
            result.add(policyConfig);
        }
        return result;
    }

    private void setMatches(final Map<String, String> matchingConditions) {
        final Matches match = new Matches();
        final HashMap<String, String> configAttributes = new HashMap<>();
        try {
            for (final Map.Entry<String, String> entry : matchingConditions.entrySet()) {
                if (PolicyCommonConfigConstants.ONAP_NAME.equalsIgnoreCase(entry.getKey())) {
                    match.setOnapName(entry.getValue());
                } else if (PolicyCommonConfigConstants.CONFIG_NAME.equalsIgnoreCase(entry.getKey())) {
                    match.setConfigName(entry.getValue());
                } else {
                    configAttributes.put(entry.getKey(), entry.getValue());
                }
            }
            if (!configAttributes.isEmpty()) {
                match.setConfigAttributes(configAttributes);
            }
            MatchStore.storeMatch(match);
        } catch (final Exception e) {
            LOGGER.error("StoreMatch failed for Onap:" + match.getOnapName() + " Config: " + match.getConfigName(), e);
        }
    }

    /*
     * Generic Rest Client to call PDP services.
     */
    <T> ResponseEntity<T> callNewPDP(final String resource, final HttpMethod method, final Object body,
            final Class<T> responseType) throws PolicyException {
        final RestTemplate restTemplate = new RestTemplate();
        final HttpEntity<?> requestEntity = new HttpEntity<>(body, getHeaders());
        ResponseEntity<T> result = null;
        HttpClientErrorException exception = null;
        int pdpsCount = 0;
        while (pdpsCount < pdps.size()) {
            try {
                result = restTemplate.exchange(pdps.get(0) + "/api/" + resource, method, requestEntity, responseType);
            } catch (final HttpClientErrorException e) {
                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + ERROR_WHILE_CONNECTING + pdps.get(0), e);
                exception = e;
            } catch (final Exception e) {
                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + ERROR_WHILE_CONNECTING + pdps.get(0), e);
                exception = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
            }
            if (result == null) {
                Collections.rotate(pdps, -1);
                Collections.rotate(encoding, -1);
                pdpsCount++;
            } else {
                break;
            }
        }
        if (exception == null || exception.getStatusCode() == null) {
            return result;
        }

        final String message = getHttpErrorMessage(exception, resource);
        LOGGER.error(message);
        throw new PolicyException(message, exception);
    }

    private String getHttpErrorMessage(final HttpClientErrorException exception, final String resource) {
        if (exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
            return XACMLErrorConstants.ERROR_PERMISSIONS + ":" + exception.getStatusCode() + ":" + ERROR_AUTH_GET_PERM
                    + resource;
        }
        if (exception.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
            return XACMLErrorConstants.ERROR_DATA_ISSUE + ":" + exception.getStatusCode() + ":"
                    + exception.getResponseBodyAsString();
        }
        if (exception.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
            return XACMLErrorConstants.ERROR_PROCESS_FLOW + ERROR_WHILE_CONNECTING + pdps + exception;
        }
        return XACMLErrorConstants.ERROR_PROCESS_FLOW + ":" + exception.getStatusCode() + ":"
                + exception.getResponseBodyAsString();
    }

    private HttpHeaders getHeaders() {
        final HttpHeaders headers = new HttpHeaders();
        headers.set("ClientAuth", "Basic " + clientEncoding);
        headers.set("Authorization", "Basic " + encoding.get(0));
        if (contentType != null) {
            headers.set("Content-Type", contentType);
        } else {
            headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        }
        headers.set("Environment", environment);
        return headers;
    }

    private static void setClientEncoding() {
        final Base64.Encoder encoder = Base64.getEncoder();
        clientEncoding = encoder.encodeToString((userName + ":" + pass).getBytes(StandardCharsets.UTF_8));
    }

    private Collection<String> listConfigImpl(final ConfigRequestParameters listRequestParameters)
            throws PolicyConfigException {
        final Collection<String> policyList = new ArrayList<>();
        if (junit) {
            policyList.add(TEST_POLICY_NAME);
            return policyList;
        }
        final Collection<PolicyConfig> policyConfig = getConfigImpl(listRequestParameters);
        for (final PolicyConfig policy : policyConfig) {
            if (policy.getPolicyConfigMessage() != null && policy.getPolicyConfigMessage().contains(PE300)) {
                policyList.add(policy.getPolicyConfigMessage());
            } else {
                policyList.add("Policy Name: " + policy.getPolicyName());
            }
        }
        return policyList;
    }

    public Collection<String> listPolicyImpl(final ConfigNameRequest listPolicyRequestParameters)
            throws PolicyConfigException {
        final Collection<String> policyList = new ArrayList<>();
        if (junit) {
            policyList.add(TEST_POLICY_NAME);
            return policyList;
        }
        String body = null;
        // Create Request.
        try {
            body = PolicyUtils.objectToJsonString(listPolicyRequestParameters);
        } catch (final JsonProcessingException e) {
            LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID, e);
            throw new PolicyConfigException(XACMLErrorConstants.ERROR_SCHEMA_INVALID, e);
        }
        // Get Response.
        try {
            final ResponseEntity<String[]> result =
                    callNewPDP(LIST_POLICY_RESOURCE_NAME, HttpMethod.POST, body, String[].class);
            for (final String policy : result.getBody()) {
                policyList.add(policy);
            }
        } catch (final Exception exception) {
            final String defaulMessage = XACMLErrorConstants.ERROR_PROCESS_FLOW + ERROR_INVALID_PDPS + pdps;
            final String message = getErrorMessage(exception, defaulMessage, LIST_POLICY_RESOURCE_NAME);
            LOGGER.error(message, exception);
            throw new PolicyConfigException(message, exception);
        }

        return policyList;
    }

    private Collection<PolicyResponse> sendEventImpl(final Map<String, String> eventAttributes, final UUID requestID)
            throws PolicyEventException {
        String body = null;
        // Create Request.
        try {
            // Long way here, can be shortened and will be done.
            final EventRequestParameters eventRequestParameters = new EventRequestParameters();
            eventRequestParameters.setEventAttributes(eventAttributes);
            eventRequestParameters.setRequestID(requestID);
            body = PolicyUtils.objectToJsonString(eventRequestParameters);
        } catch (final JsonProcessingException e) {
            final String message = XACMLErrorConstants.ERROR_SCHEMA_INVALID + e;
            LOGGER.error(message);
            throw new PolicyEventException(message, e);
        }
        // Get Response.
        try {
            final ResponseEntity<StdPolicyResponse[]> result =
                    callNewPDP(SEND_EVENT_RESOURCE_NAME, HttpMethod.POST, body, StdPolicyResponse[].class);
            // Process Response
            return eventResult(result.getBody());
        } catch (final Exception exception) {
            final String defaulMessage = XACMLErrorConstants.ERROR_PERMISSIONS + ERROR_INVALID_PDPS + pdps;
            final String message = getErrorMessage(exception, defaulMessage, SEND_EVENT_RESOURCE_NAME);
            LOGGER.error(message, exception);
            throw new PolicyEventException(message, exception);
        }
    }

    private String getErrorMessage(final Exception exception, final String defaulMessage, final String resource) {
        final Throwable cause = exception.getCause();
        if (cause != null && cause.getMessage().contains(UNAUTHORIZED_STATUS_CODE)) {
            return XACMLErrorConstants.ERROR_PERMISSIONS + ERROR_AUTH_GET_PERM + resource;
        }
        if (cause != null && exception.getCause().getMessage().contains(BAD_REQUEST_STATUS_CODE)) {
            return XACMLErrorConstants.ERROR_DATA_ISSUE + ERROR_DATA_ISSUE;
        }
        return defaulMessage;
    }

    private ArrayList<PolicyResponse> eventResult(final StdPolicyResponse[] response) {
        final ArrayList<PolicyResponse> eventResult = new ArrayList<>();
        if (response != null && response.length > 0) {
            for (final StdPolicyResponse policyConfigResponse : response) {
                eventResult.add(policyConfigResponse);
            }
        }
        return eventResult;
    }

    private void setProperty(final String propertyFilePath, String clientKey) throws PolicyEngineException {
        if (propertyFilePath == null) {
            throw new PolicyEngineException(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "Error NO PropertyFile Path provided");
        }
        final Properties prop = getProperties(propertyFilePath);
        setProperty(prop, clientKey);
    }

    private void setProperty(final Properties properties, String clientKey) throws PolicyEngineException {
        if (properties == null) {
            throw new PolicyEngineException(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "NO properties provided, the value is NULL");
        }

        // UEB and DMAAP Settings
        final String notificationTypeValue = properties.getProperty(NOTIFICATION_TYPE_PROP_NAME);
        final String serverList = properties.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
        topic = properties.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
        apiKey = properties.getProperty(UEB_API_KEY_PROP_NAME);
        apiSecret = properties.getProperty(UEB_API_SECRET_PROP_NAME);

        setNotificationType(notificationTypeValue);

        if (serverList == null) {
            notificationType.clear();
            notificationType.add(DEFAULT_NOTIFICATION);
            LOGGER.info(
                    "Properties file doesn't have the NOTIFICATION_SERVERS parameter system will use defualt websockets");
        } else {
            notificationURLList = getPropertyValueAsList(serverList.trim());
        }

        if (topic != null) {
            topic = topic.trim();
        } else {
            LOGGER.error("Properties file doesn't have the NOTIFICATION_TOPIC parameter.");
        }

        // Client ID Authorization Settings.
        final String clientID = properties.getProperty(CLIENT_ID_PROP_NAME);
        if (clientKey == null) {
            clientKey = getClientKeyFromProperties(properties);
        }
        if (clientID == null || clientKey == null || clientID.isEmpty() || clientKey.isEmpty()) {
            LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
                    + " Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
            throw new PolicyEngineException(XACMLErrorConstants.ERROR_PERMISSIONS
                    + " Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
        } else {
            setClientId(clientID.trim());
            setClientKey(clientKey.trim());
        }
        setEnvironment(properties);
        // Initializing the values.
        init();
        readPdpProperites(properties);
        // Get JUNIT property from properties file when running tests
        checkJunit(properties);
    }

    private void readPdpProperites(final Properties prop) throws PolicyEngineException {
        // Check the Keys for PDP_URLs
        for (final String propertyKey : prop.stringPropertyNames()) {
            if (propertyKey.startsWith(PDP_URL_PROP_NAME)) {
                readPDPPropertyURL(prop, propertyKey);
            }
        }
        if (pdps == null || pdps.isEmpty()) {
            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
            throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
        }
    }

    private void readPDPPropertyURL(Properties prop, String propertyKey) throws PolicyEngineException {
        final String propertyValue = prop.getProperty(propertyKey);
        if (propertyValue == null) {
            throw new PolicyEngineException(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "Properties file doesn't have the PDP_URL parameter");
        }
        if (propertyValue.contains(SEMICOLLON)) {
            final List<String> pdpDefault = Arrays.asList(propertyValue.split(REGEX));
            for (final String pdpVal : pdpDefault) {
                readPDPParam(pdpVal);
            }
        } else {
            readPDPParam(propertyValue);
        }
    }

    private void setNotificationType(final String propertyValue) {
        if (propertyValue == null) {
            notificationType.add(DEFAULT_NOTIFICATION);
            LOGGER.info(
                    "Properties file doesn't have the NOTIFICATION_TYPE parameter system will use defualt websockets");
        } else {
            notificationType = getPropertyValueAsList(propertyValue.trim());
        }
    }

    private String getClientKeyFromProperties(final Properties prop) {
        final String clientKeyValue = PeCryptoUtils.decrypt(prop.getProperty(CLIENT_KEY_PROP_NAME));
        try {
            return PolicyUtils.decode(clientKeyValue);
        } catch (UnsupportedEncodingException | IllegalArgumentException e) {
            LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
                    + " Cannot Decode the given Password Proceeding with given Password!!", e);
        }
        return clientKeyValue;
    }

    private Properties getProperties(final String propertyFilePath) throws PolicyEngineException {
        // Adding logic for remote Properties file.
        if (propertyFilePath.startsWith(HTTP)) {
            return getRemoteProperties(propertyFilePath);
        }
        return getFileProperties(propertyFilePath);
    }

    private Properties getFileProperties(final String propertyFilePath) throws PolicyEngineException {
        final Path file = Paths.get(propertyFilePath);
        if (!file.toFile().exists()) {
            throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE
                    + "File doesn't exist in the specified Path " + file.toString());
        }
        if (file.toString().endsWith(".properties")) {
            try (BufferedReader bufferedReader = Files.newBufferedReader(file);) {
                final Properties prop = new Properties();
                prop.load(bufferedReader);
                return prop;
            } catch (final IOException exception) {
                LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + exception);
                throw new PolicyEngineException(
                        XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Load the Properties file", exception);
            }
        }
        LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + propertyFilePath);
        throw new PolicyEngineException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file");
    }

    private Properties getRemoteProperties(final String propertyFilePath) throws PolicyEngineException {
        try {
            final URL configURL = new URL(propertyFilePath);
            final URLConnection connection = configURL.openConnection();
            final Properties prop = new Properties();
            prop.load(connection.getInputStream());
            return prop;
        } catch (final IOException e) {
            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
            throw new PolicyEngineException(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "Maformed property URL " + e.getMessage());
        }
    }

    private List<String> getPropertyValueAsList(final String propertyValue) {
        if (propertyValue.contains(COMMA)) {
            return Arrays.asList(propertyValue.split(COMMA));
        }
        final List<String> valuesList = new ArrayList<>();
        valuesList.add(propertyValue);
        return valuesList;
    }

    private static void checkJunit(final Properties prop) {
        final String junitFlag = prop.getProperty(JUNIT_PROP_NAME);
        if (junitFlag == null || junitFlag.isEmpty()) {
            LOGGER.info("No JUNIT property provided, this will not be executed as a test.");
        } else {
            if ("test".equalsIgnoreCase(junitFlag)) {
                StdPolicyEngine.junit = true;
            } else {
                StdPolicyEngine.junit = false;
            }
        }
    }

    private static void init() {
        pdps = new ArrayList<>();
        encoding = new ArrayList<>();
    }

    private static void setEnvironment(final Properties prop) {
        environment = prop.getProperty(ENVIRONMENT_PROP_NAME, AAFEnvironment.DEVL.toString());
        if (environment.equalsIgnoreCase(AAFEnvironment.TEST.toString())) {
            environment = AAFEnvironment.TEST.toString();
        } else if (environment.equalsIgnoreCase(AAFEnvironment.PROD.toString())) {
            environment = AAFEnvironment.PROD.toString();
        } else {
            environment = AAFEnvironment.DEVL.toString();
        }
    }

    private static void setClientId(final String clientID) {
        userName = clientID;
    }

    /*
     * Read the PDP_URL parameter
     */
    private void readPDPParam(final String pdpVal) throws PolicyEngineException {
        if (pdpVal.contains(COMMA)) {
            final List<String> pdpValues = Arrays.asList(pdpVal.split(PDP_VALUE_REGEX));
            if (pdpValues.size() == 3) {
                // 0 - PDPURL
                pdps.add(pdpValues.get(0));
                // 1:2 will be UserID:Password
                final String userID = pdpValues.get(1);
                final String userPas = PeCryptoUtils.decrypt(pdpValues.get(2));
                final Base64.Encoder encoder = Base64.getEncoder();
                encoding.add(encoder.encodeToString((userID + ":" + userPas).getBytes(StandardCharsets.UTF_8)));
            } else {
                LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Credentials to send Request: " + pdpValues);
                throw new PolicyEngineException(
                        XACMLErrorConstants.ERROR_DATA_ISSUE + "No enough Credentials to send Request. " + pdpValues);
            }
        } else {
            LOGGER.error(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values: " + pdpVal);
            throw new PolicyEngineException(
                    XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values.");
        }
    }

    /*
     * Allowing changes to the scheme and Handler.
     */
    public void notification(final NotificationScheme scheme, final NotificationHandler handler) {
        this.scheme = scheme;
        this.handler = handler;
        LOGGER.debug("Scheme is : " + scheme.toString());
        LOGGER.debug("Handler is : " + handler.getClass().getName());

        if (UEB.equals(notificationType.get(0))) {
            if (this.uebThread) {
                AutoClientUEB.setAuto(scheme, handler);
                this.uebThread = registerUEBThread.isAlive();
            }
        } else if (notificationType.get(0).equals(DMAAP)) {
            if (this.dmaapThread) {
                AutoClientDMAAP.setAuto(scheme, handler);
                this.dmaapThread = registerDMAAPThread.isAlive();
            }
        } else {
            AutoClientEnd.setAuto(scheme, handler);
        }

        if (junit) {
            return;
        }
        if (pdps == null) {
            return;
        }

        if (UEB.equals(notificationType.get(0)) && !this.uebThread) {
            this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret);
            AutoClientUEB.setAuto(scheme, handler);
            this.registerUEBThread = new Thread(this.uebClientThread);
            this.registerUEBThread.start();
            this.uebThread = true;
        } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) {
            this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass);
            AutoClientDMAAP.setAuto(scheme, handler);
            this.registerDMAAPThread = new Thread(this.dmaapClientThread);
            this.registerDMAAPThread.start();
            this.dmaapThread = true;
        } else {
            if (pdps.get(0) != null) {
                if (AutoClientEnd.getUrl() == null) {
                    AutoClientEnd.start(pdps.get(0));
                } else {
                    AutoClientEnd.stop();
                    AutoClientEnd.start(pdps.get(0));
                }
            }
        }
    }

    /*
     * Gets the Notification if one exists. Used only for Manual Polling purposes.
     */
    public PDPNotification getNotification() {
        // Check if there is proper scheme..
        PDPNotification notification;
        if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)
                || this.scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
            if (UEB.equals(notificationType.get(0))) {
                ManualClientEndUEB.start(pdps.get(0), notificationURLList, UNIQUEID);
                notification = ManualClientEndUEB.result(scheme);
            } else if (notificationType.get(0).equals(DMAAP)) {
                ManualClientEndDMAAP.start(notificationURLList, topic, UNIQUEID, userName, pass);
                notification = ManualClientEndDMAAP.result(scheme);
            } else {
                ManualClientEnd.start(pdps.get(0));
                LOGGER.debug("manual notification requested.. : " + scheme.toString());
                notification = ManualClientEnd.result(scheme);
            }
            if (notification == null) {
                LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Notification yet..");
                return null;
            }
            return notification;
        }
        return null;
    }

    /*
     * Setting the Scheme.
     */
    public void setScheme(final NotificationScheme scheme) {
        this.scheme = scheme;
        if (UEB.equals(notificationType.get(0))) {
            AutoClientUEB.setScheme(this.scheme);
            if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
                ManualClientEndUEB.createTopic(pdps.get(0), UNIQUEID, notificationURLList);
            }
        } else if (notificationType.get(0).equals(DMAAP)) {
            AutoClientDMAAP.setScheme(this.scheme);
            if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
                ManualClientEndDMAAP.createTopic(topic, UNIQUEID, notificationURLList, userName, pass);
            }
        } else {
            AutoClientEnd.setScheme(this.scheme);
        }
    }

    /*
     * Returns the Scheme
     */
    public NotificationScheme getScheme() {
        return this.scheme;
    }

    /*
     * Returns the NotificationHandler
     */
    public NotificationHandler getNotificationHandler() {
        return this.handler;
    }

    /*
     * Stop the Notification Service if its running.
     */
    public void stopNotification() {
        if (this.scheme != null && this.handler != null
                && (this.scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)
                        || this.scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS))) {
            LOGGER.info("Clear Notification called.. ");
            if (UEB.equals(notificationType.get(0))) {
                this.uebClientThread.terminate();
                this.uebThread = false;
            } else if (notificationType.get(0).equals(DMAAP)) {
                this.dmaapClientThread.terminate();
                this.dmaapThread = false;
            } else {
                AutoClientEnd.stop();
            }
        }
    }

    /*
     * Push a policy to the PDP API implementation
     */
    public String pushPolicy(final String policyScope, final String policyName, final String policyType,
            final String pdpGroup, final UUID requestID) throws PolicyException {
        validateParameters(policyName, policyScope);

        final PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
        pushPolicyParameters.setPolicyName(policyScope + "." + policyName);
        pushPolicyParameters.setPolicyType(policyType);
        pushPolicyParameters.setPdpGroup(pdpGroup);
        pushPolicyParameters.setRequestID(requestID);
        return pushPolicyImpl(pushPolicyParameters).getResponseMessage();
    }

    private boolean isNotValid(final String value) {
        return value == null || value.trim().isEmpty();
    }

    public String createUpdateConfigPolicy(final String policyName, final String policyDescription,
            final String onapName, final String configName, final Map<String, String> configAttributes,
            final String configType, final String body, final String policyScope, final UUID requestID,
            final String riskLevel, final String riskType, final String guard, final String ttlDate,
            final boolean updateFlag) throws PolicyException {
        return createUpdateConfigPolicyImpl(policyName, policyDescription, onapName, configName, configAttributes,
                configType, body, policyScope, requestID, riskLevel, riskType, guard, ttlDate, updateFlag);
    }

    /*
     * Create Config Policy API Implementation
     */
    private String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription,
            final String onapName, final String configName, final Map<String, String> configAttributes,
            final String configType, final String body, final String policyScope, final UUID requestID,
            final String riskLevel, final String riskType, final String guard, final String ttlDate,
            final boolean updateFlag) throws PolicyException {

        validateParameters(policyName, policyScope);

        final PolicyParameters policyParameters = new PolicyParameters();
        policyParameters.setPolicyClass(PolicyClass.Config);
        policyParameters.setPolicyConfigType(PolicyConfigType.Base);
        policyParameters.setPolicyName(policyScope + "." + policyName);
        policyParameters.setPolicyDescription(policyDescription);
        policyParameters.setOnapName(onapName);
        policyParameters.setConfigName(configName);

        final Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
        attributes.put(AttributeType.MATCHING, configAttributes);

        policyParameters.setAttributes(attributes);
        policyParameters.setConfigBodyType(PolicyType.valueOf(configType));
        policyParameters.setConfigBody(body);
        policyParameters.setRequestID(requestID);
        policyParameters.setRiskLevel(riskLevel);
        policyParameters.setRiskType(riskType);
        policyParameters.setGuard(Boolean.parseBoolean(guard));
        policyParameters.setTtlDate(toDate(ttlDate));
        return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
    }

    private Date toDate(final String dateString) {
        try {
            return new SimpleDateFormat(DATE_FORMAT).parse(dateString);
        } catch (final ParseException e) {
            LOGGER.warn("Error Parsing date given " + dateString);
        }
        return null;
    }

    public String createUpdateConfigFirewallPolicy(final String policyName, final JsonObject firewallJson,
            final String policyScope, final UUID requestID, final String riskLevel, final String riskType,
            final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException {
        return createUpdateConfigFirewallPolicyImpl(policyName, firewallJson, policyScope, requestID, riskLevel,
                riskType, guard, ttlDate, updateFlag);
    }

    /*
     * Create Update Config Firewall Policy API implementation
     */
    private String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson,
            final String policyScope, final UUID requestID, final String riskLevel, final String riskType,
            final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException {
        validateParameters(policyName, policyScope);

        final PolicyParameters policyParameters = new PolicyParameters();
        policyParameters.setPolicyClass(PolicyClass.Config);
        policyParameters.setPolicyConfigType(PolicyConfigType.Firewall);
        policyParameters.setPolicyName(policyScope + "." + policyName);
        policyParameters.setConfigBody(firewallJson.toString());
        policyParameters.setRequestID(requestID);
        policyParameters.setRiskLevel(riskLevel);
        policyParameters.setRiskType(riskType);
        policyParameters.setGuard(Boolean.parseBoolean(guard));
        policyParameters.setTtlDate(toDate(ttlDate));
        return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
    }

    private void validateParameters(final String policyName, final String policyScope) throws PolicyException {
        if (isNotValid(policyScope)) {
            final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
            LOGGER.error(message);
            throw new PolicyException(message);
        }
        if (isNotValid(policyName)) {
            final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
            LOGGER.error(message);
            throw new PolicyException(message);
        }
    }

    public static void setClientKey(final String clientKey) {
        if (clientKey != null && !clientKey.isEmpty()) {
            StdPolicyEngine.pass = clientKey;
            setClientEncoding();
        }
    }

    /*
     * Get the Environment.
     */
    public static String getEnvironment() {
        return environment;
    }

    /*
     * Rotate the PDP list upon WEBsocket Failures
     */
    public static void rotatePDPList() {
        Collections.rotate(pdps, -1);
        Collections.rotate(encoding, -1);
    }

    /*
     * Get the latest PDP
     */
    public static String getPDPURL() {
        return pdps.get(0);
    }

    // Added for test
    String getTopic() {
        return topic;
    }

    // Added for test
    List<String> getNotificationType() {
        return notificationType;
    }

    // Added for test
    List<String> getNotificationURLList() {
        return notificationURLList;
    }

}