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
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.sdc.be.components.impl;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.collections.MapUtils.isEmpty;
import static org.apache.commons.collections.MapUtils.isNotEmpty;
import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement;
import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue;
import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN;
import static org.openecomp.sdc.common.api.Constants.DEFAULT_GROUP_VF_MODULE;
import com.google.common.annotations.VisibleForTesting;
import fj.data.Either;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.catalog.enums.ChangeTypeEnum;
import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic;
import org.openecomp.sdc.be.components.csar.CsarBusinessLogic;
import org.openecomp.sdc.be.components.csar.CsarInfo;
import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
import org.openecomp.sdc.be.components.impl.artifact.ArtifactOperationInfo;
import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.impl.utils.CINodeFilterUtils;
import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
import org.openecomp.sdc.be.components.merge.TopologyComparator;
import org.openecomp.sdc.be.components.merge.property.PropertyDataValueMergeBusinessLogic;
import org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic;
import org.openecomp.sdc.be.components.merge.utils.MergeInstanceUtils;
import org.openecomp.sdc.be.components.property.PropertyConstraintsUtils;
import org.openecomp.sdc.be.components.validation.component.ComponentContactIdValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentDescriptionValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentIconValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentProjectCodeValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentTagsValidator;
import org.openecomp.sdc.be.components.validation.component.ComponentValidator;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.CreatedFrom;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.AttributeDefinition;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.NodeTypeInfo;
import org.openecomp.sdc.be.model.Operation;
import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.RelationshipImpl;
import org.openecomp.sdc.be.model.RelationshipInfo;
import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
import org.openecomp.sdc.be.model.RequirementDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.UploadArtifactInfo;
import org.openecomp.sdc.be.model.UploadCapInfo;
import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
import org.openecomp.sdc.be.model.UploadInfo;
import org.openecomp.sdc.be.model.UploadNodeFilterInfo;
import org.openecomp.sdc.be.model.UploadPropInfo;
import org.openecomp.sdc.be.model.UploadReqInfo;
import org.openecomp.sdc.be.model.UploadResourceInfo;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
import org.openecomp.sdc.be.model.operations.api.IElementOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
import org.openecomp.sdc.be.tosca.CsarUtils;
import org.openecomp.sdc.be.tosca.CsarUtils.NonMetaArtifactInfo;
import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.be.utils.CommonBeUtils;
import org.openecomp.sdc.be.utils.TypeUtils;
import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.Wrapper;
import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
import org.openecomp.sdc.common.log.elements.LoggerSupportability;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
import org.openecomp.sdc.common.log.enums.StatusCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.openecomp.sdc.common.util.ValidationUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
@org.springframework.stereotype.Component("resourceBusinessLogic")
public class ResourceBusinessLogic extends ComponentBusinessLogic {
private static final String DELETE_RESOURCE = "Delete Resource";
private static final String IN_RESOURCE = " in resource {} ";
private static final String PLACE_HOLDER_RESOURCE_TYPES = "validForResourceTypes";
private static final String INITIAL_VERSION = "0.1";
private static final Logger log = Logger.getLogger(ResourceBusinessLogic.class);
private static final String CERTIFICATION_ON_IMPORT = "certification on import";
private static final String CREATE_RESOURCE = "Create Resource";
private static final String VALIDATE_DERIVED_BEFORE_UPDATE = "validate derived before update";
private static final String CATEGORY_IS_EMPTY = "Resource category is empty";
private static final String CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES = "Create Resource - validateCapabilityTypesCreate";
private static final String COMPONENT_INSTANCE_WITH_NAME = "component instance with name ";
private static final String COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE = "component instance with name {} in resource {} ";
private static final LoggerSupportability loggerSupportability = LoggerSupportability.getLogger(ResourceBusinessLogic.class.getName());
private final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
private final ResourceImportManager resourceImportManager;
private final InputsBusinessLogic inputsBusinessLogic;
private final OutputsBusinessLogic outputsBusinessLogic;
private final CompositionBusinessLogic compositionBusinessLogic;
private final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic;
private final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic;
private final MergeInstanceUtils mergeInstanceUtils;
private final UiComponentDataConverter uiComponentDataConverter;
private final CsarBusinessLogic csarBusinessLogic;
private final PropertyBusinessLogic propertyBusinessLogic;
private final PolicyBusinessLogic policyBusinessLogic;
private IInterfaceLifecycleOperation interfaceTypeOperation;
private LifecycleBusinessLogic lifecycleBusinessLogic;
@Autowired
private ICapabilityTypeOperation capabilityTypeOperation;
@Autowired
private TopologyComparator topologyComparator;
@Autowired
private ComponentValidator componentValidator;
@Autowired
private PropertyDataValueMergeBusinessLogic propertyDataValueMergeBusinessLogic;
@Autowired
private SoftwareInformationBusinessLogic softwareInformationBusinessLogic;
@Autowired
public ResourceBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation,
final IGroupInstanceOperation groupInstanceOperation, final IGroupTypeOperation groupTypeOperation,
final GroupBusinessLogic groupBusinessLogic, final InterfaceOperation interfaceOperation,
final InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
final ArtifactsBusinessLogic artifactsBusinessLogic,
final ComponentInstanceBusinessLogic componentInstanceBusinessLogic,
final @Lazy ResourceImportManager resourceImportManager, final InputsBusinessLogic inputsBusinessLogic,
final OutputsBusinessLogic outputsBusinessLogic, final CompositionBusinessLogic compositionBusinessLogic,
final ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic,
final CsarArtifactsAndGroupsBusinessLogic csarArtifactsAndGroupsBusinessLogic,
final MergeInstanceUtils mergeInstanceUtils, final UiComponentDataConverter uiComponentDataConverter,
final CsarBusinessLogic csarBusinessLogic, final ArtifactsOperations artifactToscaOperation,
final PropertyBusinessLogic propertyBusinessLogic, final ComponentContactIdValidator componentContactIdValidator,
final ComponentNameValidator componentNameValidator, final ComponentTagsValidator componentTagsValidator,
final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator,
final ComponentProjectCodeValidator componentProjectCodeValidator,
final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic) {
super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation,
interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator,
componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
this.componentInstanceBusinessLogic = componentInstanceBusinessLogic;
this.resourceImportManager = resourceImportManager;
this.inputsBusinessLogic = inputsBusinessLogic;
this.outputsBusinessLogic = outputsBusinessLogic;
this.compositionBusinessLogic = compositionBusinessLogic;
this.resourceDataMergeBusinessLogic = resourceDataMergeBusinessLogic;
this.csarArtifactsAndGroupsBusinessLogic = csarArtifactsAndGroupsBusinessLogic;
this.mergeInstanceUtils = mergeInstanceUtils;
this.uiComponentDataConverter = uiComponentDataConverter;
this.csarBusinessLogic = csarBusinessLogic;
this.propertyBusinessLogic = propertyBusinessLogic;
this.policyBusinessLogic = policyBusinessLogic;
}
static <T> Either<T, RuntimeException> rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus,
final String... params) {
if (janusGraphDao != null) {
janusGraphDao.rollback();
}
return Either.right(new ByActionStatusComponentException(actionStatus, params));
}
public LifecycleBusinessLogic getLifecycleBusinessLogic() {
return lifecycleBusinessLogic;
}
@Autowired
public void setLifecycleManager(LifecycleBusinessLogic lifecycleBusinessLogic) {
this.lifecycleBusinessLogic = lifecycleBusinessLogic;
}
@VisibleForTesting
protected void setComponentValidator(ComponentValidator componentValidator) {
this.componentValidator = componentValidator;
}
public IElementOperation getElementDao() {
return elementDao;
}
public void setElementDao(IElementOperation elementDao) {
this.elementDao = elementDao;
}
public UserBusinessLogic getUserAdmin() {
return this.userAdmin;
}
@Autowired
@Override
public void setUserAdmin(UserBusinessLogic userAdmin) {
this.userAdmin = userAdmin;
}
public ComponentsUtils getComponentsUtils() {
return this.componentsUtils;
}
@Autowired
@Override
public void setComponentsUtils(ComponentsUtils componentsUtils) {
this.componentsUtils = componentsUtils;
}
public ArtifactsBusinessLogic getArtifactsManager() {
return artifactsBusinessLogic;
}
public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
this.artifactsBusinessLogic = artifactsManager;
}
public ApplicationDataTypeCache getApplicationDataTypeCache() {
return applicationDataTypeCache;
}
@Autowired
@Override
public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
this.applicationDataTypeCache = applicationDataTypeCache;
}
@Autowired
public void setInterfaceTypeOperation(IInterfaceLifecycleOperation interfaceTypeOperation) {
this.interfaceTypeOperation = interfaceTypeOperation;
}
/**
* the method returns a list of all the resources that are certified, the returned resources are only abstract or only none abstract according to
* the given param
*
* @param getAbstract
* @param userId TODO
* @return
*/
public List<Resource> getAllCertifiedResources(boolean getAbstract, HighestFilterEnum highestFilter, String userId) {
User user = validateUserExists(userId);
Boolean isHighest = null;
switch (highestFilter) {
case ALL:
break;
case HIGHEST_ONLY:
isHighest = true;
break;
case NON_HIGHEST_ONLY:
isHighest = false;
break;
default:
break;
}
Either<List<Resource>, StorageOperationStatus> getResponse = toscaOperationFacade.getAllCertifiedResources(getAbstract, isHighest);
if (getResponse.isRight()) {
throw new StorageException(getResponse.right().value());
}
return getResponse.left().value();
}
public Either<Map<String, Boolean>, ResponseFormat> validateResourceNameExists(String resourceName, ResourceTypeEnum resourceTypeEnum,
String userId) {
validateUserExists(userId);
Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
.validateComponentNameUniqueness(resourceName, resourceTypeEnum, ComponentTypeEnum.RESOURCE);
// DE242223
janusGraphDao.commit();
if (dataModelResponse.isLeft()) {
Map<String, Boolean> result = new HashMap<>();
result.put("isValid", dataModelResponse.left().value());
log.debug("validation was successfully performed.");
return Either.left(result);
}
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()));
return Either.right(responseFormat);
}
public Resource createResource(Resource resource, AuditingActionEnum auditingAction, User user, Map<String, byte[]> csarUIPayload,
String payloadName) {
validateResourceBeforeCreate(resource, user, false);
String csarUUID = payloadName == null ? resource.getCsarUUID() : payloadName;
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RESOURCE, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Starting to create resource from CSAR by user {} ", user.getUserId());
if (StringUtils.isNotEmpty(csarUUID)) {
csarBusinessLogic.validateCsarBeforeCreate(resource, auditingAction, user, csarUUID);
log.debug("CsarUUID is {} - going to create resource from CSAR", csarUUID);
Resource createResourceFromCsar = createResourceFromCsar(resource, user, csarUIPayload, csarUUID);
return updateCatalog(createResourceFromCsar, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value();
}
final Resource createResourceByDao = createResourceByDao(resource, user, auditingAction, false, false);
return updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value();
}
public Resource validateAndUpdateResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String payloadName,
String resourceUniqueId) {
String csarUUID = payloadName;
String csarVersion = null;
Resource updatedResource = null;
if (payloadName == null) {
csarUUID = resource.getCsarUUID();
csarVersion = resource.getCsarVersion();
}
if (csarUUID != null && !csarUUID.isEmpty()) {
Resource oldResource = getResourceByUniqueId(resourceUniqueId);
validateCsarUuidMatching(oldResource, resource, csarUUID, resourceUniqueId, user);
validateCsarIsNotAlreadyUsed(oldResource, resource, csarUUID, user);
if (oldResource != null && ValidationUtils.hasBeenCertified(oldResource.getVersion())) {
overrideImmutableMetadata(oldResource, resource);
}
validateResourceBeforeCreate(resource, user, false);
String oldCsarVersion = oldResource != null ? oldResource.getCsarVersion() : null;
log.debug("CsarUUID is {} - going to update resource with UniqueId {} from CSAR", csarUUID, resourceUniqueId);
// (on boarding flow): If the update includes same csarUUID and
// same csarVersion as already in the VF - no need to import the
// csar (do only metadata changes if there are).
if (csarVersion != null && oldCsarVersion != null && oldCsarVersion.equals(csarVersion)) {
updatedResource = updateResourceMetadata(resourceUniqueId, resource, oldResource, user, false);
} else {
updatedResource = updateResourceFromCsar(oldResource, resource, user, AuditingActionEnum.UPDATE_RESOURCE_METADATA, false,
csarUIPayload, csarUUID);
}
} else {
log.debug("Failed to update resource {}, csarUUID or payload name is missing", resource.getSystemName());
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CSAR_UUID, resource.getName());
componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.CREATE_RESOURCE);
throw new ByActionStatusComponentException(ActionStatus.MISSING_CSAR_UUID, resource.getName());
}
return updatedResource;
}
private void validateCsarIsNotAlreadyUsed(Resource oldResource, Resource resource, String csarUUID, User user) {
// (on boarding flow): If the update includes a csarUUID: verify this
// csarUUID is not in use by another VF, If it is - use same error as
// above:
// "Error: The VSP with UUID %1 was already imported for VF %2. Please
// select another or update the existing VF." %1 - csarUUID, %2 - VF
// name
Either<Resource, StorageOperationStatus> resourceLinkedToCsarRes = toscaOperationFacade
.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUUID, resource.getSystemName());
if (resourceLinkedToCsarRes.isRight()) {
if (StorageOperationStatus.NOT_FOUND != resourceLinkedToCsarRes.right().value()) {
log.debug("Failed to find previous resource by CSAR {} and system name {}", csarUUID, resource.getSystemName());
throw new StorageException(resourceLinkedToCsarRes.right().value());
}
} else if (!resourceLinkedToCsarRes.left().value().getUniqueId().equals(oldResource.getUniqueId()) && !resourceLinkedToCsarRes.left().value()
.getName().equals(oldResource.getName())) {
ResponseFormat errorResponse = componentsUtils
.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
throw new ByActionStatusComponentException(ActionStatus.VSP_ALREADY_EXISTS, csarUUID, resourceLinkedToCsarRes.left().value().getName());
}
}
private void validateCsarUuidMatching(Resource resource, Resource oldResource, String csarUUID, String resourceUniqueId, User user) {
// (on boarding flow): If the update includes csarUUID which is
// different from the csarUUID of the VF - fail with
// error: "Error: Resource %1 cannot be updated using since it is linked
// to a different VSP" %1 - VF name
String oldCsarUUID = oldResource.getCsarUUID();
if (oldCsarUUID != null && !oldCsarUUID.isEmpty() && !csarUUID.equals(oldCsarUUID)) {
log.debug("Failed to update resource with UniqueId {} using Csar {}, since the resource is linked to a different VSP {}",
resourceUniqueId, csarUUID, oldCsarUUID);
ResponseFormat errorResponse = componentsUtils
.getResponseFormat(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
componentsUtils.auditResource(errorResponse, user, resource, AuditingActionEnum.UPDATE_RESOURCE_METADATA);
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_LINKED_TO_DIFFERENT_VSP, resource.getName(), csarUUID, oldCsarUUID);
}
}
private Resource getResourceByUniqueId(String resourceUniqueId) {
Either<Resource, StorageOperationStatus> oldResourceRes = toscaOperationFacade.getToscaFullElement(resourceUniqueId);
if (oldResourceRes.isRight()) {
log.debug("Failed to find previous resource by UniqueId {}, status: {}", resourceUniqueId, oldResourceRes.right().value());
throw new StorageException(oldResourceRes.right().value());
}
return oldResourceRes.left().value();
}
private void overrideImmutableMetadata(Resource oldResource, Resource resource) {
resource.setName(oldResource.getName());
resource.setIcon(oldResource.getIcon());
resource.setTags(oldResource.getTags());
resource.setCategories(oldResource.getCategories());
resource.setDerivedFrom(oldResource.getDerivedFrom());
}
private Resource updateResourceFromCsar(Resource oldResource, Resource newResource, User user, AuditingActionEnum updateResource,
boolean inTransaction, Map<String, byte[]> csarUIPayload, String csarUUID) {
Resource updatedResource = null;
validateLifecycleState(oldResource, user);
String lockedResourceId = oldResource.getUniqueId();
List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID);
lockComponent(lockedResourceId, oldResource, "update Resource From Csar");
Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo();
Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
nodeTypesInfo, csarInfo, oldResource);
if (findNodeTypesArtifactsToHandleRes.isRight()) {
log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
}
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = findNodeTypesArtifactsToHandleRes.left()
.value();
try {
updatedResource = updateResourceFromYaml(oldResource, newResource, updateResource, createdArtifacts, csarInfo.getMainTemplateName(),
csarInfo.getMainTemplateContent(), csarInfo, nodeTypesInfo, nodeTypesArtifactsToHandle, null, false);
} catch (ComponentException | StorageException e) {
rollback(inTransaction, newResource, createdArtifacts, null);
throw e;
} finally {
janusGraphDao.commit();
log.debug("unlock resource {}", lockedResourceId);
graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
}
return updatedResource;
}
private void validateLifecycleState(Resource oldResource, User user) {
if (LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT == oldResource.getLifecycleState() && !oldResource.getLastUpdaterUserId()
.equals(user.getUserId())) {
log.debug("#validateLifecycleState - Current user is not last updater, last updater userId: {}, current user userId: {}",
oldResource.getLastUpdaterUserId(), user.getUserId());
throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
}
}
private Resource updateResourceFromYaml(Resource oldResource, Resource newResource, AuditingActionEnum actionEnum,
List<ArtifactDefinition> createdArtifacts, String yamlFileName, String yamlFileContent, CsarInfo csarInfo,
Map<String, NodeTypeInfo> nodeTypesInfo,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
String nodeName, boolean isNested) {
boolean inTransaction = true;
boolean shouldLock = false;
Resource preparedResource = null;
ParsedToscaYamlInfo uploadComponentInstanceInfoMap;
try {
uploadComponentInstanceInfoMap = csarBusinessLogic
.getParsedToscaYamlInfo(yamlFileContent, yamlFileName, nodeTypesInfo, csarInfo, nodeName, oldResource);
Map<String, UploadComponentInstanceInfo> instances = uploadComponentInstanceInfoMap.getInstances();
if (MapUtils.isEmpty(instances) && newResource.getResourceType() != ResourceTypeEnum.PNF) {
throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlFileName);
}
preparedResource = updateExistingResourceByImport(newResource, oldResource, csarInfo.getModifier(), inTransaction, shouldLock,
isNested).left;
log.trace("YAML topology file found in CSAR, file name: {}, contents: {}", yamlFileName, yamlFileContent);
handleResourceGenericType(preparedResource);
handleNodeTypes(yamlFileName, preparedResource, yamlFileContent, shouldLock, nodeTypesArtifactsToHandle, createdArtifacts, nodeTypesInfo,
csarInfo, nodeName);
preparedResource = createInputsOnResource(preparedResource, uploadComponentInstanceInfoMap.getInputs());
Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
preparedResource = createResourceInstances(yamlFileName, preparedResource, oldResource, instances, csarInfo.getCreatedNodes(),
existingNodeTypesByResourceNames);
preparedResource = createResourceInstancesRelations(csarInfo.getModifier(), yamlFileName, preparedResource, oldResource, instances,
existingNodeTypesByResourceNames);
} catch (ComponentException e) {
ResponseFormat responseFormat =
e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
componentsUtils
.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
throw e;
} catch (StorageException e) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
log.debug("#updateResourceFromYaml - failed to update newResource from yaml {} .The error is {}", yamlFileName, responseFormat);
componentsUtils
.auditResource(responseFormat, csarInfo.getModifier(), preparedResource == null ? oldResource : preparedResource, actionEnum);
throw e;
}
Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
.validateUpdateVfGroupNames(uploadComponentInstanceInfoMap.getGroups(), preparedResource.getSystemName());
if (validateUpdateVfGroupNamesRes.isRight()) {
throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
}
// add groups to newResource
Map<String, GroupDefinition> groups;
if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
groups = validateUpdateVfGroupNamesRes.left().value();
} else {
groups = uploadComponentInstanceInfoMap.getGroups();
}
handleGroupsProperties(preparedResource, groups);
Either<Boolean, ActionStatus> isTopologyChanged = topologyComparator.isTopologyChanged(oldResource, preparedResource);
preparedResource = updateGroupsOnResource(preparedResource, groups);
NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName, nodeTypesArtifactsToHandle);
Either<Resource, ResponseFormat> updateArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.UPDATE, createdArtifacts, yamlFileName,
csarInfo, preparedResource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
if (updateArtifactsEither.isRight()) {
log.debug("failed to update artifacts {}", updateArtifactsEither.right().value());
throw new ByResponseFormatComponentException(updateArtifactsEither.right().value());
}
preparedResource = getResourceWithGroups(updateArtifactsEither.left().value().getUniqueId());
updateGroupsName(oldResource, preparedResource, isTopologyChanged.left().value());
updateResourceInstancesNames(oldResource, csarInfo, preparedResource, isTopologyChanged.left().value());
final String preparedResourceId = preparedResource != null ? preparedResource.getUniqueId() : "";
preparedResource = getResourceWithGroups(preparedResourceId);
updateVolumeGroup(preparedResource);
ActionStatus mergingPropsAndInputsStatus = resourceDataMergeBusinessLogic.mergeResourceEntities(oldResource, preparedResource);
if (mergingPropsAndInputsStatus != ActionStatus.OK) {
ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(mergingPropsAndInputsStatus, preparedResource);
throw new ByResponseFormatComponentException(responseFormat);
}
compositionBusinessLogic.setPositionsForComponentInstances(preparedResource, csarInfo.getModifier().getUserId());
return preparedResource;
}
protected void updateVolumeGroup(Resource preparedResource) {
List<GroupDefinition> groups = preparedResource.safeGetGroups();
for (GroupDefinition group : groups) {
Map<String, ArtifactDefinition> createdNewArtifacts = preparedResource.getDeploymentArtifacts();
if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
List<PropertyDataDefinition> volumePropList = group.getProperties().stream().filter(p -> "volume_group".equals(p.getName()))
.collect(Collectors.toList());
if (!volumePropList.isEmpty()) {
PropertyDataDefinition volumeProp = volumePropList.get(0);
if (volumeProp != null) {
boolean isVolumeGroup = isVolumeGroup(group.getArtifacts(), new ArrayList<>(createdNewArtifacts.values()));
if (!volumePropList.get(0).getValue().equals(String.valueOf(isVolumeGroup))) {
volumeProp.setValue(String.valueOf(isVolumeGroup));
volumeProp.setDefaultValue(String.valueOf(isVolumeGroup));
}
}
}
}
}
}
private void updateGroupsName(Resource oldResource, Resource preparedResource, boolean isTopologyChanged) {
if (oldResource == null || preparedResource == null) {
log.debug("Failed to update groups name : oldResource or preparedResource is null");
} else if (CollectionUtils.isNotEmpty(oldResource.getGroups()) && CollectionUtils.isNotEmpty(preparedResource.getGroups())) {
Map<String, String> oldGroups = oldResource.getGroups().stream()
.collect(toMap(GroupDataDefinition::getInvariantName, GroupDataDefinition::getName));
List<GroupDefinition> updatedGroups = preparedResource.getGroups().stream()
.filter(group -> oldGroups.containsKey(group.getInvariantName()) && !group.getName().equals(oldGroups.get(group.getInvariantName())))
.collect(toList());
if (CollectionUtils.isNotEmpty(updatedGroups)) {
if (isTopologyChanged) {
updatedGroups.stream().filter(group -> !group.isVspOriginated())
.forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
} else {
updatedGroups.forEach(group -> group.setName(oldGroups.get(group.getInvariantName())));
}
groupBusinessLogic.updateGroups(preparedResource, updatedGroups, false);
}
}
}
private void updateResourceInstancesNames(Resource oldResource, CsarInfo csarInfo, Resource preparedResource, boolean isTopologyChanged) {
if (oldResource == null || preparedResource == null) {
log.debug("Failed to update resource instances names : oldResource or preparedResource is null");
} else {
if (CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
Map<String, String> oldInstances = oldResource.getComponentInstances().stream()
.collect(toMap(ComponentInstance::getInvariantName, ComponentInstance::getName));
List<ComponentInstance> updatedInstances = preparedResource.getComponentInstances().stream()
.filter(i -> oldInstances.containsKey(i.getInvariantName()) && !i.getName().equals(oldInstances.get(i.getInvariantName())))
.collect(toList());
if (CollectionUtils.isNotEmpty(updatedInstances)) {
if (isTopologyChanged) {
updatedInstances.stream().filter(i -> !i.isCreatedFromCsar()).forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
} else {
updatedInstances.forEach(i -> i.setName(oldInstances.get(i.getInvariantName())));
}
}
}
componentInstanceBusinessLogic.updateComponentInstance(ComponentTypeEnum.RESOURCE_PARAM_NAME, null, preparedResource.getUniqueId(),
csarInfo.getModifier().getUserId(), preparedResource.getComponentInstances(), false);
}
}
private Either<Resource, ResponseFormat> createOrUpdateArtifacts(ArtifactOperationEnum operation, List<ArtifactDefinition> createdArtifacts,
String yamlFileName, CsarInfo csarInfo, Resource preparedResource,
NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts,
boolean inTransaction, boolean shouldLock) {
String nodeName = nodeTypeInfoToUpdateArtifacts.getNodeName();
Resource resource = preparedResource;
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = nodeTypeInfoToUpdateArtifacts
.getNodeTypesArtifactsToHandle();
if (preparedResource.getResourceType() == ResourceTypeEnum.CVFC) {
if (nodeName != null && nodeTypesArtifactsToHandle.get(nodeName) != null && !nodeTypesArtifactsToHandle.get(nodeName).isEmpty()) {
Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = handleNodeTypeArtifacts(preparedResource,
nodeTypesArtifactsToHandle.get(nodeName), createdArtifacts, csarInfo.getModifier(), inTransaction, true);
if (handleNodeTypeArtifactsRes.isRight()) {
return Either.right(handleNodeTypeArtifactsRes.right().value());
}
}
} else {
Either<Resource, ResponseFormat> createdCsarArtifactsEither = handleVfCsarArtifacts(preparedResource, csarInfo, createdArtifacts,
new ArtifactOperationInfo(false, false, operation), shouldLock, inTransaction);
log.trace("************* Finished to add artifacts from yaml {}", yamlFileName);
if (createdCsarArtifactsEither.isRight()) {
return createdCsarArtifactsEither;
}
resource = createdCsarArtifactsEither.left().value();
}
return Either.left(resource);
}
private Resource handleResourceGenericType(Resource resource) {
Resource genericResource = fetchAndSetDerivedFromGenericType(resource);
if (resource.shouldGenerateInputs()) {
generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
}
return genericResource;
}
private Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandle(
final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo, final Resource oldResource) {
final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle = new HashMap<>();
Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> nodeTypesArtifactsToHandleRes = Either
.left(nodeTypesArtifactsToHandle);
try {
final Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts = CsarUtils.extractVfcsArtifactsFromCsar(csarInfo.getCsar());
final Map<String, ImmutablePair<String, String>> extractedVfcToscaNames = extractVfcToscaNames(nodeTypesInfo, oldResource.getName(),
csarInfo);
log.debug("Going to fetch node types for resource with name {} during import csar with UUID {}. ", oldResource.getName(),
csarInfo.getCsarUUID());
extractedVfcToscaNames.forEach(
(namespace, vfcToscaNames) -> findAddNodeTypeArtifactsToHandle(csarInfo, nodeTypesArtifactsToHandle, oldResource,
extractedVfcsArtifacts, namespace, vfcToscaNames));
} catch (Exception e) {
final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
nodeTypesArtifactsToHandleRes = Either.right(responseFormat);
log.debug("Exception occurred when findNodeTypesUpdatedArtifacts, error is:{}", e.getMessage(), e);
}
return nodeTypesArtifactsToHandleRes;
}
private void findAddNodeTypeArtifactsToHandle(CsarInfo csarInfo,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
Resource resource, Map<String, List<ArtifactDefinition>> extractedVfcsArtifacts, String namespace,
ImmutablePair<String, String> vfcToscaNames) {
EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> curNodeTypeArtifactsToHandle = null;
log.debug("Going to fetch node type with tosca name {}. ", vfcToscaNames.getLeft());
Resource curNodeType = findVfcResource(csarInfo, resource, vfcToscaNames.getLeft(), vfcToscaNames.getRight(), null);
if (!isEmpty(extractedVfcsArtifacts)) {
List<ArtifactDefinition> currArtifacts = new ArrayList<>();
if (extractedVfcsArtifacts.containsKey(namespace)) {
handleAndAddExtractedVfcsArtifacts(currArtifacts, extractedVfcsArtifacts.get(namespace));
}
curNodeTypeArtifactsToHandle = findNodeTypeArtifactsToHandle(curNodeType, currArtifacts);
} else if (curNodeType != null) {
// delete all artifacts if have not received artifacts from
// csar
curNodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
// delete all informational artifacts
artifactsToDelete.addAll(
curNodeType.getArtifacts().values().stream().filter(a -> a.getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
.collect(toList()));
// delete all deployment artifacts
artifactsToDelete.addAll(curNodeType.getDeploymentArtifacts().values());
if (!artifactsToDelete.isEmpty()) {
curNodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
}
}
if (isNotEmpty(curNodeTypeArtifactsToHandle)) {
nodeTypesArtifactsToHandle.put(namespace, curNodeTypeArtifactsToHandle);
}
}
private Resource findVfcResource(CsarInfo csarInfo, Resource resource, String currVfcToscaName, String previousVfcToscaName,
StorageOperationStatus status) {
if (status != null && status != StorageOperationStatus.NOT_FOUND) {
log.debug("Error occurred during fetching node type with tosca name {}, error: {}", currVfcToscaName, status);
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.CREATE_RESOURCE);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), csarInfo.getCsarUUID());
} else if (StringUtils.isNotEmpty(currVfcToscaName)) {
return (Resource) toscaOperationFacade.getLatestByToscaResourceName(currVfcToscaName).left()
.on(st -> findVfcResource(csarInfo, resource, previousVfcToscaName, null, st));
}
return null;
}
private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> findNodeTypeArtifactsToHandle(Resource curNodeType,
List<ArtifactDefinition> extractedArtifacts) {
try {
List<ArtifactDefinition> artifactsToUpload = new ArrayList<>(extractedArtifacts);
List<ArtifactDefinition> artifactsToUpdate = new ArrayList<>();
List<ArtifactDefinition> artifactsToDelete = new ArrayList<>();
processExistingNodeTypeArtifacts(extractedArtifacts, artifactsToUpload, artifactsToUpdate, artifactsToDelete,
collectExistingArtifacts(curNodeType));
return putFoundArtifacts(artifactsToUpload, artifactsToUpdate, artifactsToDelete);
} catch (Exception e) {
log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
}
private EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> putFoundArtifacts(List<ArtifactDefinition> artifactsToUpload,
List<ArtifactDefinition> artifactsToUpdate,
List<ArtifactDefinition> artifactsToDelete) {
EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle = null;
if (!artifactsToUpload.isEmpty() || !artifactsToUpdate.isEmpty() || !artifactsToDelete.isEmpty()) {
nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
if (!artifactsToUpload.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
}
if (!artifactsToUpdate.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
}
if (!artifactsToDelete.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
}
}
return nodeTypeArtifactsToHandle;
}
private void processExistingNodeTypeArtifacts(List<ArtifactDefinition> extractedArtifacts, List<ArtifactDefinition> artifactsToUpload,
List<ArtifactDefinition> artifactsToUpdate, List<ArtifactDefinition> artifactsToDelete,
Map<String, ArtifactDefinition> existingArtifacts) {
if (!existingArtifacts.isEmpty()) {
extractedArtifacts.stream().forEach(a -> processNodeTypeArtifact(artifactsToUpload, artifactsToUpdate, existingArtifacts, a));
artifactsToDelete.addAll(existingArtifacts.values());
}
}
private void processNodeTypeArtifact(List<ArtifactDefinition> artifactsToUpload, List<ArtifactDefinition> artifactsToUpdate,
Map<String, ArtifactDefinition> existingArtifacts, ArtifactDefinition currNewArtifact) {
Optional<ArtifactDefinition> foundArtifact = existingArtifacts.values().stream()
.filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName())).findFirst();
if (foundArtifact.isPresent()) {
if (foundArtifact.get().getArtifactType().equals(currNewArtifact.getArtifactType())) {
updateFoundArtifact(artifactsToUpdate, currNewArtifact, foundArtifact.get());
existingArtifacts.remove(foundArtifact.get().getArtifactLabel());
artifactsToUpload.remove(currNewArtifact);
} else {
log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR,
currNewArtifact.getArtifactName(), currNewArtifact.getArtifactType(), foundArtifact.get().getArtifactType());
}
}
}
private void updateFoundArtifact(List<ArtifactDefinition> artifactsToUpdate, ArtifactDefinition currNewArtifact,
ArtifactDefinition foundArtifact) {
if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
foundArtifact.setPayload(currNewArtifact.getPayloadData());
foundArtifact.setPayloadData(Base64.encodeBase64String(currNewArtifact.getPayloadData()));
foundArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(currNewArtifact.getPayloadData()));
artifactsToUpdate.add(foundArtifact);
}
}
private Map<String, ArtifactDefinition> collectExistingArtifacts(Resource curNodeType) {
Map<String, ArtifactDefinition> existingArtifacts = new HashMap<>();
if (curNodeType == null) {
return existingArtifacts;
}
if (MapUtils.isNotEmpty(curNodeType.getDeploymentArtifacts())) {
existingArtifacts.putAll(curNodeType.getDeploymentArtifacts());
}
if (MapUtils.isNotEmpty(curNodeType.getArtifacts())) {
existingArtifacts.putAll(
curNodeType.getArtifacts().entrySet().stream().filter(e -> e.getValue().getArtifactGroupType() == ArtifactGroupTypeEnum.INFORMATIONAL)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
return existingArtifacts;
}
/**
* Changes resource life cycle state to checked out
*
* @param resource
* @param user
* @param inTransaction
* @return
*/
private Either<Resource, ResponseFormat> checkoutResource(Resource resource, User user, boolean inTransaction) {
Either<Resource, ResponseFormat> checkoutResourceRes;
try {
if (!resource.getComponentMetadataDefinition().getMetadataDataDefinition().getState()
.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
log.debug("************* Going to change life cycle state of resource {} to not certified checked out. ", resource.getName());
Either<? extends Component, ResponseFormat> checkoutRes = lifecycleBusinessLogic
.changeComponentState(resource.getComponentType(), resource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT,
new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT, LifecycleChanceActionEnum.CREATE_FROM_CSAR), inTransaction, true);
if (checkoutRes.isRight()) {
log.debug("Could not change state of component {} with uid {} to checked out. Status is {}. ",
resource.getComponentType().getNodeType(), resource.getUniqueId(), checkoutRes.right().value().getStatus());
checkoutResourceRes = Either.right(checkoutRes.right().value());
} else {
checkoutResourceRes = Either.left((Resource) checkoutRes.left().value());
}
} else {
checkoutResourceRes = Either.left(resource);
}
} catch (Exception e) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
checkoutResourceRes = Either.right(responseFormat);
log.debug("Exception occurred when checkoutResource {} , error is:{}", resource.getName(), e.getMessage(), e);
}
return checkoutResourceRes;
}
/**
* Handles Artifacts of NodeType
*
* @param nodeTypeResource
* @param nodeTypeArtifactsToHandle
* @param user
* @param inTransaction
* @return
*/
public Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifacts(Resource nodeTypeResource,
Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
List<ArtifactDefinition> createdArtifacts, User user,
boolean inTransaction, boolean ignoreLifecycleState) {
List<ArtifactDefinition> handleNodeTypeArtifactsRequestRes;
Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = null;
Either<Resource, ResponseFormat> changeStateResponse;
try {
changeStateResponse = checkoutResource(nodeTypeResource, user, inTransaction);
if (changeStateResponse.isRight()) {
return Either.right(changeStateResponse.right().value());
}
nodeTypeResource = changeStateResponse.left().value();
List<ArtifactDefinition> handledNodeTypeArtifacts = new ArrayList<>();
log.debug("************* Going to handle artifacts of node type resource {}. ", nodeTypeResource.getName());
for (Entry<ArtifactOperationEnum, List<ArtifactDefinition>> curOperationEntry : nodeTypeArtifactsToHandle.entrySet()) {
ArtifactOperationEnum curOperation = curOperationEntry.getKey();
List<ArtifactDefinition> curArtifactsToHandle = curOperationEntry.getValue();
if (curArtifactsToHandle != null && !curArtifactsToHandle.isEmpty()) {
log.debug("************* Going to {} artifact to vfc {}", curOperation.name(), nodeTypeResource.getName());
handleNodeTypeArtifactsRequestRes = artifactsBusinessLogic
.handleArtifactsRequestForInnerVfcComponent(curArtifactsToHandle, nodeTypeResource, user, createdArtifacts,
new ArtifactOperationInfo(false, ignoreLifecycleState, curOperation), false, inTransaction);
if (ArtifactOperationEnum.isCreateOrLink(curOperation)) {
createdArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
}
handledNodeTypeArtifacts.addAll(handleNodeTypeArtifactsRequestRes);
}
}
if (handleNodeTypeArtifactsRes == null) {
handleNodeTypeArtifactsRes = Either.left(handledNodeTypeArtifacts);
}
} catch (Exception e) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
handleNodeTypeArtifactsRes = Either.right(responseFormat);
log.debug("Exception occurred when handleVfcArtifacts, error is:{}", e.getMessage(), e);
}
return handleNodeTypeArtifactsRes;
}
private Map<String, ImmutablePair<String, String>> extractVfcToscaNames(final Map<String, NodeTypeInfo> nodeTypesInfo,
final String vfResourceName, final CsarInfo csarInfo) {
final Map<String, ImmutablePair<String, String>> vfcToscaNames = new HashMap<>();
final Map<String, Object> nodes = extractAllNodes(nodeTypesInfo, csarInfo);
if (!nodes.isEmpty()) {
final Iterator<Entry<String, Object>> nodesNameEntry = nodes.entrySet().iterator();
while (nodesNameEntry.hasNext()) {
final Entry<String, Object> nodeType = nodesNameEntry.next();
final ImmutablePair<String, String> toscaResourceName = buildNestedToscaResourceName(ResourceTypeEnum.VFC.name(), vfResourceName,
nodeType.getKey());
vfcToscaNames.put(nodeType.getKey(), toscaResourceName);
}
}
for (final NodeTypeInfo cvfc : nodeTypesInfo.values()) {
vfcToscaNames.put(cvfc.getType(), buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), vfResourceName, cvfc.getType()));
}
return vfcToscaNames;
}
private Map<String, Object> extractAllNodes(Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
Map<String, Object> nodes = new HashMap<>();
for (NodeTypeInfo nodeTypeInfo : nodeTypesInfo.values()) {
extractNodeTypes(nodes, nodeTypeInfo.getMappedToscaTemplate());
}
extractNodeTypes(nodes, csarInfo.getMappedToscaMainTemplate());
return nodes;
}
private void extractNodeTypes(Map<String, Object> nodes, Map<String, Object> mappedToscaTemplate) {
Either<Map<String, Object>, ResultStatusEnum> eitherNodeTypes = ImportUtils
.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
if (eitherNodeTypes.isLeft()) {
nodes.putAll(eitherNodeTypes.left().value());
}
}
public Resource createResourceFromCsar(Resource resource, User user, Map<String, byte[]> csarUIPayload, String csarUUID) {
log.trace("************* created successfully from YAML, resource TOSCA ");
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}",
user.getUserId());
CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID);
Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractNodeTypesInfo();
Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle(
nodeTypesInfo, csarInfo, resource);
if (findNodeTypesArtifactsToHandleRes.isRight()) {
log.debug("failed to find node types for update with artifacts during import csar {}. ", csarInfo.getCsarUUID());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"error: {}", findNodeTypesArtifactsToHandleRes.right().value());
throw new ByResponseFormatComponentException(findNodeTypesArtifactsToHandleRes.right().value());
}
Resource vfResource = createResourceFromYaml(resource, csarInfo.getMainTemplateContent(), csarInfo.getMainTemplateName(), nodeTypesInfo,
csarInfo, findNodeTypesArtifactsToHandleRes.left().value(), true, false, null);
log.trace("*************VF Resource created successfully from YAML, resource TOSCA name: {}", vfResource.getToscaResourceName());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.COMPLETE, "Ended create Resource From Csar by user {}",
user.getUserId());
return vfResource;
}
private Resource validateResourceBeforeCreate(Resource resource, User user, boolean inTransaction) {
log.trace("validating resource before create");
user.copyData(validateUser(user, CREATE_RESOURCE, resource, AuditingActionEnum.CREATE_RESOURCE, false));
// validate user role
validateUserRole(user, resource, new ArrayList<>(), AuditingActionEnum.CREATE_RESOURCE, null);
// VF / PNF "derivedFrom" should be null (or ignored)
if (ModelConverter.isAtomicComponent(resource)) {
validateDerivedFromNotEmpty(user, resource, AuditingActionEnum.CREATE_RESOURCE);
}
return validateResourceBeforeCreate(resource, user, AuditingActionEnum.CREATE_RESOURCE, inTransaction, null);
}
private Resource createResourceFromYaml(Resource resource, String topologyTemplateYaml, String yamlName, Map<String, NodeTypeInfo> nodeTypesInfo,
CsarInfo csarInfo,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
boolean shouldLock, boolean inTransaction, String nodeName) {
List<ArtifactDefinition> createdArtifacts = new ArrayList<>();
Resource createdResource;
try {
ParsedToscaYamlInfo parsedToscaYamlInfo = csarBusinessLogic
.getParsedToscaYamlInfo(topologyTemplateYaml, yamlName, nodeTypesInfo, csarInfo, nodeName, resource);
if (MapUtils.isEmpty(parsedToscaYamlInfo.getInstances()) && resource.getResourceType() != ResourceTypeEnum.PNF) {
throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
log.debug("#createResourceFromYaml - Going to create resource {} and RIs ", resource.getName());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED, "");
createdResource = createResourceAndRIsFromYaml(yamlName, resource, parsedToscaYamlInfo, AuditingActionEnum.IMPORT_RESOURCE, false,
createdArtifacts, topologyTemplateYaml, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, shouldLock, inTransaction, nodeName);
log.debug("#createResourceFromYaml - The resource {} has been created ", resource.getName());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
"The resource has been created: {}", resource.getName());
} catch (ComponentException e) {
ResponseFormat responseFormat =
e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
throw e;
} catch (StorageException e) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
throw e;
}
return createdResource;
}
public Map<String, Resource> createResourcesFromYamlNodeTypesList(String yamlName, Resource resource, Map<String, Object> mappedToscaTemplate,
boolean needLock,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo) {
Either<String, ResultStatusEnum> toscaVersion = findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
if (toscaVersion.isRight()) {
throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
}
Map<String, Object> mapToConvert = new HashMap<>();
mapToConvert.put(TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION.getElementName(), toscaVersion.left().value());
Map<String, Object> nodeTypes = getNodeTypesFromTemplate(mappedToscaTemplate);
createNodeTypes(yamlName, resource, needLock, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, mapToConvert,
nodeTypes);
return csarInfo.getCreatedNodes();
}
private Map<String, Object> getNodeTypesFromTemplate(Map<String, Object> mappedToscaTemplate) {
return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES).left().orValue(HashMap::new);
}
private void createNodeTypes(String yamlName, Resource resource, boolean needLock,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
Map<String, Object> mapToConvert, Map<String, Object> nodeTypes) {
Iterator<Entry<String, Object>> nodesNameValueIter = nodeTypes.entrySet().iterator();
Resource vfcCreated = null;
while (nodesNameValueIter.hasNext()) {
Entry<String, Object> nodeType = nodesNameValueIter.next();
Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle =
nodeTypesArtifactsToHandle == null || nodeTypesArtifactsToHandle.isEmpty() ? null : nodeTypesArtifactsToHandle.get(nodeType.getKey());
if (nodeTypesInfo.containsKey(nodeType.getKey())) {
log.trace("************* Going to handle nested vfc {}", nodeType.getKey());
vfcCreated = handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
nodeType.getKey());
log.trace("************* Finished to handle nested vfc {}", nodeType.getKey());
} else if (csarInfo.getCreatedNodesToscaResourceNames() != null && !csarInfo.getCreatedNodesToscaResourceNames()
.containsKey(nodeType.getKey())) {
log.trace("************* Going to create node {}", nodeType.getKey());
ImmutablePair<Resource, ActionStatus> resourceCreated = createNodeTypeResourceFromYaml(yamlName, nodeType, csarInfo.getModifier(),
mapToConvert, resource, needLock, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, true, csarInfo, true);
log.debug("************* Finished to create node {}", nodeType.getKey());
vfcCreated = resourceCreated.getLeft();
csarInfo.getCreatedNodesToscaResourceNames().put(nodeType.getKey(), vfcCreated.getToscaResourceName());
}
if (vfcCreated != null) {
csarInfo.getCreatedNodes().put(nodeType.getKey(), vfcCreated);
}
mapToConvert.remove(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName());
}
}
private Resource handleNestedVfc(Resource resource, Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
String nodeName) {
String yamlName = nodesInfo.get(nodeName).getTemplateFileName();
Map<String, Object> nestedVfcJsonMap = nodesInfo.get(nodeName).getMappedToscaTemplate();
log.debug("************* Going to create node types from yaml {}", yamlName);
createResourcesFromYamlNodeTypesList(yamlName, resource, nestedVfcJsonMap, false, nodesArtifactsToHandle, createdArtifacts,
Collections.emptyMap(), csarInfo);
log.debug("************* Finished to create node types from yaml {}", yamlName);
if (nestedVfcJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE.getElementName())) {
log.debug("************* Going to handle complex VFC from yaml {}", yamlName);
resource = handleComplexVfc(resource, nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName);
}
return resource;
}
private Resource handleComplexVfc(final Resource resource,
final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
final List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
final String nodeName, final String yamlName) {
Resource oldComplexVfc = null;
Resource newComplexVfc = buildValidComplexVfc(resource, csarInfo, nodeName, nodesInfo);
Either<Resource, StorageOperationStatus> oldComplexVfcRes = toscaOperationFacade
.getFullLatestComponentByToscaResourceName(newComplexVfc.getToscaResourceName());
if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() == StorageOperationStatus.NOT_FOUND) {
oldComplexVfcRes = toscaOperationFacade.getFullLatestComponentByToscaResourceName(
buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getRight());
}
if (oldComplexVfcRes.isRight() && oldComplexVfcRes.right().value() != StorageOperationStatus.NOT_FOUND) {
log.debug("Failed to fetch previous complex VFC by tosca resource name {}. Status is {}. ", newComplexVfc.getToscaResourceName(),
oldComplexVfcRes.right().value());
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
} else if (oldComplexVfcRes.isLeft()) {
log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(oldComplexVfcRes.left().value(),
newComplexVfc, ValidationUtils.hasBeenCertified(oldComplexVfcRes.left().value().getVersion()));
if (eitherValidation.isLeft()) {
oldComplexVfc = oldComplexVfcRes.left().value();
}
}
newComplexVfc = handleComplexVfc(nodesArtifactsToHandle, createdArtifacts, nodesInfo, csarInfo, nodeName, yamlName, oldComplexVfc,
newComplexVfc);
csarInfo.getCreatedNodesToscaResourceNames().put(nodeName, newComplexVfc.getToscaResourceName());
final LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
LifecycleChanceActionEnum.CREATE_FROM_CSAR);
log.debug("Going to certify cvfc {}. ", newComplexVfc.getName());
final Resource result = propagateStateToCertified(csarInfo.getModifier(), newComplexVfc, lifecycleChangeInfo, true, false, true);
csarInfo.getCreatedNodes().put(nodeName, result);
csarInfo.removeNodeFromQueue();
return result;
}
private Resource handleComplexVfc(Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodesArtifactsToHandle,
List<ArtifactDefinition> createdArtifacts, Map<String, NodeTypeInfo> nodesInfo, CsarInfo csarInfo,
String nodeName, String yamlName, Resource oldComplexVfc, Resource newComplexVfc) {
Resource handleComplexVfcRes;
Map<String, Object> mappedToscaTemplate = nodesInfo.get(nodeName).getMappedToscaTemplate();
String yamlContent = new String(csarInfo.getCsar().get(yamlName));
Map<String, NodeTypeInfo> newNodeTypesInfo = nodesInfo.entrySet().stream().collect(toMap(Entry::getKey, e -> e.getValue().getUnmarkedCopy()));
CsarInfo.markNestedVfc(mappedToscaTemplate, newNodeTypesInfo);
if (oldComplexVfc == null) {
handleComplexVfcRes = createResourceFromYaml(newComplexVfc, yamlContent, yamlName, newNodeTypesInfo, csarInfo, nodesArtifactsToHandle,
false, true, nodeName);
} else {
handleComplexVfcRes = updateResourceFromYaml(oldComplexVfc, newComplexVfc, AuditingActionEnum.UPDATE_RESOURCE_METADATA, createdArtifacts,
yamlContent, yamlName, csarInfo, newNodeTypesInfo, nodesArtifactsToHandle, nodeName, true);
}
return handleComplexVfcRes;
}
private Resource buildValidComplexVfc(Resource resource, CsarInfo csarInfo, String nodeName, Map<String, NodeTypeInfo> nodesInfo) {
Resource complexVfc = buildComplexVfcMetadata(resource, csarInfo, nodeName, nodesInfo);
log.debug("************* Going to validate complex VFC from yaml {}", complexVfc.getName());
csarInfo.addNodeToQueue(nodeName);
return validateResourceBeforeCreate(complexVfc, csarInfo.getModifier(), AuditingActionEnum.IMPORT_RESOURCE, true, csarInfo);
}
private String getNodeTypeActualName(final String nodeTypefullName, final String nodeTypeNamePrefix) {
final String nameWithouNamespacePrefix = nodeTypefullName.substring(nodeTypeNamePrefix.length());
final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
if (findTypes.length > 1) {
final String resourceType = findTypes[0];
return nameWithouNamespacePrefix.substring(resourceType.length());
}
return nameWithouNamespacePrefix;
}
private ImmutablePair<Resource, ActionStatus> createNodeTypeResourceFromYaml(final String yamlName, final Entry<String, Object> nodeNameValue,
User user, final Map<String, Object> mapToConvert,
final Resource resourceVf, final boolean needLock,
final Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
final boolean forceCertificationAllowed, final CsarInfo csarInfo,
final boolean isNested) {
final UploadResourceInfo resourceMetaData = fillResourceMetadata(yamlName, resourceVf, nodeNameValue.getKey(), user);
final String singleVfcYaml = buildNodeTypeYaml(nodeNameValue, mapToConvert, resourceMetaData.getResourceType(), csarInfo);
user = validateUser(user, "CheckIn Resource", resourceVf, AuditingActionEnum.CHECKIN_RESOURCE, true);
return createResourceFromNodeType(singleVfcYaml, resourceMetaData, user, true, needLock, nodeTypeArtifactsToHandle,
nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeNameValue.getKey(), isNested);
}
private String buildNodeTypeYaml(final Entry<String, Object> nodeNameValue, final Map<String, Object> mapToConvert, final String nodeResourceType,
final CsarInfo csarInfo) {
// We need to create a Yaml from each node_types in order to create
// resource from each node type using import normative flow.
final DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
final Yaml yaml = new Yaml(options);
final Map<String, Object> node = new HashMap<>();
node.put(buildNestedToscaResourceName(nodeResourceType, csarInfo.getVfResourceName(), nodeNameValue.getKey()).getLeft(),
nodeNameValue.getValue());
mapToConvert.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), node);
return yaml.dumpAsMap(mapToConvert);
}
public Boolean validateResourceCreationFromNodeType(Resource resource, User creator) {
validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
return true;
}
public ImmutablePair<Resource, ActionStatus> createResourceFromNodeType(String nodeTypeYaml, UploadResourceInfo resourceMetaData, User creator,
boolean isInTransaction, boolean needLock,
Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
boolean isNested) {
LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(CERTIFICATION_ON_IMPORT,
LifecycleChanceActionEnum.CREATE_FROM_CSAR);
Function<Resource, Boolean> validator = resource -> validateResourceCreationFromNodeType(resource, creator);
return resourceImportManager
.importCertifiedResource(nodeTypeYaml, resourceMetaData, creator, validator, lifecycleChangeInfo, isInTransaction, true, needLock,
nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, forceCertificationAllowed, csarInfo, nodeName, isNested);
}
/**
* Validates if a given node type name has a valid prefix.
*
* @param nodeName node name from definition file
* @param definedResourceNamespaceList is a list of all node type name prefix allowed
* @return a valid node type name prefix if it`s found
*/
public Optional<String> validateNodeTypeNamePrefix(final String nodeName, final List<String> definedResourceNamespaceList) {
for (final String validNamespace : definedResourceNamespaceList) {
if (nodeName.startsWith(validNamespace)) {
return Optional.of(validNamespace);
}
}
return Optional.empty();
}
private List<String> getDefinedNodeTypeNamespaceList() {
return ConfigurationManager.getConfigurationManager().getConfiguration().getDefinedResourceNamespace();
}
private UploadResourceInfo fillResourceMetadata(final String yamlName, final Resource resourceVf, final String nodeName, final User user) {
final UploadResourceInfo resourceMetaData = new UploadResourceInfo();
final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeName);
log.debug("Node type Name prefix {}", nodeTypeNamePrefix);
if (!nodeName.startsWith(nodeTypeNamePrefix)) {
log.debug("invalid nodeName:{} does not start with {}.", nodeName, getDefinedNodeTypeNamespaceList());
throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
}
final String actualName = this.getNodeTypeActualName(nodeName, nodeTypeNamePrefix);
final String namePrefix = nodeName.replace(actualName, "");
String resourceType = namePrefix.substring(nodeTypeNamePrefix.length());
log.debug("initial namePrefix:{} resourceType {}. nodeName {} , actualName {} prefix {}", namePrefix, resourceType, nodeName, actualName,
nodeTypeNamePrefix);
// if we import from csar, the node_type name can be
// org.openecomp.resource.abstract.node_name - in this case we always
// create a vfc
if (resourceType.equals(Constants.ABSTRACT)) {
resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
}
if (!ResourceTypeEnum.containsIgnoreCase(resourceType)) {
resourceType = ResourceTypeEnum.VFC.name().toLowerCase();
}
// validating type
if (!ResourceTypeEnum.containsName(resourceType.toUpperCase())) {
log.debug("invalid resourceType:{} the type is not one of the valide types:{}.", resourceType.toUpperCase(), ResourceTypeEnum.values());
throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, resourceMetaData.getName(), nodeName);
}
// Setting name
resourceMetaData.setName(new StringBuilder(resourceVf.getSystemName()).append(actualName).toString());
// Setting type from name
final String type = resourceType.toUpperCase();
resourceMetaData.setResourceType(type);
resourceMetaData.setDescription(ImportUtils.Constants.INNER_VFC_DESCRIPTION);
resourceMetaData.setIcon(ImportUtils.Constants.DEFAULT_ICON);
resourceMetaData.setContactId(user.getUserId());
resourceMetaData.setVendorName(resourceVf.getVendorName());
resourceMetaData.setVendorRelease(resourceVf.getVendorRelease());
// Setting tag
final List<String> tags = new ArrayList<>();
tags.add(resourceMetaData.getName());
resourceMetaData.setTags(tags);
// Setting category
final CategoryDefinition category = new CategoryDefinition();
category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
final SubCategoryDefinition subCategory = new SubCategoryDefinition();
subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
category.addSubCategory(subCategory);
final List<CategoryDefinition> categories = new ArrayList<>();
categories.add(category);
resourceMetaData.setCategories(categories);
return resourceMetaData;
}
private Resource buildComplexVfcMetadata(final Resource resourceVf, final CsarInfo csarInfo, final String nodeName,
final Map<String, NodeTypeInfo> nodesInfo) {
final Resource cvfc = new Resource();
final NodeTypeInfo nodeTypeInfo = nodesInfo.get(nodeName);
cvfc.setName(buildCvfcName(csarInfo.getVfResourceName(), nodeName));
cvfc.setNormalizedName(ValidationUtils.normaliseComponentName(cvfc.getName()));
cvfc.setSystemName(ValidationUtils.convertToSystemName(cvfc.getName()));
cvfc.setResourceType(ResourceTypeEnum.CVFC);
cvfc.setAbstract(true);
cvfc.setDerivedFrom(nodeTypeInfo.getDerivedFrom());
cvfc.setDescription(ImportUtils.Constants.CVFC_DESCRIPTION);
cvfc.setIcon(ImportUtils.Constants.DEFAULT_ICON);
cvfc.setContactId(csarInfo.getModifier().getUserId());
cvfc.setCreatorUserId(csarInfo.getModifier().getUserId());
cvfc.setVendorName(resourceVf.getVendorName());
cvfc.setVendorRelease(resourceVf.getVendorRelease());
cvfc.setResourceVendorModelNumber(resourceVf.getResourceVendorModelNumber());
cvfc.setToscaResourceName(buildNestedToscaResourceName(ResourceTypeEnum.CVFC.name(), csarInfo.getVfResourceName(), nodeName).getLeft());
cvfc.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
final List<String> tags = new ArrayList<>();
tags.add(cvfc.getName());
cvfc.setTags(tags);
final CategoryDefinition category = new CategoryDefinition();
category.setName(ImportUtils.Constants.ABSTRACT_CATEGORY_NAME);
SubCategoryDefinition subCategory = new SubCategoryDefinition();
subCategory.setName(ImportUtils.Constants.ABSTRACT_SUBCATEGORY);
category.addSubCategory(subCategory);
final List<CategoryDefinition> categories = new ArrayList<>();
categories.add(category);
cvfc.setCategories(categories);
cvfc.setVersion(ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION);
cvfc.setLifecycleState(ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT);
cvfc.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
return cvfc;
}
private String buildCvfcName(final String resourceVfName, final String nodeName) {
String nameWithouNamespacePrefix = nodeName.substring(Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX.length());
String[] findTypes = nameWithouNamespacePrefix.split("\\.");
String resourceType = findTypes[0];
String resourceName = resourceVfName + "-" + nameWithouNamespacePrefix.substring(resourceType.length() + 1);
return addCvfcSuffixToResourceName(resourceName);
}
private Resource createResourceAndRIsFromYaml(final String yamlName, Resource resource, final ParsedToscaYamlInfo parsedToscaYamlInfo,
final AuditingActionEnum actionEnum, final boolean isNormative,
final List<ArtifactDefinition> createdArtifacts, final String topologyTemplateYaml,
final Map<String, NodeTypeInfo> nodeTypesInfo, final CsarInfo csarInfo,
final Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
final boolean shouldLock, final boolean inTransaction, final String nodeName) {
final List<ArtifactDefinition> nodeTypesNewCreatedArtifacts = new ArrayList<>();
if (shouldLock) {
final Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
if (lockResult.isRight()) {
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
throw new ByResponseFormatComponentException(lockResult.right().value());
}
log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
}
try {
log.trace("************* createResourceFromYaml before full create resource {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Starting to add inputs from yaml: {}", yamlName);
final Resource genericResource = fetchAndSetDerivedFromGenericType(resource);
resource = createResourceTransaction(resource, csarInfo.getModifier(), isNormative);
log.trace("************* createResourceFromYaml after full create resource {}", yamlName);
log.trace("************* Going to add inputs from yaml {}", yamlName);
if (resource.shouldGenerateInputs()) {
generateAndAddInputsFromGenericTypeProperties(resource, genericResource);
}
final Map<String, InputDefinition> inputs = parsedToscaYamlInfo.getInputs();
resource = createInputsOnResource(resource, inputs);
log.trace("************* Finish to add inputs from yaml {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
"Finish to add inputs from yaml: {}", yamlName);
if (resource.getResourceType() == ResourceTypeEnum.PNF) {
log.trace("************* Adding generic properties to PNF");
resource = (Resource) propertyBusinessLogic.copyPropertyToComponent(resource, genericResource.getProperties());
log.trace("************* Adding software information to PNF");
softwareInformationBusinessLogic.setSoftwareInformation(resource, csarInfo);
log.trace("************* Removing non-mano software information file from PNF");
if (csarInfo.getSoftwareInformationPath().isPresent() && !softwareInformationBusinessLogic.removeSoftwareInformationFile(csarInfo)) {
log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(), "catalog-be",
"Could not remove the software information file.");
}
}
final Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap = parsedToscaYamlInfo.getInstances();
log.trace("************* Going to create nodes, RI's and Relations from yaml {}", yamlName);
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Start create nodes, RI and Relations from yaml: {}", yamlName);
resource = createRIAndRelationsFromYaml(yamlName, resource, uploadComponentInstanceInfoMap, topologyTemplateYaml,
nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo, nodeTypesArtifactsToCreate, nodeName);
log.trace("************* Finished to create nodes, RI and Relation from yaml {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
"Finished to create nodes, RI and Relation from yaml: {}", yamlName);
// validate update vf module group names
final Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesRes = groupBusinessLogic
.validateUpdateVfGroupNames(parsedToscaYamlInfo.getGroups(), resource.getSystemName());
if (validateUpdateVfGroupNamesRes.isRight()) {
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
throw new ByResponseFormatComponentException(validateUpdateVfGroupNamesRes.right().value());
}
// add groups to resource
final Map<String, GroupDefinition> groups;
log.trace("************* Going to add groups from yaml {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Start to add groups from yaml: {}", yamlName);
if (!validateUpdateVfGroupNamesRes.left().value().isEmpty()) {
groups = validateUpdateVfGroupNamesRes.left().value();
} else {
groups = parsedToscaYamlInfo.getGroups();
}
final Either<Resource, ResponseFormat> createGroupsOnResource = createGroupsOnResource(resource, groups);
if (createGroupsOnResource.isRight()) {
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while adding groups from yaml: {}", yamlName);
throw new ByResponseFormatComponentException(createGroupsOnResource.right().value());
}
resource = createGroupsOnResource.left().value();
log.trace("************* Finished to add groups from yaml {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_GROUPS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
"Finished to add groups from yaml: {}", yamlName);
log.trace("************* Going to add artifacts from yaml {}", yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Started to add artifacts from yaml: {}", yamlName);
log.trace("************* Starting to add policies from yaml {}", yamlName);
Map<String, PolicyDefinition> policies = parsedToscaYamlInfo.getPolicies();
if (MapUtils.isNotEmpty(policies)) {
resource = createPoliciesOnResource(resource, policies);
}
log.trace("************* Finished to add policies from yaml {}", yamlName);
final NodeTypeInfoToUpdateArtifacts nodeTypeInfoToUpdateArtifacts = new NodeTypeInfoToUpdateArtifacts(nodeName,
nodeTypesArtifactsToCreate);
final Either<Resource, ResponseFormat> createArtifactsEither = createOrUpdateArtifacts(ArtifactOperationEnum.CREATE, createdArtifacts,
yamlName, csarInfo, resource, nodeTypeInfoToUpdateArtifacts, inTransaction, shouldLock);
if (createArtifactsEither.isRight()) {
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"error happened {}", createArtifactsEither.right().value());
throw new ByResponseFormatComponentException(createArtifactsEither.right().value());
}
loggerSupportability.log(LoggerSupportabilityActions.CREATE_ARTIFACTS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE,
"Finished to add artifacts from yaml: " + resource.getToscaResourceName());
final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, actionEnum);
ASDCKpiApi.countCreatedResourcesKPI();
return resource;
} catch (final BusinessLogicException e) {
log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
"An error has occurred during resource and resource instance creation", e);
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
throw new ByResponseFormatComponentException(e.getResponseFormat());
} catch (final Exception e) {
log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceBusinessLogic.class.getName(),
"An error has occurred during resource and resource instance creation", e);
rollback(inTransaction, resource, createdArtifacts, nodeTypesNewCreatedArtifacts);
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
} finally {
if (!inTransaction) {
janusGraphDao.commit();
}
if (shouldLock) {
graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
}
}
}
private void rollback(boolean inTransaction, Resource resource, List<ArtifactDefinition> createdArtifacts,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts) {
if (!inTransaction) {
janusGraphDao.rollback();
}
if (isNotEmpty(createdArtifacts) && isNotEmpty(nodeTypesNewCreatedArtifacts)) {
createdArtifacts.addAll(nodeTypesNewCreatedArtifacts);
log.debug("Found {} newly created artifacts to deleted, the component name: {}", createdArtifacts.size(), resource.getName());
}
}
private Resource getResourceWithGroups(String resourceId) {
ComponentParametersView filter = new ComponentParametersView();
filter.setIgnoreGroups(false);
Either<Resource, StorageOperationStatus> updatedResource = toscaOperationFacade.getToscaElement(resourceId, filter);
if (updatedResource.isRight()) {
rollbackWithException(componentsUtils.convertFromStorageResponse(updatedResource.right().value()), resourceId);
}
return updatedResource.left().value();
}
private Either<Resource, ResponseFormat> createGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
if (groups != null && !groups.isEmpty()) {
List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
handleGroupsProperties(resource, groups);
fillGroupsFinalFields(groupsAsList);
Either<List<GroupDefinition>, ResponseFormat> createGroups = groupBusinessLogic.createGroups(resource, groupsAsList, true);
if (createGroups.isRight()) {
return Either.right(createGroups.right().value());
}
}
return Either.left(resource);
}
private void handleGroupsProperties(Resource resource, Map<String, GroupDefinition> groups) {
List<InputDefinition> inputs = resource.getInputs();
if (MapUtils.isNotEmpty(groups)) {
groups.values().stream().filter(g -> isNotEmpty(g.getProperties())).flatMap(g -> g.getProperties().stream())
.forEach(p -> handleGetInputs(p, inputs));
}
}
private Resource createPoliciesOnResource(Resource resource, Map<String, PolicyDefinition> policies) {
policyBusinessLogic.createPoliciesFromParsedCsar(resource, policies);
return resource;
}
private void handleGetInputs(PropertyDataDefinition property, List<InputDefinition> inputs) {
if (isNotEmpty(property.getGetInputValues())) {
if (inputs == null || inputs.isEmpty()) {
log.debug("Failed to add property {} to group. Inputs list is empty ", property);
rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
}
ListIterator<GetInputValueDataDefinition> getInputValuesIter = property.getGetInputValues().listIterator();
while (getInputValuesIter.hasNext()) {
GetInputValueDataDefinition getInput = getInputValuesIter.next();
Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
if (inputEither.isRight()) {
throw inputEither.right().value();
} else {
InputDefinition input = inputEither.left().value();
getInput.setInputId(input.getUniqueId());
if (getInput.getGetInputIndex() != null) {
GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
if (newInputEither.isRight()) {
throw newInputEither.right().value();
} else {
InputDefinition newInput = newInputEither.left().value();
getInputIndex.setInputId(newInput.getUniqueId());
}
getInputValuesIter.add(getInputIndex);
}
}
}
}
}
<T> Either<T, RuntimeException> rollbackWithEither(final ActionStatus actionStatus, final String... params) {
return rollbackWithEither(janusGraphDao, actionStatus, params);
}
private Either<InputDefinition, RuntimeException> findInputByName(List<InputDefinition> inputs, GetInputValueDataDefinition getInput) {
final String inputName = getInput != null ? getInput.getInputName() : "";
if (inputs == null || inputs.isEmpty()) {
log.debug("#findInputByName - Inputs list is empty");
return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
} else {
Optional<InputDefinition> inputOpt = inputs.stream().filter(p -> p.getName().equals(inputName)).findFirst();
if (!inputOpt.isPresent()) {
log.debug("#findInputByName - Failed to find the input {} ", inputName);
return rollbackWithEither(ActionStatus.INPUTS_NOT_FOUND, inputName);
} else {
return Either.left(inputOpt.get());
}
}
}
private void fillGroupsFinalFields(List<GroupDefinition> groupsAsList) {
groupsAsList.forEach(groupDefinition -> {
groupDefinition.setInvariantName(groupDefinition.getName());
groupDefinition.setCreatedFrom(CreatedFrom.CSAR);
});
}
private Resource updateGroupsOnResource(Resource resource, Map<String, GroupDefinition> groups) {
if (isEmpty(groups)) {
return resource;
}
return updateOrCreateGroups(resource, groups);
}
private Resource updateOrCreateGroups(Resource resource, Map<String, GroupDefinition> groups) {
List<GroupDefinition> groupsFromResource = resource.getGroups();
List<GroupDefinition> groupsAsList = updateGroupsMembersUsingResource(groups, resource);
List<GroupDefinition> groupsToUpdate = new ArrayList<>();
List<GroupDefinition> groupsToDelete = new ArrayList<>();
List<GroupDefinition> groupsToCreate = new ArrayList<>();
if (isNotEmpty(groupsFromResource)) {
addGroupsToCreateOrUpdate(groupsFromResource, groupsAsList, groupsToUpdate, groupsToCreate);
addGroupsToDelete(groupsFromResource, groupsAsList, groupsToDelete);
} else {
groupsToCreate.addAll(groupsAsList);
}
if (isNotEmpty(groupsToCreate)) {
fillGroupsFinalFields(groupsToCreate);
if (isNotEmpty(groupsFromResource)) {
groupBusinessLogic.addGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
} else {
groupBusinessLogic.createGroups(resource, groupsToCreate, true).left().on(this::throwComponentException);
}
}
if (isNotEmpty(groupsToDelete)) {
groupBusinessLogic.deleteGroups(resource, groupsToDelete).left().on(this::throwComponentException);
}
if (isNotEmpty(groupsToUpdate)) {
groupBusinessLogic.updateGroups(resource, groupsToUpdate, true).left().on(this::throwComponentException);
}
return resource;
}
private void addGroupsToDelete(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
List<GroupDefinition> groupsToDelete) {
for (GroupDefinition group : groupsFromResource) {
Optional<GroupDefinition> op = groupsAsList.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
.findAny();
if (!op.isPresent() && (group.getArtifacts() == null || group.getArtifacts().isEmpty())) {
groupsToDelete.add(group);
}
}
}
private void addGroupsToCreateOrUpdate(List<GroupDefinition> groupsFromResource, List<GroupDefinition> groupsAsList,
List<GroupDefinition> groupsToUpdate, List<GroupDefinition> groupsToCreate) {
for (GroupDefinition group : groupsAsList) {
Optional<GroupDefinition> op = groupsFromResource.stream().filter(p -> p.getInvariantName().equalsIgnoreCase(group.getInvariantName()))
.findAny();
if (op.isPresent()) {
GroupDefinition groupToUpdate = op.get();
groupToUpdate.setMembers(group.getMembers());
groupToUpdate.setCapabilities(group.getCapabilities());
groupToUpdate.setProperties(group.getProperties());
groupsToUpdate.add(groupToUpdate);
} else {
groupsToCreate.add(group);
}
}
}
private Resource createInputsOnResource(Resource resource, Map<String, InputDefinition> inputs) {
List<InputDefinition> resourceProperties = resource.getInputs();
if (MapUtils.isNotEmpty(inputs) || isNotEmpty(resourceProperties)) {
Either<List<InputDefinition>, ResponseFormat> createInputs = inputsBusinessLogic.createInputsInGraph(inputs, resource);
if (createInputs.isRight()) {
loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"failed to add inputs from yaml: {}", createInputs.right().value());
throw new ByResponseFormatComponentException(createInputs.right().value());
}
resource.setInputs(createInputs.left().value());
}
return resource;
}
private List<GroupDefinition> updateGroupsMembersUsingResource(Map<String, GroupDefinition> groups, Resource component) {
List<GroupDefinition> result = new ArrayList<>();
List<ComponentInstance> componentInstances = component.getComponentInstances();
if (groups != null) {
Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies = validateCyclicGroupsDependencies(groups);
if (validateCyclicGroupsDependencies.isRight()) {
throw new ByResponseFormatComponentException(validateCyclicGroupsDependencies.right().value());
}
for (Entry<String, GroupDefinition> entry : groups.entrySet()) {
String groupName = entry.getKey();
GroupDefinition groupDefinition = entry.getValue();
GroupDefinition updatedGroupDefinition = new GroupDefinition(groupDefinition);
updatedGroupDefinition.setMembers(null);
Map<String, String> members = groupDefinition.getMembers();
if (members != null) {
updateGroupMembers(groups, updatedGroupDefinition, component, componentInstances, groupName, members);
}
result.add(updatedGroupDefinition);
}
}
return result;
}
private void updateGroupMembers(Map<String, GroupDefinition> groups, GroupDefinition updatedGroupDefinition, Resource component,
List<ComponentInstance> componentInstances, String groupName, Map<String, String> members) {
Set<String> compInstancesNames = members.keySet();
if (CollectionUtils.isEmpty(componentInstances)) {
String membersAstString = compInstancesNames.stream().collect(joining(","));
log.debug("The members: {}, in group: {}, cannot be found in component {}. There are no component instances.", membersAstString,
groupName, component.getNormalizedName());
throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
component.getNormalizedName(), getComponentTypeForResponse(component));
}
// Find all component instances with the member names
Map<String, String> memberNames = componentInstances.stream().collect(toMap(ComponentInstance::getName, ComponentInstance::getUniqueId));
memberNames.putAll(groups.keySet().stream().collect(toMap(g -> g, g -> "")));
Map<String, String> relevantInstances = memberNames.entrySet().stream().filter(n -> compInstancesNames.contains(n.getKey()))
.collect(toMap(Entry::getKey, Entry::getValue));
if (relevantInstances == null || relevantInstances.size() != compInstancesNames.size()) {
List<String> foundMembers = new ArrayList<>();
if (relevantInstances != null) {
foundMembers = relevantInstances.keySet().stream().collect(toList());
}
compInstancesNames.removeAll(foundMembers);
String membersAstString = compInstancesNames.stream().collect(joining(","));
log.debug("The members: {}, in group: {}, cannot be found in component: {}", membersAstString, groupName, component.getNormalizedName());
throw new ByActionStatusComponentException(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, membersAstString, groupName,
component.getNormalizedName(), getComponentTypeForResponse(component));
}
updatedGroupDefinition.setMembers(relevantInstances);
}
/**
* This Method validates that there is no cyclic group dependencies. meaning group A as member in group B which is member in group A
*
* @param allGroups
* @return
*/
private Either<Boolean, ResponseFormat> validateCyclicGroupsDependencies(Map<String, GroupDefinition> allGroups) {
Either<Boolean, ResponseFormat> result = Either.left(true);
try {
Iterator<Entry<String, GroupDefinition>> allGroupsItr = allGroups.entrySet().iterator();
while (allGroupsItr.hasNext() && result.isLeft()) {
Entry<String, GroupDefinition> groupAEntry = allGroupsItr.next();
// Fetches a group member A
String groupAName = groupAEntry.getKey();
// Finds all group members in group A
Set<String> allGroupAMembersNames = new HashSet<>();
fillAllGroupMemebersRecursivly(groupAEntry.getKey(), allGroups, allGroupAMembersNames);
// If A is a group member of itself found cyclic dependency
if (allGroupAMembersNames.contains(groupAName)) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GROUP_HAS_CYCLIC_DEPENDENCY, groupAName);
result = Either.right(responseFormat);
}
}
} catch (Exception e) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
result = Either.right(responseFormat);
log.debug("Exception occurred when validateCyclicGroupsDependencies, error is:{}", e.getMessage(), e);
}
return result;
}
/**
* This Method fills recursively the set groupMembers with all the members of the given group which are also of type group.
*
* @param groupName
* @param allGroups
* @param allGroupMembers
* @return
*/
private void fillAllGroupMemebersRecursivly(String groupName, Map<String, GroupDefinition> allGroups, Set<String> allGroupMembers) {
// Found Cyclic dependency
if (isfillGroupMemebersRecursivlyStopCondition(groupName, allGroups, allGroupMembers)) {
return;
}
GroupDefinition groupDefinition = allGroups.get(groupName);
// All Members Of Current Group Resource Instances & Other Groups
Set<String> currGroupMembers = groupDefinition.getMembers().keySet();
// Filtered Members Of Current Group containing only members which
// are groups
List<String> currGroupFilteredMembers = currGroupMembers.stream().
// Keep Only Elements of type group and not Resource Instances
filter(allGroups::containsKey).
// Add Filtered Elements to main Set
peek(allGroupMembers::add).
// Collect results
collect(toList());
// Recursively call the method for all the filtered group members
for (String innerGroupName : currGroupFilteredMembers) {
fillAllGroupMemebersRecursivly(innerGroupName, allGroups, allGroupMembers);
}
}
private boolean isfillGroupMemebersRecursivlyStopCondition(String groupName, Map<String, GroupDefinition> allGroups,
Set<String> allGroupMembers) {
boolean stop = false;
// In Case Not Group Stop
if (!allGroups.containsKey(groupName)) {
stop = true;
}
// In Case Group Has no members stop
if (!stop) {
GroupDefinition groupDefinition = allGroups.get(groupName);
stop = isEmpty(groupDefinition.getMembers());
}
// In Case all group members already contained stop
if (!stop) {
final Set<String> allMembers = allGroups.get(groupName).getMembers().keySet();
Set<String> membersOfTypeGroup = allMembers.stream().
// Filter In Only Group members
filter(allGroups::containsKey).
// Collect
collect(toSet());
stop = allGroupMembers.containsAll(membersOfTypeGroup);
}
return stop;
}
private Resource createRIAndRelationsFromYaml(String yamlName, Resource resource,
Map<String, UploadComponentInstanceInfo> uploadComponentInstanceInfoMap,
String topologyTemplateYaml, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,
String nodeName) {
log.debug("************* Going to create all nodes {}", yamlName);
handleNodeTypes(yamlName, resource, topologyTemplateYaml, false, nodeTypesArtifactsToCreate, nodeTypesNewCreatedArtifacts, nodeTypesInfo,
csarInfo, nodeName);
log.debug("************* Finished to create all nodes {}", yamlName);
log.debug("************* Going to create all resource instances {}", yamlName);
Map<String, Resource> existingNodeTypesByResourceNames = new HashMap<>();
resource = createResourceInstances(yamlName, resource, null, uploadComponentInstanceInfoMap, csarInfo.getCreatedNodes(),
existingNodeTypesByResourceNames);
log.debug("************* Finished to create all resource instances {}", yamlName);
log.debug("************* Going to create all relations {}", yamlName);
resource = createResourceInstancesRelations(csarInfo.getModifier(), yamlName, resource, null, uploadComponentInstanceInfoMap,
existingNodeTypesByResourceNames);
log.debug("************* Finished to create all relations {}", yamlName);
log.debug("************* Going to create positions {}", yamlName);
compositionBusinessLogic.setPositionsForComponentInstances(resource, csarInfo.getModifier().getUserId());
log.debug("************* Finished to set positions {}", yamlName);
return resource;
}
private void handleAndAddExtractedVfcsArtifacts(List<ArtifactDefinition> vfcArtifacts, List<ArtifactDefinition> artifactsToAdd) {
List<String> vfcArtifactNames = vfcArtifacts.stream().map(ArtifactDataDefinition::getArtifactName).collect(toList());
artifactsToAdd.stream().forEach(a -> {
if (!vfcArtifactNames.contains(a.getArtifactName())) {
vfcArtifacts.add(a);
} else {
log.debug("Can't upload two artifact with the same name {}. ", a.getArtifactName());
}
});
}
@SuppressWarnings("unchecked")
private void handleNodeTypes(String yamlName, Resource resource, String topologyTemplateYaml, boolean needLock,
Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo,
String nodeName) {
try {
for (Entry<String, NodeTypeInfo> nodeTypeEntry : nodeTypesInfo.entrySet()) {
if (nodeTypeEntry.getValue().isNested()) {
handleNestedVfc(resource, nodeTypesArtifactsToHandle, nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo,
nodeTypeEntry.getKey());
log.trace("************* finished to create node {}", nodeTypeEntry.getKey());
}
}
Map<String, Object> mappedToscaTemplate = null;
if (StringUtils.isNotEmpty(nodeName) && isNotEmpty(nodeTypesInfo) && nodeTypesInfo.containsKey(nodeName)) {
mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
}
if (isEmpty(mappedToscaTemplate)) {
mappedToscaTemplate = (Map<String, Object>) new Yaml().load(topologyTemplateYaml);
}
createResourcesFromYamlNodeTypesList(yamlName, resource, mappedToscaTemplate, needLock, nodeTypesArtifactsToHandle,
nodeTypesNewCreatedArtifacts, nodeTypesInfo, csarInfo);
} catch (ComponentException e) {
ResponseFormat responseFormat =
e.getResponseFormat() != null ? e.getResponseFormat() : componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams());
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
throw e;
} catch (StorageException e) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE);
throw e;
}
}
private Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, List<ArtifactDefinition> createdArtifacts,
ArtifactOperationInfo artifactOperation, boolean shouldLock,
boolean inTransaction) {
if (csarInfo.getCsar() != null) {
String vendorLicenseModelId = null;
String vfLicenseModelId = null;
if (artifactOperation.isUpdate()) {
Map<String, ArtifactDefinition> deploymentArtifactsMap = resource.getDeploymentArtifacts();
if (deploymentArtifactsMap != null && !deploymentArtifactsMap.isEmpty()) {
for (Entry<String, ArtifactDefinition> artifactEntry : deploymentArtifactsMap.entrySet()) {
if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VENDOR_LICENSE_MODEL)) {
vendorLicenseModelId = artifactEntry.getValue().getUniqueId();
}
if (artifactEntry.getValue().getArtifactName().equalsIgnoreCase(Constants.VF_LICENSE_MODEL)) {
vfLicenseModelId = artifactEntry.getValue().getUniqueId();
}
}
}
}
// Specific Behavior for license artifacts
createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VENDOR_LICENSE_MODEL,
Constants.VENDOR_LICENSE_MODEL, ArtifactTypeEnum.VENDOR_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT,
Constants.VENDOR_LICENSE_LABEL, Constants.VENDOR_LICENSE_DISPLAY_NAME, Constants.VENDOR_LICENSE_DESCRIPTION, vendorLicenseModelId,
artifactOperation, null, true, shouldLock, inTransaction);
createOrUpdateSingleNonMetaArtifact(resource, csarInfo, CsarUtils.ARTIFACTS_PATH + Constants.VF_LICENSE_MODEL, Constants.VF_LICENSE_MODEL,
ArtifactTypeEnum.VF_LICENSE.getType(), ArtifactGroupTypeEnum.DEPLOYMENT, Constants.VF_LICENSE_LABEL,
Constants.VF_LICENSE_DISPLAY_NAME, Constants.VF_LICENSE_DESCRIPTION, vfLicenseModelId, artifactOperation, null, true, shouldLock,
inTransaction);
Either<Resource, ResponseFormat> eitherCreateResult = createOrUpdateNonMetaArtifacts(csarInfo, resource, createdArtifacts, shouldLock,
inTransaction, artifactOperation);
if (eitherCreateResult.isRight()) {
return Either.right(eitherCreateResult.right().value());
}
Either<ImmutablePair<String, String>, ResponseFormat> artifacsMetaCsarStatus = CsarValidationUtils
.getArtifactsMeta(csarInfo.getCsar(), csarInfo.getCsarUUID(), componentsUtils);
if (artifacsMetaCsarStatus.isLeft()) {
String artifactsFileName = artifacsMetaCsarStatus.left().value().getKey();
String artifactsContents = artifacsMetaCsarStatus.left().value().getValue();
Either<Resource, ResponseFormat> createArtifactsFromCsar;
if (artifactOperation.isCreateOrLink()) {
createArtifactsFromCsar = csarArtifactsAndGroupsBusinessLogic
.createResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts);
} else {
Either<Component, ResponseFormat> result = csarArtifactsAndGroupsBusinessLogic
.updateResourceArtifactsFromCsar(csarInfo, resource, artifactsContents, artifactsFileName, createdArtifacts, shouldLock,
inTransaction);
if ((result.left().value() instanceof Resource) && result.isLeft()) {
Resource service1 = (Resource) result.left().value();
createArtifactsFromCsar = Either.left(service1);
} else {
createArtifactsFromCsar = Either.right(result.right().value());
}
}
if (createArtifactsFromCsar.isRight()) {
log.debug("Couldn't create artifacts from artifacts.meta");
return Either.right(createArtifactsFromCsar.right().value());
}
return Either.left(createArtifactsFromCsar.left().value());
} else {
return csarArtifactsAndGroupsBusinessLogic.deleteVFModules(resource, csarInfo, shouldLock, inTransaction);
}
}
return Either.left(resource);
}
private Either<Boolean, ResponseFormat> createOrUpdateSingleNonMetaArtifact(Resource resource, CsarInfo csarInfo, String artifactPath,
String artifactFileName, String artifactType,
ArtifactGroupTypeEnum artifactGroupType, String artifactLabel,
String artifactDisplayName, String artifactDescription,
String artifactId, ArtifactOperationInfo operation,
List<ArtifactDefinition> createdArtifacts, boolean isFromCsar,
boolean shouldLock, boolean inTransaction) {
byte[] artifactFileBytes = null;
if (csarInfo.getCsar().containsKey(artifactPath)) {
artifactFileBytes = csarInfo.getCsar().get(artifactPath);
}
Either<Boolean, ResponseFormat> result = Either.left(true);
if (operation.isUpdate() || operation.isDelete()) {
if (isArtifactDeletionRequired(artifactId, artifactFileBytes, isFromCsar)) {
Either<ArtifactDefinition, ResponseFormat> handleDelete = artifactsBusinessLogic
.handleDelete(resource.getUniqueId(), artifactId, csarInfo.getModifier(), resource, shouldLock, inTransaction);
if (handleDelete.isRight()) {
result = Either.right(handleDelete.right().value());
} else {
ArtifactDefinition value = handleDelete.left().value();
String updatedArtifactId = value.getUniqueId();
if (artifactGroupType == ArtifactGroupTypeEnum.DEPLOYMENT) {
resource.getDeploymentArtifacts().remove(updatedArtifactId);
} else {
resource.getArtifacts().remove(updatedArtifactId);
}
}
return result;
}
if (StringUtils.isEmpty(artifactId) && artifactFileBytes != null) {
operation = new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE);
}
}
if (artifactFileBytes != null) {
Map<String, Object> vendorLicenseModelJson = ArtifactUtils
.buildJsonForUpdateArtifact(artifactId, artifactFileName, artifactType, artifactGroupType, artifactLabel, artifactDisplayName,
artifactDescription, artifactFileBytes, null, isFromCsar);
Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts = csarArtifactsAndGroupsBusinessLogic
.createOrUpdateCsarArtifactFromJson(resource, csarInfo.getModifier(), vendorLicenseModelJson, operation);
addNonMetaCreatedArtifactsToSupportRollback(operation, createdArtifacts, eitherNonMetaArtifacts);
if (eitherNonMetaArtifacts.isRight()) {
BeEcompErrorManager.getInstance().logInternalFlowError("UploadLicenseArtifact",
"Failed to upload license artifact: " + artifactFileName + "With csar uuid: " + csarInfo.getCsarUUID(), ErrorSeverity.WARNING);
return Either.right(eitherNonMetaArtifacts.right().value());
}
ArtifactDefinition artifactDefinition = eitherNonMetaArtifacts.left().value().left().value();
createOrUpdateResourceWithUpdatedArtifact(artifactDefinition, resource, artifactGroupType);
}
return result;
}
private void createOrUpdateResourceWithUpdatedArtifact(ArtifactDefinition artifact, Resource resource, ArtifactGroupTypeEnum groupTypeEnum) {
if (groupTypeEnum == ArtifactGroupTypeEnum.DEPLOYMENT) {
resource.getDeploymentArtifacts().put(artifact.getArtifactLabel(), artifact);
} else {
resource.getArtifacts().put(artifact.getArtifactLabel(), artifact);
}
}
private boolean isArtifactDeletionRequired(String artifactId, byte[] artifactFileBytes, boolean isFromCsar) {
return !StringUtils.isEmpty(artifactId) && artifactFileBytes == null && isFromCsar;
}
private void addNonMetaCreatedArtifactsToSupportRollback(ArtifactOperationInfo operation, List<ArtifactDefinition> createdArtifacts,
Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherNonMetaArtifacts) {
if (operation.isCreateOrLink() && createdArtifacts != null && eitherNonMetaArtifacts.isLeft()) {
Either<ArtifactDefinition, Operation> eitherResult = eitherNonMetaArtifacts.left().value();
if (eitherResult.isLeft()) {
createdArtifacts.add(eitherResult.left().value());
}
}
}
private Either<Resource, ResponseFormat> createOrUpdateNonMetaArtifacts(CsarInfo csarInfo, Resource resource,
List<ArtifactDefinition> createdArtifacts, boolean shouldLock,
boolean inTransaction, ArtifactOperationInfo artifactOperation) {
Either<Resource, ResponseFormat> resStatus = null;
Map<String, Set<List<String>>> collectedWarningMessages = new HashMap<>();
try {
Either<List<NonMetaArtifactInfo>, String> artifactPathAndNameList = getValidArtifactNames(csarInfo, collectedWarningMessages);
if (artifactPathAndNameList.isRight()) {
return Either.right(
getComponentsUtils().getResponseFormatByArtifactId(ActionStatus.ARTIFACT_NAME_INVALID, artifactPathAndNameList.right().value()));
}
EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle = null;
if (artifactOperation.isCreateOrLink()) {
vfCsarArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
vfCsarArtifactsToHandle.put(artifactOperation.getArtifactOperationEnum(), artifactPathAndNameList.left().value());
} else {
Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandleRes = findVfCsarArtifactsToHandle(
resource, artifactPathAndNameList.left().value(), csarInfo.getModifier());
if (findVfCsarArtifactsToHandleRes.isRight()) {
resStatus = Either.right(findVfCsarArtifactsToHandleRes.right().value());
}
if (resStatus == null) {
vfCsarArtifactsToHandle = findVfCsarArtifactsToHandleRes.left().value();
}
}
if (resStatus == null && vfCsarArtifactsToHandle != null) {
resStatus = processCsarArtifacts(csarInfo, resource, createdArtifacts, shouldLock, inTransaction, resStatus, vfCsarArtifactsToHandle);
}
if (resStatus == null) {
resStatus = Either.left(resource);
}
} catch (Exception e) {
resStatus = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
log.debug("Exception occurred in createNonMetaArtifacts, message:{}", e.getMessage(), e);
} finally {
CsarUtils.handleWarningMessages(collectedWarningMessages);
}
return resStatus;
}
private Either<Resource, ResponseFormat> processCsarArtifacts(CsarInfo csarInfo, Resource resource, List<ArtifactDefinition> createdArtifacts,
boolean shouldLock, boolean inTransaction,
Either<Resource, ResponseFormat> resStatus,
EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> vfCsarArtifactsToHandle) {
for (Entry<ArtifactOperationEnum, List<NonMetaArtifactInfo>> currArtifactOperationPair : vfCsarArtifactsToHandle.entrySet()) {
Optional<ResponseFormat> optionalCreateInDBError =
// Stream of artifacts to be created
currArtifactOperationPair.getValue().stream()
// create each artifact
.map(e -> createOrUpdateSingleNonMetaArtifact(resource, csarInfo, e.getPath(), e.getArtifactName(), e.getArtifactType(),
e.getArtifactGroupType(), e.getArtifactLabel(), e.getDisplayName(), CsarUtils.ARTIFACT_CREATED_FROM_CSAR,
e.getArtifactUniqueId(), new ArtifactOperationInfo(false, false, currArtifactOperationPair.getKey()), createdArtifacts,
e.isFromCsar(), shouldLock, inTransaction))
// filter in only error
.filter(Either::isRight).
// Convert the error from either to
// ResponseFormat
map(e -> e.right().value()).
// Check if an error occurred
findAny();
// Error found on artifact Creation
if (optionalCreateInDBError.isPresent()) {
resStatus = Either.right(optionalCreateInDBError.get());
break;
}
}
return resStatus;
}
private Either<List<NonMetaArtifactInfo>, String> getValidArtifactNames(CsarInfo csarInfo,
Map<String, Set<List<String>>> collectedWarningMessages) {
List<NonMetaArtifactInfo> artifactPathAndNameList =
// Stream of file paths contained in csar
csarInfo.getCsar().entrySet().stream()
// Filter in only VF artifact path location
.filter(e -> Pattern.compile(VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN).matcher(e.getKey()).matches())
// Validate and add warnings
.map(e -> CsarUtils.validateNonMetaArtifact(e.getKey(), e.getValue(), collectedWarningMessages))
// Filter in Non Warnings
.filter(Either::isLeft)
// Convert from Either to NonMetaArtifactInfo
.map(e -> e.left().value())
// collect to List
.collect(toList());
Pattern englishNumbersAndUnderScoresOnly = Pattern.compile(CsarUtils.VALID_ENGLISH_ARTIFACT_NAME);
for (NonMetaArtifactInfo nonMetaArtifactInfo : artifactPathAndNameList) {
if (!englishNumbersAndUnderScoresOnly.matcher(nonMetaArtifactInfo.getDisplayName()).matches()) {
return Either.right(nonMetaArtifactInfo.getArtifactName());
}
}
return Either.left(artifactPathAndNameList);
}
private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> findVfCsarArtifactsToHandle(Resource resource,
List<NonMetaArtifactInfo> artifactPathAndNameList,
User user) {
List<ArtifactDefinition> existingArtifacts = new ArrayList<>();
// collect all Deployment and Informational artifacts of VF
if (resource.getDeploymentArtifacts() != null && !resource.getDeploymentArtifacts().isEmpty()) {
existingArtifacts.addAll(resource.getDeploymentArtifacts().values());
}
if (resource.getArtifacts() != null && !resource.getArtifacts().isEmpty()) {
existingArtifacts.addAll(resource.getArtifacts().values());
}
existingArtifacts = existingArtifacts.stream()
// filter MANDATORY artifacts, LICENSE artifacts and artifacts
// was created from HEAT.meta
.filter(this::isNonMetaArtifact).collect(toList());
List<String> artifactsToIgnore = new ArrayList<>();
// collect IDs of Artifacts of VF which belongs to any group
if (resource.getGroups() != null) {
resource.getGroups().stream().forEach(g -> {
if (g.getArtifacts() != null && !g.getArtifacts().isEmpty()) {
artifactsToIgnore.addAll(g.getArtifacts());
}
});
}
existingArtifacts = existingArtifacts.stream()
// filter artifacts which belongs to any group
.filter(a -> !artifactsToIgnore.contains(a.getUniqueId())).collect(toList());
return organizeVfCsarArtifactsByArtifactOperation(artifactPathAndNameList, existingArtifacts, resource, user);
}
private boolean isNonMetaArtifact(ArtifactDefinition artifact) {
boolean result = true;
if (artifact.getMandatory() || artifact.getArtifactName() == null || !isValidArtifactType(artifact)) {
result = false;
}
return result;
}
private boolean isValidArtifactType(ArtifactDefinition artifact) {
boolean result = true;
if (artifact.getArtifactType() == null || ArtifactTypeEnum.parse(artifact.getArtifactType()) == ArtifactTypeEnum.VENDOR_LICENSE
|| ArtifactTypeEnum.parse(artifact.getArtifactType()) == ArtifactTypeEnum.VF_LICENSE) {
result = false;
}
return result;
}
private Resource createResourceInstancesRelations(User user, String yamlName, Resource resource, Resource oldResource,
Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
Map<String, Resource> existingNodeTypesByResourceNames) {
log.debug("#createResourceInstancesRelations - Going to create relations ");
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Start to create relations");
List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
if (isEmpty(uploadResInstancesMap) || CollectionUtils.isEmpty(componentInstancesList) &&
resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances {
log.debug("#createResourceInstancesRelations - No instances found in the resource {} is empty, yaml template file name {}, ",
resource.getUniqueId(), yamlName);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"No instances found in the resource: {}, is empty, yaml template file name: {}", resource.getName(), yamlName);
BeEcompErrorManager.getInstance()
.logInternalDataError("createResourceInstancesRelations", "No instances found in a resource or nn yaml template. ",
ErrorSeverity.ERROR);
throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
Map<String, List<ComponentInstanceProperty>> instProperties = new HashMap<>();
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities = new HashMap<>();
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements = new HashMap<>();
Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts = new HashMap<>();
Map<String, Map<String, ArtifactDefinition>> instArtifacts = new HashMap<>();
Map<String, List<AttributeDefinition>> instAttributes = new HashMap<>();
List<RequirementCapabilityRelDef> relations = new ArrayList<>();
Map<String, List<ComponentInstanceInput>> instInputs = new HashMap<>();
log.debug("#createResourceInstancesRelations - Before get all datatypes. ");
Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll();
if (allDataTypes.isRight()) {
JanusGraphOperationStatus status = allDataTypes.right().value();
BeEcompErrorManager.getInstance()
.logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status,
ErrorSeverity.ERROR);
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while update property value on instance. Status is: " + status);
throw new ByActionStatusComponentException(
componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)), yamlName);
}
Resource finalResource = resource;
uploadResInstancesMap.values().forEach(
i -> processComponentInstance(yamlName, finalResource, componentInstancesList, allDataTypes, instProperties, instCapabilities,
instRequirements, instDeploymentArtifacts, instArtifacts, instAttributes, existingNodeTypesByResourceNames, instInputs, i));
resource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar()).forEach(
i -> processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts,
instProperties, instInputs, instAttributes));
associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties);
associateComponentInstanceInputsToComponent(yamlName, resource, instInputs);
associateDeploymentArtifactsToInstances(user, yamlName, resource, instDeploymentArtifacts);
associateArtifactsToInstances(yamlName, resource, instArtifacts);
associateOrAddCalculatedCapReq(yamlName, resource, instCapabilities, instRequirements);
associateInstAttributeToComponentToInstances(yamlName, resource, instAttributes);
addRelationsToRI(yamlName, resource, uploadResInstancesMap, componentInstancesList, relations);
associateResourceInstances(yamlName, resource, relations);
handleSubstitutionMappings(resource, uploadResInstancesMap);
log.debug("************* in create relations, getResource start");
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.COMPLETE, "create relations");
Either<Resource, StorageOperationStatus> eitherGetResource = toscaOperationFacade.getToscaFullElement(resource.getUniqueId());
log.debug("************* in create relations, getResource end");
if (eitherGetResource.isRight()) {
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while create relations");
throw new ByResponseFormatComponentException(
componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(eitherGetResource.right().value()), resource));
}
return eitherGetResource.left().value();
}
private void processUiComponentInstance(Resource oldResource, ComponentInstance instance,
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
Map<String, Map<String, ArtifactDefinition>> instArtifacts,
Map<String, List<ComponentInstanceProperty>> instProperties,
Map<String, List<ComponentInstanceInput>> instInputs,
Map<String, List<AttributeDefinition>> instAttributes) {
Optional<ComponentInstance> foundInstance = findInstance(oldResource, instance);
if (foundInstance.isPresent()) {
if (MapUtils.isNotEmpty(foundInstance.get().getCapabilities())) {
instCapabilities.put(instance, foundInstance.get().getCapabilities());
}
if (MapUtils.isNotEmpty(foundInstance.get().getRequirements())) {
instRequirements.put(instance, foundInstance.get().getRequirements());
}
if (MapUtils.isNotEmpty(foundInstance.get().getDeploymentArtifacts())) {
instDeploymentArtifacts.put(instance.getUniqueId(), foundInstance.get().getDeploymentArtifacts());
}
if (MapUtils.isNotEmpty(foundInstance.get().getArtifacts())) {
instArtifacts.put(instance.getUniqueId(), foundInstance.get().getArtifacts());
}
if (MapUtils.isNotEmpty(oldResource.getComponentInstancesProperties()) && CollectionUtils
.isNotEmpty(oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()))) {
instProperties.put(instance.getUniqueId(), oldResource.getComponentInstancesProperties().get(foundInstance.get().getUniqueId()));
}
if (MapUtils.isNotEmpty(oldResource.getComponentInstancesInputs()) && CollectionUtils
.isNotEmpty(oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()))) {
instInputs.put(instance.getUniqueId(), oldResource.getComponentInstancesInputs().get(foundInstance.get().getUniqueId()));
}
if (MapUtils.isNotEmpty(oldResource.getComponentInstancesAttributes()) && CollectionUtils
.isNotEmpty(oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()))) {
instAttributes.put(instance.getUniqueId(),
oldResource.getComponentInstancesAttributes().get(foundInstance.get().getUniqueId()).stream().map(AttributeDefinition::new)
.collect(toList()));
}
}
}
private Optional<ComponentInstance> findInstance(Resource oldResource, ComponentInstance instance) {
if (oldResource != null && CollectionUtils.isNotEmpty(oldResource.getComponentInstances())) {
return oldResource.getComponentInstances().stream().filter(i -> i.getName().equals(instance.getName())).findFirst();
}
return Optional.empty();
}
private void associateResourceInstances(String yamlName, Resource resource, List<RequirementCapabilityRelDef> relations) {
Either<List<RequirementCapabilityRelDef>, StorageOperationStatus> relationsEither = toscaOperationFacade
.associateResourceInstances(resource, resource.getUniqueId(), relations);
if (relationsEither.isRight() && relationsEither.right().value() != StorageOperationStatus.NOT_FOUND) {
StorageOperationStatus status = relationsEither.right().value();
log.debug("failed to associate instances of resource {} status is {}", resource.getUniqueId(), status);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(status), yamlName);
} else {
setResourceInstanceRelationsOnComponent(resource, relationsEither.left().value());
}
}
private void associateInstAttributeToComponentToInstances(String yamlName, Resource resource,
Map<String, List<AttributeDefinition>> instAttributes) {
StorageOperationStatus addArtToInst;
addArtToInst = toscaOperationFacade.associateInstAttributeToComponentToInstances(instAttributes, resource);
if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
log.debug("failed to associate attributes of resource {} status is {}", resource.getUniqueId(), addArtToInst);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
}
}
private void associateOrAddCalculatedCapReq(String yamlName, Resource resource,
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilities,
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements) {
StorageOperationStatus addArtToInst;
addArtToInst = toscaOperationFacade.associateOrAddCalculatedCapReq(instCapabilities, instRequirements, resource);
if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
log.debug("failed to associate cap and req of resource {} status is {}", resource.getUniqueId(), addArtToInst);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
}
}
private void associateArtifactsToInstances(String yamlName, Resource resource, Map<String, Map<String, ArtifactDefinition>> instArtifacts) {
StorageOperationStatus addArtToInst;
addArtToInst = toscaOperationFacade.associateArtifactsToInstances(instArtifacts, resource);
if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
}
}
private void associateDeploymentArtifactsToInstances(User user, String yamlName, Resource resource,
Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts) {
StorageOperationStatus addArtToInst = toscaOperationFacade.associateDeploymentArtifactsToInstances(instDeploymentArtifacts, resource, user);
if (addArtToInst != StorageOperationStatus.OK && addArtToInst != StorageOperationStatus.NOT_FOUND) {
log.debug("failed to associate artifact of resource {} status is {}", resource.getUniqueId(), addArtToInst);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addArtToInst), yamlName);
}
}
private void associateComponentInstanceInputsToComponent(String yamlName, Resource resource,
Map<String, List<ComponentInstanceInput>> instInputs) {
if (MapUtils.isNotEmpty(instInputs)) {
Either<Map<String, List<ComponentInstanceInput>>, StorageOperationStatus> addInputToInst = toscaOperationFacade
.associateComponentInstanceInputsToComponent(instInputs, resource.getUniqueId());
if (addInputToInst.isRight()) {
StorageOperationStatus addInputToInstError = addInputToInst.right().value();
log.debug("failed to associate inputs value of resource {} status is {}", resource.getUniqueId(), addInputToInstError);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(addInputToInstError), yamlName);
}
setComponentInstanceInputsOnComponent(resource, instInputs);
}
}
private void setComponentInstanceInputsOnComponent(Resource resource, Map<String, List<ComponentInstanceInput>> instInputs) {
Map<String, List<ComponentInstanceInput>> componentInstancesInputs = resource.getComponentInstancesInputs();
if (componentInstancesInputs == null) {
componentInstancesInputs = new HashMap<>();
}
componentInstancesInputs.putAll(instInputs);
resource.setComponentInstancesInputs(componentInstancesInputs);
}
private void associateComponentInstancePropertiesToComponent(String yamlName, Resource resource,
Map<String, List<ComponentInstanceProperty>> instProperties) {
Either<Map<String, List<ComponentInstanceProperty>>, StorageOperationStatus> addPropToInst = toscaOperationFacade
.associateComponentInstancePropertiesToComponent(instProperties, resource.getUniqueId());
if (addPropToInst.isRight()) {
loggerSupportability.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while associate compnent insatnce properties of resource: {} status is: {}", resource.getName(),
addPropToInst.right().value());
StorageOperationStatus storageOperationStatus = addPropToInst.right().value();
log.debug("failed to associate properties of resource {} status is {}", resource.getUniqueId(), storageOperationStatus);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageOperationStatus), yamlName);
}
setComponentInstancePropertiesOnComponent(resource, instProperties);
}
private void setComponentInstancePropertiesOnComponent(Resource resource, Map<String, List<ComponentInstanceProperty>> instProperties) {
Map<String, List<ComponentInstanceProperty>> componentInstanceProps = resource.getComponentInstancesProperties();
if (componentInstanceProps == null) {
componentInstanceProps = new HashMap<>();
}
componentInstanceProps.putAll(instProperties);
resource.setComponentInstancesProperties(componentInstanceProps);
}
private void handleSubstitutionMappings(Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
if (resource.getResourceType() == ResourceTypeEnum.CVFC) {
Either<Resource, StorageOperationStatus> getResourceRes = updateCalculatedCapReqWithSubstitutionMappings(resource, uploadResInstancesMap);
if (getResourceRes.isRight()) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResourceRes.right().value()), resource);
throw new ByResponseFormatComponentException(responseFormat);
}
}
}
private void addRelationsToRI(String yamlName, Resource resource, Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
List<ComponentInstance> componentInstancesList, List<RequirementCapabilityRelDef> relations) {
for (Entry<String, UploadComponentInstanceInfo> entry : uploadResInstancesMap.entrySet()) {
UploadComponentInstanceInfo uploadComponentInstanceInfo = entry.getValue();
ComponentInstance currentCompInstance = null;
for (ComponentInstance compInstance : componentInstancesList) {
if (compInstance.getName().equals(uploadComponentInstanceInfo.getName())) {
currentCompInstance = compInstance;
break;
}
}
if (currentCompInstance == null) {
log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
BeEcompErrorManager.getInstance()
.logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
ErrorSeverity.ERROR);
throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
ResponseFormat addRelationToRiRes = addRelationToRI(yamlName, resource, entry.getValue(), relations);
if (addRelationToRiRes.getStatus() != 200) {
throw new ByResponseFormatComponentException(addRelationToRiRes);
}
}
}
private void setResourceInstanceRelationsOnComponent(Resource resource, List<RequirementCapabilityRelDef> relations) {
if (resource.getComponentInstancesRelations() != null) {
resource.getComponentInstancesRelations().addAll(relations);
} else {
resource.setComponentInstancesRelations(relations);
}
}
private void processComponentInstance(String yamlName, Resource resource, List<ComponentInstance> componentInstancesList,
Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
Map<String, List<ComponentInstanceProperty>> instProperties,
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instRequirements,
Map<String, Map<String, ArtifactDefinition>> instDeploymentArtifacts,
Map<String, Map<String, ArtifactDefinition>> instArtifacts,
Map<String, List<AttributeDefinition>> instAttributes, Map<String, Resource> originCompMap,
Map<String, List<ComponentInstanceInput>> instInputs,
UploadComponentInstanceInfo uploadComponentInstanceInfo) {
Optional<ComponentInstance> currentCompInstanceOpt = componentInstancesList.stream()
.filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())).findFirst();
if (!currentCompInstanceOpt.isPresent()) {
log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, uploadComponentInstanceInfo.getName(), resource.getUniqueId());
BeEcompErrorManager.getInstance()
.logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadComponentInstanceInfo.getName() + IN_RESOURCE, resource.getUniqueId(),
ErrorSeverity.ERROR);
throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
ComponentInstance currentCompInstance = currentCompInstanceOpt.get();
String resourceInstanceId = currentCompInstance.getUniqueId();
Resource originResource = getOriginResource(originCompMap, currentCompInstance);
if (isNotEmpty(originResource.getRequirements())) {
instRequirements.put(currentCompInstance, originResource.getRequirements());
}
if (isNotEmpty(originResource.getCapabilities())) {
processComponentInstanceCapabilities(allDataTypes, instCapabilties, uploadComponentInstanceInfo, currentCompInstance, originResource);
}
if (originResource.getDeploymentArtifacts() != null && !originResource.getDeploymentArtifacts().isEmpty()) {
instDeploymentArtifacts.put(resourceInstanceId, originResource.getDeploymentArtifacts());
}
if (originResource.getArtifacts() != null && !originResource.getArtifacts().isEmpty()) {
instArtifacts.put(resourceInstanceId, originResource.getArtifacts());
}
if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) {
instAttributes.put(resourceInstanceId, originResource.getAttributes());
}
if (originResource.getResourceType() != ResourceTypeEnum.CVFC) {
ResponseFormat addPropertiesValueToRiRes = addPropertyValuesToRi(uploadComponentInstanceInfo, resource, originResource,
currentCompInstance, instProperties, allDataTypes.left().value());
if (addPropertiesValueToRiRes.getStatus() != 200) {
throw new ByResponseFormatComponentException(addPropertiesValueToRiRes);
}
} else {
addInputsValuesToRi(uploadComponentInstanceInfo, resource, originResource, currentCompInstance, instInputs, allDataTypes.left().value());
}
}
private Resource getOriginResource(Map<String, Resource> originCompMap, ComponentInstance currentCompInstance) {
Resource originResource;
if (!originCompMap.containsKey(currentCompInstance.getComponentUid())) {
Either<Resource, StorageOperationStatus> getOriginResourceRes = toscaOperationFacade
.getToscaFullElement(currentCompInstance.getComponentUid());
if (getOriginResourceRes.isRight()) {
log.debug("failed to fetch resource with uniqueId {} and tosca component name {} status is {}", currentCompInstance.getComponentUid(),
currentCompInstance.getToscaComponentName(), getOriginResourceRes);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(getOriginResourceRes.right().value()),
currentCompInstance.getComponentUid());
}
originResource = getOriginResourceRes.left().value();
originCompMap.put(originResource.getUniqueId(), originResource);
} else {
originResource = originCompMap.get(currentCompInstance.getComponentUid());
}
return originResource;
}
private void processComponentInstanceCapabilities(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> instCapabilties,
UploadComponentInstanceInfo uploadComponentInstanceInfo, ComponentInstance currentCompInstance,
Resource originResource) {
Map<String, List<CapabilityDefinition>> originCapabilities;
if (isNotEmpty(uploadComponentInstanceInfo.getCapabilities())) {
originCapabilities = new HashMap<>();
Map<String, Map<String, UploadPropInfo>> newPropertiesMap = new HashMap<>();
originResource.getCapabilities().forEach((k, v) -> addCapabilities(originCapabilities, k, v));
uploadComponentInstanceInfo.getCapabilities().values().forEach(l -> addCapabilitiesProperties(newPropertiesMap, l));
updateCapabilityPropertiesValues(allDataTypes, originCapabilities, newPropertiesMap);
} else {
originCapabilities = originResource.getCapabilities();
}
instCapabilties.put(currentCompInstance, originCapabilities);
}
private void updateCapabilityPropertiesValues(Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes,
Map<String, List<CapabilityDefinition>> originCapabilities,
Map<String, Map<String, UploadPropInfo>> newPropertiesMap) {
originCapabilities.values().stream().flatMap(Collection::stream).filter(c -> newPropertiesMap.containsKey(c.getName()))
.forEach(c -> updatePropertyValues(c.getProperties(), newPropertiesMap.get(c.getName()), allDataTypes.left().value()));
}
private void addCapabilitiesProperties(Map<String, Map<String, UploadPropInfo>> newPropertiesMap, List<UploadCapInfo> capabilities) {
for (UploadCapInfo capability : capabilities) {
if (isNotEmpty(capability.getProperties())) {
newPropertiesMap.put(capability.getName(), capability.getProperties().stream().collect(toMap(UploadInfo::getName, p -> p)));
}
}
}
private void addCapabilities(Map<String, List<CapabilityDefinition>> originCapabilities, String type, List<CapabilityDefinition> capabilities) {
List<CapabilityDefinition> list = capabilities.stream().map(CapabilityDefinition::new).collect(toList());
originCapabilities.put(type, list);
}
private void updatePropertyValues(List<ComponentInstanceProperty> properties, Map<String, UploadPropInfo> newProperties,
Map<String, DataTypeDefinition> allDataTypes) {
properties.forEach(p -> updatePropertyValue(p, newProperties.get(p.getName()), allDataTypes));
}
private String updatePropertyValue(ComponentInstanceProperty property, UploadPropInfo propertyInfo,
Map<String, DataTypeDefinition> allDataTypes) {
String value = null;
List<GetInputValueDataDefinition> getInputs = null;
boolean isValidate = true;
if (null != propertyInfo && propertyInfo.getValue() != null) {
getInputs = propertyInfo.getGet_input();
isValidate = getInputs == null || getInputs.isEmpty();
if (isValidate) {
value = getPropertyJsonStringValue(propertyInfo.getValue(), property.getType());
} else {
value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
}
}
property.setValue(value);
return validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
}
private Either<Resource, StorageOperationStatus> updateCalculatedCapReqWithSubstitutionMappings(Resource resource,
Map<String, UploadComponentInstanceInfo> uploadResInstancesMap) {
Either<Resource, StorageOperationStatus> updateRes = null;
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities = new HashMap<>();
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements = new HashMap<>();
StorageOperationStatus status = toscaOperationFacade.deleteAllCalculatedCapabilitiesRequirements(resource.getUniqueId());
if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
log.debug("Failed to delete all calculated capabilities and requirements of resource {} upon update. Status is {}",
resource.getUniqueId(), status);
updateRes = Either.right(status);
}
if (updateRes == null) {
fillUpdatedInstCapabilitiesRequirements(resource.getComponentInstances(), uploadResInstancesMap, updatedInstCapabilities,
updatedInstRequirements);
status = toscaOperationFacade.associateOrAddCalculatedCapReq(updatedInstCapabilities, updatedInstRequirements, resource);
if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
log.debug(
"Failed to associate capabilities and requirementss of resource {}, updated according to a substitution mapping. Status is {}",
resource.getUniqueId(), status);
updateRes = Either.right(status);
}
}
if (updateRes == null) {
updateRes = Either.left(resource);
}
return updateRes;
}
private void fillUpdatedInstCapabilitiesRequirements(List<ComponentInstance> componentInstances,
Map<String, UploadComponentInstanceInfo> uploadResInstancesMap,
Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilities,
Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements) {
componentInstances.stream().forEach(i -> {
fillUpdatedInstCapabilities(updatedInstCapabilities, i, uploadResInstancesMap.get(i.getName()).getCapabilitiesNamesToUpdate());
fillUpdatedInstRequirements(updatedInstRequirements, i, uploadResInstancesMap.get(i.getName()).getRequirementsNamesToUpdate());
});
}
private void fillUpdatedInstRequirements(Map<ComponentInstance, Map<String, List<RequirementDefinition>>> updatedInstRequirements,
ComponentInstance instance, Map<String, String> requirementsNamesToUpdate) {
Map<String, List<RequirementDefinition>> updatedRequirements = new HashMap<>();
Set<String> updatedReqNames = new HashSet<>();
if (isNotEmpty(requirementsNamesToUpdate)) {
for (Map.Entry<String, List<RequirementDefinition>> requirements : instance.getRequirements().entrySet()) {
updatedRequirements.put(requirements.getKey(), requirements.getValue().stream().filter(
r -> requirementsNamesToUpdate.containsKey(r.getName()) && !updatedReqNames.contains(requirementsNamesToUpdate.get(r.getName())))
.map(r -> {
r.setParentName(r.getName());
r.setName(requirementsNamesToUpdate.get(r.getName()));
updatedReqNames.add(r.getName());
return r;
}).collect(toList()));
}
}
if (isNotEmpty(updatedRequirements)) {
updatedInstRequirements.put(instance, updatedRequirements);
}
}
private void fillUpdatedInstCapabilities(Map<ComponentInstance, Map<String, List<CapabilityDefinition>>> updatedInstCapabilties,
ComponentInstance instance, Map<String, String> capabilitiesNamesToUpdate) {
Map<String, List<CapabilityDefinition>> updatedCapabilities = new HashMap<>();
Set<String> updatedCapNames = new HashSet<>();
if (isNotEmpty(capabilitiesNamesToUpdate)) {
for (Map.Entry<String, List<CapabilityDefinition>> requirements : instance.getCapabilities().entrySet()) {
updatedCapabilities.put(requirements.getKey(), requirements.getValue().stream().filter(
c -> capabilitiesNamesToUpdate.containsKey(c.getName()) && !updatedCapNames.contains(capabilitiesNamesToUpdate.get(c.getName())))
.map(c -> {
c.setParentName(c.getName());
c.setName(capabilitiesNamesToUpdate.get(c.getName()));
updatedCapNames.add(c.getName());
return c;
}).collect(toList()));
}
}
if (isNotEmpty(updatedCapabilities)) {
updatedInstCapabilties.put(instance, updatedCapabilities);
}
}
private ResponseFormat addRelationToRI(String yamlName, Resource resource, UploadComponentInstanceInfo nodesInfoValue,
List<RequirementCapabilityRelDef> relations) {
List<ComponentInstance> componentInstancesList = resource.getComponentInstances();
ComponentInstance currentCompInstance = null;
for (ComponentInstance compInstance : componentInstancesList) {
if (compInstance.getName().equals(nodesInfoValue.getName())) {
currentCompInstance = compInstance;
break;
}
}
if (currentCompInstance == null) {
log.debug(COMPONENT_INSTANCE_WITH_NAME_IN_RESOURCE, nodesInfoValue.getName(), resource.getUniqueId());
BeEcompErrorManager.getInstance()
.logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + nodesInfoValue.getName() + IN_RESOURCE, resource.getUniqueId(),
ErrorSeverity.ERROR);
return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
String resourceInstanceId = currentCompInstance.getUniqueId();
Map<String, List<UploadReqInfo>> regMap = nodesInfoValue.getRequirements();
if (regMap != null) {
Iterator<Entry<String, List<UploadReqInfo>>> nodesRegValue = regMap.entrySet().iterator();
while (nodesRegValue.hasNext()) {
Entry<String, List<UploadReqInfo>> nodesRegInfoEntry = nodesRegValue.next();
List<UploadReqInfo> uploadRegInfoList = nodesRegInfoEntry.getValue();
for (UploadReqInfo uploadRegInfo : uploadRegInfoList) {
log.debug("Going to create relation {}", uploadRegInfo.getName());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.STARTED,
"Started to create relations on instance: {}", uploadRegInfo.getName());
String regName = uploadRegInfo.getName();
RequirementCapabilityRelDef regCapRelDef = new RequirementCapabilityRelDef();
regCapRelDef.setFromNode(resourceInstanceId);
log.debug("try to find available requirement {} ", regName);
Either<RequirementDefinition, ResponseFormat> eitherReqStatus = findAviableRequiremen(regName, yamlName, nodesInfoValue,
currentCompInstance, uploadRegInfo.getCapabilityName());
if (eitherReqStatus.isRight()) {
log.debug("failed to find available requirement {} status is {}", regName, eitherReqStatus.right().value());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while search available requirement {} status is: {}", regName, eitherReqStatus.right().value());
return eitherReqStatus.right().value();
}
RequirementDefinition validReq = eitherReqStatus.left().value();
List<CapabilityRequirementRelationship> reqAndRelationshipPairList = regCapRelDef.getRelationships();
if (reqAndRelationshipPairList == null) {
reqAndRelationshipPairList = new ArrayList<>();
}
RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
reqAndRelationshipPair.setRequirement(regName);
reqAndRelationshipPair.setRequirementOwnerId(validReq.getOwnerId());
reqAndRelationshipPair.setRequirementUid(validReq.getUniqueId());
RelationshipImpl relationship = new RelationshipImpl();
relationship.setType(validReq.getCapability());
reqAndRelationshipPair.setRelationships(relationship);
ComponentInstance currentCapCompInstance = null;
for (ComponentInstance compInstance : componentInstancesList) {
if (compInstance.getName().equals(uploadRegInfo.getNode())) {
currentCapCompInstance = compInstance;
break;
}
}
if (currentCapCompInstance == null) {
log.debug("The component instance with name {} not found on resource {} ", uploadRegInfo.getNode(), resource.getUniqueId());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR component instance with name: {} not found on resource: {}", uploadRegInfo.getNode(), resource.getUniqueId());
BeEcompErrorManager.getInstance()
.logInternalDataError(COMPONENT_INSTANCE_WITH_NAME + uploadRegInfo.getNode() + IN_RESOURCE, resource.getUniqueId(),
ErrorSeverity.ERROR);
return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
regCapRelDef.setToNode(currentCapCompInstance.getUniqueId());
log.debug("try to find aviable Capability req name is {} ", validReq.getName());
CapabilityDefinition aviableCapForRel = findAvailableCapabilityByTypeOrName(validReq, currentCapCompInstance, uploadRegInfo);
if (aviableCapForRel == null) {
log.debug("aviable capability was not found. req name is {} component instance is {}", validReq.getName(),
currentCapCompInstance.getUniqueId());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RELATIONS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR available capability was not found. req name is: {} component instance is: {}", validReq.getName(),
currentCapCompInstance.getUniqueId());
BeEcompErrorManager.getInstance().logInternalDataError(
"aviable capability was not found. req name is " + validReq.getName() + " component instance is " + currentCapCompInstance
.getUniqueId(), resource.getUniqueId(), ErrorSeverity.ERROR);
return componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, yamlName);
}
reqAndRelationshipPair.setCapability(aviableCapForRel.getName());
reqAndRelationshipPair.setCapabilityUid(aviableCapForRel.getUniqueId());
reqAndRelationshipPair.setCapabilityOwnerId(aviableCapForRel.getOwnerId());
CapabilityRequirementRelationship capReqRel = new CapabilityRequirementRelationship();
capReqRel.setRelation(reqAndRelationshipPair);
reqAndRelationshipPairList.add(capReqRel);
regCapRelDef.setRelationships(reqAndRelationshipPairList);
relations.add(regCapRelDef);
}
}
} else if (resource.getResourceType() != ResourceTypeEnum.CVFC) {
return componentsUtils.getResponseFormat(ActionStatus.OK, yamlName);
}
return componentsUtils.getResponseFormat(ActionStatus.OK);
}
private void addInputsValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceInput>> instInputs,
Map<String, DataTypeDefinition> allDataTypes) {
Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
if (MapUtils.isNotEmpty(propMap)) {
Map<String, InputDefinition> currPropertiesMap = new HashMap<>();
List<ComponentInstanceInput> instPropList = new ArrayList<>();
if (CollectionUtils.isEmpty(originResource.getInputs())) {
log.debug("failed to find properties ");
loggerSupportability.log(LoggerSupportabilityActions.CREATE_INPUTS, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while try to find properties");
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
}
originResource.getInputs().forEach(p -> addInput(currPropertiesMap, p));
for (List<UploadPropInfo> propertyList : propMap.values()) {
processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
}
currPropertiesMap.values().forEach(p -> instPropList.add(new ComponentInstanceInput(p)));
instInputs.put(currentCompInstance.getUniqueId(), instPropList);
}
}
private void processProperty(Resource resource, ComponentInstance currentCompInstance, Map<String, DataTypeDefinition> allDataTypes,
Map<String, InputDefinition> currPropertiesMap, List<ComponentInstanceInput> instPropList,
List<UploadPropInfo> propertyList) {
UploadPropInfo propertyInfo = propertyList.get(0);
String propName = propertyInfo.getName();
if (!currPropertiesMap.containsKey(propName)) {
loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR failed to find property: {}", propName);
log.debug("failed to find property {} ", propName);
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, propName);
}
InputDefinition curPropertyDef = currPropertiesMap.get(propName);
ComponentInstanceInput property = null;
String value = null;
List<GetInputValueDataDefinition> getInputs = null;
boolean isValidate = true;
if (propertyInfo.getValue() != null) {
getInputs = propertyInfo.getGet_input();
isValidate = getInputs == null || getInputs.isEmpty();
if (isValidate) {
value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
} else {
value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
}
}
property = new ComponentInstanceInput(curPropertyDef, value, null);
String validPropertyVAlue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
property.setValue(validPropertyVAlue);
if (isNotEmpty(getInputs)) {
List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
for (GetInputValueDataDefinition getInput : getInputs) {
List<InputDefinition> inputs = resource.getInputs();
if (CollectionUtils.isEmpty(inputs)) {
loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR Failed to add property: " + propName + " to resource instance: {}. Inputs list is empty ",
currentCompInstance.getUniqueId());
log.debug("Failed to add property {} to resource instance {}. Inputs list is empty ", property,
currentCompInstance.getUniqueId());
throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
}
Optional<InputDefinition> optional = inputs.stream().filter(p -> p.getName().equals(getInput.getInputName())).findAny();
if (!optional.isPresent()) {
loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR Failed to find input: " + getInput.getInputName());
log.debug("Failed to find input {} ", getInput.getInputName());
// @@TODO error message
throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
}
InputDefinition input = optional.get();
getInput.setInputId(input.getUniqueId());
getInputValues.add(getInput);
GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
processGetInput(getInputValues, inputs, getInputIndex);
}
property.setGetInputValues(getInputValues);
}
instPropList.add(property);
// delete overriden property
currPropertiesMap.remove(property.getName());
}
private void processGetInput(List<GetInputValueDataDefinition> getInputValues, List<InputDefinition> inputs,
GetInputValueDataDefinition getInputIndex) {
Optional<InputDefinition> optional;
if (getInputIndex != null) {
optional = inputs.stream().filter(p -> p.getName().equals(getInputIndex.getInputName())).findAny();
if (!optional.isPresent()) {
log.debug("Failed to find input {} ", getInputIndex.getInputName());
// @@TODO error message
throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
}
InputDefinition inputIndex = optional.get();
getInputIndex.setInputId(inputIndex.getUniqueId());
getInputValues.add(getInputIndex);
}
}
private void addInput(Map<String, InputDefinition> currPropertiesMap, InputDefinition prop) {
String propName = prop.getName();
if (!currPropertiesMap.containsKey(propName)) {
currPropertiesMap.put(propName, prop);
}
}
private ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Resource resource, Resource originResource,
ComponentInstance currentCompInstance, Map<String, List<ComponentInstanceProperty>> instProperties,
Map<String, DataTypeDefinition> allDataTypes) {
Map<String, List<UploadPropInfo>> propMap = uploadComponentInstanceInfo.getProperties();
Map<String, PropertyDefinition> currPropertiesMap = new HashMap<>();
List<PropertyDefinition> listFromMap = originResource.getProperties();
if ((propMap != null && !propMap.isEmpty()) && (listFromMap == null || listFromMap.isEmpty())) {
loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR Failed to find properties");
log.debug("failed to find properties");
return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND);
}
if (listFromMap == null || listFromMap.isEmpty()) {
return componentsUtils.getResponseFormat(ActionStatus.OK);
}
for (PropertyDefinition prop : listFromMap) {
String propName = prop.getName();
if (!currPropertiesMap.containsKey(propName)) {
currPropertiesMap.put(propName, prop);
}
}
List<ComponentInstanceProperty> instPropList = new ArrayList<>();
if (propMap != null && propMap.size() > 0) {
for (List<UploadPropInfo> propertyList : propMap.values()) {
UploadPropInfo propertyInfo = propertyList.get(0);
String propName = propertyInfo.getName();
if (!currPropertiesMap.containsKey(propName)) {
log.debug("failed to find property {} ", propName);
loggerSupportability.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR Failed to find property: {}", propName);
return componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, propName);
}
PropertyDefinition curPropertyDef = currPropertiesMap.get(propName);
ComponentInstanceProperty property = null;
String value = null;
List<GetInputValueDataDefinition> getInputs = null;
boolean isValidate = true;
if (propertyInfo.getValue() != null) {
getInputs = propertyInfo.getGet_input();
isValidate = getInputs == null || getInputs.isEmpty();
if (isValidate) {
value = getPropertyJsonStringValue(propertyInfo.getValue(), curPropertyDef.getType());
} else {
value = getPropertyJsonStringValue(propertyInfo.getValue(), TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName());
}
}
property = new ComponentInstanceProperty(curPropertyDef, value, null);
String validatePropValue = validatePropValueBeforeCreate(property, value, isValidate, allDataTypes);
property.setValue(validatePropValue);
if (getInputs != null && !getInputs.isEmpty()) {
List<GetInputValueDataDefinition> getInputValues = new ArrayList<>();
for (GetInputValueDataDefinition getInput : getInputs) {
List<InputDefinition> inputs = resource.getInputs();
if (inputs == null || inputs.isEmpty()) {
log.debug("Failed to add property {} to instance. Inputs list is empty ", property);
loggerSupportability
.log(LoggerSupportabilityActions.PROPERTY, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"Failed to add property: {} to instance. Inputs list is empty", propName);
rollbackWithException(ActionStatus.INPUTS_NOT_FOUND,
property.getGetInputValues().stream().map(GetInputValueDataDefinition::getInputName).collect(toList()).toString());
}
Either<InputDefinition, RuntimeException> inputEither = findInputByName(inputs, getInput);
if (inputEither.isRight()) {
throw inputEither.right().value();
} else {
InputDefinition input = inputEither.left().value();
getInput.setInputId(input.getUniqueId());
getInputValues.add(getInput);
GetInputValueDataDefinition getInputIndex = getInput.getGetInputIndex();
if (getInputIndex != null) {
Either<InputDefinition, RuntimeException> newInputEither = findInputByName(inputs, getInputIndex);
if (inputEither.isRight()) {
throw newInputEither.right().value();
} else {
InputDefinition newInput = newInputEither.left().value();
getInputIndex.setInputId(newInput.getUniqueId());
}
getInputValues.add(getInputIndex);
}
}
}
property.setGetInputValues(getInputValues);
}
instPropList.add(property);
// delete overriden property
currPropertiesMap.remove(property.getName());
}
}
// add rest of properties
if (!currPropertiesMap.isEmpty()) {
for (PropertyDefinition value : currPropertiesMap.values()) {
instPropList.add(new ComponentInstanceProperty(value));
}
}
instProperties.put(currentCompInstance.getUniqueId(), instPropList);
return componentsUtils.getResponseFormat(ActionStatus.OK);
}
// US740820 Relate RIs according to capability name
private CapabilityDefinition findAvailableCapabilityByTypeOrName(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
UploadReqInfo uploadReqInfo) {
if (null == uploadReqInfo.getCapabilityName() || validReq.getCapability()
.equals(uploadReqInfo.getCapabilityName())) {// get
// by
// capability
// type
return findAvailableCapability(validReq, currentCapCompInstance);
}
return findAvailableCapability(validReq, currentCapCompInstance, uploadReqInfo);
}
private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance currentCapCompInstance,
UploadReqInfo uploadReqInfo) {
CapabilityDefinition cap = null;
Map<String, List<CapabilityDefinition>> capMap = currentCapCompInstance.getCapabilities();
if (!capMap.containsKey(validReq.getCapability())) {
return null;
}
Optional<CapabilityDefinition> capByName = capMap.get(validReq.getCapability()).stream()
.filter(p -> p.getName().equals(uploadReqInfo.getCapabilityName())).findAny();
if (!capByName.isPresent()) {
return null;
}
cap = capByName.get();
if (isBoundedByOccurrences(cap)) {
String leftOccurrences = cap.getLeftOccurrences();
int left = Integer.parseInt(leftOccurrences);
if (left > 0) {
--left;
cap.setLeftOccurrences(String.valueOf(left));
}
}
return cap;
}
private CapabilityDefinition findAvailableCapability(RequirementDefinition validReq, ComponentInstance instance) {
Map<String, List<CapabilityDefinition>> capMap = instance.getCapabilities();
if (capMap.containsKey(validReq.getCapability())) {
List<CapabilityDefinition> capList = capMap.get(validReq.getCapability());
for (CapabilityDefinition cap : capList) {
if (isBoundedByOccurrences(cap)) {
String leftOccurrences = cap.getLeftOccurrences() != null ? cap.getLeftOccurrences() : cap.getMaxOccurrences();
int left = Integer.parseInt(leftOccurrences);
if (left > 0) {
--left;
cap.setLeftOccurrences(String.valueOf(left));
return cap;
}
} else {
return cap;
}
}
}
return null;
}
private boolean isBoundedByOccurrences(CapabilityDefinition cap) {
return cap.getMaxOccurrences() != null && !cap.getMaxOccurrences().equals(CapabilityDataDefinition.MAX_OCCURRENCES);
}
private Either<RequirementDefinition, ResponseFormat> findAviableRequiremen(String regName, String yamlName,
UploadComponentInstanceInfo uploadComponentInstanceInfo,
ComponentInstance currentCompInstance, String capName) {
Map<String, List<RequirementDefinition>> comInstRegDefMap = currentCompInstance.getRequirements();
List<RequirementDefinition> list = comInstRegDefMap.get(capName);
RequirementDefinition validRegDef = null;
if (list == null) {
for (Entry<String, List<RequirementDefinition>> entry : comInstRegDefMap.entrySet()) {
for (RequirementDefinition reqDef : entry.getValue()) {
if (reqDef.getName().equals(regName)) {
if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
String leftOccurrences = reqDef.getLeftOccurrences();
if (leftOccurrences == null) {
leftOccurrences = reqDef.getMaxOccurrences();
}
int left = Integer.parseInt(leftOccurrences);
if (left > 0) {
--left;
reqDef.setLeftOccurrences(String.valueOf(left));
validRegDef = reqDef;
break;
} else {
continue;
}
} else {
validRegDef = reqDef;
break;
}
}
}
if (validRegDef != null) {
break;
}
}
} else {
for (RequirementDefinition reqDef : list) {
if (reqDef.getName().equals(regName)) {
if (reqDef.getMaxOccurrences() != null && !reqDef.getMaxOccurrences().equals(RequirementDataDefinition.MAX_OCCURRENCES)) {
String leftOccurrences = reqDef.getLeftOccurrences();
if (leftOccurrences == null) {
leftOccurrences = reqDef.getMaxOccurrences();
}
int left = Integer.parseInt(leftOccurrences);
if (left > 0) {
--left;
reqDef.setLeftOccurrences(String.valueOf(left));
validRegDef = reqDef;
break;
} else {
continue;
}
} else {
validRegDef = reqDef;
break;
}
}
}
}
if (validRegDef == null) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
uploadComponentInstanceInfo.getType());
return Either.right(responseFormat);
}
return Either.left(validRegDef);
}
private Resource createResourceInstances(String yamlName, Resource resource, Resource oldResource,
Map<String, UploadComponentInstanceInfo> uploadResInstancesMap, Map<String, Resource> nodeNamespaceMap,
Map<String, Resource> existingNodeTypesByResourceNames) {
Either<Resource, ResponseFormat> eitherResource;
log.debug("createResourceInstances is {} - going to create resource instanse from CSAR", yamlName);
if (isEmpty(uploadResInstancesMap) && resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
throw new ByResponseFormatComponentException(responseFormat);
}
if (MapUtils.isNotEmpty(nodeNamespaceMap)) {
nodeNamespaceMap.forEach((k, v) -> existingNodeTypesByResourceNames.put(v.getToscaResourceName(), v));
}
Map<ComponentInstance, Resource> resourcesInstancesMap = new HashMap<>();
uploadResInstancesMap.values().forEach(
i -> createAndAddResourceInstance(i, yamlName, resource, nodeNamespaceMap, existingNodeTypesByResourceNames, resourcesInstancesMap));
if (oldResource != null && oldResource.getResourceType() != ResourceTypeEnum.CVFC && oldResource.getComponentInstances() != null) {
Map<String, Resource> existingNodeTypesByUids = existingNodeTypesByResourceNames.values().stream()
.collect(toMap(Resource::getUniqueId, r -> r));
oldResource.getComponentInstances().stream().filter(i -> !i.isCreatedFromCsar())
.forEach(uiInst -> resourcesInstancesMap.put(uiInst, getOriginResource(existingNodeTypesByUids, uiInst)));
}
if (isNotEmpty(resourcesInstancesMap)) {
try {
toscaOperationFacade.associateComponentInstancesToComponent(resource, resourcesInstancesMap, false, oldResource != null);
} catch (StorageException exp) {
if (exp.getStorageOperationStatus() != null && exp.getStorageOperationStatus() != StorageOperationStatus.OK) {
log.debug("Failed to add component instances to container component {}", resource.getName());
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(exp.getStorageOperationStatus()));
eitherResource = Either.right(responseFormat);
throw new ByResponseFormatComponentException(eitherResource.right().value());
}
}
}
if (CollectionUtils.isEmpty(resource.getComponentInstances()) &&
resource.getResourceType() != ResourceTypeEnum.PNF) { // PNF can have no resource instances
log.debug("Error when create resource instance from csar. ComponentInstances list empty");
BeEcompErrorManager.getInstance().logBeDaoSystemError("Error when create resource instance from csar. ComponentInstances list empty");
throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE));
}
return resource;
}
private void createAndAddResourceInstance(UploadComponentInstanceInfo uploadComponentInstanceInfo, String yamlName, Resource resource,
Map<String, Resource> nodeNamespaceMap, Map<String, Resource> existingnodeTypeMap,
Map<ComponentInstance, Resource> resourcesInstancesMap) {
Either<Resource, ResponseFormat> eitherResource;
log.debug("*************Going to create resource instances {}", yamlName);
// updating type if the type is node type name - we need to take the
// updated name
log.debug("*************Going to create resource instances {}", uploadComponentInstanceInfo.getName());
if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
uploadComponentInstanceInfo.setType(nodeNamespaceMap.get(uploadComponentInstanceInfo.getType()).getToscaResourceName());
}
Resource refResource = validateResourceInstanceBeforeCreate(yamlName, uploadComponentInstanceInfo, existingnodeTypeMap, resource);
ComponentInstance componentInstance = new ComponentInstance();
componentInstance.setComponentUid(refResource.getUniqueId());
Collection<String> directives = uploadComponentInstanceInfo.getDirectives();
if (directives != null && !directives.isEmpty()) {
componentInstance.setDirectives(new ArrayList<>(directives));
}
UploadNodeFilterInfo uploadNodeFilterInfo = uploadComponentInstanceInfo.getUploadNodeFilterInfo();
if (uploadNodeFilterInfo != null) {
componentInstance
.setNodeFilter(new CINodeFilterUtils().getNodeFilterDataDefinition(uploadNodeFilterInfo, componentInstance.getUniqueId()));
}
ComponentTypeEnum containerComponentType = resource.getComponentType();
NodeTypeEnum containerNodeType = containerComponentType.getNodeType();
if (containerNodeType == NodeTypeEnum.Resource && isNotEmpty(uploadComponentInstanceInfo.getCapabilities()) && isNotEmpty(
refResource.getCapabilities())) {
setCapabilityNamesTypes(refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
Map<String, List<CapabilityDefinition>> validComponentInstanceCapabilities = getValidComponentInstanceCapabilities(
refResource.getUniqueId(), refResource.getCapabilities(), uploadComponentInstanceInfo.getCapabilities());
componentInstance.setCapabilities(validComponentInstanceCapabilities);
}
if (isNotEmpty(uploadComponentInstanceInfo.getArtifacts())) {
Map<String, Map<String, UploadArtifactInfo>> artifacts = uploadComponentInstanceInfo.getArtifacts();
Map<String, ToscaArtifactDataDefinition> toscaArtifacts = new HashMap<>();
Map<String, Map<String, UploadArtifactInfo>> arts = artifacts.entrySet().stream()
.filter(e -> e.getKey().contains(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName()))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
Map<String, UploadArtifactInfo> artifact = arts.get(TypeUtils.ToscaTagNamesEnum.ARTIFACTS.getElementName());
for (Map.Entry<String, UploadArtifactInfo> entry : artifact.entrySet()) {
ToscaArtifactDataDefinition to = new ToscaArtifactDataDefinition();
to.setFile(entry.getValue().getFile());
to.setType(entry.getValue().getType());
toscaArtifacts.put(entry.getKey(), to);
}
componentInstance.setToscaArtifacts(toscaArtifacts);
}
if (!existingnodeTypeMap.containsKey(uploadComponentInstanceInfo.getType())) {
log.debug("createResourceInstances - not found lates version for resource instance with name {} and type ",
uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
uploadComponentInstanceInfo.getType());
}
Resource origResource = existingnodeTypeMap.get(uploadComponentInstanceInfo.getType());
componentInstance.setName(uploadComponentInstanceInfo.getName());
componentInstance.setIcon(origResource.getIcon());
componentInstance.setCreatedFrom(CreatedFrom.CSAR);
resourcesInstancesMap.put(componentInstance, origResource);
}
private void setCapabilityNamesTypes(Map<String, List<CapabilityDefinition>> originCapabilities,
Map<String, List<UploadCapInfo>> uploadedCapabilities) {
for (Entry<String, List<UploadCapInfo>> currEntry : uploadedCapabilities.entrySet()) {
if (originCapabilities.containsKey(currEntry.getKey())) {
currEntry.getValue().stream().forEach(cap -> cap.setType(currEntry.getKey()));
}
}
for (Map.Entry<String, List<CapabilityDefinition>> capabilities : originCapabilities.entrySet()) {
capabilities.getValue().stream().forEach(cap -> {
if (uploadedCapabilities.containsKey(cap.getName())) {
uploadedCapabilities.get(cap.getName()).stream().forEach(c -> {
c.setName(cap.getName());
c.setType(cap.getType());
});
}
});
}
}
private Resource validateResourceInstanceBeforeCreate(String yamlName, UploadComponentInstanceInfo uploadComponentInstanceInfo,
Map<String, Resource> nodeNamespaceMap, Resource resource) {
log.debug("validateResourceInstanceBeforeCreate - going to validate resource instance with name {} and type {} before create",
uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
Resource refResource;
if (nodeNamespaceMap.containsKey(uploadComponentInstanceInfo.getType())) {
refResource = nodeNamespaceMap.get(uploadComponentInstanceInfo.getType());
} else {
Either<Resource, StorageOperationStatus> findResourceEither = toscaOperationFacade
.getByToscaResourceNameMatchingVendorRelease(uploadComponentInstanceInfo.getType(),
((ResourceMetadataDataDefinition) resource.getComponentMetadataDefinition().getMetadataDataDefinition()).getVendorRelease());
if (findResourceEither.isRight()) {
log.debug("validateResourceInstanceBeforeCreate - not found latest version for resource instance with name {} and type {}",
uploadComponentInstanceInfo.getName(), uploadComponentInstanceInfo.getType());
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(findResourceEither.right().value()));
}
refResource = findResourceEither.left().value();
nodeNamespaceMap.put(refResource.getToscaResourceName(), refResource);
}
String componentState = refResource.getComponentMetadataDefinition().getMetadataDataDefinition().getState();
if (componentState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name())) {
log.debug(
"validateResourceInstanceBeforeCreate - component instance of component {} can not be created because the component is in an illegal state {}.",
refResource.getName(), componentState);
throw new ByActionStatusComponentException(ActionStatus.ILLEGAL_COMPONENT_STATE, refResource.getComponentType().getValue(),
refResource.getName(), componentState);
}
if (!ModelConverter.isAtomicComponent(refResource) && refResource.getResourceType() != ResourceTypeEnum.CVFC) {
log.debug("validateResourceInstanceBeforeCreate - ref resource type is ", refResource.getResourceType());
throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TEMPLATE, yamlName, uploadComponentInstanceInfo.getName(),
uploadComponentInstanceInfo.getType());
}
return refResource;
}
public Resource propagateStateToCertified(User user, Resource resource, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
boolean needLock, boolean forceCertificationAllowed) {
boolean failed = false;
try {
if (resource.getLifecycleState() != LifecycleStateEnum.CERTIFIED && forceCertificationAllowed && lifecycleBusinessLogic
.isFirstCertification(resource.getVersion())) {
nodeForceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
}
if (resource.getLifecycleState() == LifecycleStateEnum.CERTIFIED) {
Either<ArtifactDefinition, Operation> eitherPopulated = populateToscaArtifacts(resource, user, false, inTransaction, needLock, false);
return resource;
}
return nodeFullCertification(resource.getUniqueId(), user, lifecycleChangeInfo, inTransaction, needLock);
} catch (ComponentException e) {
failed = true;
log.debug("The exception has occurred upon certification of resource {}. ", resource.getName(), e);
throw e;
} finally {
if (failed) {
BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
if (!inTransaction) {
janusGraphDao.rollback();
}
} else if (!inTransaction) {
janusGraphDao.commit();
}
}
}
private Resource nodeFullCertification(String uniqueId, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
boolean needLock) {
Either<Resource, ResponseFormat> resourceResponse = lifecycleBusinessLogic
.changeState(uniqueId, user, LifeCycleTransitionEnum.CERTIFY, lifecycleChangeInfo, inTransaction, needLock);
if (resourceResponse.isRight()) {
throw new ByResponseFormatComponentException(resourceResponse.right().value());
}
return resourceResponse.left().value();
}
private Resource nodeForceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction,
boolean needLock) {
return lifecycleBusinessLogic.forceResourceCertification(resource, user, lifecycleChangeInfo, inTransaction, needLock);
}
public ImmutablePair<Resource, ActionStatus> createOrUpdateResourceByImport(final Resource resource, final User user, final boolean isNormative,
final boolean isInTransaction, final boolean needLock,
final CsarInfo csarInfo, final String nodeName,
final boolean isNested) {
ImmutablePair<Resource, ActionStatus> result = null;
// check if resource already exists (search by tosca name = type)
final boolean isNestedResource = isNestedResourceUpdate(csarInfo, nodeName);
final Either<Resource, StorageOperationStatus> latestByToscaName = toscaOperationFacade
.getLatestByToscaResourceName(resource.getToscaResourceName());
if (latestByToscaName.isLeft()) {
Resource foundResource = latestByToscaName.left().value();
// we don't allow updating names of top level types
if (!isNestedResource && !StringUtils.equals(resource.getName(), foundResource.getName())) {
BeEcompErrorManager.getInstance()
.logBeComponentMissingError("Create / Update resource by import", ComponentTypeEnum.RESOURCE.getValue(), resource.getName());
log.debug("resource already exist new name={} old name={} same type={}", resource.getName(), foundResource.getName(),
resource.getToscaResourceName());
final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_ALREADY_EXISTS);
componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
throwComponentException(responseFormat);
}
result = updateExistingResourceByImport(resource, foundResource, user, isNormative, needLock, isNested);
} else if (isNotFound(latestByToscaName)) {
if (isNestedResource) {
result = createOrUpdateNestedResource(resource, user, isNormative, isInTransaction, needLock, csarInfo, isNested, nodeName);
} else {
result = createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
}
} else {
StorageOperationStatus status = latestByToscaName.right().value();
log.debug("failed to get latest version of resource {}. status={}", resource.getName(), status);
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(latestByToscaName.right().value()), resource);
componentsUtils.auditResource(responseFormat, user, resource, AuditingActionEnum.IMPORT_RESOURCE);
throwComponentException(responseFormat);
}
return result;
}
private boolean isNestedResourceUpdate(CsarInfo csarInfo, String nodeName) {
return csarInfo != null && csarInfo.isUpdate() && nodeName != null;
}
private ImmutablePair<Resource, ActionStatus> createOrUpdateNestedResource(final Resource resource, final User user, final boolean isNormative,
final boolean isInTransaction, final boolean needLock,
final CsarInfo csarInfo, final boolean isNested,
final String nodeName) {
final Either<Component, StorageOperationStatus> latestByToscaName = toscaOperationFacade.getLatestByToscaResourceName(
buildNestedToscaResourceName(resource.getResourceType().name(), csarInfo.getVfResourceName(), nodeName).getRight());
if (latestByToscaName.isLeft()) {
final Resource nestedResource = (Resource) latestByToscaName.left().value();
log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
final Either<Boolean, ResponseFormat> eitherValidation = validateNestedDerivedFromDuringUpdate(nestedResource, resource,
ValidationUtils.hasBeenCertified(nestedResource.getVersion()));
if (eitherValidation.isRight()) {
return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
}
return updateExistingResourceByImport(resource, nestedResource, user, isNormative, needLock, isNested);
} else {
return createResourceByImport(resource, user, isNormative, isInTransaction, csarInfo);
}
}
private boolean isNotFound(Either<Resource, StorageOperationStatus> getResourceEither) {
return getResourceEither.isRight() && getResourceEither.right().value() == StorageOperationStatus.NOT_FOUND;
}
private ImmutablePair<Resource, ActionStatus> createResourceByImport(Resource resource, User user, boolean isNormative, boolean isInTransaction,
CsarInfo csarInfo) {
log.debug("resource with name {} does not exist. create new resource", resource.getName());
validateResourceBeforeCreate(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isInTransaction, csarInfo);
final Resource createResourceByDao = createResourceByDao(resource, user, AuditingActionEnum.IMPORT_RESOURCE, isNormative, isInTransaction);
Resource createdResource = updateCatalog(createResourceByDao, ChangeTypeEnum.LIFECYCLE).left().map(r -> (Resource) r).left().value();
ImmutablePair<Resource, ActionStatus> resourcePair = new ImmutablePair<>(createdResource, ActionStatus.CREATED);
ASDCKpiApi.countImportResourcesKPI();
return resourcePair;
}
public boolean isResourceExist(String resourceName) {
Either<Resource, StorageOperationStatus> latestByName = toscaOperationFacade.getLatestByName(resourceName);
return latestByName.isLeft();
}
private ImmutablePair<Resource, ActionStatus> updateExistingResourceByImport(Resource newResource, Resource oldResource, User user,
boolean inTransaction, boolean needLock, boolean isNested) {
String lockedResourceId = oldResource.getUniqueId();
log.debug("found resource: name={}, id={}, version={}, state={}", oldResource.getName(), lockedResourceId, oldResource.getVersion(),
oldResource.getLifecycleState());
ImmutablePair<Resource, ActionStatus> resourcePair = null;
try {
lockComponent(lockedResourceId, oldResource, needLock, "Update Resource by Import");
oldResource = prepareResourceForUpdate(oldResource, newResource, user, inTransaction, false);
mergeOldResourceMetadataWithNew(oldResource, newResource);
validateResourceFieldsBeforeUpdate(oldResource, newResource, inTransaction, isNested);
validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), newResource, AuditingActionEnum.IMPORT_RESOURCE, inTransaction);
// contact info normalization
newResource.setContactId(newResource.getContactId().toLowerCase());
PropertyConstraintsUtils.validatePropertiesConstraints(newResource, oldResource);
// non-updatable fields
newResource.setCreatorUserId(user.getUserId());
newResource.setCreatorFullName(user.getFullName());
newResource.setLastUpdaterUserId(user.getUserId());
newResource.setLastUpdaterFullName(user.getFullName());
newResource.setUniqueId(oldResource.getUniqueId());
newResource.setVersion(oldResource.getVersion());
newResource.setInvariantUUID(oldResource.getInvariantUUID());
newResource.setLifecycleState(oldResource.getLifecycleState());
newResource.setUUID(oldResource.getUUID());
newResource.setNormalizedName(oldResource.getNormalizedName());
newResource.setSystemName(oldResource.getSystemName());
if (oldResource.getCsarUUID() != null) {
newResource.setCsarUUID(oldResource.getCsarUUID());
}
if (oldResource.getImportedToscaChecksum() != null) {
newResource.setImportedToscaChecksum(oldResource.getImportedToscaChecksum());
}
newResource.setAbstract(oldResource.isAbstract());
if (CollectionUtils.isEmpty(newResource.getDerivedFrom())) {
newResource.setDerivedFrom(oldResource.getDerivedFrom());
}
if (CollectionUtils.isEmpty(newResource.getDataTypes())) {
newResource.setDataTypes(oldResource.getDataTypes());
}
if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())) {
newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType());
}
if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())) {
newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion());
}
// add for new)
// created without tosca artifacts - add the placeholders
if (MapUtils.isEmpty(newResource.getToscaArtifacts())) {
setToscaArtifactsPlaceHolders(newResource, user);
}
if (MapUtils.isEmpty(newResource.getInterfaces())) {
newResource.setInterfaces(oldResource.getInterfaces());
}
if (CollectionUtils.isEmpty(newResource.getAttributes())) {
newResource.setAttributes(oldResource.getAttributes());
}
if (CollectionUtils.isEmpty(newResource.getProperties())) {
newResource.setProperties(oldResource.getProperties());
}
Either<Resource, StorageOperationStatus> overrideResource = toscaOperationFacade.overrideComponent(newResource, oldResource);
if (overrideResource.isRight()) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(overrideResource.right().value()), newResource);
componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE);
throwComponentException(responseFormat);
}
updateCatalog(overrideResource.left().value(), ChangeTypeEnum.LIFECYCLE);
log.debug("Resource updated successfully!!!");
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
componentsUtils.auditResource(responseFormat, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
resourcePair = new ImmutablePair<>(overrideResource.left().value(), ActionStatus.OK);
return resourcePair;
} finally {
if (resourcePair == null) {
BeEcompErrorManager.getInstance().logBeSystemError("Change LifecycleState - Certify");
janusGraphDao.rollback();
} else if (!inTransaction) {
janusGraphDao.commit();
}
if (needLock) {
log.debug("unlock resource {}", lockedResourceId);
graphLockOperation.unlockComponent(lockedResourceId, NodeTypeEnum.Resource);
}
}
}
/**
* Merge old resource with new. Keep old category and vendor name without change
*
* @param oldResource
* @param newResource
*/
private void mergeOldResourceMetadataWithNew(Resource oldResource, Resource newResource) {
// keep old category and vendor name without change
// merge the rest of the resource metadata
if (newResource.getTags() == null || newResource.getTags().isEmpty()) {
newResource.setTags(oldResource.getTags());
}
if (newResource.getDescription() == null) {
newResource.setDescription(oldResource.getDescription());
}
if (newResource.getVendorRelease() == null) {
newResource.setVendorRelease(oldResource.getVendorRelease());
}
if (newResource.getResourceVendorModelNumber() == null) {
newResource.setResourceVendorModelNumber(oldResource.getResourceVendorModelNumber());
}
if (newResource.getContactId() == null) {
newResource.setContactId(oldResource.getContactId());
}
newResource.setCategories(oldResource.getCategories());
if (newResource.getVendorName() == null) {
newResource.setVendorName(oldResource.getVendorName());
}
List<GroupDefinition> oldForUpdate = oldResource.getGroups();
if (CollectionUtils.isNotEmpty(oldForUpdate)) {
List<GroupDefinition> groupForUpdate = oldForUpdate.stream().map(group -> new GroupDefinition(group)).collect(Collectors.toList());
groupForUpdate.stream().filter(group -> group.isVspOriginated()).forEach(group -> group.setName(group.getInvariantName()));
newResource.setGroups(groupForUpdate);
}
if (newResource.getResourceType().isAtomicType() && !newResource.getName().equals("Root")
&& newResource.getResourceType() != ResourceTypeEnum.CVFC) {
ResourceTypeEnum updatedResourceType = newResource.getResourceType();
Component derivedFromResource = getParentComponent(newResource);
if (derivedFromResource.getComponentType() == ComponentTypeEnum.RESOURCE) {
Resource parentResource = (Resource) derivedFromResource;
if (!(parentResource.isAbstract() && (ResourceTypeEnum.VFC == parentResource.getResourceType()
|| ResourceTypeEnum.ABSTRACT == parentResource.getResourceType())) && parentResource.getResourceType() != updatedResourceType
&& oldResource.getResourceType() != updatedResourceType) {
BeEcompErrorManager.getInstance().logInternalDataError("mergeOldResourceMetadataWithNew",
"resource type of the resource does not match to derived from resource type", ErrorSeverity.ERROR);
log.debug(
"#mergeOldResourceMetadataWithNew - resource type {} of the resource {} does not match to derived from resource type {}",
newResource.getResourceType(), newResource.getToscaResourceName(), parentResource.getResourceType());
throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_TYPE);
}
}
}
}
private Component getParentComponent(Resource newResource) {
String toscaResourceNameDerivedFrom = newResource.getDerivedFrom().get(0);
Either<Component, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade
.getLatestByToscaResourceName(toscaResourceNameDerivedFrom);
if (latestByToscaResourceName.isRight()) {
BeEcompErrorManager.getInstance()
.logInternalDataError("mergeOldResourceMetadataWithNew", "derived from resource not found", ErrorSeverity.ERROR);
log.debug("#mergeOldResourceMetadataWithNew - derived from resource {} not found", toscaResourceNameDerivedFrom);
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NOT_FOUND, toscaResourceNameDerivedFrom);
}
return latestByToscaResourceName.left().value();
}
private Resource prepareResourceForUpdate(Resource oldResource, Resource newResource, User user, boolean inTransaction, boolean needLock) {
if (!ComponentValidationUtils.canWorkOnResource(oldResource, user.getUserId())) {
// checkout
return lifecycleBusinessLogic
.changeState(oldResource.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, new LifecycleChangeInfoWithAction("update by import"),
inTransaction, needLock).left().on(response -> failOnChangeState(response, user, oldResource, newResource));
}
return oldResource;
}
private Resource failOnChangeState(ResponseFormat response, User user, Resource oldResource, Resource newResource) {
log.info("resource {} cannot be updated. reason={}", oldResource.getUniqueId(), response.getFormattedMessage());
componentsUtils.auditResource(response, user, newResource, AuditingActionEnum.IMPORT_RESOURCE,
ResourceVersionInfo.newBuilder().state(oldResource.getLifecycleState().name()).version(oldResource.getVersion()).build());
throw new ByResponseFormatComponentException(response);
}
public Resource validateResourceBeforeCreate(Resource resource, User user, AuditingActionEnum actionEnum, boolean inTransaction,
CsarInfo csarInfo) {
validateResourceFieldsBeforeCreate(user, resource, actionEnum, inTransaction);
validateCapabilityTypesCreate(user, getCapabilityTypeOperation(), resource, actionEnum, inTransaction);
validateLifecycleTypesCreate(user, resource, actionEnum);
validateResourceType(user, resource, actionEnum);
resource.setCreatorUserId(user.getUserId());
resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
resource.setContactId(resource.getContactId().toLowerCase());
if (StringUtils.isEmpty(resource.getToscaResourceName()) && !ModelConverter.isAtomicComponent(resource)) {
String resourceSystemName;
if (csarInfo != null && StringUtils.isNotEmpty(csarInfo.getVfResourceName())) {
resourceSystemName = ValidationUtils.convertToSystemName(csarInfo.getVfResourceName());
} else {
resourceSystemName = resource.getSystemName();
}
resource
.setToscaResourceName(CommonBeUtils.generateToscaResourceName(resource.getResourceType().name().toLowerCase(), resourceSystemName));
}
// Generate invariant UUID - must be here and not in operation since it
// should stay constant during clone
// TODO
String invariantUUID = UniqueIdBuilder.buildInvariantUUID();
resource.setInvariantUUID(invariantUUID);
return resource;
}
private Either<Boolean, ResponseFormat> validateResourceType(User user, Resource resource, AuditingActionEnum actionEnum) {
Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
if (resource.getResourceType() == null) {
log.debug("Invalid resource type for resource");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
eitherResult = Either.right(errorResponse);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
}
return eitherResult;
}
private Either<Boolean, ResponseFormat> validateLifecycleTypesCreate(User user, Resource resource, AuditingActionEnum actionEnum) {
Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
if (resource.getInterfaces() != null && resource.getInterfaces().size() > 0) {
log.debug("validate interface lifecycle Types Exist");
Iterator<InterfaceDefinition> intItr = resource.getInterfaces().values().iterator();
while (intItr.hasNext() && eitherResult.isLeft()) {
InterfaceDefinition interfaceDefinition = intItr.next();
String intType = interfaceDefinition.getUniqueId();
Either<InterfaceDefinition, StorageOperationStatus> eitherCapTypeFound = interfaceTypeOperation.getInterface(intType);
if (eitherCapTypeFound.isRight()) {
if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
BeEcompErrorManager.getInstance()
.logBeGraphObjectMissingError("Create Resource - validateLifecycleTypesCreate", "Interface", intType);
log.debug("Lifecycle Type: {} is required by resource: {} but does not exist in the DB", intType, resource.getName());
BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateLifecycleTypesCreate");
log.debug("request to data model failed with error: {}", eitherCapTypeFound.right().value().name());
}
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_LIFECYCLE_TYPE, intType);
eitherResult = Either.right(errorResponse);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
}
}
}
return eitherResult;
}
private Either<Boolean, ResponseFormat> validateCapabilityTypesCreate(User user, ICapabilityTypeOperation capabilityTypeOperation,
Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
if (resource.getCapabilities() != null && resource.getCapabilities().size() > 0) {
log.debug("validate capability Types Exist - capabilities section");
for (Entry<String, List<CapabilityDefinition>> typeEntry : resource.getCapabilities().entrySet()) {
eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, actionEnum, eitherResult, typeEntry,
inTransaction);
if (eitherResult.isRight()) {
return Either.right(eitherResult.right().value());
}
}
}
if (resource.getRequirements() != null && resource.getRequirements().size() > 0) {
log.debug("validate capability Types Exist - requirements section");
for (String type : resource.getRequirements().keySet()) {
eitherResult = validateCapabilityTypeExists(user, capabilityTypeOperation, resource, resource.getRequirements().get(type), actionEnum,
eitherResult, type, inTransaction);
if (eitherResult.isRight()) {
return Either.right(eitherResult.right().value());
}
}
}
return eitherResult;
}
// @param typeObject- the object to which the validation is done
private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
Resource resource, List<?> validationObjects, AuditingActionEnum actionEnum,
Either<Boolean, ResponseFormat> eitherResult, String type,
boolean inTransaction) {
Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation.getCapabilityType(type, inTransaction);
if (eitherCapTypeFound.isRight()) {
if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
BeEcompErrorManager.getInstance().logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", type);
log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", type, resource.getName());
BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
}
log.debug("Trying to get capability type {} failed with error: {}", type, eitherCapTypeFound.right().value().name());
ResponseFormat errorResponse = null;
if (type != null) {
errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, type);
} else {
errorResponse = componentsUtils.getResponseFormatByElement(ActionStatus.MISSING_CAPABILITY_TYPE, validationObjects);
}
eitherResult = Either.right(errorResponse);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
}
return eitherResult;
}
private Either<Boolean, ResponseFormat> validateCapabilityTypeExists(User user, ICapabilityTypeOperation capabilityTypeOperation,
Resource resource, AuditingActionEnum actionEnum,
Either<Boolean, ResponseFormat> eitherResult,
Entry<String, List<CapabilityDefinition>> typeEntry, boolean inTransaction) {
Either<CapabilityTypeDefinition, StorageOperationStatus> eitherCapTypeFound = capabilityTypeOperation
.getCapabilityType(typeEntry.getKey(), inTransaction);
if (eitherCapTypeFound.isRight()) {
if (eitherCapTypeFound.right().value() == StorageOperationStatus.NOT_FOUND) {
BeEcompErrorManager.getInstance()
.logBeGraphObjectMissingError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES, "Capability Type", typeEntry.getKey());
log.debug("Capability Type: {} is required by resource: {} but does not exist in the DB", typeEntry.getKey(), resource.getName());
BeEcompErrorManager.getInstance().logBeDaoSystemError(CREATE_RESOURCE_VALIDATE_CAPABILITY_TYPES);
}
log.debug("Trying to get capability type {} failed with error: {}", typeEntry.getKey(), eitherCapTypeFound.right().value().name());
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_CAPABILITY_TYPE, typeEntry.getKey());
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
return Either.right(errorResponse);
}
CapabilityTypeDefinition capabilityTypeDefinition = eitherCapTypeFound.left().value();
if (capabilityTypeDefinition.getProperties() != null) {
for (CapabilityDefinition capDef : typeEntry.getValue()) {
List<ComponentInstanceProperty> properties = capDef.getProperties();
List<ComponentInstanceProperty> changedProperties = new ArrayList<>();
if (properties == null || properties.isEmpty()) {
for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
ComponentInstanceProperty newProp = new ComponentInstanceProperty(prop.getValue());
changedProperties.add(newProp);
}
} else {
List<ComponentInstanceProperty> propsToAdd = new ArrayList<>();
for (Entry<String, PropertyDefinition> prop : capabilityTypeDefinition.getProperties().entrySet()) {
PropertyDefinition propFromDef = prop.getValue();
boolean propFound = false;
for (ComponentInstanceProperty cip : properties) {
if (propFromDef.getName().equals(cip.getName())) {
//merge property value and property description only, ignore other fields
if (cip.getDescription() != null && !cip.getDescription().equals(propFromDef.getDescription())) {
propFromDef.setDescription(cip.getDescription());
}
propertyDataValueMergeBusinessLogic.mergePropertyValue(propFromDef, cip, new ArrayList<>());
if (cip.getValue() != null) {
propFromDef.setValue(cip.getValue());
}
propsToAdd.add(new ComponentInstanceProperty(propFromDef));
propFound = true;
properties.remove(cip);
break;
}
}
if (!propFound) {
propsToAdd.add(new ComponentInstanceProperty(propFromDef));
}
}
if (!propsToAdd.isEmpty()) {
changedProperties.addAll(propsToAdd);
}
}
capDef.setProperties(changedProperties);
}
}
return eitherResult;
}
public Resource createResourceByDao(Resource resource, User user, AuditingActionEnum actionEnum, boolean isNormative, boolean inTransaction) {
// create resource
// lock new resource name in order to avoid creation resource with same
// name
Resource createdResource = null;
if (!inTransaction) {
Either<Boolean, ResponseFormat> lockResult = lockComponentByName(resource.getSystemName(), resource, CREATE_RESOURCE);
if (lockResult.isRight()) {
ResponseFormat responseFormat = lockResult.right().value();
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByResponseFormatComponentException(responseFormat);
}
log.debug("name is locked {} status = {}", resource.getSystemName(), lockResult);
}
try {
if (resource.deriveFromGeneric()) {
handleResourceGenericType(resource);
}
createdResource = createResourceTransaction(resource, user, isNormative);
componentsUtils.auditResource(componentsUtils.getResponseFormat(ActionStatus.CREATED), user, createdResource, actionEnum);
ASDCKpiApi.countCreatedResourcesKPI();
} catch (ComponentException e) {
ResponseFormat responseFormat =
e.getResponseFormat() == null ? componentsUtils.getResponseFormat(e.getActionStatus(), e.getParams()) : e.getResponseFormat();
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw e;
} catch (StorageException e) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(e.getStorageOperationStatus()));
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw e;
} finally {
if (!inTransaction) {
graphLockOperation.unlockComponentByName(resource.getSystemName(), resource.getUniqueId(), NodeTypeEnum.Resource);
}
}
return createdResource;
}
private Resource createResourceTransaction(Resource resource, User user, boolean isNormative) {
// validate resource name uniqueness
log.debug("validate resource name");
Either<Boolean, StorageOperationStatus> eitherValidation = toscaOperationFacade
.validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
if (eitherValidation.isRight()) {
loggerSupportability.log(LoggerSupportabilityActions.VALIDATE_NAME, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"ERROR while validate component name {} Status is: {}", resource.getName(), eitherValidation.right().value());
log.debug("Failed to validate component name {}. Status is {}. ", resource.getName(), eitherValidation.right().value());
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(eitherValidation.right().value()));
}
if (eitherValidation.left().value()) {
log.debug("resource with name: {}, already exists", resource.getName());
loggerSupportability
.log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, resource.getComponentMetadataForSupportLog(), StatusCode.ERROR,
"resource with name: {} already exists", resource.getName());
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
resource.getName());
}
log.debug("send resource {} to dao for create", resource.getName());
createArtifactsPlaceHolderData(resource, user);
// enrich object
if (!isNormative) {
log.debug("enrich resource with creator, version and state");
resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
resource.setVersion(INITIAL_VERSION);
resource.setHighestVersion(true);
if (resource.getResourceType() != null && resource.getResourceType() != ResourceTypeEnum.CVFC) {
resource.setAbstract(false);
}
}
return toscaOperationFacade.createToscaComponent(resource).left().on(r -> throwComponentExceptionByResource(r, resource));
}
private Resource throwComponentExceptionByResource(StorageOperationStatus status, Resource resource) {
ResponseFormat responseFormat = componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(status), resource);
throw new ByResponseFormatComponentException(responseFormat);
}
private void createArtifactsPlaceHolderData(Resource resource, User user) {
// create mandatory artifacts
// TODO it must be removed after that artifact uniqueId creation will be
// moved to ArtifactOperation
setInformationalArtifactsPlaceHolder(resource, user);
setDeploymentArtifactsPlaceHolder(resource, user);
setToscaArtifactsPlaceHolders(resource, user);
}
@SuppressWarnings("unchecked")
@Override
public void setDeploymentArtifactsPlaceHolder(Component component, User user) {
Resource resource = (Resource) component;
Map<String, ArtifactDefinition> artifactMap = resource.getDeploymentArtifacts();
if (artifactMap == null) {
artifactMap = new HashMap<>();
}
Map<String, Object> deploymentResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
.getDeploymentResourceArtifacts();
if (deploymentResourceArtifacts != null) {
Map<String, ArtifactDefinition> finalArtifactMap = artifactMap;
deploymentResourceArtifacts.forEach((k, v) -> processDeploymentResourceArtifacts(user, resource, finalArtifactMap, k, v));
}
resource.setDeploymentArtifacts(artifactMap);
}
private void processDeploymentResourceArtifacts(User user, Resource resource, Map<String, ArtifactDefinition> artifactMap, String k, Object v) {
Map<String, Object> artifactDetails = (Map<String, Object>) v;
Object object = artifactDetails.get(PLACE_HOLDER_RESOURCE_TYPES);
if (object != null) {
List<String> artifactTypes = (List<String>) object;
if (!artifactTypes.contains(resource.getResourceType().name())) {
return;
}
} else {
log.info("resource types for artifact placeholder {} were not defined. default is all resources", k);
}
if (artifactsBusinessLogic != null) {
ArtifactDefinition artifactDefinition = artifactsBusinessLogic
.createArtifactPlaceHolderInfo(resource.getUniqueId(), k, (Map<String, Object>) v, user, ArtifactGroupTypeEnum.DEPLOYMENT);
if (artifactDefinition != null && !artifactMap.containsKey(artifactDefinition.getArtifactLabel())) {
artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
}
}
}
@SuppressWarnings("unchecked")
private void setInformationalArtifactsPlaceHolder(Resource resource, User user) {
Map<String, ArtifactDefinition> artifactMap = resource.getArtifacts();
if (artifactMap == null) {
artifactMap = new HashMap<>();
}
String resourceUniqueId = resource.getUniqueId();
List<String> exludeResourceCategory = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceCategory();
List<String> exludeResourceType = ConfigurationManager.getConfigurationManager().getConfiguration().getExcludeResourceType();
Map<String, Object> informationalResourceArtifacts = ConfigurationManager.getConfigurationManager().getConfiguration()
.getInformationalResourceArtifacts();
List<CategoryDefinition> categories = resource.getCategories();
boolean isCreateArtifact = true;
if (exludeResourceCategory != null) {
String category = categories.get(0).getName();
isCreateArtifact = exludeResourceCategory.stream().noneMatch(e -> e.equalsIgnoreCase(category));
}
if (isCreateArtifact && exludeResourceType != null) {
String resourceType = resource.getResourceType().name();
isCreateArtifact = exludeResourceType.stream().noneMatch(e -> e.equalsIgnoreCase(resourceType));
}
if (informationalResourceArtifacts != null && isCreateArtifact) {
Set<String> keys = informationalResourceArtifacts.keySet();
for (String informationalResourceArtifactName : keys) {
Map<String, Object> artifactInfoMap = (Map<String, Object>) informationalResourceArtifacts.get(informationalResourceArtifactName);
ArtifactDefinition artifactDefinition = artifactsBusinessLogic
.createArtifactPlaceHolderInfo(resourceUniqueId, informationalResourceArtifactName, artifactInfoMap, user,
ArtifactGroupTypeEnum.INFORMATIONAL);
artifactMap.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
}
}
resource.setArtifacts(artifactMap);
}
/**
* deleteResource
*
* @param resourceId
* @param user
* @return
*/
public ResponseFormat deleteResource(String resourceId, User user) {
ResponseFormat responseFormat;
validateUserExists(user);
Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade.getToscaElement(resourceId);
if (resourceStatus.isRight()) {
log.debug("failed to get resource {}", resourceId);
return componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), "");
}
Resource resource = resourceStatus.left().value();
StorageOperationStatus result = StorageOperationStatus.OK;
lockComponent(resourceId, resource, "Mark resource to delete");
try {
result = markComponentToDelete(resource);
if (result == StorageOperationStatus.OK) {
responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
} else {
ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
}
return responseFormat;
} finally {
if (result == null || result != StorageOperationStatus.OK) {
janusGraphDao.rollback();
} else {
janusGraphDao.commit();
}
graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource);
}
}
public ResponseFormat deleteResourceByNameAndVersion(String resourceName, String version, User user) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT);
validateUserExists(user);
Resource resource = null;
StorageOperationStatus result = StorageOperationStatus.OK;
boolean failed = false;
try {
Either<Resource, StorageOperationStatus> resourceStatus = toscaOperationFacade
.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, version);
if (resourceStatus.isRight()) {
log.debug("failed to get resource {} version {}", resourceName, version);
return componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceStatus.right().value()), resourceName);
}
resource = resourceStatus.left().value();
} finally {
janusGraphDao.commit();
}
if (resource != null) {
lockComponent(resource.getUniqueId(), resource, DELETE_RESOURCE);
try {
result = markComponentToDelete(resource);
if (result != StorageOperationStatus.OK) {
ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(result);
responseFormat = componentsUtils.getResponseFormatByResource(actionStatus, resource.getName());
return responseFormat;
}
} catch (ComponentException e) {
failed = true;
throw e;
} finally {
if (failed || result == null || result != StorageOperationStatus.OK) {
janusGraphDao.rollback();
} else {
janusGraphDao.commit();
}
graphLockOperation.unlockComponent(resource.getUniqueId(), NodeTypeEnum.Resource);
}
}
return responseFormat;
}
public Either<Resource, ResponseFormat> getResource(String resourceId, User user) {
if (user != null) {
validateUserExists(user);
}
Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceId);
if (storageStatus.isRight()) {
log.debug("failed to get resource by id {}", resourceId);
return Either.right(
componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), resourceId));
}
if (!(storageStatus.left().value() instanceof Resource)) {
return Either.right(componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND), resourceId));
}
return Either.left(storageStatus.left().value());
}
public Either<Resource, ResponseFormat> getResourceByNameAndVersion(String resourceName, String resourceVersion, String userId) {
validateUserExists(userId);
Either<Resource, StorageOperationStatus> getResource = toscaOperationFacade
.getComponentByNameAndVersion(ComponentTypeEnum.RESOURCE, resourceName, resourceVersion);
if (getResource.isRight()) {
log.debug("failed to get resource by name {} and version {}", resourceName, resourceVersion);
return Either.right(
componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(getResource.right().value()), resourceName));
}
return Either.left(getResource.left().value());
}
/**
* updateResourceMetadata
*
* @param user - modifier data (userId)
* @param inTransaction TODO
* @param resourceIdToUpdate - the resource identifier
* @param newResource
* @return Either<Resource, responseFormat>
*/
public Resource updateResourceMetadata(String resourceIdToUpdate, Resource newResource, Resource currentResource, User user,
boolean inTransaction) {
validateUserExists(user.getUserId());
log.debug("Get resource with id {}", resourceIdToUpdate);
boolean needToUnlock = false;
try {
if (currentResource == null) {
Either<Resource, StorageOperationStatus> storageStatus = toscaOperationFacade.getToscaElement(resourceIdToUpdate);
if (storageStatus.isRight()) {
throw new ByResponseFormatComponentException(
componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus.right().value()), ""));
}
currentResource = storageStatus.left().value();
}
// verify that resource is checked-out and the user is the last
// updater
if (!ComponentValidationUtils.canWorkOnResource(currentResource, user.getUserId())) {
throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
}
// lock resource
StorageOperationStatus lockResult = graphLockOperation.lockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
if (lockResult != StorageOperationStatus.OK) {
BeEcompErrorManager.getInstance()
.logBeFailedLockObjectError("Upload Artifact - lock ", NodeTypeEnum.Resource.getName(), resourceIdToUpdate);
log.debug("Failed to lock resource: {}, error - {}", resourceIdToUpdate, lockResult);
ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult));
throw new ByResponseFormatComponentException(responseFormat);
}
needToUnlock = true;
// critical section starts here
// convert json to object
// Update and updated resource must have a non-empty "derivedFrom"
// list
// This code is not called from import resources, because of root
// VF "derivedFrom" should be null (or ignored)
if (ModelConverter.isAtomicComponent(currentResource)) {
validateDerivedFromNotEmpty(null, newResource, null);
validateDerivedFromNotEmpty(null, currentResource, null);
} else {
newResource.setDerivedFrom(null);
}
Either<Resource, ResponseFormat> dataModelResponse = updateResourceMetadata(resourceIdToUpdate, newResource, user, currentResource, false,
true);
if (dataModelResponse.isRight()) {
log.debug("failed to update resource metadata!!!");
throw new ByResponseFormatComponentException(dataModelResponse.right().value());
}
log.debug("Resource metadata updated successfully!!!");
return dataModelResponse.left().value();
} catch (ComponentException | StorageException e) {
rollback(inTransaction, newResource, null, null);
throw e;
} finally {
if (!inTransaction) {
janusGraphDao.commit();
}
if (needToUnlock) {
graphLockOperation.unlockComponent(resourceIdToUpdate, NodeTypeEnum.Resource);
}
}
}
private Either<Resource, ResponseFormat> updateResourceMetadata(String resourceIdToUpdate, Resource newResource, User user,
Resource currentResource, boolean shouldLock, boolean inTransaction) {
updateVfModuleGroupsNames(currentResource, newResource);
validateResourceFieldsBeforeUpdate(currentResource, newResource, inTransaction, false);
// Setting last updater and uniqueId
newResource.setContactId(newResource.getContactId().toLowerCase());
newResource.setLastUpdaterUserId(user.getUserId());
newResource.setUniqueId(resourceIdToUpdate);
// Cannot set highest version through UI
newResource.setHighestVersion(currentResource.isHighestVersion());
newResource.setCreationDate(currentResource.getCreationDate());
Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom = processUpdateOfDerivedFrom(currentResource, newResource, user.getUserId(),
inTransaction);
if (processUpdateOfDerivedFrom.isRight()) {
log.debug("Couldn't update derived from for resource {}", resourceIdToUpdate);
return Either.right(processUpdateOfDerivedFrom.right().value());
}
log.debug("send resource {} to dao for update", newResource.getUniqueId());
if (isNotEmpty(newResource.getGroups())) {
for (GroupDefinition group : newResource.getGroups()) {
if (DEFAULT_GROUP_VF_MODULE.equals(group.getType())) {
groupBusinessLogic
.validateAndUpdateGroupMetadata(newResource.getComponentMetadataDefinition().getMetadataDataDefinition().getUniqueId(), user,
newResource.getComponentType(), group, true, false);
}
}
}
Either<Resource, StorageOperationStatus> dataModelResponse = toscaOperationFacade.updateToscaElement(newResource);
if (dataModelResponse.isRight()) {
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(dataModelResponse.right().value()), newResource);
return Either.right(responseFormat);
} else if (dataModelResponse.left().value() == null) {
log.debug("No response from updateResource");
return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
}
return Either.left(dataModelResponse.left().value());
}
private void updateVfModuleGroupsNames(Resource currentResource, Resource newResource) {
if (currentResource.getGroups() != null && !currentResource.getName().equals(newResource.getName())) {
List<GroupDefinition> updatedGroups = currentResource.getGroups().stream()
.map(group -> getUpdatedGroup(group, currentResource.getName(), newResource.getName())).collect(toList());
newResource.setGroups(updatedGroups);
}
}
private GroupDefinition getUpdatedGroup(GroupDefinition currGroup, String replacePattern, String with) {
GroupDefinition updatedGroup = new GroupDefinition(currGroup);
if (updatedGroup.isSamePrefix(replacePattern) && updatedGroup.getType().equals(DEFAULT_GROUP_VF_MODULE)) {
String prefix = updatedGroup.getName().substring(0, replacePattern.length());
String newGroupName = updatedGroup.getName().replaceFirst(prefix, with);
updatedGroup.setName(newGroupName);
}
return updatedGroup;
}
/**
* validateResourceFieldsBeforeCreate
*
* @param user - modifier data (userId)
*/
private void validateResourceFieldsBeforeCreate(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
componentValidator.validate(user, resource, actionEnum);
// validate category
log.debug("validate category");
validateCategory(user, resource, actionEnum, inTransaction);
// validate vendor name & release & model number
log.debug("validate vendor name");
validateVendorName(user, resource, actionEnum);
log.debug("validate vendor release");
validateVendorReleaseName(user, resource, actionEnum);
log.debug("validate resource vendor model number");
validateResourceVendorModelNumber(user, resource, actionEnum);
// validate cost
log.debug("validate cost");
validateCost(resource);
// validate licenseType
log.debug("validate licenseType");
validateLicenseType(user, resource, actionEnum);
// validate template (derived from)
log.debug("validate derived from");
if (!ModelConverter.isAtomicComponent(resource) && resource.getResourceType() != ResourceTypeEnum.CVFC) {
resource.setDerivedFrom(null);
}
validateDerivedFromExist(user, resource, actionEnum);
// warn about non-updatable fields
checkComponentFieldsForOverrideAttempt(resource);
String currentCreatorFullName = resource.getCreatorFullName();
if (currentCreatorFullName != null) {
log.debug("Resource Creator fullname is automatically set and cannot be updated");
}
String currentLastUpdaterFullName = resource.getLastUpdaterFullName();
if (currentLastUpdaterFullName != null) {
log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
}
Long currentLastUpdateDate = resource.getLastUpdateDate();
if (currentLastUpdateDate != null) {
log.debug("Resource last update date is automatically set and cannot be updated");
}
Boolean currentAbstract = resource.isAbstract();
if (currentAbstract != null) {
log.debug("Resource abstract is automatically set and cannot be updated");
}
}
/**
* validateResourceFieldsBeforeUpdate
*
* @param currentResource - Resource object to validate
* @param isNested
*/
private void validateResourceFieldsBeforeUpdate(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
validateFields(currentResource, updateInfoResource, inTransaction, isNested);
warnNonEditableFields(currentResource, updateInfoResource);
}
private void warnNonEditableFields(Resource currentResource, Resource updateInfoResource) {
String currentResourceVersion = currentResource.getVersion();
String updatedResourceVersion = updateInfoResource.getVersion();
if ((updatedResourceVersion != null) && (!updatedResourceVersion.equals(currentResourceVersion))) {
log.debug("Resource version is automatically set and cannot be updated");
}
String currentCreatorUserId = currentResource.getCreatorUserId();
String updatedCreatorUserId = updateInfoResource.getCreatorUserId();
if ((updatedCreatorUserId != null) && (!updatedCreatorUserId.equals(currentCreatorUserId))) {
log.debug("Resource Creator UserId is automatically set and cannot be updated");
}
String currentCreatorFullName = currentResource.getCreatorFullName();
String updatedCreatorFullName = updateInfoResource.getCreatorFullName();
if ((updatedCreatorFullName != null) && (!updatedCreatorFullName.equals(currentCreatorFullName))) {
log.debug("Resource Creator fullname is automatically set and cannot be updated");
}
String currentLastUpdaterUserId = currentResource.getLastUpdaterUserId();
String updatedLastUpdaterUserId = updateInfoResource.getLastUpdaterUserId();
if ((updatedLastUpdaterUserId != null) && (!updatedLastUpdaterUserId.equals(currentLastUpdaterUserId))) {
log.debug("Resource LastUpdater userId is automatically set and cannot be updated");
}
String currentLastUpdaterFullName = currentResource.getLastUpdaterFullName();
String updatedLastUpdaterFullName = updateInfoResource.getLastUpdaterFullName();
if ((updatedLastUpdaterFullName != null) && (!updatedLastUpdaterFullName.equals(currentLastUpdaterFullName))) {
log.debug("Resource LastUpdater fullname is automatically set and cannot be updated");
}
Long currentCreationDate = currentResource.getCreationDate();
Long updatedCreationDate = updateInfoResource.getCreationDate();
if ((updatedCreationDate != null) && (!updatedCreationDate.equals(currentCreationDate))) {
log.debug("Resource Creation date is automatically set and cannot be updated");
}
Long currentLastUpdateDate = currentResource.getLastUpdateDate();
Long updatedLastUpdateDate = updateInfoResource.getLastUpdateDate();
if ((updatedLastUpdateDate != null) && (!updatedLastUpdateDate.equals(currentLastUpdateDate))) {
log.debug("Resource last update date is automatically set and cannot be updated");
}
LifecycleStateEnum currentLifecycleState = currentResource.getLifecycleState();
LifecycleStateEnum updatedLifecycleState = updateInfoResource.getLifecycleState();
if ((updatedLifecycleState != null) && (!updatedLifecycleState.equals(currentLifecycleState))) {
log.debug("Resource lifecycle state date is automatically set and cannot be updated");
}
Boolean currentAbstract = currentResource.isAbstract();
Boolean updatedAbstract = updateInfoResource.isAbstract();
if ((updatedAbstract != null) && (!updatedAbstract.equals(currentAbstract))) {
log.debug("Resource abstract is automatically set and cannot be updated");
}
Boolean currentHighestVersion = currentResource.isHighestVersion();
Boolean updatedHighestVersion = updateInfoResource.isHighestVersion();
if ((updatedHighestVersion != null) && (!updatedHighestVersion.equals(currentHighestVersion))) {
log.debug("Resource highest version is automatically set and cannot be updated");
}
String currentUuid = currentResource.getUUID();
String updatedUuid = updateInfoResource.getUUID();
if ((updatedUuid != null) && (!updatedUuid.equals(currentUuid))) {
log.debug("Resource UUID is automatically set and cannot be updated");
}
log.debug("Resource Type cannot be updated");
String currentInvariantUuid = currentResource.getInvariantUUID();
String updatedInvariantUuid = updateInfoResource.getInvariantUUID();
if ((updatedInvariantUuid != null) && (!updatedInvariantUuid.equals(currentInvariantUuid))) {
log.debug("Resource invariant UUID is automatically set and cannot be updated");
updateInfoResource.setInvariantUUID(currentInvariantUuid);
}
}
private void validateFields(Resource currentResource, Resource updateInfoResource, boolean inTransaction, boolean isNested) {
boolean hasBeenCertified = ValidationUtils.hasBeenCertified(currentResource.getVersion());
log.debug("validate resource name before update");
validateResourceName(currentResource, updateInfoResource, hasBeenCertified, isNested);
log.debug("validate description before update");
componentDescriptionValidator.validateAndCorrectField(null, updateInfoResource, null);
log.debug("validate icon before update");
validateIcon(currentResource, updateInfoResource, hasBeenCertified);
log.debug("validate tags before update");
componentTagsValidator.validateAndCorrectField(null, updateInfoResource, null);
log.debug("validate vendor name before update");
validateVendorName(null, updateInfoResource, null);
log.debug("validate resource vendor model number before update");
validateResourceVendorModelNumber(currentResource, updateInfoResource);
log.debug("validate vendor release before update");
validateVendorReleaseName(null, updateInfoResource, null);
log.debug("validate contact info before update");
componentContactIdValidator.validateAndCorrectField(null, updateInfoResource, null);
log.debug(VALIDATE_DERIVED_BEFORE_UPDATE);
validateDerivedFromDuringUpdate(currentResource, updateInfoResource, hasBeenCertified);
log.debug("validate category before update");
validateCategory(currentResource, updateInfoResource, hasBeenCertified, inTransaction);
}
private boolean isResourceNameEquals(Resource currentResource, Resource updateInfoResource) {
String resourceNameUpdated = updateInfoResource.getName();
String resourceNameCurrent = currentResource.getName();
if (resourceNameCurrent.equals(resourceNameUpdated)) {
return true;
}
// In case of CVFC type we should support the case of old VF with CVFC
// instances that were created without the "Cvfc" suffix
return currentResource.getResourceType() == ResourceTypeEnum.CVFC && resourceNameUpdated
.equals(addCvfcSuffixToResourceName(resourceNameCurrent));
}
private String addCvfcSuffixToResourceName(String resourceName) {
return resourceName + "Cvfc";
}
private void validateResourceName(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified, boolean isNested) {
String resourceNameUpdated = updateInfoResource.getName();
if (!isResourceNameEquals(currentResource, updateInfoResource)) {
if (isNested || !hasBeenCertified) {
componentNameValidator.validateAndCorrectField(null, updateInfoResource, null);
validateResourceNameUniqueness(updateInfoResource);
currentResource.setName(resourceNameUpdated);
currentResource.setNormalizedName(ValidationUtils.normaliseComponentName(resourceNameUpdated));
currentResource.setSystemName(ValidationUtils.convertToSystemName(resourceNameUpdated));
} else {
log.info("Resource name: {}, cannot be updated once the resource has been certified once.", resourceNameUpdated);
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_NAME_CANNOT_BE_CHANGED);
}
}
}
private void validateIcon(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified) {
String iconUpdated = updateInfoResource.getIcon();
String iconCurrent = currentResource.getIcon();
if (!iconCurrent.equals(iconUpdated)) {
if (!hasBeenCertified) {
componentIconValidator.validateAndCorrectField(null, updateInfoResource, null);
} else {
log.info("Icon {} cannot be updated once the resource has been certified once.", iconUpdated);
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_ICON_CANNOT_BE_CHANGED);
}
}
}
private void validateResourceVendorModelNumber(Resource currentResource, Resource updateInfoResource) {
String updatedResourceVendorModelNumber = updateInfoResource.getResourceVendorModelNumber();
String currentResourceVendorModelNumber = currentResource.getResourceVendorModelNumber();
if (!currentResourceVendorModelNumber.equals(updatedResourceVendorModelNumber)) {
validateResourceVendorModelNumber(null, updateInfoResource, null);
}
}
private Either<Boolean, ResponseFormat> validateCategory(Resource currentResource, Resource updateInfoResource, boolean hasBeenCertified,
boolean inTransaction) {
validateCategory(null, updateInfoResource, null, inTransaction);
if (hasBeenCertified) {
CategoryDefinition currentCategory = currentResource.getCategories().get(0);
SubCategoryDefinition currentSubCategory = currentCategory.getSubcategories().get(0);
CategoryDefinition updateCategory = updateInfoResource.getCategories().get(0);
SubCategoryDefinition updtaeSubCategory = updateCategory.getSubcategories().get(0);
if (!currentCategory.getName().equals(updateCategory.getName()) || !currentSubCategory.getName().equals(updtaeSubCategory.getName())) {
log.info("Category {} cannot be updated once the resource has been certified once.", currentResource.getCategories());
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_CATEGORY_CANNOT_BE_CHANGED);
return Either.right(errorResponse);
}
}
return Either.left(true);
}
private Either<Boolean, ResponseFormat> validateDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
boolean hasBeenCertified) {
List<String> currentDerivedFrom = currentResource.getDerivedFrom();
List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
log.trace("Update normative types");
return Either.left(true);
}
String derivedFromCurrent = currentDerivedFrom.get(0);
String derivedFromUpdated = updatedDerivedFrom.get(0);
if (!derivedFromCurrent.equals(derivedFromUpdated)) {
if (!hasBeenCertified) {
validateDerivedFromExist(null, updateInfoResource, null);
} else {
Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
null);
if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
return validateDerivedFromExtending;
}
}
} else {
// For derived from, we must know whether it was actually changed,
// otherwise we must do no action.
// Due to changes it inflicts on data model (remove artifacts,
// properties...), it's not like a flat field which can be
// overwritten if not changed.
// So we must indicate that derived from is not changed
updateInfoResource.setDerivedFrom(null);
}
return Either.left(true);
}
private Either<Boolean, ResponseFormat> validateNestedDerivedFromDuringUpdate(Resource currentResource, Resource updateInfoResource,
boolean hasBeenCertified) {
List<String> currentDerivedFrom = currentResource.getDerivedFrom();
List<String> updatedDerivedFrom = updateInfoResource.getDerivedFrom();
if (currentDerivedFrom == null || currentDerivedFrom.isEmpty() || updatedDerivedFrom == null || updatedDerivedFrom.isEmpty()) {
log.trace("Update normative types");
return Either.left(true);
}
String derivedFromCurrent = currentDerivedFrom.get(0);
String derivedFromUpdated = updatedDerivedFrom.get(0);
if (!derivedFromCurrent.equals(derivedFromUpdated)) {
if (!hasBeenCertified) {
validateDerivedFromExist(null, updateInfoResource, null);
} else {
Either<Boolean, ResponseFormat> validateDerivedFromExtending = validateDerivedFromExtending(null, currentResource, updateInfoResource,
null);
if (validateDerivedFromExtending.isRight() || !validateDerivedFromExtending.left().value()) {
log.debug("Derived from cannot be updated if it doesnt inherits directly or extends inheritance");
return validateDerivedFromExtending;
}
}
}
return Either.left(true);
}
private void validateDerivedFromExist(User user, Resource resource, AuditingActionEnum actionEnum) {
if (resource.getDerivedFrom() == null || resource.getDerivedFrom().isEmpty()) {
return;
}
String templateName = resource.getDerivedFrom().get(0);
Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade.validateToscaResourceNameExists(templateName);
if (dataModelResponse.isRight()) {
StorageOperationStatus storageStatus = dataModelResponse.right().value();
BeEcompErrorManager.getInstance().logBeDaoSystemError("Create Resource - validateDerivedFromExist");
log.debug("request to data model failed with error: {}", storageStatus);
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), resource);
log.trace("audit before sending response");
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(storageStatus));
} else if (!dataModelResponse.left().value()) {
log.info("resource template with name: {}, does not exists", templateName);
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.PARENT_RESOURCE_NOT_FOUND);
}
}
// Tal G for extending inheritance US815447
private Either<Boolean, ResponseFormat> validateDerivedFromExtending(User user, Resource currentResource, Resource updateInfoResource,
AuditingActionEnum actionEnum) {
String currentTemplateName = currentResource.getDerivedFrom().get(0);
String updatedTemplateName = updateInfoResource.getDerivedFrom().get(0);
Either<Boolean, StorageOperationStatus> dataModelResponse = toscaOperationFacade
.validateToscaResourceNameExtends(currentTemplateName, updatedTemplateName);
if (dataModelResponse.isRight()) {
StorageOperationStatus storageStatus = dataModelResponse.right().value();
BeEcompErrorManager.getInstance().logBeDaoSystemError("Create/Update Resource - validateDerivingFromExtendingType");
ResponseFormat responseFormat = componentsUtils
.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(storageStatus), currentResource);
log.trace("audit before sending response");
componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
return Either.right(responseFormat);
}
if (!dataModelResponse.left().value()) {
log.info("resource template with name {} does not inherit as original {}", updatedTemplateName, currentTemplateName);
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PARENT_RESOURCE_DOES_NOT_EXTEND);
componentsUtils.auditResource(responseFormat, user, currentResource, actionEnum);
return Either.right(responseFormat);
}
return Either.left(true);
}
public void validateDerivedFromNotEmpty(User user, Resource resource, AuditingActionEnum actionEnum) {
log.debug("validate resource derivedFrom field");
if ((resource.getDerivedFrom() == null) || (resource.getDerivedFrom().isEmpty()) || (resource.getDerivedFrom().get(0)) == null || (resource
.getDerivedFrom().get(0).trim().isEmpty())) {
log.info("derived from (template) field is missing for the resource");
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.MISSING_DERIVED_FROM_TEMPLATE);
}
}
private void validateResourceNameUniqueness(Resource resource) {
Either<Boolean, StorageOperationStatus> resourceOperationResponse = toscaOperationFacade
.validateComponentNameExists(resource.getName(), resource.getResourceType(), resource.getComponentType());
if (resourceOperationResponse.isLeft() && resourceOperationResponse.left().value()) {
log.debug("resource with name: {}, already exists", resource.getName());
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, ComponentTypeEnum.RESOURCE.getValue(),
resource.getName());
} else if (resourceOperationResponse.isRight()) {
log.debug("error while validateResourceNameExists for resource: {}", resource.getName());
throw new StorageException(resourceOperationResponse.right().value());
}
}
private void validateCategory(User user, Resource resource, AuditingActionEnum actionEnum, boolean inTransaction) {
List<CategoryDefinition> categories = resource.getCategories();
if (CollectionUtils.isEmpty(categories)) {
log.debug(CATEGORY_IS_EMPTY);
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
}
if (categories.size() > 1) {
log.debug("Must be only one category for resource");
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_TOO_MUCH_CATEGORIES, ComponentTypeEnum.RESOURCE.getValue());
}
CategoryDefinition category = categories.get(0);
List<SubCategoryDefinition> subcategories = category.getSubcategories();
if (CollectionUtils.isEmpty(subcategories)) {
log.debug("Missinig subcategory for resource");
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY);
}
if (subcategories.size() > 1) {
log.debug("Must be only one sub category for resource");
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_TOO_MUCH_SUBCATEGORIES);
}
SubCategoryDefinition subcategory = subcategories.get(0);
if (!ValidationUtils.validateStringNotEmpty(category.getName())) {
log.debug(CATEGORY_IS_EMPTY);
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
}
if (!ValidationUtils.validateStringNotEmpty(subcategory.getName())) {
log.debug(CATEGORY_IS_EMPTY);
ResponseFormat responseFormat = componentsUtils
.getResponseFormat(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_MISSING_SUBCATEGORY, ComponentTypeEnum.RESOURCE.getValue());
}
validateCategoryListed(category, subcategory, user, resource, actionEnum, inTransaction);
}
private void validateCategoryListed(CategoryDefinition category, SubCategoryDefinition subcategory, User user, Resource resource,
AuditingActionEnum actionEnum, boolean inTransaction) {
ResponseFormat responseFormat;
if (category != null && subcategory != null) {
log.debug("validating resource category {} against valid categories list", category);
Either<List<CategoryDefinition>, ActionStatus> categories = elementDao.getAllCategories(NodeTypeEnum.ResourceNewCategory, inTransaction);
if (categories.isRight()) {
log.debug("failed to retrieve resource categories from JanusGraph");
responseFormat = componentsUtils.getResponseFormat(categories.right().value());
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(categories.right().value());
}
List<CategoryDefinition> categoryList = categories.left().value();
Optional<CategoryDefinition> foundCategory = categoryList.stream().filter(cat -> cat.getName().equals(category.getName())).findFirst();
if (!foundCategory.isPresent()) {
log.debug("Category {} is not part of resource category group. Resource category valid values are {}", category, categoryList);
failOnInvalidCategory(user, resource, actionEnum);
}
Optional<SubCategoryDefinition> foundSubcategory = foundCategory.get().getSubcategories().stream()
.filter(subcat -> subcat.getName().equals(subcategory.getName())).findFirst();
if (!foundSubcategory.isPresent()) {
log.debug("SubCategory {} is not part of resource category group. Resource subcategory valid values are {}", subcategory,
foundCategory.get().getSubcategories());
failOnInvalidCategory(user, resource, actionEnum);
}
}
}
private void failOnInvalidCategory(User user, Resource resource, AuditingActionEnum actionEnum) {
ResponseFormat responseFormat;
responseFormat = componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_CATEGORY, ComponentTypeEnum.RESOURCE.getValue());
}
public void validateVendorReleaseName(User user, Resource resource, AuditingActionEnum actionEnum) {
String vendorRelease = resource.getVendorRelease();
log.debug("validate vendor relese name");
if (!ValidationUtils.validateStringNotEmpty(vendorRelease)) {
log.info("vendor relese name is missing.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_RELEASE);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_RELEASE);
}
validateVendorReleaseName(vendorRelease, user, resource, actionEnum);
}
public void validateVendorReleaseName(String vendorRelease, User user, Resource resource, AuditingActionEnum actionEnum) {
if (vendorRelease != null) {
if (!ValidationUtils.validateVendorReleaseLength(vendorRelease)) {
log.info("vendor release exceds limit.");
ResponseFormat errorResponse = componentsUtils
.getResponseFormat(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.VENDOR_RELEASE_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_RELEASE_MAX_LENGTH);
}
if (!ValidationUtils.validateVendorRelease(vendorRelease)) {
log.info("vendor release is not valid.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_RELEASE);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_RELEASE, vendorRelease);
}
}
}
private void validateVendorName(User user, Resource resource, AuditingActionEnum actionEnum) {
String vendorName = resource.getVendorName();
if (!ValidationUtils.validateStringNotEmpty(vendorName)) {
log.info("vendor name is missing.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.MISSING_VENDOR_NAME);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.MISSING_VENDOR_NAME);
}
validateVendorName(vendorName, user, resource, actionEnum);
}
private void validateVendorName(String vendorName, User user, Resource resource, AuditingActionEnum actionEnum) {
if (vendorName != null) {
if (!ValidationUtils.validateVendorNameLength(vendorName)) {
log.info("vendor name exceds limit.");
ResponseFormat errorResponse = componentsUtils
.getResponseFormat(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.VENDOR_NAME_EXCEEDS_LIMIT, "" + ValidationUtils.VENDOR_NAME_MAX_LENGTH);
}
if (!ValidationUtils.validateVendorName(vendorName)) {
log.info("vendor name is not valid.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_VENDOR_NAME);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.INVALID_VENDOR_NAME, vendorName);
}
}
}
private void validateResourceVendorModelNumber(User user, Resource resource, AuditingActionEnum actionEnum) {
String resourceVendorModelNumber = resource.getResourceVendorModelNumber();
if (StringUtils.isNotEmpty(resourceVendorModelNumber)) {
if (!ValidationUtils.validateResourceVendorModelNumberLength(resourceVendorModelNumber)) {
log.info("resource vendor model number exceeds limit.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
"" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.RESOURCE_VENDOR_MODEL_NUMBER_EXCEEDS_LIMIT,
"" + ValidationUtils.RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH);
}
// resource vendor model number is currently validated as vendor
// name
if (!ValidationUtils.validateVendorName(resourceVendorModelNumber)) {
log.info("resource vendor model number is not valid.");
ResponseFormat errorResponse = componentsUtils.getResponseFormat(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
componentsUtils.auditResource(errorResponse, user, resource, actionEnum);
throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_VENDOR_MODEL_NUMBER);
}
}
}
private void validateCost(Resource resource) {
String cost = resource.getCost();
if (cost != null) {
if (!ValidationUtils.validateCost(cost)) {
log.debug("resource cost is invalid.");
throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
}
}
}
private void validateLicenseType(User user, Resource resource, AuditingActionEnum actionEnum) {
log.debug("validate licenseType");
String licenseType = resource.getLicenseType();
if (licenseType != null) {
List<String> licenseTypes = ConfigurationManager.getConfigurationManager().getConfiguration().getLicenseTypes();
if (!licenseTypes.contains(licenseType)) {
log.debug("License type {} isn't configured", licenseType);
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT);
if (actionEnum != null) {
// In update case, no audit is required
componentsUtils.auditResource(responseFormat, user, resource, actionEnum);
}
throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
}
}
}
private Either<Boolean, ResponseFormat> processUpdateOfDerivedFrom(Resource currentResource, Resource updatedResource, String userId,
boolean inTransaction) {
if (updatedResource.getDerivedFrom() != null) {
log.debug("Starting derived from update for resource {}", updatedResource.getUniqueId());
log.debug("1. Removing interface artifacts from graph");
// Remove all interface artifacts of resource
String resourceId = updatedResource.getUniqueId();
Map<String, InterfaceDefinition> interfaces = currentResource.getInterfaces();
if (interfaces != null) {
Collection<InterfaceDefinition> values = interfaces.values();
for (InterfaceDefinition interfaceDefinition : values) {
String interfaceType = interfaceTypeOperation.getShortInterfaceName(interfaceDefinition);
log.trace("Starting interface artifacts removal for interface type {}", interfaceType);
Map<String, Operation> operations = interfaceDefinition.getOperationsMap();
if (operations != null) {
for (Entry<String, Operation> operationEntry : operations.entrySet()) {
Operation operation = operationEntry.getValue();
ArtifactDefinition implementation = operation.getImplementationArtifact();
if (implementation != null) {
String uniqueId = implementation.getUniqueId();
log.debug("Removing interface artifact definition {}, operation {}, interfaceType {}", uniqueId,
operationEntry.getKey(), interfaceType);
// only thing that transacts and locks here
Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface = artifactsBusinessLogic
.deleteArtifactByInterface(resourceId, userId, uniqueId, true);
if (deleteArtifactByInterface.isRight()) {
log.debug("Couldn't remove artifact definition with id {}", uniqueId);
if (!inTransaction) {
janusGraphDao.rollback();
}
return Either.right(deleteArtifactByInterface.right().value());
}
} else {
log.trace("No implementation found for operation {} - nothing to delete", operationEntry.getKey());
}
}
} else {
log.trace("No operations found for interface type {}", interfaceType);
}
}
}
log.debug("2. Removing properties");
Either<Map<String, PropertyDefinition>, StorageOperationStatus> findPropertiesOfNode = propertyOperation
.deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, resourceId);
if (findPropertiesOfNode.isRight() && findPropertiesOfNode.right().value() != StorageOperationStatus.OK) {
log.debug("Failed to remove all properties of resource");
if (!inTransaction) {
janusGraphDao.rollback();
}
return Either
.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findPropertiesOfNode.right().value())));
}
} else {
log.debug("Derived from wasn't changed during update");
}
if (inTransaction) {
return Either.left(true);
}
janusGraphDao.commit();
return Either.left(true);
}
public ICapabilityTypeOperation getCapabilityTypeOperation() {
return capabilityTypeOperation;
}
@Autowired
public void setCapabilityTypeOperation(ICapabilityTypeOperation capabilityTypeOperation) {
this.capabilityTypeOperation = capabilityTypeOperation;
}
public Boolean validatePropertiesDefaultValues(Resource resource) {
log.debug("validate resource properties default values");
List<PropertyDefinition> properties = resource.getProperties();
if (properties != null) {
iterateOverProperties(properties);
}
return true;
}
public void iterateOverProperties(List<PropertyDefinition> properties) {
String type = null;
String innerType = null;
for (PropertyDefinition property : properties) {
if (!propertyOperation.isPropertyTypeValid(property)) {
log.info("Invalid type for property {}", property);
throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
}
Map<String, DataTypeDefinition> allDataTypes = getAllDataTypes(applicationDataTypeCache);
type = property.getType();
if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
ResponseFormat responseFormat = validateMapOrListPropertyType(property, innerType, allDataTypes);
if (responseFormat != null) {
break;
}
}
validateDefaultPropertyValue(property, allDataTypes, type, innerType);
}
}
private void validateDefaultPropertyValue(PropertyDefinition property, Map<String, DataTypeDefinition> allDataTypes, String type,
String innerType) {
if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes)) {
log.info("Invalid default value for property {}", property);
ResponseFormat responseFormat;
if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
throw new ByActionStatusComponentException(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType,
property.getDefaultValue());
}
throw new ByActionStatusComponentException(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue());
}
}
private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, String innerType,
Map<String, DataTypeDefinition> allDataTypes) {
ResponseFormat responseFormat = null;
ImmutablePair<String, Boolean> propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes);
innerType = propertyInnerTypeValid.getLeft();
if (!propertyInnerTypeValid.getRight().booleanValue()) {
log.info("Invalid inner type for property {}", property);
responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName());
}
return responseFormat;
}
@Override
public Either<List<String>, ResponseFormat> deleteMarkedComponents() {
return deleteMarkedComponents(ComponentTypeEnum.RESOURCE);
}
@Override
public ComponentInstanceBusinessLogic getComponentInstanceBL() {
return componentInstanceBusinessLogic;
}
private String getComponentTypeForResponse(Component component) {
String componentTypeForResponse = "SERVICE";
if (component instanceof Resource) {
componentTypeForResponse = ((Resource) component).getResourceType().name();
}
return componentTypeForResponse;
}
public Either<Resource, ResponseFormat> getLatestResourceFromCsarUuid(String csarUuid, User user) {
// validate user
if (user != null) {
validateUserExists(user);
}
// get resource from csar uuid
Either<Resource, StorageOperationStatus> either = toscaOperationFacade
.getLatestComponentByCsarOrName(ComponentTypeEnum.RESOURCE, csarUuid, "");
if (either.isRight()) {
ResponseFormat resp = componentsUtils.getResponseFormat(ActionStatus.RESOURCE_FROM_CSAR_NOT_FOUND, csarUuid);
return Either.right(resp);
}
return Either.left(either.left().value());
}
@Override
public Either<List<ComponentInstance>, ResponseFormat> getComponentInstancesFilteredByPropertiesAndInputs(String componentId, String userId) {
return null;
}
private Map<String, List<CapabilityDefinition>> getValidComponentInstanceCapabilities(String resourceId,
Map<String, List<CapabilityDefinition>> defaultCapabilities,
Map<String, List<UploadCapInfo>> uploadedCapabilities) {
Map<String, List<CapabilityDefinition>> validCapabilitiesMap = new HashMap<>();
uploadedCapabilities.forEach((k, v) -> addValidComponentInstanceCapabilities(k, v, resourceId, defaultCapabilities, validCapabilitiesMap));
return validCapabilitiesMap;
}
private void addValidComponentInstanceCapabilities(String key, List<UploadCapInfo> capabilities, String resourceId,
Map<String, List<CapabilityDefinition>> defaultCapabilities,
Map<String, List<CapabilityDefinition>> validCapabilitiesMap) {
String capabilityType = capabilities.get(0).getType();
if (defaultCapabilities.containsKey(capabilityType)) {
CapabilityDefinition defaultCapability = getCapability(resourceId, defaultCapabilities, capabilityType);
validateCapabilityProperties(capabilities, resourceId, defaultCapability);
List<CapabilityDefinition> validCapabilityList = new ArrayList<>();
validCapabilityList.add(defaultCapability);
validCapabilitiesMap.put(key, validCapabilityList);
} else {
throw new ByActionStatusComponentException(ActionStatus.MISSING_CAPABILITY_TYPE, capabilityType);
}
}
private void validateCapabilityProperties(List<UploadCapInfo> capabilities, String resourceId, CapabilityDefinition defaultCapability) {
if (CollectionUtils.isEmpty(defaultCapability.getProperties()) && isNotEmpty(capabilities.get(0).getProperties())) {
log.debug("Failed to validate capability {} of component {}. Property list is empty. ", defaultCapability.getName(), resourceId);
log.debug("Failed to update capability property values. Property list of fetched capability {} is empty. ", defaultCapability.getName());
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND, resourceId);
} else if (isNotEmpty(capabilities.get(0).getProperties())) {
validateUniquenessUpdateUploadedComponentInstanceCapability(defaultCapability, capabilities.get(0));
}
}
private CapabilityDefinition getCapability(String resourceId, Map<String, List<CapabilityDefinition>> defaultCapabilities,
String capabilityType) {
CapabilityDefinition defaultCapability;
if (isNotEmpty(defaultCapabilities.get(capabilityType).get(0).getProperties())) {
defaultCapability = defaultCapabilities.get(capabilityType).get(0);
} else {
Either<Component, StorageOperationStatus> getFullComponentRes = toscaOperationFacade.getToscaFullElement(resourceId);
if (getFullComponentRes.isRight()) {
log.debug("Failed to get full component {}. Status is {}. ", resourceId, getFullComponentRes.right().value());
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NOT_FOUND, resourceId);
}
defaultCapability = getFullComponentRes.left().value().getCapabilities().get(capabilityType).get(0);
}
return defaultCapability;
}
private void validateUniquenessUpdateUploadedComponentInstanceCapability(CapabilityDefinition defaultCapability,
UploadCapInfo uploadedCapability) {
List<ComponentInstanceProperty> validProperties = new ArrayList<>();
Map<String, PropertyDefinition> defaultProperties = defaultCapability.getProperties().stream()
.collect(toMap(PropertyDefinition::getName, Function.identity()));
List<UploadPropInfo> uploadedProperties = uploadedCapability.getProperties();
for (UploadPropInfo property : uploadedProperties) {
String propertyName = property.getName().toLowerCase();
String propertyType = property.getType();
ComponentInstanceProperty validProperty;
if (defaultProperties.containsKey(propertyName) && propertTypeEqualsTo(defaultProperties, propertyName, propertyType)) {
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NAME_ALREADY_EXISTS, propertyName);
}
validProperty = new ComponentInstanceProperty();
validProperty.setName(propertyName);
if (property.getValue() != null) {
validProperty.setValue(property.getValue().toString());
}
validProperty.setDescription(property.getDescription());
validProperty.setPassword(property.isPassword());
validProperties.add(validProperty);
}
defaultCapability.setProperties(validProperties);
}
private boolean propertTypeEqualsTo(Map<String, PropertyDefinition> defaultProperties, String propertyName, String propertyType) {
return propertyType != null && !defaultProperties.get(propertyName).getType().equals(propertyType);
}
private Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> organizeVfCsarArtifactsByArtifactOperation(
List<NonMetaArtifactInfo> artifactPathAndNameList, List<ArtifactDefinition> existingArtifactsToHandle, Resource resource, User user) {
EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>> nodeTypeArtifactsToHandle = new EnumMap<>(ArtifactOperationEnum.class);
Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
Either<EnumMap<ArtifactOperationEnum, List<NonMetaArtifactInfo>>, ResponseFormat> nodeTypeArtifactsToHandleRes = Either
.left(nodeTypeArtifactsToHandle);
try {
// add all found Csar artifacts to list to upload
List<NonMetaArtifactInfo> artifactsToUpload = new ArrayList<>(artifactPathAndNameList);
List<NonMetaArtifactInfo> artifactsToUpdate = new ArrayList<>();
List<NonMetaArtifactInfo> artifactsToDelete = new ArrayList<>();
for (NonMetaArtifactInfo currNewArtifact : artifactPathAndNameList) {
ArtifactDefinition foundArtifact;
if (!existingArtifactsToHandle.isEmpty()) {
foundArtifact = existingArtifactsToHandle.stream().filter(a -> a.getArtifactName().equals(currNewArtifact.getArtifactName()))
.findFirst().orElse(null);
if (foundArtifact != null) {
if (foundArtifact.getArtifactType().equals(currNewArtifact.getArtifactType())) {
if (!foundArtifact.getArtifactChecksum().equals(currNewArtifact.getArtifactChecksum())) {
currNewArtifact.setArtifactUniqueId(foundArtifact.getUniqueId());
// if current artifact already exists, but has
// different content, add him to the list to
// update
artifactsToUpdate.add(currNewArtifact);
}
// remove found artifact from the list of existing
// artifacts to handle, because it was already
// handled
existingArtifactsToHandle.remove(foundArtifact);
// and remove found artifact from the list to
// upload, because it should either be updated or be
// ignored
artifactsToUpload.remove(currNewArtifact);
} else {
log.debug("Can't upload two artifact with the same name {}.", currNewArtifact.getArtifactName());
ResponseFormat responseFormat = ResponseFormatManager.getInstance()
.getResponseFormat(ActionStatus.ARTIFACT_ALREADY_EXIST_IN_DIFFERENT_TYPE_IN_CSAR, currNewArtifact.getArtifactName(),
currNewArtifact.getArtifactType(), foundArtifact.getArtifactType());
AuditingActionEnum auditingAction = artifactsBusinessLogic
.detectAuditingType(new ArtifactOperationInfo(false, false, ArtifactOperationEnum.CREATE),
foundArtifact.getArtifactChecksum());
artifactsBusinessLogic
.handleAuditing(auditingAction, resource, resource.getUniqueId(), user, null, null, foundArtifact.getUniqueId(),
responseFormat, resource.getComponentType(), null);
responseWrapper.setInnerElement(responseFormat);
break;
}
}
}
}
if (responseWrapper.isEmpty()) {
for (ArtifactDefinition currArtifact : existingArtifactsToHandle) {
if (currArtifact.getIsFromCsar()) {
artifactsToDelete.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
} else {
artifactsToUpdate.add(new NonMetaArtifactInfo(currArtifact.getArtifactName(), null, currArtifact.getArtifactType(),
currArtifact.getArtifactGroupType(), null, currArtifact.getUniqueId(), currArtifact.getIsFromCsar()));
}
}
}
if (responseWrapper.isEmpty()) {
if (!artifactsToUpload.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.CREATE, artifactsToUpload);
}
if (!artifactsToUpdate.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.UPDATE, artifactsToUpdate);
}
if (!artifactsToDelete.isEmpty()) {
nodeTypeArtifactsToHandle.put(ArtifactOperationEnum.DELETE, artifactsToDelete);
}
}
if (!responseWrapper.isEmpty()) {
nodeTypeArtifactsToHandleRes = Either.right(responseWrapper.getInnerElement());
}
} catch (Exception e) {
ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
responseWrapper.setInnerElement(responseFormat);
log.debug("Exception occurred when findNodeTypeArtifactsToHandle, error is:{}", e.getMessage(), e);
}
return nodeTypeArtifactsToHandleRes;
}
ImmutablePair<String, String> buildNestedToscaResourceName(final String nodeResourceType, final String vfResourceName,
final String nodeTypeFullName) {
String actualType;
String actualVfName;
if (ResourceTypeEnum.CVFC.name().equals(nodeResourceType)) {
actualVfName = vfResourceName + ResourceTypeEnum.CVFC.name();
actualType = ResourceTypeEnum.VFC.name();
} else {
actualVfName = vfResourceName;
actualType = nodeResourceType;
}
String nameWithouNamespacePrefix;
try {
final String nodeTypeNamePrefix = getNodeTypeNamePrefix(nodeTypeFullName);
log.debug("####### buildNestedToscaResourceName nodeResourceType {}, vfResourceName {}, "
+ "nodeTypeFullName {}, actualType {}, vfResourceName {} ", nodeResourceType, vfResourceName, nodeTypeFullName, actualType,
vfResourceName);
final StringBuilder toscaResourceName = new StringBuilder(nodeTypeNamePrefix);
if (!nodeTypeFullName.contains(nodeTypeNamePrefix)) {
nameWithouNamespacePrefix = nodeTypeFullName;
} else {
nameWithouNamespacePrefix = nodeTypeFullName.substring(nodeTypeNamePrefix.length());
}
final String[] findTypes = nameWithouNamespacePrefix.split("\\.");
String actualName;
if (nodeResourceType.equalsIgnoreCase(findTypes[0])) {
actualName = nameWithouNamespacePrefix.substring(nodeResourceType.length());
} else {
actualName = "." + nameWithouNamespacePrefix;
}
if (actualName.startsWith(Constants.ABSTRACT)) {
toscaResourceName.append(nodeResourceType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName));
} else {
toscaResourceName.append(actualType.toLowerCase()).append('.').append(ValidationUtils.convertToSystemName(actualVfName)).append('.')
.append(Constants.ABSTRACT);
}
final StringBuilder previousToscaResourceName = new StringBuilder(toscaResourceName);
final String[] actualNames = actualName.split("\\.");
if (actualNames.length < 3) {
return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
previousToscaResourceName.append(actualName).toString());
}
return new ImmutablePair<>(toscaResourceName.append(actualName.toLowerCase()).toString(),
previousToscaResourceName.append(actualName.substring(actualNames[1].length() + 1).toLowerCase()).toString());
} catch (final Exception e) {
log.debug("Exception occured when buildNestedToscaResourceName, error is:{}", e.getMessage(), e);
throw new ByActionStatusComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE, vfResourceName);
}
}
/**
* Extracts a Node Type Name prefix from the given Node Type Name.
*
* @param fullName Node Type Name
* @return Node Type Name Prefix
*/
private String getNodeTypeNamePrefix(final String fullName) {
String tempPrefix = Constants.USER_DEFINED_RESOURCE_NAMESPACE_PREFIX;
final List<String> definedNodeTypeNamespaceList = getDefinedNodeTypeNamespaceList();
log.debug("************* getPrefiX fullName {} FROM {}", fullName, definedNodeTypeNamespaceList);
final Optional<String> validNameSpace = validateNodeTypeNamePrefix(fullName, definedNodeTypeNamespaceList);
if (validNameSpace.isPresent()) {
tempPrefix = validNameSpace.get();
}
log.debug("************* getNodeTypeNamePrefix return fullName {} ", tempPrefix);
return tempPrefix;
}
@Override
public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String resourceId,
List<String> dataParamsToReturn) {
ComponentParametersView paramsToReturn = new ComponentParametersView(dataParamsToReturn);
Either<Resource, StorageOperationStatus> resourceResultEither = toscaOperationFacade.getToscaElement(resourceId, paramsToReturn);
if (resourceResultEither.isRight()) {
if (resourceResultEither.right().value() == StorageOperationStatus.NOT_FOUND) {
log.debug("Failed to found resource with id {} ", resourceId);
Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
}
log.debug("failed to get resource by id {} with filters {}", resourceId, dataParamsToReturn);
return Either.right(
componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(resourceResultEither.right().value()), ""));
}
Resource resource = resourceResultEither.left().value();
if (dataParamsToReturn.contains(ComponentFieldsEnum.INPUTS.getValue())) {
ListUtils.emptyIfNull(resource.getInputs()).forEach(input -> input.setConstraints(setInputConstraint(input)));
}
UiComponentDataTransfer dataTransfer = uiComponentDataConverter.getUiDataTransferFromResourceByParams(resource, dataParamsToReturn);
return Either.left(dataTransfer);
}
@Override
public Either<Component, ActionStatus> shouldUpgradeToLatestDerived(Component clonedComponent) {
Resource resource = (Resource) clonedComponent;
if (ModelConverter.isAtomicComponent(resource.getResourceType())) {
Either<Component, StorageOperationStatus> shouldUpgradeToLatestDerived = toscaOperationFacade.shouldUpgradeToLatestDerived(resource);
if (shouldUpgradeToLatestDerived.isRight()) {
return Either.right(componentsUtils.convertFromStorageResponse(shouldUpgradeToLatestDerived.right().value()));
}
return Either.left(shouldUpgradeToLatestDerived.left().value());
} else {
return super.shouldUpgradeToLatestDerived(clonedComponent);
}
}
}
|