summaryrefslogtreecommitdiffstats
path: root/portal-BE/src/main/java/org/onap/portal/service/ExternalAccessRolesService.java
blob: fbe02af6c3e211e65db00bce78ae9440fc33ba41 (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
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
/*
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software 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.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.portal.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import javax.management.InvalidApplicationException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Tuple;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.portal.domain.db.DomainVo;
import org.onap.portal.domain.db.ep.EpAppFunction;
import org.onap.portal.domain.db.ep.EpAppRoleFunction;
import org.onap.portal.domain.db.fn.FnApp;
import org.onap.portal.domain.db.fn.FnFunction;
import org.onap.portal.domain.db.fn.FnRole;
import org.onap.portal.domain.db.fn.FnRoleFunction;
import org.onap.portal.domain.db.fn.FnUser;
import org.onap.portal.domain.db.fn.FnUserRole;
import org.onap.portal.domain.dto.ecomp.EPAppRoleFunction;
import org.onap.portal.domain.dto.ecomp.EPUserAppRolesRequest;
import org.onap.portal.domain.dto.ecomp.ExternalRoleDetails;
import org.onap.portal.domain.dto.ecomp.UploadRoleFunctionExtSystem;
import org.onap.portal.domain.dto.model.ExternalSystemUser;
import org.onap.portal.domain.dto.transport.BulkUploadRoleFunction;
import org.onap.portal.domain.dto.transport.BulkUploadUserRoles;
import org.onap.portal.domain.dto.transport.CentralApp;
import org.onap.portal.domain.dto.transport.CentralRole;
import org.onap.portal.domain.dto.transport.CentralRoleFunction;
import org.onap.portal.domain.dto.transport.CentralUser;
import org.onap.portal.domain.dto.transport.CentralUserApp;
import org.onap.portal.domain.dto.transport.CentralV2Role;
import org.onap.portal.domain.dto.transport.CentralV2User;
import org.onap.portal.domain.dto.transport.CentralV2UserApp;
import org.onap.portal.domain.dto.transport.EPUserAppCurrentRoles;
import org.onap.portal.domain.dto.transport.EcompUserRoles;
import org.onap.portal.domain.dto.transport.ExternalAccessPerms;
import org.onap.portal.domain.dto.transport.ExternalAccessPermsDetail;
import org.onap.portal.domain.dto.transport.ExternalAccessRole;
import org.onap.portal.domain.dto.transport.ExternalAccessRolePerms;
import org.onap.portal.domain.dto.transport.ExternalAccessUser;
import org.onap.portal.domain.dto.transport.ExternalRequestFieldsValidator;
import org.onap.portal.domain.dto.transport.GlobalRoleWithApplicationRoleFunction;
import org.onap.portal.domain.dto.transport.LocalRole;
import org.onap.portal.exception.DeleteDomainObjectFailedException;
import org.onap.portal.exception.ExternalAuthSystemException;
import org.onap.portal.exception.InactiveApplicationException;
import org.onap.portal.exception.InvalidUserException;
import org.onap.portal.exception.RoleFunctionException;
import org.onap.portal.logging.logic.EPLogUtil;
import org.onap.portal.service.app.FnAppService;
import org.onap.portal.service.appFunction.EpAppFunctionService;
import org.onap.portal.service.appRoleFunction.EpAppRoleFunctionService;
import org.onap.portal.service.role.FnRoleService;
import org.onap.portal.service.roleFunction.FnRoleFunctionService;
import org.onap.portal.service.user.FnUserService;
import org.onap.portal.utils.EPCommonSystemProperties;
import org.onap.portal.utils.EPUserUtils;
import org.onap.portal.utils.EcompPortalUtils;
import org.onap.portal.utils.PortalConstants;
import org.onap.portalsdk.core.domain.Role;
import org.onap.portalsdk.core.domain.RoleFunction;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.restful.domain.EcompRole;
import org.onap.portalsdk.core.restful.domain.EcompRoleFunction;
import org.onap.portalsdk.core.restful.domain.EcompUser;
import org.onap.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@SuppressWarnings("unchecked")
@Service
public class ExternalAccessRolesService {

    private static final String APP_ROLE_NAME_PARAM = "appRoleName";
    private static final String GET_PORTAL_APP_ROLES_QUERY = "getPortalAppRoles";
    private static final String GET_ROLE_FUNCTION_QUERY = "getRoleFunction";
    private static final String FUNCTION_CODE_PARAMS = "functionCode";
    private static final String AND_FUNCTION_CD_EQUALS = " and function_cd = '";
    private static final String OWNER = ".owner";
    private static final String ADMIN = ".admin";
    private static final String ACCOUNT_ADMINISTRATOR = ".Account_Administrator";
    private static final String FUNCTION_PIPE = "|";
    private static final String EXTERNAL_AUTH_PERMS = "perms";
    private static final String EXTERNAL_AUTH_ROLE_DESCRIPTION = "description";
    private static final String IS_EMPTY_JSON_STRING = "{}";
    private static final String CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE = "Connecting to External Auth system";
    private static final String APP_ID = "appId";
    private static final String ROLE_NAME = "name";
    private static final String APP_ID_EQUALS = " app_id = ";

    private static final String GET_GLOBAL_ROLE_WITH_APPLICATION_ROLE_FUNCTIONS = "select"
        + "  distinct d.roleId as roleId,"
        + "  d.roleName as roleName,"
        + "  d.activeYn as active,"
        + "  d.priority as priority,"
        + "  c.epAppFunction.functionCd as functionCd,"
        + "  e.functionName as functionName,"
        + "  c.epAppFunction.appId as appId,"
        + "  c.roleAppId as roleAppId"
        + " from"
        + "  FnUserRole a,"
        + "  FnApp b,"
        + "  EpAppRoleFunction c,"
        + "  FnRole d,"
        + "  EpAppFunction e"
        + " where"
        + "  b.appId = c.appId.appId"
        + "  and a.appId = c.roleAppId"
        + "  and b.enabled = 'Y'"
        + "  and c.fnRole.roleId = d.roleId"
        + "  and d.activeYn = 'Y'"
        + "  and e.functionCd = c.epAppFunction.functionCd"
        + "  and c.appId.appId = :appId"
        + "  and e.appId.appId = c.appId.appId";

    private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAccessRolesService.class);
    private final RestTemplate template = new RestTemplate();

    private final FnUserService fnUserService;
    private final FnRoleService fnRoleService;
    private final FnAppService fnAppService;
    private final EntityManager entityManager;
    private final FnRoleFunctionService fnRoleFunctionService;
    private final EpAppFunctionService epAppFunctionService;
    private final EpAppRoleFunctionService epAppRoleFunctionService;
    private final LocalRoleService localRoleService;
    private final BulkUploadUserRolesService bulkUploadUserRolesService;

    @Autowired
    public ExternalAccessRolesService(
        final FnUserService fnUserService,
        final FnRoleService fnRoleService,
        final FnAppService fnAppService, EntityManager entityManager,
        FnRoleFunctionService fnRoleFunctionService,
        final EpAppFunctionService epAppFunctionService,
        final EpAppRoleFunctionService epAppRoleFunctionService,
        final LocalRoleService localRoleService,
        BulkUploadUserRolesService bulkUploadUserRolesService) {
        this.fnUserService = fnUserService;
        this.fnRoleService = fnRoleService;
        this.fnAppService = fnAppService;
        this.entityManager = entityManager;
        this.fnRoleFunctionService = fnRoleFunctionService;
        this.epAppFunctionService = epAppFunctionService;
        this.epAppRoleFunctionService = epAppRoleFunctionService;
        this.localRoleService = localRoleService;
        this.bulkUploadUserRolesService = bulkUploadUserRolesService;
    }

    public String getFunctionCodeType(String roleFuncItem) {
        String type = null;
        if ((roleFuncItem.contains(FUNCTION_PIPE) && roleFuncItem.contains("menu"))
            || (!roleFuncItem.contains(FUNCTION_PIPE) && roleFuncItem.contains("menu"))) {
            type = "menu";
        } else if (checkIfCodeHasNoPipesAndHasTypeUrl(roleFuncItem) || checkIfCodeHasPipesAndHasTypeUrl(roleFuncItem)
            || checkIfCodeHasNoPipesAndHasNoTypeUrl(roleFuncItem)) {
            type = "url";
        } else if (roleFuncItem.contains(FUNCTION_PIPE)
            && (!roleFuncItem.contains("menu") || roleFuncItem.contains("url"))) {
            type = EcompPortalUtils.getFunctionType(roleFuncItem);
        }
        return type;
    }

    private boolean checkIfCodeHasNoPipesAndHasTypeUrl(String roleFuncItem) {
        return !roleFuncItem.contains(FUNCTION_PIPE) && roleFuncItem.contains("url");
    }

    private boolean checkIfCodeHasPipesAndHasTypeUrl(String roleFuncItem) {
        return roleFuncItem.contains(FUNCTION_PIPE) && roleFuncItem.contains("url");
    }

    private boolean checkIfCodeHasNoPipesAndHasNoTypeUrl(String roleFuncItem) {
        return !roleFuncItem.contains(FUNCTION_PIPE) && !roleFuncItem.contains("url");
    }

    List<FnRole> getPortalAppRoleInfo(Long roleId) {
        return fnRoleService.retrieveAppRoleByRoleIdWhereAppIdIsNull(roleId);
    }

    ResponseEntity<String> getUserRolesFromExtAuthSystem(String name, HttpEntity<String> getUserRolesEntity) {
        logger.debug(EELFLoggerDelegate.debugLogger, "Connecting to external system to get current user roles");
        ResponseEntity<String> getResponse = template
            .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
                + "roles/user/" + name, HttpMethod.GET, getUserRolesEntity, String.class);
        if (getResponse.getStatusCode().value() == 200) {
            logger.debug(EELFLoggerDelegate.debugLogger,
                "getAllUserRoleFromExtAuthSystem: Finished GET user roles from external system and received user roles {}",
                getResponse.getBody());
        } else {
            logger.error(EELFLoggerDelegate.errorLogger,
                "getAllUserRoleFromExtAuthSystem: Failed GET user roles from external system and received user roles {}",
                getResponse.getBody());
            EPLogUtil.logExternalAuthAccessAlarm(logger, getResponse.getStatusCode());
        }
        return getResponse;
    }

    Map<String, FnRole> getAppRoleNamesWithUnderscoreMap(FnApp app) {
        final Map<String, FnRole> currentRolesInDB = new HashMap<>();
        List<FnRole> getCurrentRoleList = null;
        final Map<String, Long> appParams = new HashMap<>();
        if (app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
            getCurrentRoleList = fnRoleService.retrieveAppRolesWhereAppIdIsNull();
        } else {
            appParams.put("appId", app.getId());
            getCurrentRoleList = fnRoleService.retrieveAppRolesByAppId(app.getId());
        }
        for (FnRole role : getCurrentRoleList) {
            currentRolesInDB.put(role.getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"), role);
        }
        return currentRolesInDB;
    }

    List<CentralV2Role> createCentralRoleObject(List<FnApp> app, List<FnRole> roleInfo,
        List<CentralV2Role> roleList) throws RoleFunctionException {
        for (FnRole role : roleInfo) {
            List<EpAppFunction> cenRoleFuncList = epAppFunctionService
                .getAppRoleFunctionList(role.getId(), app.get(0).getId());
            SortedSet<DomainVo> roleFunctionSet = new TreeSet<>();
            for (EpAppFunction roleFunc : cenRoleFuncList) {
                String functionCode = EcompPortalUtils.getFunctionCode(roleFunc.getFunctionCd());
                functionCode = EPUserUtils.decodeFunctionCode(functionCode);
                String type = getFunctionCodeType(roleFunc.getFunctionCd());
                String action = getFunctionCodeAction(roleFunc.getFunctionCd());
                FnRoleFunction cenRoleFunc = new FnRoleFunction(role,
                    FnFunction.builder().code(functionCode).name(roleFunc.getFunctionName()).type(type).action(action)
                        .build());
            }
            SortedSet<CentralV2Role> childRoles = new TreeSet<>();
            SortedSet<CentralV2Role> parentRoles = new TreeSet<>();
            CentralV2Role cenRole;
            if (role.getAppRoleId() == null) {
                cenRole = CentralV2Role.builder().id(role.getId()).created(role.getCreated())
                    .modified(role.getModified())
                    .rowNum(role.getRowNum()).name(role.getRoleName())
                    .active(role.getActiveYn()).priority(role.getPriority()).roleFunctions(roleFunctionSet)
                    .childRoles(childRoles).parentRoles(parentRoles).build();
            } else {
                cenRole = CentralV2Role.builder().id(role.getAppRoleId())
                    .created(role.getCreated()).modified(role.getModified())
                    .rowNum(role.getRowNum()).name(role.getRoleName())
                    .active(role.getActiveYn()).priority(role.getPriority()).roleFunctions(roleFunctionSet)
                    .childRoles(childRoles).parentRoles(parentRoles).build();
            }
            roleList.add(cenRole);
        }
        return roleList;
    }

    public String getFunctionCodeAction(String roleFuncItem) {
        return (!roleFuncItem.contains(FUNCTION_PIPE)) ? "*" : EcompPortalUtils.getFunctionAction(roleFuncItem);
    }

    public List<CentralV2Role> getRolesForApp(String uebkey) throws Exception {
        logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Entering into getRolesForApp");
        List<CentralV2Role> roleList = new ArrayList<>();
        try {
            List<FnApp> app = fnAppService.getByUebKey(uebkey);
            List<FnRole> appRolesList = fnRoleService.getAppRoles(app.get(0).getId());
            roleList = createCentralRoleObject(app, appRolesList, roleList);
            if (!Objects.equals(app.get(0).getId(), PortalConstants.PORTAL_APP_ID)) {
                List<CentralV2Role> globalRoleList = getGlobalRolesOfApplication(app.get(0).getId());
                List<FnRole> globalRolesList = fnRoleService.getGlobalRolesOfPortal();
                List<CentralV2Role> portalsGlobalRolesFinlaList = new ArrayList<>();
                if (!globalRolesList.isEmpty()) {
                    for (FnRole eprole : globalRolesList) {
                        CentralV2Role cenRole = convertRoleToCentralV2Role(eprole);
                        portalsGlobalRolesFinlaList.add(cenRole);
                    }
                    roleList.addAll(globalRoleList);
                    for (CentralV2Role role : portalsGlobalRolesFinlaList) {
                        CentralV2Role result = roleList.stream().filter(x -> role.getId().equals(x.getId())).findAny()
                            .orElse(null);
                        if (result == null) {
                            roleList.add(role);
                        }
                    }
                } else {
                    for (FnRole role : globalRolesList) {
                        CentralV2Role cenRole = convertRoleToCentralV2Role(role);
                        roleList.add(cenRole);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getRolesForApp: Failed!", e);
            throw e;
        }
        logger.debug(EELFLoggerDelegate.debugLogger, "getRolesForApp: Finished!");
        return roleList.stream().distinct().collect(Collectors.toList());
    }

    private List<CentralV2Role> getGlobalRolesOfApplication(final Long appId) {
        List<GlobalRoleWithApplicationRoleFunction> globalRoles = new ArrayList<>();
        try {
            List<Tuple> tuples = entityManager.createQuery(GET_GLOBAL_ROLE_WITH_APPLICATION_ROLE_FUNCTIONS, Tuple.class)
                .setParameter("appId", appId)
                .getResultList();
            globalRoles = tuples.stream().map(this::tupleToGlobalRoleWithApplicationRoleFunction)
                .collect(Collectors.toList());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getCentralizedAppsOfUser failed", e);
        }
        List<CentralV2Role> roleList = new ArrayList<>();
        if (globalRoles.size() > 0) {
            roleList = finalListOfCentralRoles(globalRoles);
        }
        return roleList;
    }

    private GlobalRoleWithApplicationRoleFunction tupleToGlobalRoleWithApplicationRoleFunction(Tuple tuple) {
        return GlobalRoleWithApplicationRoleFunction.builder().roleId((Long) tuple.get("roleId"))
            .roleName((String) tuple.get("roleName"))
            .functionCd((String) tuple.get("functionCd")).functionName((String) tuple.get("functionName"))
            .active((Boolean) tuple.get("active")).priority((Integer) tuple.get("priority"))
            .appId((Long) tuple.get("appId")).roleAppId((Long) tuple.get("roleAppId")).build();
    }

    private List<CentralV2Role> finalListOfCentralRoles(List<GlobalRoleWithApplicationRoleFunction> globalRoles) {
        List<CentralV2Role> rolesfinalList = new ArrayList<>();
        for (GlobalRoleWithApplicationRoleFunction role : globalRoles) {
            boolean found = false;
            for (CentralV2Role cenRole : rolesfinalList) {
                if (role.getRoleId().equals(cenRole.getId())) {
                    SortedSet<DomainVo> roleFunctions = new TreeSet<>();
                    for (DomainVo vo : cenRole.getRoleFunctions()) {
                        Optional<FnRoleFunction> roleFunction = fnRoleFunctionService.findById(vo.getId());
                        roleFunction.ifPresent(roleFunctions::add);
                    }
                    FnRoleFunction cenRoleFun = createCentralRoleFunctionForGlobalRole(role);
                    roleFunctions.add(cenRoleFun);
                    cenRole.setRoleFunctions(roleFunctions);
                    found = true;
                    break;
                }
            }
            if (!found) {
                CentralV2Role cenrole = new CentralV2Role();
                cenrole.setName(role.getRoleName());
                cenrole.setId(role.getRoleId());
                cenrole.setActive(role.getActive());
                cenrole.setPriority(role.getPriority());
                SortedSet<DomainVo> roleFunctions = new TreeSet<>();
                FnRoleFunction cenRoleFun = createCentralRoleFunctionForGlobalRole(role);
                roleFunctions.add(cenRoleFun);
                cenrole.setRoleFunctions(roleFunctions);
                rolesfinalList.add(cenrole);
            }
        }
        return rolesfinalList;
    }

    public String getV2UserWithRoles(String loginId, String uebkey) throws Exception {
        final Map<String, String> params = new HashMap<>();
        FnUser user = null;
        CentralV2User cenV2User = null;
        String result = null;
        try {
            params.put("orgUserIdValue", loginId);
            List<FnApp> appList = getApp(uebkey);
            if (!appList.isEmpty()) {
                user = fnUserService.loadUserByUsername(loginId);
                ObjectMapper mapper = new ObjectMapper();
                cenV2User = getV2UserAppRoles(loginId, uebkey);
                result = mapper.writeValueAsString(cenV2User);
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getUser: failed", e);
            throw e;
        }
        return result;
    }

    public void syncApplicationRolesWithEcompDB(FnApp app) {
        try {
            logger.debug(EELFLoggerDelegate.debugLogger, "syncRoleFunctionFromExternalAccessSystem: Started");
            // Sync functions and roles assigned to it which also creates new roles if does
            // not exits in portal
            syncRoleFunctionFromExternalAccessSystem(app);
            logger.debug(EELFLoggerDelegate.debugLogger, "syncRoleFunctionFromExternalAccessSystem: Finished");
            ObjectMapper mapper = new ObjectMapper();
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering to getAppRolesJSONFromExtAuthSystem");
            // Get Permissions from External Auth System
            JSONArray extRole = getAppRolesJSONFromExtAuthSystem(app.getId(), app.getAuthNamespace());
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering into getExternalRoleDetailsList");
            // refactoring done
            List<ExternalRoleDetails> externalRoleDetailsList = getExternalRoleDetailsList(app, mapper, extRole);
            List<FnRole> finalRoleList = new ArrayList<>();
            for (ExternalRoleDetails externalRole : externalRoleDetailsList) {
                FnRole ecompRole = convertExternalRoleDetailsToEpRole(externalRole);
                finalRoleList.add(ecompRole);
            }
            List<FnRole> applicationRolesList;
            applicationRolesList = getAppRoles(app.getId());
            List<String> applicationRoleIdList = new ArrayList<>();
            for (FnRole applicationRole : applicationRolesList) {
                applicationRoleIdList.add(applicationRole.getRoleName());
            }
            List<FnRole> roleListToBeAddInEcompDB = new ArrayList<>();
            for (FnRole aafRole : finalRoleList) {
                if (!applicationRoleIdList.contains(aafRole.getRoleName())) {
                    roleListToBeAddInEcompDB.add(aafRole);
                }
            }
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering into inactiveRolesNotInExternalAuthSystem");
            // Check if roles exits in external Access system and if not make inactive in DB
            inactiveRolesNotInExternalAuthSystem(app.getId(), finalRoleList, applicationRolesList);
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering into addNewRoleInEcompDBUpdateDescInExtAuthSystem");
            // Add new roles in DB and updates role description in External Auth System
            addNewRoleInEcompDBUpdateDescInExtAuthSystem(app, roleListToBeAddInEcompDB);
            logger.debug(EELFLoggerDelegate.debugLogger, "syncApplicationRolesWithEcompDB: Finished");
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "syncApplicationRolesWithEcompDB: Failed due to the External Auth System", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "syncApplicationRolesWithEcompDB: Failed ", e);
        }
    }

    public List<FnRole> getAppRoles(Long appId) {
        List<FnRole> applicationRoles;
        try {
            if (appId == 1) {
                applicationRoles = fnRoleService.retrieveAppRolesWhereAppIdIsNull();
            } else {
                applicationRoles = fnRoleService.retrieveAppRolesByAppId(appId);
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getAppRoles: failed", e);
            throw e;
        }
        return applicationRoles;
    }

    private FnRole convertExternalRoleDetailsToEpRole(ExternalRoleDetails externalRoleDetails) {
        FnRole role = new FnRole();
        role.setActiveYn(true);
        role.setAppId(externalRoleDetails.getAppId());
        role.setAppRoleId(externalRoleDetails.getAppRoleId());
        role.setRoleName(externalRoleDetails.getName());
        role.setPriority(externalRoleDetails.getPriority());
        return role;
    }

    public List<ExternalRoleDetails> getExternalRoleDetailsList(FnApp app, ObjectMapper mapper, JSONArray extRole)
        throws IOException {
        List<ExternalRoleDetails> externalRoleDetailsList = new ArrayList<>();
        ExternalAccessPerms externalAccessPerms;
        List<String> functionCodelist = new ArrayList<>();
        Map<String, FnRole> curRolesMap = getAppRoleNamesMap(app.getId());
        Map<String, FnRole> curRolesUnderscoreMap = getAppRoleNamesWithUnderscoreMap(app);
        for (int i = 0; i < extRole.length(); i++) {
            ExternalRoleDetails externalRoleDetail = new ExternalRoleDetails();
            EPAppRoleFunction ePAppRoleFunction = new EPAppRoleFunction();
            JSONObject Role = (JSONObject) extRole.get(i);
            String name = extRole.getJSONObject(i).getString(ROLE_NAME);
            String actualRoleName = name.substring(app.getAuthNamespace().length() + 1);
            if (extRole.getJSONObject(i).has(EXTERNAL_AUTH_ROLE_DESCRIPTION)) {
                actualRoleName = extRole.getJSONObject(i).getString(EXTERNAL_AUTH_ROLE_DESCRIPTION);
            }
            SortedSet<ExternalAccessPerms> externalAccessPermsOfRole = new TreeSet<>();
            if (extRole.getJSONObject(i).has(EXTERNAL_AUTH_PERMS)) {
                JSONArray extPerm = (JSONArray) Role.get(EXTERNAL_AUTH_PERMS);
                for (int j = 0; j < extPerm.length(); j++) {
                    JSONObject perms = extPerm.getJSONObject(j);
                    boolean isNamespaceMatching = EcompPortalUtils.checkNameSpaceMatching(perms.getString("type"),
                        app.getAuthNamespace());
                    if (isNamespaceMatching) {
                        externalAccessPerms = new ExternalAccessPerms(perms.getString("type"),
                            perms.getString("instance"), perms.getString("action"));
                        ePAppRoleFunction.setCode(externalAccessPerms.getInstance());
                        functionCodelist.add(ePAppRoleFunction.getCode());
                        externalAccessPermsOfRole.add(externalAccessPerms);
                    }
                }
            }
            externalRoleDetail.setActive(true);
            externalRoleDetail.setName(actualRoleName);
            if (app.getId() == 1) {
                externalRoleDetail.setAppId(null);
            } else {
                externalRoleDetail.setAppId(app.getId());
            }
            FnRole currRole = null;
            currRole = (!extRole.getJSONObject(i).has(EXTERNAL_AUTH_ROLE_DESCRIPTION))
                ? curRolesUnderscoreMap.get(actualRoleName)
                : curRolesMap.get(actualRoleName);
            Long roleId = null;
            if (currRole != null) {
                roleId = currRole.getId();
            }
            final Map<String, EpAppRoleFunction> roleFunctionsMap = new HashMap<>();
            if (roleId != null) {
                List<EpAppRoleFunction> appRoleFunctions = epAppRoleFunctionService
                    .getAppRoleFunctionOnRoleIdAndAppId(app.getId(), roleId);
                if (!appRoleFunctions.isEmpty()) {
                    for (EpAppRoleFunction roleFunc : appRoleFunctions) {
                        roleFunctionsMap.put(roleFunc.getEpAppFunction().getFunctionCd(), roleFunc);
                    }
                }
            }
            if (!externalAccessPermsOfRole.isEmpty()) {
                // Adding functions to role
                for (ExternalAccessPerms externalpermission : externalAccessPermsOfRole) {
                    EpAppRoleFunction checkRoleFunctionExits = roleFunctionsMap.get(externalpermission.getInstance());
                    if (checkRoleFunctionExits == null) {
                        String funcCode = externalpermission.getType().substring(app.getAuthNamespace().length() + 1)
                            + FUNCTION_PIPE + externalpermission.getInstance() + FUNCTION_PIPE
                            + externalpermission.getAction();
                        EpAppRoleFunction checkRoleFunctionPipeExits = roleFunctionsMap.get(funcCode);
                        if (checkRoleFunctionPipeExits == null) {
                            try {
                                logger.debug(EELFLoggerDelegate.debugLogger,
                                    "SyncApplicationRolesWithEcompDB: Adding function to the role: {}",
                                    externalpermission.getInstance());
                                List<EpAppFunction> roleFunction = epAppFunctionService
                                    .getAppFunctionOnCodeAndAppId(app.getId(), externalpermission.getInstance());
                                if (roleFunction.isEmpty()) {
                                    roleFunction = epAppFunctionService
                                        .getAppFunctionOnCodeAndAppId(app.getId(), funcCode);
                                }
                                if (!roleFunction.isEmpty()) {
                                    EpAppRoleFunction apRoleFunction = new EpAppRoleFunction();
                                    apRoleFunction.setAppId(app);
                                    apRoleFunction.setFnRole(currRole);
                                    apRoleFunction.setEpAppFunction(roleFunction.get(0));
                                    epAppRoleFunctionService.save(apRoleFunction);
                                }
                            } catch (Exception e) {
                                logger.error(EELFLoggerDelegate.errorLogger,
                                    "SyncApplicationRolesWithEcompDB: Failed to add role function", e);
                            }
                        }
                    }
                }
            }
            externalRoleDetailsList.add(externalRoleDetail);
        }
        return externalRoleDetailsList;
    }

    private Map<String, FnRole> getAppRoleNamesMap(final Long appId) {
        final Map<String, FnRole> currentRolesInDB = new HashMap<>();
        List<FnRole> getCurrentRoleList = null;
        final Map<String, Long> appParams = new HashMap<>();
        if (appId.equals(PortalConstants.PORTAL_APP_ID)) {
            getCurrentRoleList = fnRoleService.retrieveAppRolesWhereAppIdIsNull();
        } else {
            getCurrentRoleList = fnRoleService.retrieveAppRolesByAppId(appId);
        }
        for (FnRole role : getCurrentRoleList) {
            currentRolesInDB.put(role.getRoleName(), role);
        }
        return currentRolesInDB;
    }

    public JSONArray getAppRolesJSONFromExtAuthSystem(final long appId, final String authNamespace) throws Exception {
        ResponseEntity<String> response = null;
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "syncApplicationRolesWithEcompDB: {} ",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
        response = template.exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
            + "roles/ns/" + authNamespace, HttpMethod.GET, entity, String.class);
        String res = response.getBody();
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncApplicationRolesWithEcompDB: Finished GET roles from External Auth system and the result is :",
            res);
        JSONObject jsonObj = new JSONObject(res);
        JSONArray extRole = jsonObj.getJSONArray("role");
        for (int i = 0; i < extRole.length(); i++) {
            if (extRole.getJSONObject(i).getString(ROLE_NAME).equals(authNamespace + ADMIN)
                || extRole.getJSONObject(i).getString(ROLE_NAME).equals(authNamespace + OWNER)
                || (extRole.getJSONObject(i).getString(ROLE_NAME).equals(authNamespace + ACCOUNT_ADMINISTRATOR)
                && !(appId == PortalConstants.PORTAL_APP_ID))) {
                extRole.remove(i);
                i--;
            }
        }
        return extRole;
    }

    private void addNewRoleInEcompDBUpdateDescInExtAuthSystem(FnApp app, List<FnRole> roleListToBeAddInEcompDB) {
        FnRole roleToBeAddedInEcompDB;
        for (FnRole fnRole : roleListToBeAddInEcompDB) {
            try {
                roleToBeAddedInEcompDB = fnRole;
                if (app.getId() == 1) {
                    roleToBeAddedInEcompDB.setAppRoleId(null);
                }
                fnRoleService.saveOne(roleToBeAddedInEcompDB);
                List<FnRole> getRoleCreatedInSync = null;
                if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                    getRoleCreatedInSync = fnRoleService
                        .retrieveAppRolesByRoleNameAndByAppId(roleToBeAddedInEcompDB.getRoleName(), app.getId());
                    FnRole epUpdateRole = getRoleCreatedInSync.get(0);
                    epUpdateRole.setAppRoleId(epUpdateRole.getId());
                    fnRoleService.saveOne(epUpdateRole);
                }
                List<FnRole> roleList;
                final Map<String, String> params = new HashMap<>();
                params.put(APP_ROLE_NAME_PARAM, roleToBeAddedInEcompDB.getRoleName());
                boolean isPortalRole;
                if (app.getId() == 1) {
                    isPortalRole = true;
                    roleList = fnRoleService
                        .retrieveAppRolesByRoleNameAndWhereAppIdIsNull(roleToBeAddedInEcompDB.getRoleName());
                } else {
                    isPortalRole = false;
                    roleList = fnRoleService
                        .retrieveAppRolesByRoleNameAndByAppId(roleToBeAddedInEcompDB.getRoleName(), app.getId());
                }
                FnRole role = roleList.get(0);
                Role aaFrole = new Role();
                aaFrole.setId(role.getId());
                aaFrole.setActive(role.getActiveYn());
                aaFrole.setPriority(role.getPriority());
                aaFrole.setName(role.getRoleName());
                updateRoleInExternalSystem(aaFrole, app, isPortalRole);
            } catch (Exception e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "SyncApplicationRolesWithEcompDB: Failed to add or update role in external auth system", e);
            }
        }
    }

    private void updateRoleInExternalSystem(Role updateExtRole, FnApp app, boolean isGlobalRole) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        ResponseEntity<String> deleteResponse = null;
        List<FnRole> epRoleList = null;
        if (app.getId().equals(PortalConstants.PORTAL_APP_ID)
            || (isGlobalRole && !app.getId().equals(PortalConstants.PORTAL_APP_ID))) {
            epRoleList = getPortalAppRoleInfo(updateExtRole.getId());
        } else {
            epRoleList = getPartnerAppRoleInfo(updateExtRole.getId(), app.getId());
        }
        // Assigning functions to global role
        if ((isGlobalRole && !app.getId().equals(PortalConstants.PORTAL_APP_ID))) {
            List<FnFunction> functions = new ArrayList<>();
            for (FnRoleFunction roleFunction : convertSetToListOfRoleFunctions(updateExtRole)) {
                functions.add(roleFunction.getFunctionCd());
            }
            // TODO HARDCODED ID
            FnApp portalAppInfo = fnAppService.getById(PortalConstants.PORTAL_APP_ID);
            addFunctionsTOGlobalRole(epRoleList, updateExtRole, functions, mapper, app, portalAppInfo);
        } else {
            String appRole = getSingleAppRole(epRoleList.get(0).getRoleName(), app);
            List<FnRoleFunction> roleFunctionListNew = convertSetToListOfRoleFunctions(updateExtRole);
            if (!appRole.equals(IS_EMPTY_JSON_STRING)) {
                JSONObject jsonObj = new JSONObject(appRole);
                JSONArray extRole = jsonObj.getJSONArray("role");
                if (!extRole.getJSONObject(0).has(EXTERNAL_AUTH_ROLE_DESCRIPTION)) {
                    String roleName = extRole.getJSONObject(0).getString(ROLE_NAME);
                    Map<String, String> delRoleKeyMapper = new HashMap<>();
                    delRoleKeyMapper.put(ROLE_NAME, roleName);
                    String delRoleKeyValue = mapper.writeValueAsString(delRoleKeyMapper);
                    deleteResponse = deleteRoleInExternalSystem(delRoleKeyValue);
                    if (deleteResponse.getStatusCode().value() != 200) {
                        throw new ExternalAuthSystemException(deleteResponse.getBody());
                    }
                    addRole(updateExtRole, app.getUebKey());
                } else {
                    String desc = extRole.getJSONObject(0).getString(EXTERNAL_AUTH_ROLE_DESCRIPTION);
                    String name = extRole.getJSONObject(0).getString(ROLE_NAME);
                    List<ExternalAccessPerms> list = new ArrayList<>();
                    if (extRole.getJSONObject(0).has(EXTERNAL_AUTH_PERMS)) {
                        JSONArray perms = extRole.getJSONObject(0).getJSONArray(EXTERNAL_AUTH_PERMS);
                        list = mapper.readValue(perms.toString(), TypeFactory.defaultInstance()
                            .constructCollectionType(List.class, ExternalAccessPerms.class));
                    }
                    // If role name or role functions are updated then delete
                    // record in External System and add new record to avoid
                    // conflicts
                    boolean isRoleNameChanged = false;
                    if (!desc.equals(updateExtRole.getName())) {
                        isRoleNameChanged = true;
                        deleteRoleInExtSystem(mapper, name);
                        addRole(updateExtRole, app.getUebKey());
                        // add partner functions to the global role in External
                        // Auth System
                        if (!list.isEmpty() && isGlobalRole) {
                            addPartnerHasRoleFunctionsToGlobalRole(list, mapper, app, updateExtRole);
                        }
                        list.removeIf(
                            perm -> EcompPortalUtils.checkNameSpaceMatching(perm.getType(), app.getAuthNamespace()));
                        // if role name is changes please ignore the previous
                        // functions in External Auth
                        // and update with user requested functions
                        addRemoveFunctionsToRole(updateExtRole, app, mapper, roleFunctionListNew, name, list);
                    }
                    // Delete role in External System if role is inactive
                    if (!updateExtRole.getActive()) {
                        deleteRoleInExtSystem(mapper, name);
                    }
                    if (!isRoleNameChanged) {
                        addRemoveFunctionsToRole(updateExtRole, app, mapper, roleFunctionListNew, name,
                            list);
                    }
                }
            } else {
                // It seems like role exists in local DB but not in External
                // Access system
                if (updateExtRole.getActive()) {
                    addRole(updateExtRole, app.getUebKey());
                    ExternalAccessRolePerms extAddRolePerms = null;
                    ExternalAccessPerms extAddPerms = null;
                    List<FnRoleFunction> roleFunctionListAdd = convertSetToListOfRoleFunctions(updateExtRole);
                    HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
                    for (FnRoleFunction roleFunc : roleFunctionListAdd) {
                        extAddPerms = new ExternalAccessPerms(
                            app.getAuthNamespace() + "." + roleFunc.getFunctionCd().getType(),
                            roleFunc.getFunctionCd().getCode(), roleFunc.getFunctionCd().getAction());
                        extAddRolePerms = new ExternalAccessRolePerms(extAddPerms,
                            app.getAuthNamespace() + "." + updateExtRole.getName().replaceAll(
                                EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
                        addRoleFuncExtSysRestAPI(mapper, extAddRolePerms, headers);
                    }
                }
            }
        }
    }

    private void addRemoveFunctionsToRole(Role updateExtRole, FnApp app, ObjectMapper mapper,
        List<FnRoleFunction> fnRoleFunctions, String name, List<ExternalAccessPerms> list) throws Exception {
        boolean response;
        List<FnFunction> roleFunctionListNew = new ArrayList<>();
        for (FnRoleFunction roleFunction : fnRoleFunctions) {
            roleFunctionListNew.add(roleFunction.getFunctionCd());
        }
        Map<String, FnFunction> updateRoleFunc = new HashMap<>();
        for (FnFunction addPerm : roleFunctionListNew) {
            updateRoleFunc.put(addPerm.getCode(), addPerm);
        }
        final Map<String, ExternalAccessPerms> extRolePermMap = new HashMap<>();
        final Map<String, ExternalAccessPerms> extRolePermMapPipes = new HashMap<>();
        list.removeIf(perm -> !EcompPortalUtils.checkNameSpaceMatching(perm.getType(), app.getAuthNamespace()));
        // Update permissions in the ExternalAccess System
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        if (!list.isEmpty()) {
            for (ExternalAccessPerms perm : list) {
                FnFunction roleFunc = updateRoleFunc.get(perm.getType().substring(app.getAuthNamespace().length() + 1)
                    + FUNCTION_PIPE + perm.getInstance() + FUNCTION_PIPE + perm.getAction());
                if (roleFunc == null) {
                    FnFunction roleFuncPipeFilter = updateRoleFunc.get(perm.getInstance());
                    if (roleFuncPipeFilter == null) {
                        removePermForRole(perm, mapper, name, headers);
                    }
                }
                extRolePermMap.put(perm.getInstance(), perm);
                extRolePermMapPipes.put(perm.getType().substring(app.getAuthNamespace().length() + 1) + FUNCTION_PIPE
                    + perm.getInstance() + FUNCTION_PIPE + perm.getAction(), perm);
            }
        }
        response = true;
        if (!roleFunctionListNew.isEmpty()) {
            for (FnFunction roleFunc : roleFunctionListNew) {
                if (roleFunc.getCode().contains(FUNCTION_PIPE)) {
                    ExternalAccessPerms perm = extRolePermMapPipes.get(roleFunc.getCode());
                    if (perm == null) {
                        response = addFunctionsToRoleInExternalAuthSystem(updateExtRole, app, mapper, headers,
                            roleFunc);
                    }
                } else {
                    if (!extRolePermMap.containsKey(EcompPortalUtils.getFunctionCode(roleFunc.getCode()))) {
                        response = addFunctionsToRoleInExternalAuthSystem(updateExtRole, app, mapper, headers,
                            roleFunc);
                    }
                }
            }
        }
    }

    private boolean addFunctionsToRoleInExternalAuthSystem(Role updateExtRole, FnApp app, ObjectMapper mapper,
        HttpHeaders headers, FnFunction roleFunc) throws JsonProcessingException {
        boolean response;
        ExternalAccessRolePerms extRolePerms;
        ExternalAccessPerms extPerms;
        String code;
        String type;
        String action;
        if (roleFunc.getCode().contains(FUNCTION_PIPE)) {
            code = EcompPortalUtils.getFunctionCode(roleFunc.getCode());
            type = EcompPortalUtils.getFunctionType(roleFunc.getCode());
            action = getFunctionCodeAction(roleFunc.getCode());
        } else {
            code = roleFunc.getCode();
            type = roleFunc.getCode().contains("menu") ? "menu" : "url";
            action = "*";
        }
        extPerms = new ExternalAccessPerms(app.getAuthNamespace() + "." + type, code, action);
        extRolePerms = new ExternalAccessRolePerms(extPerms, app.getAuthNamespace() + "." + updateExtRole.getName()
            .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
        String updateRolePerms = mapper.writeValueAsString(extRolePerms);
        HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "updateRoleInExternalSystem: {} for POST: {}",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, updateRolePerms);
        ResponseEntity<String> addResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role/perm",
            HttpMethod.POST, entity, String.class);
        if (addResponse.getStatusCode().value() != 201 && addResponse.getStatusCode().value() != 409) {
            response = false;
            logger.debug(EELFLoggerDelegate.debugLogger,
                "updateRoleInExternalSystem: Connected to External Auth system but something went wrong! due to {} and statuscode: {}",
                addResponse.getStatusCode().getReasonPhrase(), addResponse.getStatusCode().value());
        } else {
            response = true;
            logger.debug(EELFLoggerDelegate.debugLogger,
                "updateRoleInExternalSystem: Finished adding permissions to roles in External Auth system {} and status code: {} ",
                updateRolePerms, addResponse.getStatusCode().value());
        }
        return response;
    }

    private void addRoleFuncExtSysRestAPI(ObjectMapper addPermsMapper, ExternalAccessRolePerms extAddRolePerms,
        HttpHeaders headers) throws JsonProcessingException {
        boolean response;
        String updateRolePerms = addPermsMapper.writeValueAsString(extAddRolePerms);
        HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "addRoleFunctionsInExternalSystem: {} for POST: {} ",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, updateRolePerms);
        ResponseEntity<String> addResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role/perm",
            HttpMethod.POST, entity, String.class);
        if (addResponse.getStatusCode().value() != 201 && addResponse.getStatusCode().value() != 409) {
            response = false;
            logger.debug(EELFLoggerDelegate.debugLogger,
                "addRoleFunctionsInExternalSystem: While adding permission to the role in  External Auth system something went wrong! due to {} and statuscode: {}",
                addResponse.getStatusCode().getReasonPhrase(), addResponse.getStatusCode().value());
        } else {
            response = true;
            logger.debug(EELFLoggerDelegate.debugLogger,
                "addRoleFunctionsInExternalSystem: Finished adding permissions to roles in External Auth system {} and status code: {} ",
                updateRolePerms, addResponse.getStatusCode().value());
        }
    }

    private void addPartnerHasRoleFunctionsToGlobalRole(List<ExternalAccessPerms> permslist, ObjectMapper mapper,
        FnApp app, Role updateExtRole) throws Exception {
        for (ExternalAccessPerms perm : permslist) {
            if (!EcompPortalUtils.checkNameSpaceMatching(perm.getType(), app.getAuthNamespace())) {
                ExternalAccessRolePerms extAddGlobalRolePerms = null;
                ExternalAccessPerms extAddPerms = null;
                extAddPerms = new ExternalAccessPerms(perm.getType(), perm.getInstance(), perm.getAction());
                extAddGlobalRolePerms = new ExternalAccessRolePerms(extAddPerms,
                    app.getAuthNamespace() + "." + updateExtRole.getName().replaceAll(
                        EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
                String addPerms = mapper.writeValueAsString(extAddGlobalRolePerms);
                HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
                HttpEntity<String> entity = new HttpEntity<>(addPerms, headers);
                logger.debug(EELFLoggerDelegate.debugLogger, "addPartnerHasRoleFunctionsToGlobalRole: {} ",
                    CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
                try {
                    ResponseEntity<String> addResponse = template
                        .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
                            + "role/perm", HttpMethod.POST, entity, String.class);
                    if (addResponse.getStatusCode().value() != 201) {
                        logger.debug(EELFLoggerDelegate.debugLogger,
                            "addPartnerHasRoleFunctionsToGlobalRole: While adding permission to the role in  External Auth system something went wrong! due to {} and statuscode: {}",
                            addResponse.getStatusCode().getReasonPhrase(), addResponse.getStatusCode().value());
                    } else {
                        logger.debug(EELFLoggerDelegate.debugLogger,
                            "addPartnerHasRoleFunctionsToGlobalRole: Finished adding permissions to roles in External Auth system and status code: {} ",
                            addResponse.getStatusCode().value());
                    }
                } catch (Exception e) {
                    logger.error(EELFLoggerDelegate.errorLogger,
                        "addPartnerHasRoleFunctionsToGlobalRole: Failed for POST request: {} due to ", addPerms, e);
                }
            }
        }
    }

    private void deleteRoleInExtSystem(ObjectMapper mapper, String name)
        throws JsonProcessingException, Exception, ExternalAuthSystemException {
        ResponseEntity<String> deleteResponse;
        Map<String, String> delRoleKeyMapper = new HashMap<>();
        delRoleKeyMapper.put(ROLE_NAME, name);
        String delRoleKeyValue = mapper.writeValueAsString(delRoleKeyMapper);
        deleteResponse = deleteRoleInExternalSystem(delRoleKeyValue);
        if (deleteResponse.getStatusCode().value() != 200) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "updateRoleInExternalSystem:  Failed to delete role in external system due to {} ",
                deleteResponse.getBody());
            throw new ExternalAuthSystemException(deleteResponse.getBody());
        }
    }

    public void addRole(Role addRole, String uebkey) throws Exception {
        boolean response = false;
        ResponseEntity<String> addResponse = null;
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        FnApp app = getApp(uebkey).get(0);
        String newRole = updateExistingRoleInExternalSystem(addRole.getName(), app.getAuthNamespace());
        HttpEntity<String> entity = new HttpEntity<>(newRole, headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "addRole: Connecting to External Auth system");
        addResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
            HttpMethod.POST, entity, String.class);
        if (addResponse.getStatusCode().value() == 201) {
            response = true;
            logger.debug(EELFLoggerDelegate.debugLogger,
                "addRole: Finished adding role in the External Auth system  and response code: {} ",
                addResponse.getStatusCode().value());
        }
        if (addResponse.getStatusCode().value() == 406) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "addRole: Failed to add in the External Auth system due to {} and status code: {}",
                addResponse.getBody(), addResponse.getStatusCode().value());
        }
    }

    private ResponseEntity<String> deleteRoleInExternalSystem(String delRole) throws Exception {
        ResponseEntity<String> delResponse = null;
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(delRole, headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "deleteRoleInExternalSystem: {} for DELETE: {}",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, delRole);
        delResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role?force=true",
            HttpMethod.DELETE, entity, String.class);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "deleteRoleInExternalSystem: Finished DELETE operation in the External Auth system {} and status code: {} ",
            delRole, delResponse.getStatusCode().value());
        return delResponse;
    }

    private String getSingleAppRole(String addRole, FnApp app) throws Exception {
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = null;
        logger.debug(EELFLoggerDelegate.debugLogger, "getSingleAppRole: Connecting to External Auth system");
        response = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "roles/"
                + app.getAuthNamespace() + "." + addRole
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"),
            HttpMethod.GET, entity, String.class);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "getSingleAppRole: Finished GET app role from External Auth system and status code: {} ",
            response.getStatusCode().value());
        return response.getBody();
    }

    private void addFunctionsTOGlobalRole(List<FnRole> epRoleList, Role updateExtRole,
        List<FnFunction> roleFunctionListNew, ObjectMapper mapper, FnApp app, FnApp portalAppInfo)
        throws Exception {
        try {
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering into addFunctionsTOGlobalRole");
            // GET Permissions from External Auth System
            JSONArray extPerms = getExtAuthPermissions(app.getAuthNamespace());
            List<ExternalAccessPermsDetail> permsDetailList = getExtAuthPerrmissonList(app, extPerms);
            final Map<String, ExternalAccessPermsDetail> existingPermsWithRoles = new HashMap<>();
            final Map<String, ExternalAccessPermsDetail> existingPermsWithRolesWithPipes = new HashMap<>();
            final Map<String, FnFunction> userRquestedFunctionsMap = new HashMap<>();
            final Map<String, FnFunction> userRquestedFunctionsMapPipesFilter = new HashMap<>();
            for (ExternalAccessPermsDetail permDetail : permsDetailList) {
                existingPermsWithRoles.put(EcompPortalUtils.getFunctionCode(permDetail.getInstance()), permDetail);
                existingPermsWithRolesWithPipes.put(permDetail.getInstance(), permDetail);
            }
            // Add If function does not exists for role in External Auth System
            for (FnFunction roleFunc : roleFunctionListNew) {
                String roleFuncCode = "";
                ExternalAccessPermsDetail permsDetail;
                if (roleFunc.getCode().contains(FUNCTION_PIPE)) {
                    roleFuncCode = roleFunc.getCode();
                    permsDetail = existingPermsWithRolesWithPipes.get(roleFunc.getCode());
                } else {
                    roleFuncCode = EcompPortalUtils.getFunctionCode(roleFunc.getCode());
                    permsDetail = existingPermsWithRoles.get(roleFuncCode);
                }
                if (null == permsDetail.getRoles()
                    || !permsDetail.getRoles()
                    .contains(portalAppInfo.getAuthNamespace() + FUNCTION_PIPE
                        + epRoleList.get(0).getRoleName().replaceAll(
                        EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS,
                        "_"))) {
                    addRoleFunctionsToGlobalRoleInExternalSystem(roleFunc, updateExtRole, mapper, app, portalAppInfo);
                }
                userRquestedFunctionsMap.put(roleFuncCode, roleFunc);
                userRquestedFunctionsMapPipesFilter.put(EcompPortalUtils.getFunctionCode(roleFuncCode), roleFunc);
            }
            List<GlobalRoleWithApplicationRoleFunction> globalRoleFunctionList = entityManager
                .createNamedQuery("getGlobalRoleForRequestedApp")
                .setParameter("requestedAppId", app.getId())
                .setParameter("roleId", updateExtRole.getId())
                .getResultList();
            for (GlobalRoleWithApplicationRoleFunction globalRoleFunc : globalRoleFunctionList) {
                String globalRoleFuncWithoutPipes = "";
                FnFunction roleFunc = null;
                if (globalRoleFunc.getFunctionCd().contains(FUNCTION_PIPE)) {
                    globalRoleFuncWithoutPipes = globalRoleFunc.getFunctionCd();
                    roleFunc = userRquestedFunctionsMap.get(globalRoleFuncWithoutPipes);
                } else {
                    globalRoleFuncWithoutPipes = EcompPortalUtils.getFunctionCode(globalRoleFunc.getFunctionCd());
                    roleFunc = userRquestedFunctionsMapPipesFilter.get(globalRoleFuncWithoutPipes);
                }
                if (roleFunc == null) {
                    ExternalAccessPermsDetail permDetailFromMap = globalRoleFunc.getFunctionCd().contains(FUNCTION_PIPE)
                        ? existingPermsWithRolesWithPipes.get(globalRoleFuncWithoutPipes)
                        : existingPermsWithRoles.get(globalRoleFuncWithoutPipes);
                    ExternalAccessPerms perm = new ExternalAccessPerms(permDetailFromMap.getType(),
                        EcompPortalUtils.getFunctionCode(permDetailFromMap.getInstance()),
                        permDetailFromMap.getAction());
                    String roleName = portalAppInfo.getAuthNamespace() + "." + globalRoleFunc.getRoleName()
                        .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_");
                    HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
                    removePermForRole(perm, mapper, roleName, headers);
                }
            }
            logger.debug(EELFLoggerDelegate.debugLogger, "Finished addFunctionsTOGlobalRole");
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addFunctionsTOGlobalRole: Failed", e);
            throw e;
        }
    }

    private void removePermForRole(ExternalAccessPerms perm, ObjectMapper permMapper, String name, HttpHeaders headers)
        throws ExternalAuthSystemException, JsonProcessingException {
        ExternalAccessRolePerms extAccessRolePerms = new ExternalAccessRolePerms(perm, name);
        String permDetails = permMapper.writeValueAsString(extAccessRolePerms);
        try {
            HttpEntity<String> deleteEntity = new HttpEntity<>(permDetails, headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "removePermForRole: {} for DELETE: {} ",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, permDetails);
            ResponseEntity<String> deletePermResponse = template
                .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
                    + "role/" + name + "/perm", HttpMethod.DELETE, deleteEntity, String.class);
            if (deletePermResponse.getStatusCode().value() != 200) {
                throw new ExternalAuthSystemException(deletePermResponse.getBody());
            }
            logger.debug(EELFLoggerDelegate.debugLogger,
                "removePermForRole: Finished deleting permission to role in External Auth system: {} and status code: {}",
                permDetails, deletePermResponse.getStatusCode().value());
        } catch (Exception e) {
            if (e.getMessage().contains("404")) {
                logger.error(EELFLoggerDelegate.errorLogger, "Failed to add role for DELETE request: {} due to {}",
                    permDetails, e.getMessage());
            } else {
                throw e;
            }
        }
    }

    private void addRoleFunctionsToGlobalRoleInExternalSystem(FnFunction addFunction, Role globalRole,
        ObjectMapper mapper, FnApp app, FnApp portalAppInfo) throws Exception {
        try {
            logger.debug(EELFLoggerDelegate.debugLogger, "Entering into addRoleFunctionsToGlobalRoleInExternalSystem");
            ExternalAccessRolePerms extAddRolePerms = null;
            ExternalAccessPerms extAddPerms = null;
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            String code = "";
            String type = "";
            String action = "";
            if (addFunction.getFunctionCd().contains(FUNCTION_PIPE)) {
                code = EcompPortalUtils.getFunctionCode(addFunction.getFunctionCd());
                type = getFunctionCodeType(addFunction.getFunctionCd());
                action = getFunctionCodeAction(addFunction.getFunctionCd());
            } else {
                code = addFunction.getFunctionCd();
                type = addFunction.getFunctionCd().contains("menu") ? "menu" : "url";
                action = "*";
            }
            extAddPerms = new ExternalAccessPerms(app.getAuthNamespace() + "." + type, code, action);
            extAddRolePerms = new ExternalAccessRolePerms(extAddPerms,
                portalAppInfo.getAuthNamespace() + "." + globalRole
                    .getName().replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
            String updateRolePerms = mapper.writeValueAsString(extAddRolePerms);
            HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "addRoleFunctionsInExternalSystem: {} ",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
            ResponseEntity<String> addResponse = template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role/perm",
                HttpMethod.POST, entity, String.class);
            if (addResponse.getStatusCode().value() != 201) {
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "addRoleFunctionsInExternalSystem: While adding permission to the role in  External Auth system something went wrong! due to {} and statuscode: {}",
                    addResponse.getStatusCode().getReasonPhrase(), addResponse.getStatusCode().value());
            } else {
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "addRoleFunctionsInExternalSystem: Finished adding permissions to roles in External Auth system and status code: {} ",
                    addResponse.getStatusCode().value());
            }
            logger.debug(EELFLoggerDelegate.debugLogger, "Finished addRoleFunctionsToGlobalRoleInExternalSystem");
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunctionsToGlobalRoleInExternalSystem: Failed", e);
            throw e;
        }
    }

    private List<FnRoleFunction> convertSetToListOfRoleFunctions(Role updateExtRole) {
        Set<FnRoleFunction> roleFunctionSetList = updateExtRole.getRoleFunctions();
        List<FnRoleFunction> roleFunctionList = new ArrayList<>();
        ObjectMapper roleFuncMapper = new ObjectMapper();
        for (Object nextValue : roleFunctionSetList) {
            FnRoleFunction roleFunction = roleFuncMapper.convertValue(nextValue, FnRoleFunction.class);
            roleFunctionList.add(roleFunction);
        }
        return roleFunctionList.stream().distinct().collect(Collectors.toList());
    }

    private List<FnRole> getPartnerAppRoleInfo(Long roleId, Long appId) {
        List<FnRole> roleInfo = fnRoleService.retrieveAppRoleByAppRoleIdAndByAppId(roleId, appId);
        if (roleInfo.isEmpty()) {
            roleInfo = fnRoleService.retrieveAppRoleByAppRoleIdAndByAppId(appId, roleId);
        }
        return roleInfo;
    }

    private void inactiveRolesNotInExternalAuthSystem(final Long appId, List<FnRole> finalRoleList,
        List<FnRole> applicationRolesList) {
        final Map<String, FnRole> checkRolesInactive = new HashMap<>();
        for (FnRole extrole : finalRoleList) {
            checkRolesInactive.put(extrole.getRoleName(), extrole);
        }
        for (FnRole role : applicationRolesList) {
            try {
                List<FnRole> roleList;
                if (!checkRolesInactive.containsKey(role.getRoleName())) {
                    if (appId == 1) {
                        roleList = fnRoleService.retrieveAppRolesByRoleNameAndWhereAppIdIsNull(role.getRoleName());
                    } else {
                        roleList = fnRoleService.retrieveAppRolesByRoleNameAndByAppId(role.getRoleName(), appId);
                    }
                    if (!roleList.isEmpty()) {
                        FnRole updateRoleInactive = roleList.get(0);
                        updateRoleInactive.setActiveYn(false);
                        fnRoleService.saveOne(updateRoleInactive);
                    }
                }
            } catch (Exception e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "syncApplicationRolesWithEcompDB: Failed to de-activate role ", e);
            }
        }
    }

    private JSONArray getExtAuthPermissions(String authNamespace) throws Exception {
        ResponseEntity<String> response = null;
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "syncRoleFunctionFromExternalAccessSystem: {} ",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
        response = template.exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
            + "perms/ns/" + authNamespace, HttpMethod.GET, entity, String.class);
        String res = response.getBody();
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncRoleFunctionFromExternalAccessSystem: Finished GET permissions from External Auth system and response: {} ",
            response.getBody());
        JSONObject jsonObj = new JSONObject(res);
        JSONArray extPerms = jsonObj.getJSONArray("perm");
        for (int i = 0; i < extPerms.length(); i++) {
            if (extPerms.getJSONObject(i).getString("type").equals(authNamespace + ".access")) {
                extPerms.remove(i);
                i--;
            }
        }
        return extPerms;
    }

    public void syncRoleFunctionFromExternalAccessSystem(FnApp app) {
        try {
            // get Permissions from External Auth System
            JSONArray extPerms = getExtAuthPermissions(app.getAuthNamespace());
            List<ExternalAccessPermsDetail> permsDetailList = getExtAuthPerrmissonList(app, extPerms);
            final Map<String, EpAppFunction> roleFuncMap = new HashMap<>();
            List<EpAppFunction> appFunctions = epAppFunctionService.getAllRoleFunctions(app.getId());
            if (!appFunctions.isEmpty()) {
                for (EpAppFunction roleFunc : appFunctions) {
                    roleFuncMap.put(roleFunc.getFunctionCd(), roleFunc);
                }
            }
            // get Roles for portal in DB
            List<FnRole> portalRoleList = getGlobalRolesOfPortal();
            final Map<String, FnRole> existingPortalRolesMap = new HashMap<>();
            for (FnRole epRole : portalRoleList) {
                existingPortalRolesMap.put(epRole.getRoleName().replaceAll(
                    EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"), epRole);
            }
            // get Roles in DB
            final Map<String, FnRole> currentRolesInDB = getAppRoleNamesWithUnderscoreMap(app);
            // store External Permissions with Pipe and without Pipe (just
            // instance)
            final Map<String, ExternalAccessPermsDetail> extAccessPermsContainsPipeMap = new HashMap<>();
            final Map<String, ExternalAccessPermsDetail> extAccessPermsMap = new HashMap<>();
            for (ExternalAccessPermsDetail permsDetailInfoWithPipe : permsDetailList) {
                extAccessPermsContainsPipeMap.put(permsDetailInfoWithPipe.getInstance(), permsDetailInfoWithPipe);
                String finalFunctionCodeVal = EcompPortalUtils.getFunctionCode(permsDetailInfoWithPipe.getInstance());
                extAccessPermsMap.put(finalFunctionCodeVal, permsDetailInfoWithPipe);
            }
            // Add if new functions and app role functions were added in
            // external auth system
            for (ExternalAccessPermsDetail permsDetail : permsDetailList) {
                String code = permsDetail.getInstance();
                EpAppFunction getFunctionCodeKey = roleFuncMap.get(permsDetail.getInstance());
                List<EpAppFunction> roleFunctionList = addGetLocalFunction(app, roleFuncMap, permsDetail, code,
                    getFunctionCodeKey);
                List<String> roles = permsDetail.getRoles();
                if (roles != null) {
                    addRemoveIfFunctionsRolesIsSyncWithExternalAuth(app, currentRolesInDB, roleFunctionList, roles,
                        existingPortalRolesMap);
                }
            }
            // Check if function does exits in External Auth System but exits in
            // local then delete function and its dependencies
            for (EpAppFunction roleFunc : appFunctions) {
                try {
                    ExternalAccessPermsDetail getFunctionCodeContainsPipeKey = extAccessPermsContainsPipeMap
                        .get(roleFunc.getFunctionCd());
                    if (null == getFunctionCodeContainsPipeKey) {
                        ExternalAccessPermsDetail getFunctionCodeKey = extAccessPermsMap.get(roleFunc.getFunctionCd());
                        if (null == getFunctionCodeKey) {
                            deleteAppRoleFuncDoesNotExitsInExtSystem(app.getId(), roleFunc.getFunctionCd());
                        }
                    }
                } catch (Exception e) {
                    logger.error(EELFLoggerDelegate.errorLogger,
                        "syncRoleFunctionFromExternalAccessSystem: Failed to delete function", e);
                }
            }
            logger.debug(EELFLoggerDelegate.debugLogger,
                "syncRoleFunctionFromExternalAccessSystem: Finished syncRoleFunctionFromExternalAccessSystem");
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "syncRoleFunctionFromExternalAccessSystem: Failed syncRoleFunctionFromExternalAccessSystem", e);
        }
    }

    private List<EpAppFunction> addGetLocalFunction(FnApp app,
        final Map<String, EpAppFunction> roleFuncMap, ExternalAccessPermsDetail permsDetail, String code,
        EpAppFunction getFunctionCodeKey) {
        String finalFunctionCodeVal = addToLocalIfFunctionNotExists(app, roleFuncMap, permsDetail, code,
            getFunctionCodeKey);
        List<EpAppFunction> roleFunctionList = epAppFunctionService
            .getAppFunctionOnCodeAndAppId(app.getId(), finalFunctionCodeVal);
        if (roleFunctionList.isEmpty()) {
            roleFunctionList = epAppFunctionService.getAppFunctionOnCodeAndAppId(app.getId(), code);
        }
        return roleFunctionList;
    }

    private String addToLocalIfFunctionNotExists(FnApp app, final Map<String, EpAppFunction> roleFuncMap,
        ExternalAccessPermsDetail permsDetail, String code, EpAppFunction getFunctionCodeKey) {
        String finalFunctionCodeVal = "";
        if (null == getFunctionCodeKey) {
            finalFunctionCodeVal = EcompPortalUtils.getFunctionCode(permsDetail.getInstance());
            EpAppFunction checkIfCodeStillExits = roleFuncMap.get(finalFunctionCodeVal);
            // If function does not exist in local then add!
            if (null == checkIfCodeStillExits) {
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "syncRoleFunctionFromExternalAccessSystem: Adding function: {} ", code);
                addFunctionInEcompDB(app, permsDetail, code);
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "syncRoleFunctionFromExternalAccessSystem: Finished adding function: {} ", code);
            }
        }
        return finalFunctionCodeVal;
    }

    private void addFunctionInEcompDB(FnApp app, ExternalAccessPermsDetail permsDetail, String code) {
        try {
            EpAppFunction addFunction = new EpAppFunction();
            addFunction.setAppId(app);
            addFunction.setFunctionCd(code);
            addFunction.setFunctionName(permsDetail.getDescription());
            epAppFunctionService.save(addFunction);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addFunctionInEcompDB: Failed to add function", e);
        }
    }

    private List<ExternalAccessPermsDetail> getExtAuthPerrmissonList(FnApp app, JSONArray extPerms) throws IOException {
        ExternalAccessPermsDetail permDetails = null;
        List<ExternalAccessPermsDetail> permsDetailList = new ArrayList<>();
        for (int i = 0; i < extPerms.length(); i++) {
            String description = null;
            if (extPerms.getJSONObject(i).has("description")) {
                description = extPerms.getJSONObject(i).getString(EXTERNAL_AUTH_ROLE_DESCRIPTION);
            } else {
                description =
                    extPerms.getJSONObject(i).getString("type").substring(app.getAuthNamespace().length() + 1) + "|"
                        + extPerms.getJSONObject(i).getString("instance") + "|"
                        + extPerms.getJSONObject(i).getString("action");
            }
            if (extPerms.getJSONObject(i).has("roles")) {
                ObjectMapper rolesListMapper = new ObjectMapper();
                JSONArray resRoles = extPerms.getJSONObject(i).getJSONArray("roles");
                List<String> list = rolesListMapper.readValue(resRoles.toString(),
                    TypeFactory.defaultInstance().constructCollectionType(List.class, String.class));
                permDetails = new ExternalAccessPermsDetail(extPerms.getJSONObject(i).getString("type"),
                    extPerms.getJSONObject(i).getString("type").substring(app.getAuthNamespace().length() + 1)
                        + FUNCTION_PIPE + extPerms.getJSONObject(i).getString("instance") + FUNCTION_PIPE
                        + extPerms.getJSONObject(i).getString("action"),
                    extPerms.getJSONObject(i).getString("action"), list, description);
                permsDetailList.add(permDetails);
            } else {
                permDetails = new ExternalAccessPermsDetail(extPerms.getJSONObject(i).getString("type"),
                    extPerms.getJSONObject(i).getString("type").substring(app.getAuthNamespace().length() + 1)
                        + FUNCTION_PIPE + extPerms.getJSONObject(i).getString("instance") + FUNCTION_PIPE
                        + extPerms.getJSONObject(i).getString("action"),
                    extPerms.getJSONObject(i).getString("action"), description);
                permsDetailList.add(permDetails);
            }
        }
        return permsDetailList;
    }

    public List<FnRole> getGlobalRolesOfPortal() {
        List<FnRole> globalRoles = new ArrayList<>();
        try {
            globalRoles = fnRoleService.getGlobalRolesOfPortal();
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getGlobalRolesOfPortal failed", e);
        }
        return globalRoles;
    }

    private void deleteAppRoleFuncDoesNotExitsInExtSystem(final Long appId, final String roleFunc) {
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncRoleFunctionFromExternalAccessSystem: Deleting app role function {}", roleFunc);
        epAppRoleFunctionService.deleteByAppIdAndFunctionCd(appId, roleFunc);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncRoleFunctionFromExternalAccessSystem: Deleted app role function {}", roleFunc);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncRoleFunctionFromExternalAccessSystem: Deleting app function {}", roleFunc);
        epAppFunctionService.deleteByAppIdAndFunctionCd(appId, roleFunc);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "syncRoleFunctionFromExternalAccessSystem: Deleted app function {}", roleFunc);
    }

    private CentralV2Role convertRoleToCentralV2Role(FnRole role) {
        return CentralV2Role.builder().id(role.getId()).created(role.getCreated())
            .modified(role.getModified()).createdId(role.getCreatedId().getId())
            .modifiedId(role.getModifiedId().getId())
            .rowNum(role.getRowNum()).name(role.getRoleName()).active(role.getActiveYn())
            .priority(role.getPriority()).roleFunctions(new TreeSet<>()).childRoles(new TreeSet<>())
            .parentRoles(new TreeSet<>()).build();
    }

    private void addRemoveIfFunctionsRolesIsSyncWithExternalAuth(FnApp app, final Map<String, FnRole> currentRolesInDB,
        List<EpAppFunction> roleFunctionList, List<String> roles,
        Map<String, FnRole> existingPortalRolesMap) throws Exception {
        if (!roleFunctionList.isEmpty()) {
            final Map<String, LocalRole> currentAppRoleFunctionsMap = new HashMap<>();
            final Map<String, String> currentRolesInExtSystem = new HashMap<>();
            List<LocalRole> localRoleList = localRoleService
                .getCurrentAppRoleFunctions(app.getId(), roleFunctionList.get(0).getFunctionCd());
            for (LocalRole localRole : localRoleList) {
                currentAppRoleFunctionsMap.put(localRole.getRolename().replaceAll(
                    EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"), localRole);
            }
            for (String addRole : roles) {
                currentRolesInExtSystem.put(addRole.substring(addRole.indexOf(FUNCTION_PIPE) + 1), addRole);
            }
            for (String extAuthrole : roles) {
                String roleNameSpace = extAuthrole.substring(0, extAuthrole.indexOf(FUNCTION_PIPE));
                boolean isNameSpaceMatching = EcompPortalUtils.checkNameSpaceMatching(roleNameSpace,
                    app.getAuthNamespace());
                if (isNameSpaceMatching) {
                    if (!currentAppRoleFunctionsMap
                        .containsKey(extAuthrole.substring(app.getAuthNamespace().length() + 1))) {
                        FnRole localAddFuntionRole = currentRolesInDB
                            .get(extAuthrole.substring(app.getAuthNamespace().length() + 1));
                        if (localAddFuntionRole == null) {
                            checkAndAddRoleInDB(app, currentRolesInDB, roleFunctionList, extAuthrole);
                        } else {
                            EpAppRoleFunction addAppRoleFunc = new EpAppRoleFunction();
                            addAppRoleFunc.setAppId(app);
                            addAppRoleFunc.setEpAppFunction(roleFunctionList.get(0));
                            addAppRoleFunc.setFnRole(localAddFuntionRole);
                            epAppRoleFunctionService.save(addAppRoleFunc);
                        }
                    }
                    // This block is to save global role function if exists
                } else {
                    String extAuthAppRoleName = extAuthrole.substring(extAuthrole.indexOf(FUNCTION_PIPE) + 1);
                    boolean checkIfGlobalRoleExists = existingPortalRolesMap.containsKey(extAuthAppRoleName);
                    if (checkIfGlobalRoleExists) {
                        FnRole role = existingPortalRolesMap.get(extAuthAppRoleName);
                        EpAppRoleFunction addGlobalRoleFunctions = new EpAppRoleFunction();
                        List<EpAppRoleFunction> currentGlobalRoleFunctionsList = epAppRoleFunctionService
                            .getAppRoleFunctionOnRoleIdAndAppId(app.getId(), role.getId());
                        boolean checkIfRoleFunctionExists = currentGlobalRoleFunctionsList.stream()
                            .anyMatch(currentGlobalRoleFunction -> currentGlobalRoleFunction.getEpAppFunction()
                                .getFunctionCd()
                                .equals(roleFunctionList.get(0).getFunctionCd()));
                        if (!checkIfRoleFunctionExists) {
                            addGlobalRoleFunctions.setAppId(app);
                            addGlobalRoleFunctions.setFnRole(role);
                            if (!app.getId().equals(role.getAppRoleId())) {
                                addGlobalRoleFunctions.setRoleAppId((PortalConstants.PORTAL_APP_ID).toString());
                            } else {
                                addGlobalRoleFunctions.setRoleAppId(null);
                            }
                            addGlobalRoleFunctions.setEpAppFunction(roleFunctionList.get(0));
                            epAppRoleFunctionService.save(addGlobalRoleFunctions);
                        }
                    }
                }
            }
            for (LocalRole localRoleDelete : localRoleList) {
                if (!currentRolesInExtSystem.containsKey(localRoleDelete.getRolename()
                    .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"))) {
                    epAppRoleFunctionService
                        .deleteByAppIdAndFunctionCdAndRoleId(app.getId(), roleFunctionList.get(0).getFunctionCd(),
                            localRoleDelete.getRoleId());
                }
            }
        }
    }

    private void checkAndAddRoleInDB(FnApp app, final Map<String, FnRole> currentRolesInDB,
        List<EpAppFunction> roleFunctionList, String roleList) throws Exception {
        if (!currentRolesInDB.containsKey(roleList.substring(app.getAuthNamespace().length() + 1))) {
            FnRole role = addRoleInDBIfDoesNotExists(app.getId(),
                roleList.substring(app.getAuthNamespace().length() + 1));
            addRoleDescriptionInExtSystem(role.getRoleName(), app.getAuthNamespace());
            if (!roleFunctionList.isEmpty()) {
                try {
                    EpAppRoleFunction addAppRoleFunc = new EpAppRoleFunction();
                    addAppRoleFunc.setAppId(app);
                    addAppRoleFunc.setEpAppFunction(roleFunctionList.get(0));
                    addAppRoleFunc.setFnRole(role);
                    epAppRoleFunctionService.save(addAppRoleFunc);
                } catch (Exception e) {
                    logger.error(EELFLoggerDelegate.errorLogger,
                        "syncRoleFunctionFromExternalAccessSystem: Failed to save app role function ", e);
                }
            }
        }
    }

    private FnRole addRoleInDBIfDoesNotExists(final Long appId, final String role) {
        FnRole setNewRole = new FnRole();
        try {
            boolean isCreated = checkIfRoleExitsElseCreateInSyncFunctions(role, appId);
            List<FnRole> getRoleCreated = null;
            if (!appId.equals(PortalConstants.PORTAL_APP_ID)) {
                List<FnRole> roleCreated = fnRoleService.retrieveAppRolesByRoleNameAndByAppId(role, appId);
                if (!isCreated) {
                    FnRole epUpdateRole = roleCreated.get(0);
                    epUpdateRole.setAppRoleId(epUpdateRole.getId());
                    fnRoleService.saveOne(epUpdateRole);
                    getRoleCreated = fnRoleService.retrieveAppRolesByRoleNameAndByAppId(role, appId);
                } else {
                    getRoleCreated = roleCreated;
                }
            } else {
                getRoleCreated = fnRoleService.retrieveAppRolesByRoleNameAndWhereAppIdIsNull(role);
            }
            if (getRoleCreated != null && !getRoleCreated.isEmpty()) {
                FnRole roleObject = getRoleCreated.get(0);
                setNewRole.setId(roleObject.getId());
                setNewRole.setRoleName(roleObject.getRoleName());
                setNewRole.setActiveYn(roleObject.getActiveYn());
                setNewRole.setPriority(roleObject.getPriority());
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addRoleInDBIfDoesNotExists: Failed", e);
        }
        return setNewRole;
    }

    private boolean checkIfRoleExitsElseCreateInSyncFunctions(final String role, final long appId) {
        boolean isCreated;
        List<FnRole> roleCreated = null;
        if (appId == PortalConstants.PORTAL_APP_ID) {
            roleCreated = fnRoleService.retrieveAppRolesByRoleNameAndWhereAppIdIsNull(role);
        } else {
            roleCreated = fnRoleService.retrieveAppRolesByRoleNameAndByAppId(role, appId);
        }
        if (roleCreated == null || roleCreated.isEmpty()) {
            FnRole epRoleNew = new FnRole();
            epRoleNew.setActiveYn(true);
            epRoleNew.setRoleName(role);
            if (appId == PortalConstants.PORTAL_APP_ID) {
                epRoleNew.setAppId(null);
            } else {
                epRoleNew.setAppId(appId);
            }
            fnRoleService.saveOne(epRoleNew);
            isCreated = false;
        } else {
            isCreated = true;
        }
        return isCreated;
    }

    private String updateExistingRoleInExternalSystem(final String roleName, final String authNamespace)
        throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        String addNewRole = "";
        ExternalAccessRole extRole = new ExternalAccessRole();
        extRole.setName(authNamespace + "." + roleName
            .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
        extRole.setDescription(String.valueOf(roleName));
        addNewRole = mapper.writeValueAsString(extRole);
        return addNewRole;
    }

    private boolean addRoleDescriptionInExtSystem(final String roleName, final String authNamespace) throws Exception {
        boolean status = false;
        try {
            String addRoleNew = updateExistingRoleInExternalSystem(roleName, authNamespace);
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            HttpEntity<String> entity = new HttpEntity<>(addRoleNew, headers);
            template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
                HttpMethod.PUT, entity, String.class);
            status = true;
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to addRoleDescriptionInExtSystem", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addRoleDescriptionInExtSystem: Failed", e);
        }
        return status;
    }

    public List<CentralRole> convertV2CentralRoleListToOldVerisonCentralRoleList(List<CentralV2Role> v2CenRoleList) {
        List<CentralRole> cenRoleList = new ArrayList<>();
        for (CentralV2Role v2CenRole : v2CenRoleList) {
            SortedSet<EpAppFunction> cenRoleFuncList = new TreeSet<>();
            for (DomainVo vo : v2CenRole.getRoleFunctions()) {
                Optional<FnRoleFunction> v2CenRoleFunc = fnRoleFunctionService.findById(vo.getId());
                if (v2CenRoleFunc.isPresent()) {
                    EpAppFunction roleFunc = EpAppFunction.builder()
                        .functionCd(v2CenRoleFunc.get().getFunctionCd().getCode())
                        .functionName(v2CenRoleFunc.get().getRole().getRoleName())
                        .build();
                    cenRoleFuncList.add(roleFunc);
                }
            }
            CentralRole role = new CentralRole(v2CenRole.getId(), v2CenRole.getName(), v2CenRole.isActive(),
                v2CenRole.getPriority(), cenRoleFuncList);
            cenRoleList.add(role);
        }
        return cenRoleList;
    }

    public ExternalRequestFieldsValidator saveRoleForApplication(Role saveRole, String uebkey) throws Exception {
        boolean response = false;
        String message = "";
        try {
            FnApp app = getApp(uebkey).get(0);
            addRoleInEcompDB(saveRole, app);
            response = true;
        } catch (Exception e) {
            message = e.getMessage();
            logger.error(EELFLoggerDelegate.errorLogger, "saveRoleForApplication failed", e);
        }
        return new ExternalRequestFieldsValidator(response, message);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addRoleInEcompDB(Role addRoleInDB, FnApp app) throws Exception {
        boolean result;
        FnRole epRole;
        Set<FnFunction> roleFunctionList = addRoleInDB.getRoleFunctions();
        List<FnFunction> roleFunctionListNew = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (Object nextValue : roleFunctionList) {
            FnFunction roleFunction = mapper.convertValue(nextValue, FnFunction.class);
            roleFunctionListNew.add(roleFunction);
        }
        List<FnFunction> listWithoutDuplicates = roleFunctionListNew.stream().distinct().collect(Collectors.toList());
        try {
            if (addRoleInDB.getId() == null) { // check if it is new role
                if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                    checkIfRoleExitsInExternalSystem(addRoleInDB, app);
                }
                FnRole epRoleNew = new FnRole();
                epRoleNew.setActiveYn(addRoleInDB.getActive());
                epRoleNew.setRoleName(addRoleInDB.getName());
                epRoleNew.setPriority(addRoleInDB.getPriority());
                if (app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                    epRoleNew.setAppId(null);
                } else {
                    epRoleNew.setAppId(app.getId());
                }
                fnRoleService.saveOne(epRoleNew);
                List<FnRole> getRoleCreated = null;
                if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                    List<FnRole> roleCreated = fnRoleService
                        .retrieveAppRolesByRoleNameAndByAppId(addRoleInDB.getName(), app.getId());
                    FnRole epUpdateRole = roleCreated.get(0);
                    epUpdateRole.setAppRoleId(epUpdateRole.getId());
                    fnRoleService.saveOne(epUpdateRole);
                    getRoleCreated = fnRoleService
                        .retrieveAppRolesByRoleNameAndByAppId(addRoleInDB.getName(), app.getId());
                } else {
                    getRoleCreated = fnRoleService.retrieveAppRolesByRoleNameAndWhereAppIdIsNull(addRoleInDB.getName());
                }
                // Add role in External Auth system
                if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                    addNewRoleInExternalSystem(getRoleCreated, app);
                }
                result = true;
            } else { // if role already exists then update it
                FnRole globalRole = null;
                List<FnRole> applicationRoles;
                List<FnRole> globalRoleList = getGlobalRolesOfPortal();
                boolean isGlobalRole = false;
                if (!globalRoleList.isEmpty()) {
                    FnRole role = globalRoleList.stream().filter(x -> addRoleInDB.getId().equals(x.getId())).findAny()
                        .orElse(null);
                    if (role != null) {
                        globalRole = role;
                        isGlobalRole = true;
                    }
                }
                if (app.getId().equals(PortalConstants.PORTAL_APP_ID)
                    || (globalRole != null && app.getId() != globalRole.getAppId())) {
                    applicationRoles = getPortalAppRoleInfo(addRoleInDB.getId());
                } else {
                    applicationRoles = getPartnerAppRoleInfo(addRoleInDB.getId(), app.getId());
                }
                if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                    updateRoleInExternalSystem(addRoleInDB, app, isGlobalRole);
                    // Add all user to the re-named role in external auth system
                    if (!applicationRoles.isEmpty()
                        && !addRoleInDB.getName().equals(applicationRoles.get(0).getRoleName())) {
                        bulkUploadUsersSingleRole(app.getUebKey(), applicationRoles.get(0).getId(),
                            addRoleInDB.getName());
                    }
                }
                deleteRoleFunction(app, applicationRoles);
                if (!applicationRoles.isEmpty()) {
                    epRole = applicationRoles.get(0);
                    epRole.setRoleName(addRoleInDB.getName());
                    epRole.setPriority(addRoleInDB.getPriority());
                    epRole.setActiveYn(addRoleInDB.getActive());
                    if (app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                        epRole.setAppId(null);
                        epRole.setAppRoleId(null);
                    } else if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)
                        && applicationRoles.get(0).getAppRoleId() == null) {
                        epRole.setAppRoleId(epRole.getId());
                    }
                    fnRoleService.saveOne(epRole);
                }
                Long roleAppId = null;
                if (globalRole != null && !app.getId().equals(globalRole.getAppId())) {
                    roleAppId = PortalConstants.PORTAL_APP_ID;
                }
                saveRoleFunction(listWithoutDuplicates, app, applicationRoles, roleAppId);
                result = true;
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "addRoleInEcompDB is failed", e);
            throw e;
        }
    }

    private void saveRoleFunction(List<FnFunction> roleFunctionListNew, FnApp app, List<FnRole> applicationRoles,
        Long roleAppId) {
        for (FnFunction roleFunc : roleFunctionListNew) {
            String code = EcompPortalUtils.getFunctionCode(roleFunc.getCode());
            EpAppRoleFunction appRoleFunc = new EpAppRoleFunction();
            appRoleFunc.setAppId(app);
            appRoleFunc.setFnRole(applicationRoles.get(0));
            appRoleFunc.setRoleAppId(String.valueOf(roleAppId));
            List<EpAppFunction> roleFunction = epAppFunctionService.getRoleFunction(roleFunc.getCode(), app.getId());
            if (roleFunction.isEmpty()) {
                roleFunction = epAppFunctionService.getRoleFunction(code, app.getId());
            }
            if (roleFunction.size() > 1) {
                EpAppFunction getExactFunctionCode = appFunctionListFilter(code, roleFunction);
                appRoleFunc.setEpAppFunction(getExactFunctionCode);
            } else {
                appRoleFunc.setEpAppFunction(roleFunction.get(0));
            }
            epAppRoleFunctionService.save(appRoleFunc);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteRoleForApplication(String deleteRole, String uebkey) throws Exception {
        boolean result;
        try {
            List<FnRole> epRoleList;
            FnApp app = getApp(uebkey).get(0);
            if (app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                epRoleList = fnRoleService.retrieveAppRolesByRoleNameAndWhereAppIdIsNull(deleteRole);
            } else {
                epRoleList = fnRoleService.retrieveAppRolesByRoleNameAndByAppId(deleteRole, app.getId());
            }
            if (!epRoleList.isEmpty()) {
                // Delete app role functions before deleting role
                deleteRoleFunction(app, epRoleList);
                if (app.getId() == 1) {
                    // Delete fn_user_ role
                    String query =
                        "DELETE FROM FN_USER_ROLE WHERE " + APP_ID_EQUALS + app.getId() + " and role_id = " + epRoleList
                            .get(0).getId();
                    entityManager.createQuery(query).executeUpdate();
                    boolean isPortalRequest = false;
                    deleteRoleDependencyRecords(epRoleList.get(0).getId(), app.getId(), isPortalRequest);
                }
                deleteRoleInExternalAuthSystem(epRoleList, app);
                logger.debug(EELFLoggerDelegate.debugLogger, "deleteRoleForApplication: committed the transaction");
                fnRoleService.delete(epRoleList.get(0));
            }
            result = true;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleForApplication: failed", e);
            result = false;
        }
        return result;
    }

    private void deleteRoleInExternalAuthSystem(List<FnRole> epRoleList, FnApp app) throws Exception {
        ResponseEntity<String> deleteResponse;
        ResponseEntity<String> res = getNameSpaceIfExists(app);
        if (res.getStatusCode() == HttpStatus.OK) {
            // Delete Role in External System
            String deleteRoleKey = "{\"name\":\"" + app.getAuthNamespace() + "." + epRoleList.get(0).getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_") + "\"}";
            deleteResponse = deleteRoleInExternalSystem(deleteRoleKey);
            if (deleteResponse.getStatusCode().value() != 200 && deleteResponse.getStatusCode().value() != 404) {
                EPLogUtil.logExternalAuthAccessAlarm(logger, deleteResponse.getStatusCode());
                logger.error(EELFLoggerDelegate.errorLogger,
                    "deleteRoleForApplication: Failed to delete role in external auth system! due to {} ",
                    deleteResponse.getBody());
            }
            logger.debug(EELFLoggerDelegate.debugLogger, "deleteRoleForApplication: about to commit the transaction");
        }
    }

    private void deleteRoleFunction(FnApp app, List<FnRole> role) {
        List<EpAppRoleFunction> appRoleFunctionList = epAppRoleFunctionService
            .getAppRoleFunctionOnRoleIdAndAppId(app.getId(), role.get(0).getId());
        epAppRoleFunctionService.deleteInBatch(appRoleFunctionList);
    }

    public List<CentralV2Role> getActiveRoles(String uebkey) throws Exception {
        List<CentralV2Role> roleList = new ArrayList<>();
        try {
            List<FnApp> app = getApp(uebkey);
            Long appId = null;
            if (!app.get(0).getId().equals(PortalConstants.PORTAL_APP_ID)) {
                appId = app.get(0).getId();
            }
            List<FnRole> epRole;
            if (appId == null) {
                epRole = fnRoleService.retrieveActiveRolesWhereAppIdIsNull();
            } else {
                epRole = fnRoleService.retrieveActiveRolesOfApplication(appId);
            }
            roleList = createCentralRoleObject(app, epRole, roleList);
            List<CentralV2Role> globalRoleList = getGlobalRolesOfApplication(app.get(0).getId());
            if (globalRoleList.size() > 0) {
                roleList.addAll(globalRoleList);
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles: failed", e);
            throw e;
        }
        return roleList;
    }

    public Integer bulkUploadRoles(String uebkey) throws Exception {
        List<FnApp> app = getApp(uebkey);
        List<FnRole> roles = getAppRoles(app.get(0).getId());
        List<CentralV2Role> cenRoleList = new ArrayList<>();
        final Map<String, Long> params = new HashMap<>();
        Integer rolesListAdded = 0;
        try {
            cenRoleList = createCentralRoleObject(app, roles, cenRoleList);
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
            String roleList = mapper.writeValueAsString(cenRoleList);
            List<Role> roleObjectList = mapper.readValue(roleList,
                TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
            for (Role role : roleObjectList) {
                addRoleInExternalSystem(role, app.get(0));
                rolesListAdded++;
            }
            if (!app.get(0).getId().equals(PortalConstants.PORTAL_APP_ID)) {
                // Add Account Admin role in External AUTH System
                try {
                    String addAccountAdminRole = "";
                    ExternalAccessRole extRole = new ExternalAccessRole();
                    extRole.setName(app.get(0).getAuthNamespace() + "." + PortalConstants.ADMIN_ROLE
                        .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
                    addAccountAdminRole = mapper.writeValueAsString(extRole);
                    HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
                    HttpEntity<String> entity = new HttpEntity<>(addAccountAdminRole, headers);
                    template.exchange(
                        SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
                        HttpMethod.POST, entity, String.class);
                    rolesListAdded++;
                } catch (HttpClientErrorException e) {
                    logger.error(EELFLoggerDelegate.errorLogger,
                        "HttpClientErrorException - Failed to create Account Admin role", e);
                    EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                } catch (Exception e) {
                    if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                        logger.error(EELFLoggerDelegate.errorLogger,
                            "bulkUploadRoles: Account Admin Role already exits but does not break functionality",
                            e);
                    } else {
                        logger.error(EELFLoggerDelegate.errorLogger,
                            "bulkUploadRoles: Failed to create Account Admin role", e.getMessage());
                    }
                }
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoles: failed", e);
            throw e;
        }
        return rolesListAdded;
    }

    private void addRoleInExternalSystem(Role role, FnApp app) throws Exception {
        String addRoleNew = updateExistingRoleInExternalSystem(role.getName(), app.getAuthNamespace());
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        try {
            HttpEntity<String> entity = new HttpEntity<>(addRoleNew, headers);
            template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
                HttpMethod.POST, entity, String.class);
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - Failed to addRoleInExternalSystem",
                e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleInExternalSystem: Role already exits but does not break functionality", e);
            } else {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleInExternalSystem: Failed to addRoleInExternalSystem", e.getMessage());
            }
        }
    }

    public Integer bulkUploadFunctions(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<FnRoleFunction> roleFuncList = fnRoleFunctionService.findAll();
        EpAppFunction cenRoleFunc;
        Integer functionsAdded = 0;
        try {
            for (FnRoleFunction roleFunc : roleFuncList) {
                cenRoleFunc = EpAppFunction.builder()
                    .functionCd(roleFunc.getFunctionCd().getName())
                    .roleId(roleFunc.getRole().getId())
                    .build();
                addRoleFunctionInExternalSystem(cenRoleFunc, app);
                functionsAdded++;
            }
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - bulkUploadFunctions failed", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions: failed", e.getMessage(), e);
        }
        return functionsAdded;
    }

    public Integer bulkUploadRolesFunctions(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<FnRole> roles = getAppRoles(app.getId());
        Integer roleFunctions = 0;
        try {
            for (FnRole role : roles) {
                List<BulkUploadRoleFunction> appRoleFunc = bulkUploadUserRolesService
                    .uploadAllRoleFunctions(role.getId());
                if (!appRoleFunc.isEmpty()) {
                    for (BulkUploadRoleFunction addRoleFunc : appRoleFunc) {
                        addRoleFunctionsInExternalSystem(addRoleFunc, role, app);
                        roleFunctions++;
                    }
                }
            }
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to bulkUploadRolesFunctions", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRolesFunctions: failed", e);
        }
        return roleFunctions;
    }

    private void addRoleFunctionsInExternalSystem(BulkUploadRoleFunction addRoleFunc, FnRole role, FnApp app) {
        String type;
        String instance = "";
        String action = "";
        if (addRoleFunc.getFunctionCd().contains(FUNCTION_PIPE)) {
            type = EcompPortalUtils.getFunctionType(addRoleFunc.getFunctionCd());
            instance = EcompPortalUtils.getFunctionCode(addRoleFunc.getFunctionCd());
            action = EcompPortalUtils.getFunctionAction(addRoleFunc.getFunctionCd());
        } else {
            type = addRoleFunc.getFunctionCd().contains("menu") ? "menu" : "url";
            instance = addRoleFunc.getFunctionCd();
            action = "*";
        }
        ExternalAccessRolePerms extRolePerms = null;
        ExternalAccessPerms extPerms = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            extPerms = new ExternalAccessPerms(app.getAuthNamespace() + "." + type, instance, action,
                addRoleFunc.getFunctionName());
            extRolePerms = new ExternalAccessRolePerms(extPerms, app.getAuthNamespace() + "." + role.getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
            String updateRolePerms = mapper.writeValueAsString(extRolePerms);
            HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
            template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role/perm",
                HttpMethod.POST, entity, String.class);
        } catch (Exception e) {
            if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleFunctionsInExternalSystem: RoleFunction already exits but does not break functionality",
                    e);
            } else {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleFunctionsInExternalSystem: Failed to addRoleFunctionsInExternalSystem", e.getMessage());
            }
        }
    }


    public Integer bulkUploadUserRoles(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<BulkUploadUserRoles> userRolesList;
        Integer userRolesAdded = 0;
        if (app.getAuthCentral()) {
            userRolesList = bulkUploadUserRolesService.getBulkUserRoles(app.getUebKey());
            for (BulkUploadUserRoles userRolesUpload : userRolesList) {
                if (!userRolesUpload.getOrgUserId().equals("su1234")) {
                    addUserRoleInExternalSystem(userRolesUpload);
                    userRolesAdded++;
                }
            }
        }
        return userRolesAdded;
    }

    public Integer bulkUploadPartnerFunctions(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<EpAppFunction> roleFuncList = epAppFunctionService.getAllRoleFunctions(app.getId());
        Integer functionsAdded = 0;
        try {
            for (EpAppFunction roleFunc : roleFuncList) {
                addFunctionInExternalSystem(roleFunc, app);
                functionsAdded++;
            }
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "HttpClientErrorException - bulkUploadPartnerFunctions failed",
                e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadPartnerFunctions: failed", e.getMessage(), e);
        }
        return functionsAdded;
    }

    public void bulkUploadPartnerRoles(String uebkey, List<Role> roleList) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        for (Role role : roleList) {
            addRoleInExternalSystem(role, app);
        }
    }

    private void addFunctionInExternalSystem(EpAppFunction roleFunc, FnApp app) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        ExternalAccessPerms extPerms = new ExternalAccessPerms();
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        String type = "";
        String instance = "";
        String action = "";
        if ((roleFunc.getFunctionCd().contains(FUNCTION_PIPE))
            || (roleFunc.getType() != null && roleFunc.getAction() != null)) {
            type = EcompPortalUtils.getFunctionType(roleFunc.getFunctionCd());
            instance = EcompPortalUtils.getFunctionCode(roleFunc.getFunctionCd());
            action = EcompPortalUtils.getFunctionAction(roleFunc.getFunctionCd());
        } else {
            type = roleFunc.getFunctionCd().contains("menu") ? "menu" : "url";
            instance = roleFunc.getFunctionCd();
            action = "*";
        }
        try {
            extPerms.setAction(action);
            extPerms.setInstance(instance);
            extPerms.setType(app.getAuthNamespace() + "." + type);
            extPerms.setDescription(roleFunc.getFunctionName());
            String addFunction = mapper.writeValueAsString(extPerms);
            HttpEntity<String> entity = new HttpEntity<>(addFunction, headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "addFunctionInExternalSystem: {} for POST: {}",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, addFunction);
            ResponseEntity<String> addPermResponse = template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm",
                HttpMethod.POST, entity, String.class);
            logger.debug(EELFLoggerDelegate.debugLogger,
                "addFunctionInExternalSystem: Finished adding permission for POST: {} and status code: {} ",
                addPermResponse.getStatusCode().value(), addFunction);
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to add function in external central auth system", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
            throw e;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "addFunctionInExternalSystem: Failed to add fucntion in external central auth system", e);
            throw e;
        }
    }

    public Integer bulkUploadPartnerRoleFunctions(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<FnRole> roles = getAppRoles(app.getId());
        Integer roleFunctions = 0;
        try {
            for (FnRole role : roles) {
                List<BulkUploadRoleFunction> appRoleFunc = bulkUploadUserRolesService
                    .uploadPartnerRoleFunctions(role.getId());
                if (!appRoleFunc.isEmpty()) {
                    for (BulkUploadRoleFunction addRoleFunc : appRoleFunc) {
                        addRoleFunctionsInExternalSystem(addRoleFunc, role, app);
                        roleFunctions++;
                    }
                }
            }
            // upload global role functions to ext auth system
            if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                roleFunctions = bulkUploadGlobalRoleFunctions(app, roleFunctions);
            }
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to bulkUploadRolesFunctions", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRolesFunctions: failed", e);
        }
        return roleFunctions;
    }

    private Integer bulkUploadGlobalRoleFunctions(FnApp app, Integer roleFunctions) throws Exception {
        try {
            //TODO HARDCODED ID!!!!!
            FnApp portalApp = fnAppService.getById(1L);
            String getBulkUploadPartnerGlobalRoleFunctions =
                "select distinct fr.role_id, fr.role_name, fr.active_yn, fr.priority, epr.function_cd, ep.function_name, ep.app_id, epr.role_app_id"
                    + " from fn_role fr, ep_app_function ep, ep_app_role_function epr"
                    + " where fr.role_id = epr.role_id and ep.function_cd = epr.function_cd and ep.app_id = epr.app_id and  epr.app_id = :appId and epr.role_app_id = 1";
            List<GlobalRoleWithApplicationRoleFunction> globalRoleFuncs = entityManager
                .createQuery(getBulkUploadPartnerGlobalRoleFunctions)
                .setParameter("appId", app.getId())
                .getResultList();
            ObjectMapper mapper = new ObjectMapper();
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            for (GlobalRoleWithApplicationRoleFunction globalRoleFunc : globalRoleFuncs) {
                ExternalAccessRolePerms extRolePerms;
                ExternalAccessPerms extPerms;
                String type = "";
                String instance = "";
                String action = "";
                if (globalRoleFunc.getFunctionCd().contains(FUNCTION_PIPE)) {
                    type = EcompPortalUtils.getFunctionType(globalRoleFunc.getFunctionCd());
                    instance = EcompPortalUtils.getFunctionCode(globalRoleFunc.getFunctionCd());
                    action = EcompPortalUtils.getFunctionAction(globalRoleFunc.getFunctionCd());
                } else {
                    type = globalRoleFunc.getFunctionCd().contains("menu") ? "menu" : "url";
                    instance = globalRoleFunc.getFunctionCd();
                    action = "*";
                }
                extPerms = new ExternalAccessPerms(app.getAuthNamespace() + "." + type, instance, action);
                extRolePerms = new ExternalAccessRolePerms(extPerms,
                    portalApp.getAuthNamespace() + "." + globalRoleFunc.getRoleName().replaceAll(
                        EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
                String updateRolePerms = mapper.writeValueAsString(extRolePerms);
                HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
                updateRoleFunctionInExternalSystem(updateRolePerms, entity);
                roleFunctions++;
            }
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to add role function in external central auth system", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
            throw e;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "bulkUploadGlobalRoleFunctions: Failed to add role fucntion in external central auth system", e);
            throw e;
        }
        return roleFunctions;
    }

    private void updateRoleFunctionInExternalSystem(String updateRolePerms, HttpEntity<String> entity) {
        logger.debug(EELFLoggerDelegate.debugLogger, "bulkUploadRoleFunc: {} for POST: {}",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, updateRolePerms);
        ResponseEntity<String> addPermResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role/perm",
            HttpMethod.POST, entity, String.class);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "bulkUploadRoleFunc: Finished adding permission for POST: {} and status code: {} ",
            addPermResponse.getStatusCode().value(), updateRolePerms);
    }

    public List<String> getMenuFunctionsList(String uebkey) throws Exception {
        List<String> appMenuFunctionsList = null;
        List<String> appMenuFunctionsFinalList = new ArrayList<>();
        try {
            FnApp app = getApp(uebkey).get(0);
            String getMenuFunctions = "select f.function_cd from ep_app_function f"
                + " where f.app_id =:appId"
                + " UNION"
                + " select epa.function_cd from fn_role fnr, ep_app_role_function epr, ep_app_function epa where epr.role_id = fnr.role_id"
                + " and epa.function_cd = epr.function_cd and fnr.role_name like 'global%' and fnr.app_id is null and epr.app_id = 1";
            appMenuFunctionsList = entityManager.createQuery(getMenuFunctions).setParameter(APP_ID, app.getId())
                .getResultList();
            for (String appMenuFunction : appMenuFunctionsList) {
                if (appMenuFunction.contains(FUNCTION_PIPE)) {
                    appMenuFunctionsFinalList.add(EcompPortalUtils.getFunctionCode(appMenuFunction));
                } else {
                    appMenuFunctionsFinalList.add(appMenuFunction);
                }
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getMenuFunctionsList: Failed", e);
            return appMenuFunctionsFinalList;
        }
        return appMenuFunctionsFinalList;
    }

    public List<EcompUser> getAllAppUsers(String uebkey) throws Exception {
        List<String> usersList = new ArrayList<>();
        List<EcompUser> usersfinalList = new ArrayList<>();
        try {
            FnApp app = getApp(uebkey).get(0);
            String ApplicationUserRoles =
                "select distinct fu.org_id, fu.manager_id, fu.first_name, fu.middle_name, fu.last_name, fu.phone, fu.email, fu.hrid, fu.org_user_id, fu.org_code, fu.org_manager_userid, fu.job_title, fu.login_id, \n"
                    + " fu.active_yn , fr.app_role_id, fr.role_name, epr.function_cd , epf.function_name\n"
                    + " from fn_user fu, fn_role fr, fn_user_role fur, ep_app_role_function epr , ep_app_function epf\n"
                    + " where fu.user_id = fur.user_id and fu.active_yn='Y' and fur.role_id = fr.role_id and fr.app_id =:appId and fr.active_yn='Y' and epr.function_cd= epf.function_cd and epf.app_id=epr.app_id and fur.role_id=epr.role_id\n"
                    + " union\n"
                    + " select distinct fu.org_id, fu.manager_id, fu.first_name, fu.middle_name, fu.last_name, fu.phone, fu.email, fu.hrid, fu.org_user_id, fu.org_code, fu.org_manager_userid, fu.job_title, \n"
                    + " fu.login_id, fu.active_yn , fr.role_id, fr.role_name, earf.function_cd , eaf.function_name\n"
                    + " from fn_user_role a, fn_role fr, fn_user fu , ep_app_role_function earf, ep_app_function eaf\n"
                    + " where a.role_id in (select b.role_id from ep_app_role_function b where b.role_app_id = 1 and b.app_id =:appId) and a.user_id =fu.user_id and a.role_id = fr.role_id and fr.active_yn='Y' and fu.active_yn='Y'\n"
                    + " and earf.role_id = a.role_id and earf.function_cd = eaf.function_cd and earf.app_id = eaf.app_id  and earf.role_app_id = 1 and fr.active_yn='Y' and fu.active_yn='Y'";

            List<EcompUserRoles> userList = entityManager.createQuery(ApplicationUserRoles)
                .setParameter("appId", app.getId()).getResultList();
            for (EcompUserRoles ecompUserRole : userList) {
                boolean found = false;
                Set<EcompRole> roles = null;
                for (EcompUser user : usersfinalList) {
                    if (user.getOrgUserId().equals(ecompUserRole.getOrgUserId())) {
                        EcompRole ecompRole = new EcompRole();
                        ecompRole.setId(ecompUserRole.getRoleId());
                        ecompRole.setName(ecompUserRole.getRoleName());
                        roles = user.getRoles();
                        EcompRole role = roles.stream().filter(x -> x.getName().equals(ecompUserRole.getRoleName()))
                            .findAny().orElse(null);
                        SortedSet<EcompRoleFunction> roleFunctionSet = new TreeSet<>();
                        if (role != null) {
                            roleFunctionSet = (SortedSet<EcompRoleFunction>) role.getRoleFunctions();
                        }
                        String functionCode = EcompPortalUtils.getFunctionCode(ecompUserRole.getFunctionCode());
                        functionCode = EPUserUtils.decodeFunctionCode(functionCode);
                        EcompRoleFunction epRoleFunction = new EcompRoleFunction();
                        epRoleFunction.setName(ecompUserRole.getFunctionName());
                        epRoleFunction.setCode(EPUserUtils.decodeFunctionCode(functionCode));
                        epRoleFunction.setType(getFunctionCodeType(ecompUserRole.getFunctionCode()));
                        epRoleFunction.setAction(getFunctionCodeAction(ecompUserRole.getFunctionCode()));
                        roleFunctionSet.add(epRoleFunction);
                        ecompRole.setRoleFunctions(roleFunctionSet);
                        roles.add(ecompRole);
                        user.setRoles(roles);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    EcompUser epUser = new EcompUser();
                    epUser.setOrgId(ecompUserRole.getOrgId());
                    epUser.setManagerId(ecompUserRole.getManagerId());
                    epUser.setFirstName(ecompUserRole.getFirstName());
                    epUser.setLastName(ecompUserRole.getLastName());
                    epUser.setPhone(ecompUserRole.getPhone());
                    epUser.setEmail(ecompUserRole.getEmail());
                    epUser.setOrgUserId(ecompUserRole.getOrgUserId());
                    epUser.setOrgCode(ecompUserRole.getOrgCode());
                    epUser.setOrgManagerUserId(ecompUserRole.getOrgManagerUserId());
                    epUser.setJobTitle(ecompUserRole.getJobTitle());
                    epUser.setLoginId(ecompUserRole.getLoginId());
                    epUser.setActive(true);
                    roles = new HashSet<>();
                    EcompRole ecompRole = new EcompRole();
                    ecompRole.setId(ecompUserRole.getRoleId());
                    ecompRole.setName(ecompUserRole.getRoleName());
                    SortedSet<EcompRoleFunction> roleFunctionSet = new TreeSet<>();
                    String functionCode = EcompPortalUtils.getFunctionCode(ecompUserRole.getFunctionCode());
                    functionCode = EPUserUtils.decodeFunctionCode(functionCode);
                    EcompRoleFunction epRoleFunction = new EcompRoleFunction();
                    epRoleFunction.setName(ecompUserRole.getFunctionName());
                    epRoleFunction.setCode(EPUserUtils.decodeFunctionCode(functionCode));
                    epRoleFunction.setType(getFunctionCodeType(ecompUserRole.getFunctionCode()));
                    epRoleFunction.setAction(getFunctionCodeAction(ecompUserRole.getFunctionCode()));
                    roleFunctionSet.add(epRoleFunction);
                    ecompRole.setRoleFunctions(roleFunctionSet);
                    roles.add(ecompRole);
                    epUser.setRoles(roles);
                    usersfinalList.add(epUser);
                }
            }
            ObjectMapper mapper = new ObjectMapper();
            for (EcompUser u1 : usersfinalList) {
                String str = mapper.writeValueAsString(u1);
                usersList.add(str);
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getAllUsers failed", e);
            throw e;
        }
        return usersfinalList;
    }

    public List<EcompRole> missingUserApplicationRoles(String uebkey, String loginId, Set<EcompRole> CurrentUserRoles)
        throws Exception {
        List<FnApp> appList = getApp(uebkey);
        FnApp app = appList.get(0);
        List<FnUser> epUserList;
        epUserList = getUser(loginId);
        List<EcompRole> missingUserAppRoles = new ArrayList<>();
        List<String> roleNamesList = CurrentUserRoles.stream().map(EcompRole::getName).collect(Collectors.toList());
        logger.debug(EELFLoggerDelegate.debugLogger, "Roles of User from hibernate :" + roleNamesList);
        List<EcompRole> userApplicationsRolesfromDB = getUserAppRoles(app, epUserList.get(0));
        if (userApplicationsRolesfromDB.size() > 0) {
            missingUserAppRoles = userApplicationsRolesfromDB.stream().filter(x -> !roleNamesList.contains(x.getName()))
                .collect(Collectors.toList());
        }
        List<String> missingroleNamesList = missingUserAppRoles.stream().map(EcompRole::getName)
            .collect(Collectors.toList());
        logger.debug(EELFLoggerDelegate.debugLogger, "MissingUserAppRoles():" + missingroleNamesList);

        List<EcompRole> finalMissingRoleList = new ArrayList<>();
        if (missingUserAppRoles.size() > 0) {
            final Map<String, Long> params = new HashMap<>();
            for (EcompRole role : missingUserAppRoles) {
                EcompRole epRole = new EcompRole();
                epRole.setId(role.getId());
                epRole.setName(role.getName());
                String getAppRoleFunctionList =
                    "SELECT DISTINCT f.app_id , f.function_cd, f.function_name from ep_app_role_function rf, ep_app_function f"
                        + " where rf.role_id =:roleId and rf.app_id =:appId and rf.app_id = f.app_id and rf.function_cd = f.function_cd";
                List<EpAppFunction> appRoleFunctionList = entityManager.createQuery(getAppRoleFunctionList)
                    .setParameter("roleId", role.getId()).setParameter(APP_ID, app.getId()).getResultList();
                SortedSet<EcompRoleFunction> roleFunctionSet = new TreeSet<>();
                for (EpAppFunction roleFunc : appRoleFunctionList) {
                    String functionCode = EcompPortalUtils.getFunctionCode(roleFunc.getFunctionCd());
                    String type = getFunctionCodeType(roleFunc.getFunctionCd());
                    String action = getFunctionCodeAction(roleFunc.getFunctionCd());
                    EcompRoleFunction fun = new EcompRoleFunction();
                    fun.setAction(action);
                    fun.setCode(functionCode);
                    fun.setType(type);
                    fun.setName(roleFunc.getFunctionName());
                    roleFunctionSet.add(fun);

                }
                epRole.setRoleFunctions(roleFunctionSet);
                finalMissingRoleList.add(epRole);
            }
        }

        return finalMissingRoleList;
    }

    private List<EcompRole> getUserAppRoles(FnApp app, FnUser user) {
        String getUserAppCurrentRoles = "select distinct fu.role_id, fr.user_id, fu.role_name, fu.priority from fn_role fu left outer join fn_user_role fr ON fu.role_id = fr.role_id and fu.app_id = fr.app_id and fr.role_id != 999 where fu.app_id =:appId and fr.user_id =:userId and fu.active_yn='Y' \n";
        List<EPUserAppCurrentRoles> userAppsRolesList = entityManager.createQuery(getUserAppCurrentRoles)
            .setParameter("appId", app.getId())
            .setParameter("userId", user.getId())
            .getResultList();
        List<EcompRole> setUserRoles = new ArrayList<>();
        for (EPUserAppCurrentRoles role : userAppsRolesList) {
            logger.debug(EELFLoggerDelegate.debugLogger, "In getUserAppRoles()- get userRolename = {}",
                role.getRoleName());
            EcompRole ecompRole = new EcompRole();
            ecompRole.setId(role.getRoleId());
            ecompRole.setName(role.getRoleName());
            setUserRoles.add(ecompRole);
        }
        logger.debug(EELFLoggerDelegate.debugLogger, "In getUserAppRoles()- get userrole list size = {}",
            setUserRoles.size());
        return setUserRoles;
    }

    public List<FnUser> getUser(String loginId) throws InvalidUserException {
        List<FnUser> userList = fnUserService.getUserWithOrgUserId(loginId);
        if (userList.isEmpty()) {
            throw new InvalidUserException("User not found");
        }
        return userList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public ExternalRequestFieldsValidator deleteDependencyRoleRecord(Long roleId, String uebkey, String LoginId)
        throws Exception {
        String message = "";
        boolean response = false;
        FnApp app = null;
        try {
            List<FnRole> epRoleList = null;
            app = getApp(uebkey).get(0);
            if (app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                epRoleList = getPortalAppRoleInfo(roleId);
            } else {
                epRoleList = getPartnerAppRoleInfo(roleId, app.getId());
            }
            if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                // Delete User Role in External System before deleting role
                deleteUserRoleInExternalSystem(epRoleList.get(0), app, LoginId);
            }
            // Delete user app roles
            fnRoleService.delete(epRoleList.get(0));
            boolean isPortalRequest = false;
            deleteRoleDependencyRecords(epRoleList.get(0).getId(), app.getId(), isPortalRequest);
            if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                // Final call to delete role once all dependencies has been
                // deleted
                deleteRoleInExternalAuthSystem(epRoleList, app);
            }
            fnRoleService.delete(epRoleList.get(0));
            logger.debug(EELFLoggerDelegate.debugLogger, "deleteDependencyRoleRecord: committed the transaction");
            response = true;
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "deleteDependencyRoleRecord: HttpClientErrorException", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
            message = e.getMessage();
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "deleteDependencyRoleRecord failed", e);
            message = e.getMessage();
        }
        return new ExternalRequestFieldsValidator(response, message);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteRoleDependencyRecords(Long roleId, Long appId, boolean isPortalRequest)
        throws Exception {
        try {
            String sql = "";
            Query query = null;
            // It should delete only when it portal's roleId
            if (appId.equals(PortalConstants.PORTAL_APP_ID)) {
                // Delete from fn_role_function
                sql = "DELETE FROM fn_role_function WHERE role_id=" + roleId;
                logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
                query = entityManager.createQuery(sql);
                query.executeUpdate();
                // Delete from fn_role_composite
                sql = "DELETE FROM fn_role_composite WHERE parent_role_id=" + roleId + " OR child_role_id=" + roleId;
                logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
                query = entityManager.createQuery(sql);
                query.executeUpdate();
            }
            // Delete from ep_app_role_function
            sql = "DELETE FROM ep_app_role_function WHERE role_id=" + roleId;
            logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
            query = entityManager.createQuery(sql);
            query.executeUpdate();
            // Delete from ep_role_notification
            sql = "DELETE FROM ep_role_notification WHERE role_id=" + roleId;
            logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
            query = entityManager.createQuery(sql);
            query.executeUpdate();
            // Delete from fn_user_pseudo_role
            sql = "DELETE FROM fn_user_pseudo_role WHERE pseudo_role_id=" + roleId;
            logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
            query = entityManager.createQuery(sql);
            query.executeUpdate();
            // Delete form EP_WIDGET_CATALOG_ROLE
            sql = "DELETE FROM EP_WIDGET_CATALOG_ROLE WHERE role_id=" + roleId;
            logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
            query = entityManager.createQuery(sql);
            query.executeUpdate();
            // Delete form EP_WIDGET_CATALOG_ROLE
            sql = "DELETE FROM ep_user_roles_request_det WHERE requested_role_id=" + roleId;
            logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
            query = entityManager.createQuery(sql);
            query.executeUpdate();
            if (!isPortalRequest) {
                // Delete form fn_menu_functional_roles
                sql = "DELETE FROM fn_menu_functional_roles WHERE role_id=" + roleId;
                logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
                query = entityManager.createQuery(sql);
                query.executeUpdate();
            }
        } catch (Exception e) {
            logger.debug(EELFLoggerDelegate.debugLogger, "deleteRoleDependeciesRecord: failed ", e);
            throw new DeleteDomainObjectFailedException("delete Failed" + e.getMessage());
        }
    }

    private void deleteUserRoleInExternalSystem(FnRole role, FnApp app, String LoginId) throws Exception {
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        getNameSpaceIfExists(app);
        logger.debug(EELFLoggerDelegate.debugLogger, "deleteUserRoleInExternalSystem: {} ",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
        ResponseEntity<String> getResponse = template.exchange(
            SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "userRole/"
                + LoginId
                + SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)
                + "/" + app.getAuthNamespace() + "."
                + role.getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"),
            HttpMethod.GET, entity, String.class);
        logger.debug(EELFLoggerDelegate.debugLogger,
            "deleteUserRoleInExternalSystem: Finished GET user roles from External Auth system and response: {} ",
            getResponse.getBody());
        if (getResponse.getStatusCode().value() != 200) {
            throw new ExternalAuthSystemException(getResponse.getBody());
        }
        String res = getResponse.getBody();
        if (!res.equals(IS_EMPTY_JSON_STRING)) {
            HttpEntity<String> userRoleentity = new HttpEntity<>(headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "deleteUserRoleInExternalSystem: {} ",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
            ResponseEntity<String> deleteResponse = template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "userRole/"
                    + LoginId
                    + SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)
                    + "/" + app.getAuthNamespace() + "."
                    + role.getRoleName().replaceAll(
                    EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"),
                HttpMethod.DELETE, userRoleentity, String.class);
            if (deleteResponse.getStatusCode().value() != 200) {
                throw new ExternalAuthSystemException("Failed to delete user role");
            }
            logger.debug(EELFLoggerDelegate.debugLogger,
                "deleteUserRoleInExternalSystem: Finished deleting user role in External Auth system and status code: {} ",
                deleteResponse.getStatusCode().value());
        }
    }

    public Integer bulkUploadUsersSingleRole(String uebkey, Long roleId, String modifiedRoleName) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<BulkUploadUserRoles> userRolesList;
        Integer userRolesAdded = 0;
        if (app.getAuthCentral()) {
            userRolesList = bulkUploadUserRolesService.getBulkUsersForSingleRole(app.getUebKey(), roleId);
            for (BulkUploadUserRoles userRolesUpload : userRolesList) {
                userRolesUpload.setRoleName(modifiedRoleName);
                if (!userRolesUpload.getOrgUserId().equals("su1234")) {
                    addUserRoleInExternalSystem(userRolesUpload);
                    userRolesAdded++;
                }
            }
        }
        return userRolesAdded;
    }

    private void addUserRoleInExternalSystem(BulkUploadUserRoles userRolesUpload) {
        try {
            String name = "";
            ObjectMapper mapper = new ObjectMapper();
            if (EPCommonSystemProperties
                .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN)) {
                name = userRolesUpload.getOrgUserId()
                    + SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_USER_DOMAIN);
            }
            ExternalAccessUser extUser = new ExternalAccessUser(name,
                userRolesUpload.getAppNameSpace() + "." + userRolesUpload.getRoleName()
                    .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
            String userRole = mapper.writeValueAsString(extUser);
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            HttpEntity<String> entity = new HttpEntity<>(userRole, headers);
            template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "userRole",
                HttpMethod.POST, entity, String.class);
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to addUserRoleInExternalSystem", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            if (e.getMessage().equalsIgnoreCase("409 Conflict")) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addUserRoleInExternalSystem: UserRole already exits but does not break functionality");
            } else {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addUserRoleInExternalSystem: Failed to addUserRoleInExternalSystem", e);
            }
        }
    }

    private void addNewRoleInExternalSystem(List<FnRole> newRole, FnApp app)
        throws Exception {
        try {
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            ObjectMapper mapper = new ObjectMapper();
            String addNewRole;
            ExternalAccessRole extRole = new ExternalAccessRole();
            extRole.setName(app.getAuthNamespace() + "." + newRole.get(0).getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
            extRole.setDescription(String.valueOf(newRole.get(0).getRoleName()));
            addNewRole = mapper.writeValueAsString(extRole);
            HttpEntity<String> postEntity = new HttpEntity<>(addNewRole, headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "addNewRoleInExternalSystem: {} for POST: {} ",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, addNewRole);
            ResponseEntity<String> addNewRoleInExternalSystem = template.exchange(
                SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "role",
                HttpMethod.POST, postEntity, String.class);
            if (addNewRoleInExternalSystem.getStatusCode().value() == 201) {
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "addNewRoleInExternalSystem: Finished adding into External Auth system for POST: {} and status code: {}",
                    addNewRole, addNewRoleInExternalSystem.getStatusCode().value());
            }
        } catch (HttpClientErrorException ht) {
            fnRoleService.delete(newRole.get(0));
            logger.error(EELFLoggerDelegate.debugLogger,
                "addNewRoleInExternalSystem: Failed to add in External Auth system and status code: {}", ht);
            throw new HttpClientErrorException(ht.getStatusCode());
        }
    }

    private void checkIfRoleExitsInExternalSystem(Role checkRole, FnApp app) throws Exception {
        getNameSpaceIfExists(app);
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        String roleName = app.getAuthNamespace() + "." + checkRole.getName()
            .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_");
        HttpEntity<String> checkRoleEntity = new HttpEntity<>(headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "checkIfRoleExitsInExternalSystem: {} ",
            CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE);
        ResponseEntity<String> checkRoleInExternalSystem = template
            .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "roles/"
                + roleName, HttpMethod.GET, checkRoleEntity, String.class);
        if (!checkRoleInExternalSystem.getBody().equals(IS_EMPTY_JSON_STRING)) {
            logger.debug(
                "checkIfRoleExitsInExternalSystem: Role already exists in external system {} and status code: {} ",
                checkRoleInExternalSystem.getBody(), checkRoleInExternalSystem.getStatusCode().value());
            throw new ExternalAuthSystemException(" Role already exists in external system");
        }
    }

    public ResponseEntity<String> getNameSpaceIfExists(FnApp app) throws Exception {
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        HttpEntity<String> entity = new HttpEntity<>(headers);
        logger.debug(EELFLoggerDelegate.debugLogger, "checkIfNameSpaceExists: Connecting to External Auth system");
        ResponseEntity<String> response = null;
        try {
            response = template
                .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
                    + "nss/" + app.getAuthNamespace(), HttpMethod.GET, entity, String.class);
            logger.debug(EELFLoggerDelegate.debugLogger, "checkIfNameSpaceExists: Finished ",
                response.getStatusCode().value());
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "checkIfNameSpaceExists failed", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
            if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
                throw new InvalidApplicationException("Invalid NameSpace");
            } else {
                throw e;
            }
        }
        return response;
    }

    private FnRoleFunction createCentralRoleFunctionForGlobalRole(GlobalRoleWithApplicationRoleFunction role) {
        String instance;
        String type;
        String action;
        FnRoleFunction cenRoleFun = null;
        if (role.getFunctionCd().contains(FUNCTION_PIPE)) {
            instance = EcompPortalUtils.getFunctionCode(role.getFunctionCd());
            type = EcompPortalUtils.getFunctionType(role.getFunctionCd());
            action = EcompPortalUtils.getFunctionAction(role.getFunctionCd());
            cenRoleFun = FnRoleFunction.builder().build();
            FnRole fnRole = new FnRole();
            FnFunction fnFunction = FnFunction.builder().functionCd(instance).name(role.getFunctionName()).type(type)
                .action(action).build();
            cenRoleFun.setRole(fnRole);
            cenRoleFun.setFunctionCd(fnFunction);
        } else {
            type = getFunctionCodeType(role.getFunctionCd());
            action = getFunctionCodeAction(role.getFunctionCd());
            FnFunction fnFunction = FnFunction.builder().functionCd(role.getFunctionCd()).name(role.getFunctionName())
                .type(type).action(action).build();
            cenRoleFun.setRole(new FnRole());
            cenRoleFun.setFunctionCd(fnFunction);
        }
        return cenRoleFun;
    }

    public CentralUser getUserRoles(String loginId, String uebkey) throws Exception {
        CentralUser sendUserRoles = null;
        try {
            CentralV2User cenV2User = getV2UserAppRoles(loginId, uebkey);
            sendUserRoles = convertV2UserRolesToOlderVersion(cenV2User);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getUserRoles: failed", e);
            throw e;
        }
        return sendUserRoles;
    }

    private CentralV2User getV2UserAppRoles(String loginId, String uebkey) throws Exception {
        FnApp app;
        List<FnApp> appList = getApp(uebkey);
        app = appList.get(0);
        FnUser user = fnUserService.loadUserByUsername(loginId);
        Set<FnUserRole> userAppSet = user.getUserApps();
        return createEPUser(user, userAppSet, app);
    }

    public List<FnApp> getApp(String uebkey) throws Exception {
        List<FnApp> app = null;
        try {
            app = fnAppService.getByUebKey(uebkey);
            if (!app.isEmpty() && !app.get(0).getEnabled()
                && !app.get(0).getId().equals(PortalConstants.PORTAL_APP_ID)) {
                throw new InactiveApplicationException("Application:" + app.get(0).getAppName() + " is Unavailable");
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getApp: failed", e);
            throw e;
        }
        return app;
    }

    private CentralV2User createEPUser(FnUser userInfo, Set<FnUserRole> userAppSet, FnApp app) {
        CentralV2User userAppList = CentralV2User.builder().build();
        CentralV2User user1;
        List<FnRole> globalRoleList = new ArrayList<>();
        try {
            if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                globalRoleList = fnRoleService.userAppGlobalRoles(userInfo.getId(), app.getId());
            }
            userAppList.setUserApps(new TreeSet<>());
            for (FnUserRole userApp : userAppSet) {
                if (userApp.getRoleId().getActiveYn()) {
                    FnApp epApp = userApp.getFnAppId();
                    String globalRole = userApp.getRoleId().getRoleName().toLowerCase();
                    if (((epApp.getId().equals(app.getId()))
                        && (!userApp.getRoleId().getId().equals(PortalConstants.ACCOUNT_ADMIN_ROLE_ID)))
                        || ((epApp.getId().equals(PortalConstants.PORTAL_APP_ID))
                        && (globalRole.toLowerCase().startsWith("global_")))) {
                        CentralV2UserApp cua = new CentralV2UserApp();
                        cua.setUserId(null);
                        CentralApp cenApp = CentralApp.builder().id(1L).created(epApp.getCreated())
                            .modified(epApp.getModified()).createdId(epApp.getId())
                            .modifiedId(epApp.getModifiedId().getId()).rowNum(epApp.getRowNum())
                            .name(epApp.getAppName()).imageUrl(epApp.getAppImageUrl())
                            .description(epApp.getAppDescription()).notes(epApp.getAppNotes())
                            .url(epApp.getAppUrl()).alternateUrl(epApp.getAppAlternateUrl())
                            .restEndpoint(epApp.getAppRestEndpoint()).mlAppName(epApp.getMlAppName())
                            .mlAppAdminId(epApp.getMlAppAdminId()).motsId(String.valueOf(epApp.getMotsId()))
                            .appPassword(epApp.getAppPassword()).open(String.valueOf(epApp.getOpen()))
                            .enabled(String.valueOf(epApp.getEnabled())).thumbnail(epApp.getThumbnail())
                            .username(epApp.getAppUsername()).uebKey(epApp.getUebKey())
                            .uebSecret(epApp.getUebSecret()).uebTopicName(epApp.getUebTopicName())
                            .build();
                        cenApp.setAppPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);
                        cua.setApp(cenApp);
                        Long appId = null;
                        if (globalRole.toLowerCase().startsWith("global_")
                            && epApp.getId().equals(PortalConstants.PORTAL_APP_ID)
                            && !epApp.getId().equals(app.getId())) {
                            appId = app.getId();
                            FnRole result = null;
                            if (globalRoleList.size() > 0) {
                                result = globalRoleList.stream()
                                    .filter(x -> userApp.getRoleId().getId().equals(x.getId())).findAny()
                                    .orElse(null);
                            }
                            if (result == null) {
                                continue;
                            }
                        } else {
                            appId = userApp.getFnAppId().getId();
                        }
                        List<EpAppFunction> appRoleFunctionList = epAppFunctionService
                            .getAppRoleFunctionList(userApp.getRoleId().getId(), appId);
                        SortedSet<EpAppFunction> roleFunctionSet = new TreeSet<>();
                        for (EpAppFunction roleFunc : appRoleFunctionList) {
                            String functionCode = EcompPortalUtils.getFunctionCode(roleFunc.getFunctionCd());
                            String type = getFunctionCodeType(roleFunc.getFunctionCd());
                            String action = getFunctionCodeAction(roleFunc.getFunctionCd());
                            EpAppFunction cenRoleFunc = new EpAppFunction(roleFunc.getId(),
                                functionCode, roleFunc.getFunctionName(), null, type, action, null);
                            roleFunctionSet.add(cenRoleFunc);
                        }
                        Long userRoleId;
                        if (globalRole.toLowerCase().startsWith("global_")
                            || epApp.getId().equals(PortalConstants.PORTAL_APP_ID)) {
                            userRoleId = userApp.getRoleId().getId();
                        } else {
                            userRoleId = userApp.getRoleId().getAppRoleId();
                        }
                        CentralV2Role cenRole = CentralV2Role.builder().id(userRoleId)
                            .created(userApp.getRoleId().getCreated()).modified(userApp.getRoleId().getModified())
                            .createdId(userApp.getRoleId().getCreatedId().getId())
                            .modifiedId(userApp.getRoleId().getModifiedId().getId())
                            .rowNum(userApp.getRoleId().getRowNum()).name(userApp.getRoleId().getRoleName())
                            .active(userApp.getRoleId().getActiveYn()).priority(userApp.getRoleId().getPriority())
                            //.roleFunctions(roleFunctionSet).setChildRoles(null).setParentRoles(null)
                            .build();
                        cua.setRole(cenRole);
                        userAppList.getUserApps().add(cua);
                    }
                }
            }
            user1 = CentralV2User.builder().id(null).created(userInfo.getCreated())
                .modified(userInfo.getModified()).createdId(userInfo.getCreatedId().getId())
                .modifiedId(userInfo.getModifiedId().getId()).rowNum(userInfo.getRowNum())
                .orgId(userInfo.getOrgId().getOrgId()).managerId(userInfo.getOrgManagerUserId())
                .firstName(userInfo.getFirstName()).middleInitial(userInfo.getMiddleName())
                .lastName(userInfo.getLastName()).phone(userInfo.getPhone()).fax(userInfo.getFax())
                .cellular(userInfo.getCellular()).email(userInfo.getEmail())
                .addressId(userInfo.getAddressId()).alertMethodCd(userInfo.getAlertMethodCd().getAlertMethodCd())
                .hrid(userInfo.getHrid()).orgUserId(userInfo.getOrgUserId()).orgCode(userInfo.getOrgCode())
                .address1(userInfo.getAddressLine1()).address2(userInfo.getAddressLine2()).city(userInfo.getCity())
                .state(userInfo.getStateCd()).zipCode(userInfo.getZipCode()).country(userInfo.getCountryCd())
                .orgManagerUserId(userInfo.getOrgManagerUserId()).locationClli(userInfo.getLocationClli())
                .businessCountryCode(userInfo.getBusinessUnit())
                .businessCountryName(userInfo.getBusinessUnitName())
                .businessUnit(userInfo.getBusinessUnit()).businessUnitName(userInfo.getBusinessUnitName())
                .department(userInfo.getDepartment()).departmentName(userInfo.getDepartmentName())
                .companyCode(userInfo.getOrgCode()).company(userInfo.getCompany())
                .zipCodeSuffix(userInfo.getZipCode()).jobTitle(userInfo.getJobTitle())
                //.commandChain(userInfo.getCommandChain()).siloStatus(userInfo.getSiloStatus())
                .costCenter(userInfo.getCostCenter()).financialLocCode(userInfo.getFinLocCode())
                .loginId(userInfo.getLoginId()).loginPwd(userInfo.getLoginPwd())
                .lastLoginDate(userInfo.getLastLoginDate()).active(userInfo.getActiveYn())
                //.internal(userInfo.getIsInternalYn()).selectedProfileId(userInfo.getSelectedProfileId())
                //.timeZoneId(userInfo.getTimezone().getTimezoneId()).online(userInfo.isOnline())
                //.chatId(userInfo.getChatId()).setUserApps(userAppList.getUserApps()).setPseudoRoles(null)
                .build();
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "createEPUser: createEPUser failed", e);
            throw e;
        }
        return user1;
    }

    private CentralUser convertV2UserRolesToOlderVersion(CentralV2User cenV2User) {
        Set<CentralV2UserApp> userV2Apps = cenV2User.getUserApps();
        Set<CentralUserApp> userApps = new TreeSet<>();
        for (CentralV2UserApp userApp : userV2Apps) {
            CentralApp app = userApp.getApp();
            CentralUserApp cua = new CentralUserApp();
            cua.setUserId(null);
            cua.setApp(app);
            SortedSet<EpAppFunction> cenRoleFunction = new TreeSet<>();
            for (DomainVo vo : userApp.getRole().getRoleFunctions()) {
                Optional<EpAppFunction> epApp = epAppFunctionService.getForId(vo.getId());
                if (epApp.isPresent()) {
                    EpAppFunction cenRoleFunc = EpAppFunction.builder().functionCd(epApp.get().getFunctionCd())
                        .functionName(
                            epApp.get().getFunctionName()).build();
                    cenRoleFunction.add(cenRoleFunc);
                }
            }
            CentralRole role = new CentralRole(userApp.getRole().getId(), userApp.getRole().getName(),
                userApp.getRole().isActive(), userApp.getRole().getPriority(), cenRoleFunction);
            cua.setRole(role);
            userApps.add(cua);
        }
        return CentralUser.builder().id(cenV2User.getId()).created(cenV2User.getCreated())
            .modified(cenV2User.getModified()).createdId(cenV2User.getCreatedId())
            .modifiedId(cenV2User.getModifiedId()).rowNum(cenV2User.getRowNum())
            .orgId(cenV2User.getOrgId()).managerId(cenV2User.getManagerId())
            .firstName(cenV2User.getFirstName()).middleInitial(cenV2User.getMiddleInitial())
            .lastName(cenV2User.getLastName()).phone(cenV2User.getPhone()).fax(cenV2User.getFax())
            .cellular(cenV2User.getCellular()).email(cenV2User.getEmail())
            .addressId(cenV2User.getAddressId()).alertMethodCd(cenV2User.getAlertMethodCd())
            .hrid(cenV2User.getHrid()).orgUserId(cenV2User.getOrgUserId()).orgCode(cenV2User.getOrgCode())
            .address1(cenV2User.getAddress1()).address2(cenV2User.getAddress2()).city(cenV2User.getCity())
            .state(cenV2User.getState()).zipCode(cenV2User.getZipCode()).country(cenV2User.getCountry())
            .orgManagerUserId(cenV2User.getOrgManagerUserId()).locationClli(cenV2User.getLocationClli())
            .businessCountryCode(cenV2User.getBusinessCountryCode())
            .businessCountryName(cenV2User.getBusinessCountryName()).businessUnit(cenV2User.getBusinessUnit())
            .businessUnitName(cenV2User.getBusinessUnitName()).department(cenV2User.getDepartment())
            .departmentName(cenV2User.getDepartmentName()).companyCode(cenV2User.getCompanyCode())
            .company(cenV2User.getCompany()).zipCodeSuffix(cenV2User.getZipCodeSuffix())
            .jobTitle(cenV2User.getJobTitle()).commandChain(cenV2User.getCommandChain())
            .siloStatus(cenV2User.getSiloStatus()).costCenter(cenV2User.getCostCenter())
            .financialLocCode(cenV2User.getFinancialLocCode()).loginId(cenV2User.getLoginId())
            .loginPwd(cenV2User.getLoginPwd()).lastLoginDate(cenV2User.getLastLoginDate())
            .active(cenV2User.isActive()).internal(cenV2User.isInternal())
            .selectedProfileId(cenV2User.getSelectedProfileId()).timeZoneId(cenV2User.getTimeZoneId())
            .online(cenV2User.isOnline()).chatId(cenV2User.getChatId()).userApps(userApps).build();
    }

    public CentralV2Role getRoleInfo(final Long roleId, final String uebkey) throws Exception {
        List<CentralV2Role> roleList = new ArrayList<>();
        CentralV2Role cenRole = CentralV2Role.builder().build();
        List<FnRole> roleInfo;
        List<FnApp> app;
        try {
            app = getApp(uebkey);
            if (app.isEmpty()) {
                throw new InactiveApplicationException("Application not found");
            }
            if (!app.get(0).getId().equals(PortalConstants.PORTAL_APP_ID)) {
                List<FnRole> globalRoleList = getGlobalRolesOfPortal();
                if (globalRoleList.size() > 0) {
                    FnRole result = globalRoleList.stream().filter(x -> roleId.equals(x.getId())).findAny()
                        .orElse(null);
                    if (result != null) {
                        return getGlobalRoleForRequestedApp(app.get(0).getId(), roleId);
                    }
                }
            }
            if (app.get(0).getId().equals(PortalConstants.PORTAL_APP_ID)) {
                roleInfo = getPortalAppRoleInfo(roleId);
            } else {
                roleInfo = getPartnerAppRoleInfo(roleId, app.get(0).getId());
            }
            roleList = createCentralRoleObject(app, roleInfo, roleList);
            if (roleList.isEmpty()) {
                return cenRole;
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getRoleInfo: failed", e);
            throw e;
        }
        return roleList.get(0);
    }

    private CentralV2Role getGlobalRoleForRequestedApp(long requestedAppId, long roleId) {
        CentralV2Role finalGlobalrole;
        List<GlobalRoleWithApplicationRoleFunction> roleWithApplicationRoleFucntions = new ArrayList<>();
        try {
            roleWithApplicationRoleFucntions = entityManager
                .createNamedQuery("getGlobalRoleForRequestedApp")
                .setParameter("roleId", roleId)
                .setParameter("requestedAppId", requestedAppId)
                .getResultList();
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getGlobalRoleForRequestedApp failed", e);
        }
        if (roleWithApplicationRoleFucntions.size() > 0) {
            List<CentralV2Role> rolesfinalList = finalListOfCentralRoles(roleWithApplicationRoleFucntions);
            finalGlobalrole = rolesfinalList.get(0);
        } else {
            List<FnRole> roleList = getPortalAppRoleInfo(roleId);
            finalGlobalrole = convertRoleToCentralV2Role(roleList.get(0));
        }
        return finalGlobalrole;
    }

    public EpAppFunction getRoleFunction(String functionCode, String uebkey) throws Exception {
        String code = EcompPortalUtils.getFunctionCode(functionCode);
        String encodedCode = EcompPortalUtils.encodeFunctionCode(code);
        EpAppFunction roleFunc = null;
        FnApp app = getApp(uebkey).get(0);
        List<EpAppFunction> getRoleFuncList = null;
        try {
            getRoleFuncList = epAppFunctionService.getRoleFunction(functionCode, app.getId());
            if (getRoleFuncList.isEmpty()) {
                getRoleFuncList = epAppFunctionService.getRoleFunction(encodedCode, app.getId());
                if (getRoleFuncList.isEmpty()) {
                    return roleFunc;
                }
            }
            if (getRoleFuncList.size() > 1) {
                EpAppFunction cenV2RoleFunction = appFunctionListFilter(encodedCode, getRoleFuncList);
                if (cenV2RoleFunction == null) {
                    return roleFunc;
                }
                roleFunc = checkIfPipesExitsInFunctionCode(cenV2RoleFunction);
            } else {
                // Check even if single record have pipes
                if (!getRoleFuncList.isEmpty() && getRoleFuncList.get(0).getFunctionCd().contains(FUNCTION_PIPE)) {
                    roleFunc = checkIfPipesExitsInFunctionCode(getRoleFuncList.get(0));
                } else {
                    roleFunc = getRoleFuncList.get(0);
                }
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunction: failed", e);
            throw e;
        }
        return roleFunc;
    }

    private EpAppFunction appFunctionListFilter(String roleFuncCode, List<EpAppFunction> roleFunction) {
        final Map<String, EpAppFunction> appFunctionsFilter = new HashMap<>();
        final Map<String, EpAppFunction> appFunctionsFilterPipes = new HashMap<>();
        EpAppFunction getExactFunctionCode;
        for (EpAppFunction cenRoleFunction : roleFunction) {
            appFunctionsFilter.put(cenRoleFunction.getFunctionCd(), cenRoleFunction);
            appFunctionsFilterPipes
                .put(EcompPortalUtils.getFunctionCode(cenRoleFunction.getFunctionCd()), cenRoleFunction);
        }
        getExactFunctionCode = appFunctionsFilter.get(roleFuncCode);
        if (getExactFunctionCode == null) {
            getExactFunctionCode = appFunctionsFilterPipes.get(roleFuncCode);
        }
        return getExactFunctionCode;
    }

    private EpAppFunction checkIfPipesExitsInFunctionCode(EpAppFunction getRoleFuncList) {
        EpAppFunction roleFunc;
        String functionCodeFormat = getRoleFuncList.getFunctionCd();
        if (functionCodeFormat.contains(FUNCTION_PIPE)) {
            String newfunctionCodeFormat = EcompPortalUtils.getFunctionCode(functionCodeFormat);
            String newfunctionTypeFormat = EcompPortalUtils.getFunctionType(functionCodeFormat);
            String newfunctionActionFormat = EcompPortalUtils.getFunctionAction(functionCodeFormat);
            roleFunc = new EpAppFunction(getRoleFuncList.getId(), newfunctionCodeFormat,
                getRoleFuncList.getFunctionName(), getRoleFuncList.getAppId(), newfunctionTypeFormat,
                newfunctionActionFormat, getRoleFuncList.getEditUrl());
        } else {
            roleFunc = EpAppFunction.builder()
                .id(getRoleFuncList.getId())
                .functionCd(functionCodeFormat)
                .functionName(getRoleFuncList.getFunctionName())
                .appId(getRoleFuncList.getAppId())
                .editUrl(getRoleFuncList.getEditUrl())
                .build();
        }
        return roleFunc;
    }

    public boolean saveCentralRoleFunction(EpAppFunction domainCentralRoleFunction, FnApp app)
        throws Exception {
        boolean saveOrUpdateFunction = false;
        try {
            if (EcompPortalUtils.checkFunctionCodeHasEncodePattern(domainCentralRoleFunction.getFunctionCd())) {
                domainCentralRoleFunction
                    .setFunctionCd(EcompPortalUtils.encodeFunctionCode(domainCentralRoleFunction.getFunctionCd()));
            }
            final Map<String, String> functionParams = new HashMap<>();
            functionParams.put("appId", String.valueOf(app.getId()));
            if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                addRoleFunctionInExternalSystem(domainCentralRoleFunction, app);
            }
            if (domainCentralRoleFunction.getType() != null && domainCentralRoleFunction.getAction() != null) {
                domainCentralRoleFunction.setFunctionCd(domainCentralRoleFunction.getType() + FUNCTION_PIPE
                    + domainCentralRoleFunction.getFunctionCd() + FUNCTION_PIPE + domainCentralRoleFunction
                    .getAction());
            }
            domainCentralRoleFunction.setAppId(app);
            epAppFunctionService.save(domainCentralRoleFunction);
            saveOrUpdateFunction = true;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "saveCentralRoleFunction: failed", e);
            throw e;
        }
        return saveOrUpdateFunction;
    }

    private void addRoleFunctionInExternalSystem(EpAppFunction domainCentralRoleFunction, FnApp app)
        throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        ExternalAccessPerms extPerms = new ExternalAccessPerms();
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        String type = "";
        String instance = "";
        String action = "";
        if ((domainCentralRoleFunction.getType() != null && domainCentralRoleFunction.getAction() != null)
            || domainCentralRoleFunction.getFunctionCd().contains(FUNCTION_PIPE)) {
            type = domainCentralRoleFunction.getFunctionCd().contains(FUNCTION_PIPE)
                ? EcompPortalUtils.getFunctionType(domainCentralRoleFunction.getFunctionCd())
                : domainCentralRoleFunction.getType();
            instance = domainCentralRoleFunction.getFunctionCd().contains(FUNCTION_PIPE)
                ? EcompPortalUtils.getFunctionCode(domainCentralRoleFunction.getFunctionCd())
                : domainCentralRoleFunction.getFunctionCd();
            action = domainCentralRoleFunction.getFunctionCd().contains(FUNCTION_PIPE)
                ? EcompPortalUtils.getFunctionAction(domainCentralRoleFunction.getFunctionCd())
                : domainCentralRoleFunction.getAction();
        } else {
            type = domainCentralRoleFunction.getFunctionCd().contains("menu") ? "menu" : "url";
            instance = domainCentralRoleFunction.getFunctionCd();
            action = "*";
        }
        // get Permissions from External Auth System
        JSONArray extPermsList = getExtAuthPermissions(app.getAuthNamespace());
        List<ExternalAccessPermsDetail> permsDetailList = getExtAuthPerrmissonList(app, extPermsList);
        String requestedPerm = type + FUNCTION_PIPE + instance + FUNCTION_PIPE + action;
        boolean checkIfFunctionsExits = permsDetailList.stream()
            .anyMatch(permsDetail -> permsDetail.getInstance().equals(requestedPerm));
        if (!checkIfFunctionsExits) {
            try {
                extPerms.setAction(action);
                extPerms.setInstance(instance);
                extPerms.setType(app.getAuthNamespace() + "." + type);
                extPerms.setDescription(domainCentralRoleFunction.getFunctionName());
                String addFunction = mapper.writeValueAsString(extPerms);
                HttpEntity<String> entity = new HttpEntity<>(addFunction, headers);
                logger.debug(EELFLoggerDelegate.debugLogger, "addRoleFunctionInExternalSystem: {} for POST: {}",
                    CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, addFunction);
                ResponseEntity<String> addPermResponse = template.exchange(
                    SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm",
                    HttpMethod.POST, entity, String.class);
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "addRoleFunctionInExternalSystem: Finished adding permission for POST: {} and status code: {} ",
                    addPermResponse.getStatusCode().value(), addFunction);
            } catch (HttpClientErrorException e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "HttpClientErrorException - Failed to add function in external central auth system", e);
                EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                throw e;
            } catch (Exception e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleFunctionInExternalSystem: Failed to add fucntion in external central auth system", e);
                throw e;
            }
        } else {
            try {
                extPerms.setAction(action);
                extPerms.setInstance(instance);
                extPerms.setType(app.getAuthNamespace() + "." + type);
                extPerms.setDescription(domainCentralRoleFunction.getFunctionName());
                String updateRoleFunction = mapper.writeValueAsString(extPerms);
                HttpEntity<String> entity = new HttpEntity<>(updateRoleFunction, headers);
                logger.debug(EELFLoggerDelegate.debugLogger, "addRoleFunctionInExternalSystem: {} for PUT: {}",
                    CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, updateRoleFunction);
                ResponseEntity<String> updatePermResponse = template.exchange(
                    SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "perm",
                    HttpMethod.PUT, entity, String.class);
                logger.debug(EELFLoggerDelegate.debugLogger,
                    "addRoleFunctionInExternalSystem: Finished updating permission in External Auth system {} and response: {} ",
                    updateRoleFunction, updatePermResponse.getStatusCode().value());
            } catch (HttpClientErrorException e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "HttpClientErrorException - Failed to add function in external central auth system", e);
                EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
                throw e;
            } catch (Exception e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "addRoleFunctionInExternalSystem: Failed to update function in external central auth system",
                    e);
                throw e;
            }
        }
    }

    public CentralRole convertV2CentralRoleToOldVerisonCentralRole(CentralV2Role v2CenRole) {
        SortedSet<EpAppFunction> cenRoleFuncList = new TreeSet<>();
        for (DomainVo vo : v2CenRole.getRoleFunctions()) {
            Optional<EpAppFunction> v2CenRoleFunc = epAppFunctionService.getForId(vo.getId());
            if (v2CenRoleFunc.isPresent()) {
                EpAppFunction roleFunc = EpAppFunction.builder()
                    .functionCd(v2CenRoleFunc.get().getFunctionCd())
                    .functionName(v2CenRoleFunc.get().getFunctionName())
                    .build();
                cenRoleFuncList.add(roleFunc);
            }

        }
        return new CentralRole(v2CenRole.getId(), v2CenRole.getName(), v2CenRole.isActive(), v2CenRole.getPriority(),
            cenRoleFuncList);
    }

    public List<EpAppFunction> getRoleFuncList(String uebkey) throws Exception {
        FnApp app = getApp(uebkey).get(0);
        List<EpAppFunction> finalRoleList = new ArrayList<>();
        List<EpAppFunction> getRoleFuncList = epAppFunctionService.getAllRoleFunctions(app.getId());
        for (EpAppFunction roleFuncItem : getRoleFuncList) {
            String code = EcompPortalUtils.getFunctionCode(roleFuncItem.getFunctionCd());
            String type = "";
            if (roleFuncItem.getFunctionCd().contains("|")) {
                type = EcompPortalUtils.getFunctionType(roleFuncItem.getFunctionCd());
            } else {
                type = getFunctionCodeType(roleFuncItem.getFunctionCd());
            }
            String action = getFunctionCodeAction(roleFuncItem.getFunctionCd());
            roleFuncItem.setFunctionCd(EPUserUtils.decodeFunctionCode(code));
            roleFuncItem.setType(type);
            roleFuncItem.setAction(action);
            finalRoleList.add(roleFuncItem);
        }
        return finalRoleList;
    }

    public List<CentralRoleFunction> convertCentralRoleFunctionToRoleFunctionObject(
        List<EpAppFunction> answer) {
        List<CentralRoleFunction> addRoleFuncList = new ArrayList<>();
        for (EpAppFunction cenRoleFunc : answer) {
            CentralRoleFunction setRoleFunc = new CentralRoleFunction();
            setRoleFunc.setCode(cenRoleFunc.getFunctionCd());
            setRoleFunc.setName(cenRoleFunc.getFunctionName());
            addRoleFuncList.add(setRoleFunc);
        }
        return addRoleFuncList;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCentralRoleFunction(String code, FnApp app) {
        boolean deleteFunctionResponse = false;
        try {
            List<EpAppFunction> domainCentralRoleFunction = epAppFunctionService.getRoleFunction(code, app.getId());
            EpAppFunction appFunctionCode = appFunctionListFilter(code, domainCentralRoleFunction);
            if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
                deleteRoleFunctionInExternalSystem(appFunctionCode, app);
                // Delete role function dependency records
                deleteAppRoleFunctions(appFunctionCode.getFunctionCd(), app.getId());
            }
            epAppFunctionService.deleteOne(appFunctionCode);
            deleteFunctionResponse = true;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "deleteCentralRoleFunction: failed", e);
        }
        return deleteFunctionResponse;
    }

    private void deleteRoleFunctionInExternalSystem(EpAppFunction domainCentralRoleFunction, FnApp app) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            ExternalAccessPerms extPerms = new ExternalAccessPerms();
            String instanceValue = EcompPortalUtils.getFunctionCode(domainCentralRoleFunction.getFunctionCd());
            String checkType = getFunctionCodeType(domainCentralRoleFunction.getFunctionCd());
            String actionValue = getFunctionCodeAction(domainCentralRoleFunction.getFunctionCd());
            HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
            extPerms.setAction(actionValue);
            extPerms.setInstance(instanceValue);
            extPerms.setType(app.getAuthNamespace() + "." + checkType);
            extPerms.setDescription(domainCentralRoleFunction.getFunctionName());
            String deleteRoleFunction = mapper.writeValueAsString(extPerms);
            HttpEntity<String> entity = new HttpEntity<>(deleteRoleFunction, headers);
            logger.debug(EELFLoggerDelegate.debugLogger, "deleteRoleFunctionInExternalSystem: {} for DELETE: {} ",
                CONNECTING_TO_EXTERNAL_AUTH_SYSTEM_LOG_MESSAGE, deleteRoleFunction);
            ResponseEntity<String> delPermResponse = template
                .exchange(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL)
                    + "perm?force=true", HttpMethod.DELETE, entity, String.class);
            logger.debug(EELFLoggerDelegate.debugLogger,
                "deleteRoleFunctionInExternalSystem: Finished deleting permission in External Auth system {} and status code: {} ",
                deleteRoleFunction, delPermResponse.getStatusCode().value());
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to delete functions in External System", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
        } catch (Exception e) {
            if (e.getMessage().equalsIgnoreCase("404 Not Found")) {
                logger.debug(EELFLoggerDelegate.debugLogger,
                    " deleteRoleFunctionInExternalSystem: It seems like function is already deleted in external central auth system  but exists in local DB",
                    e.getMessage());
            } else {
                logger.error(EELFLoggerDelegate.errorLogger,
                    "deleteRoleFunctionInExternalSystem: Failed to delete functions in External System", e);
            }
        }
    }

    private void deleteAppRoleFunctions(final String code, final Long appId) {
        epAppFunctionService.deleteByAppIdAndFunctionCd(appId, code);
    }

    public Integer updateAppRoleDescription(String uebkey) {
        Integer roleDescUpdated = 0;
        FnApp app;
        try {
            app = getApp(uebkey).get(0);
            List<FnRole> roles = getAppRoles(app.getId());
            for (FnRole epRole : roles) {
                Role role = new Role();
                role.setName(epRole.getRoleName());
                boolean status = addRoleDescriptionInExtSystem(role.getName(), app.getAuthNamespace());
                if (status) {
                    roleDescUpdated++;
                }
            }
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "updateAppRoleDescription: Failed! ", e);
        }
        return roleDescUpdated;
    }

    public Role convertCentralRoleToRole(String result) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Role newRole = new Role();
        try {
            newRole = mapper.readValue(result, Role.class);
        } catch (IOException e) {
            logger.error(EELFLoggerDelegate.errorLogger, "Failed to convert the result to Role Object", e);
        }
        if (newRole.getRoleFunctions() != null) {
            Set<RoleFunction> roleFunctionList = newRole.getRoleFunctions();
            Set<RoleFunction> roleFunctionListNew = new HashSet<>();
            for (Object nextValue : roleFunctionList) {
                RoleFunction roleFun = mapper.convertValue(nextValue, RoleFunction.class);
                roleFunctionListNew.add(roleFun);
            }
            newRole.setRoleFunctions(roleFunctionListNew);
        }
        return newRole;
    }

    public void bulkUploadRoleFunc(UploadRoleFunctionExtSystem data, FnApp app) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
        try {
            ExternalAccessRolePerms extRolePerms;
            ExternalAccessPerms extPerms;
            extPerms = new ExternalAccessPerms(app.getAuthNamespace() + "." + data.getType(),
                EcompPortalUtils.encodeFunctionCode(data.getInstance()), data.getAction());
            String appNameSpace = "";
            if (data.isGlobalRolePartnerFunc()) {
                //TODO HARDCODED ID
                appNameSpace = fnAppService.getById(1L).getAuthNamespace();
            } else {
                appNameSpace = app.getAuthNamespace();
            }
            extRolePerms = new ExternalAccessRolePerms(extPerms, appNameSpace + "." + data.getRoleName()
                .replaceAll(EcompPortalUtils.EXTERNAL_CENTRAL_AUTH_ROLE_HANDLE_SPECIAL_CHARACTERS, "_"));
            String updateRolePerms = mapper.writeValueAsString(extRolePerms);
            HttpEntity<String> entity = new HttpEntity<>(updateRolePerms, headers);
            updateRoleFunctionInExternalSystem(updateRolePerms, entity);
        } catch (HttpClientErrorException e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "HttpClientErrorException - Failed to add role function in external central auth system", e);
            EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
            throw e;
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger,
                "addFunctionInExternalSystem: Failed to add role fucntion in external central auth system", e);
            throw e;
        }
    }
}