aboutsummaryrefslogtreecommitdiffstats
path: root/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java
blob: 51b708d503bd344c3768ea36a949d8b102ab0b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.asdc.installer.heat;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.LockAcquisitionException;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.notification.IResourceInstance;
import org.onap.sdc.api.notification.IStatusData;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
import org.onap.sdc.tosca.parser.impl.SdcTypes;
import org.onap.sdc.toscaparser.api.CapabilityAssignment;
import org.onap.sdc.toscaparser.api.CapabilityAssignments;
import org.onap.sdc.toscaparser.api.Group;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.Policy;
import org.onap.sdc.toscaparser.api.Property;
import org.onap.sdc.toscaparser.api.RequirementAssignment;
import org.onap.sdc.toscaparser.api.RequirementAssignments;
import org.onap.sdc.toscaparser.api.elements.Metadata;
import org.onap.sdc.toscaparser.api.functions.GetInput;
import org.onap.sdc.toscaparser.api.parameters.Input;
import org.onap.sdc.utils.DistributionStatusEnum;
import org.onap.so.asdc.client.ASDCConfiguration;
import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
import org.onap.so.asdc.installer.ASDCElementInfo;
import org.onap.so.asdc.installer.BigDecimalVersion;
import org.onap.so.asdc.installer.IVfModuleData;
import org.onap.so.asdc.installer.ToscaResourceStructure;
import org.onap.so.asdc.installer.VfModuleArtifact;
import org.onap.so.asdc.installer.VfModuleStructure;
import org.onap.so.asdc.installer.VfResourceStructure;
import org.onap.so.asdc.util.YamlEditor;
import org.onap.so.db.catalog.beans.AllottedResource;
import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
import org.onap.so.db.catalog.beans.CollectionResource;
import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
import org.onap.so.db.catalog.beans.ConfigurationResource;
import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
import org.onap.so.db.catalog.beans.CvnfcCustomization;
import org.onap.so.db.catalog.beans.HeatEnvironment;
import org.onap.so.db.catalog.beans.HeatFiles;
import org.onap.so.db.catalog.beans.HeatTemplate;
import org.onap.so.db.catalog.beans.HeatTemplateParam;
import org.onap.so.db.catalog.beans.InstanceGroupType;
import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
import org.onap.so.db.catalog.beans.NetworkInstanceGroup;
import org.onap.so.db.catalog.beans.NetworkResource;
import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
import org.onap.so.db.catalog.beans.Service;
import org.onap.so.db.catalog.beans.ServiceProxyResource;
import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
import org.onap.so.db.catalog.beans.SubType;
import org.onap.so.db.catalog.beans.TempNetworkHeatTemplateLookup;
import org.onap.so.db.catalog.beans.ToscaCsar;
import org.onap.so.db.catalog.beans.VFCInstanceGroup;
import org.onap.so.db.catalog.beans.VfModule;
import org.onap.so.db.catalog.beans.VfModuleCustomization;
import org.onap.so.db.catalog.beans.VnfResource;
import org.onap.so.db.catalog.beans.VnfResourceCustomization;
import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
import org.onap.so.db.catalog.beans.VnfcCustomization;
import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
import org.onap.so.db.catalog.data.repository.CollectionResourceCustomizationRepository;
import org.onap.so.db.catalog.data.repository.CollectionResourceRepository;
import org.onap.so.db.catalog.data.repository.ConfigurationResourceCustomizationRepository;
import org.onap.so.db.catalog.data.repository.ConfigurationResourceRepository;
import org.onap.so.db.catalog.data.repository.CvnfcCustomizationRepository;
import org.onap.so.db.catalog.data.repository.ExternalServiceToInternalServiceRepository;
import org.onap.so.db.catalog.data.repository.HeatTemplateRepository;
import org.onap.so.db.catalog.data.repository.InstanceGroupRepository;
import org.onap.so.db.catalog.data.repository.NetworkResourceCustomizationRepository;
import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
import org.onap.so.db.catalog.data.repository.ServiceProxyResourceCustomizationRepository;
import org.onap.so.db.catalog.data.repository.ServiceProxyResourceRepository;
import org.onap.so.db.catalog.data.repository.ServiceRepository;
import org.onap.so.db.catalog.data.repository.TempNetworkHeatTemplateRepository;
import org.onap.so.db.catalog.data.repository.VFModuleCustomizationRepository;
import org.onap.so.db.catalog.data.repository.VFModuleRepository;
import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
import org.onap.so.db.catalog.data.repository.VnfcCustomizationRepository;
import org.onap.so.db.catalog.data.repository.VnfcInstanceGroupCustomizationRepository;
import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
import org.onap.so.db.request.beans.WatchdogDistributionStatus;
import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
import org.onap.so.logger.MessageEnum;
import org.onap.so.logger.MsoLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class ToscaResourceInstaller {

	protected static final String ALLOTTED_RESOURCE = "Allotted Resource";

	protected static final String MULTI_STAGE_DESIGN = "multi_stage_design";

	protected static final String SCALABLE = "scalable";

	protected static final String BASIC = "BASIC";

	protected static final String PROVIDER = "PROVIDER";

	protected static final String HEAT = "HEAT";

	protected static final String MANUAL_RECORD = "MANUAL_RECORD";

	protected static final String MSO = "SO";

	@Autowired
	protected ServiceRepository serviceRepo;
	
	@Autowired
	protected InstanceGroupRepository instanceGroupRepo;
	
	@Autowired
	protected ServiceProxyResourceCustomizationRepository serviceProxyCustomizationRepo;
	
	@Autowired
	protected ServiceProxyResourceRepository serviceProxyRepo;
	
	@Autowired
	protected CollectionResourceRepository collectionRepo;
	
	@Autowired
	protected CollectionResourceCustomizationRepository collectionCustomizationRepo;
	
	@Autowired
	protected ConfigurationResourceCustomizationRepository configCustomizationRepo;
	
	@Autowired
	protected ConfigurationResourceRepository configRepo;

	@Autowired
	protected VnfResourceRepository vnfRepo;

	@Autowired
	protected VnfCustomizationRepository vnfCustomizationRepo;
	
	@Autowired
	protected VFModuleRepository vfModuleRepo;

	@Autowired
	protected VFModuleCustomizationRepository vfModuleCustomizationRepo;	
	
	@Autowired
	protected VnfcInstanceGroupCustomizationRepository vnfcInstanceGroupCustomizationRepo;	
	
	@Autowired
	protected VnfcCustomizationRepository vnfcCustomizationRepo;
	
	@Autowired
	protected CvnfcCustomizationRepository cvnfcCustomizationRepo;

	@Autowired
	protected AllottedResourceRepository allottedRepo;

	@Autowired
	protected AllottedResourceCustomizationRepository allottedCustomizationRepo;

	@Autowired
	protected NetworkResourceRepository networkRepo;
	 
	@Autowired
	protected HeatTemplateRepository heatRepo;

	@Autowired
	protected NetworkResourceCustomizationRepository networkCustomizationRepo;

	@Autowired
	protected WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
	@Autowired
	protected WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
	@Autowired
	protected WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;	
	
	@Autowired
	protected TempNetworkHeatTemplateRepository tempNetworkLookupRepo;
	
	@Autowired
	protected ExternalServiceToInternalServiceRepository externalServiceToInternalServiceRepository;
	
	protected static final MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,ToscaResourceInstaller.class);

	public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {
		boolean status = false;
		VfResourceStructure vfResourceStructure = vfResourceStruct;
		try {
		    status = vfResourceStructure.isDeployedSuccessfully();
		} catch (RuntimeException e) {
		    status = false;
		}
		try {					
			Service existingService = serviceRepo.findOneByModelUUID(vfResourceStructure.getNotification().getServiceUUID()); 
			if(existingService != null)
				status = true;
			if (status) {
				logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
						vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
						vfResourceStructure.getNotification().getServiceName(),
						BigDecimalVersion.castAndCheckNotificationVersionToString(
								vfResourceStructure.getNotification().getServiceVersion()),
						vfResourceStructure.getNotification().getServiceUUID(),
						vfResourceStructure.getResourceInstance().getResourceName(), "", "");
				WatchdogComponentDistributionStatus wdStatus = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
				wdStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
				watchdogCDStatusRepository.saveAndFlush(wdStatus);
			} else {			
				logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
						vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
						vfResourceStructure.getNotification().getServiceName(),
						BigDecimalVersion.castAndCheckNotificationVersionToString(
								vfResourceStructure.getNotification().getServiceVersion()),
						vfResourceStructure.getNotification().getServiceUUID(),
						vfResourceStructure.getResourceInstance().getResourceName(), "", "");
			}
			return status;
		} catch (Exception e) {
			logger.error(MessageEnum.ASDC_ARTIFACT_CHECK_EXC, "", "", MsoLogger.ErrorCode.SchemaError,
					"Exception - isResourceAlreadyDeployed");
			throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
		}
	}

	public void installTheComponentStatus(IStatusData iStatus) throws ArtifactInstallerException {
		logger.debug("Entering installTheComponentStatus for distributionId " + iStatus.getDistributionID()
				+ " and ComponentName " + iStatus.getComponentName());

		try {
			WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(iStatus.getDistributionID(),
					iStatus.getComponentName());
			cdStatus.setComponentDistributionStatus(iStatus.getStatus().toString());
			watchdogCDStatusRepository.save(cdStatus);

		} catch (Exception e) {
			logger.debug("Exception caught in installTheComponentStatus " + e.getMessage());
			throw new ArtifactInstallerException("Exception caught in installTheComponentStatus " + e.getMessage());
		}
	}

	@Transactional(rollbackFor = { ArtifactInstallerException.class })
	public void installTheResource(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStruct)
			throws ArtifactInstallerException {
		VfResourceStructure vfResourceStructure = vfResourceStruct;
		extractHeatInformation(toscaResourceStruct, vfResourceStructure);

		// PCLO: in case of deployment failure, use a string that will represent
		// the type of artifact that failed...
		List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
		try {
			createToscaCsar(toscaResourceStruct);			
			createService(toscaResourceStruct, vfResourceStruct);			
			Service service = toscaResourceStruct.getCatalogService();				

			processResourceSequence(toscaResourceStruct, service);
			processVFResources(toscaResourceStruct, service, vfResourceStructure);
			processAllottedResources(toscaResourceStruct, service);
			processNetworks(toscaResourceStruct, service);	
			// process Network Collections
			processNetworkCollections(toscaResourceStruct, service);
			// Process Service Proxy & Configuration
			processServiceProxyAndConfiguration(toscaResourceStruct, service);
			
			serviceRepo.save(service);
			
			WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
			status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
			watchdogCDStatusRepository.save(status);

			toscaResourceStruct.setSuccessfulDeployment();

		} catch (Exception e) {
			logger.debug("Exception :", e);
			WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
			status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_ERROR.name());
			watchdogCDStatusRepository.save(status);
			Throwable dbExceptionToCapture = e;
			while (!(dbExceptionToCapture instanceof ConstraintViolationException
					|| dbExceptionToCapture instanceof LockAcquisitionException)
					&& (dbExceptionToCapture.getCause() != null)) {
				dbExceptionToCapture = dbExceptionToCapture.getCause();
			}

			if (dbExceptionToCapture instanceof ConstraintViolationException
					|| dbExceptionToCapture instanceof LockAcquisitionException) {
				logger.warn(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED,
						vfResourceStructure.getResourceInstance().getResourceName(),
						vfResourceStructure.getNotification().getServiceVersion(), "", MsoLogger.ErrorCode.DataError, "Exception - ASCDC Artifact already deployed", e);
			} else {
				String elementToLog = (!artifactListForLogging.isEmpty()
						? artifactListForLogging.get(artifactListForLogging.size() - 1).toString()
						: "No element listed");
				logger.error(MessageEnum.ASDC_ARTIFACT_INSTALL_EXC, elementToLog, "", "", MsoLogger.ErrorCode.DataError,
						"Exception caught during installation of "
								+ vfResourceStructure.getResourceInstance().getResourceName()
								+ ". Transaction rollback",
						e);
				throw new ArtifactInstallerException("Exception caught during installation of "
						+ vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
			}
		}
	}


	List<NodeTemplate> getRequirementList(List<NodeTemplate> resultList, List<NodeTemplate> nodeTemplates,
														 ISdcCsarHelper iSdcCsarHelper) {

		List<NodeTemplate> nodes = new ArrayList<NodeTemplate>();
		nodes.addAll(nodeTemplates);

		for (NodeTemplate nodeTemplate : nodeTemplates) {
			RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate);
			List<RequirementAssignment> reqAs = requirement.getAll();
			for (RequirementAssignment ra : reqAs) {
				String reqNode = ra.getNodeTemplateName();
				for (NodeTemplate rNode : resultList) {
					if (rNode.getName().equals(reqNode)) {
						if(!resultList.contains(nodeTemplate)) {
							resultList.add(nodeTemplate);
						}
						if(nodes.contains(nodeTemplate)) {
							nodes.remove(nodeTemplate);
						}
						break;
					}
				}
			}
		}

		if (!nodes.isEmpty()) {
			getRequirementList(resultList, nodes, iSdcCsarHelper);
		}

		return resultList;
	}

	// This method retrieve resource sequence from csar file
	void processResourceSequence(ToscaResourceStructure toscaResourceStructure, Service service) {
		List<String> resouceSequence = new ArrayList<String>();
		List<NodeTemplate> resultList = new ArrayList<NodeTemplate>();

		ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper();
		List<NodeTemplate> nodeTemplates = iSdcCsarHelper.getServiceNodeTemplates();
		List<NodeTemplate> nodes = new ArrayList<NodeTemplate>();
		nodes.addAll(nodeTemplates);

		for (NodeTemplate nodeTemplate : nodeTemplates) {
			RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate);

			if (requirement == null || requirement.getAll() == null || requirement.getAll().isEmpty()) {
				resultList.add(nodeTemplate);
				nodes.remove(nodeTemplate);
			}
		}

		resultList = getRequirementList(resultList, nodes, iSdcCsarHelper);

		for (NodeTemplate node : resultList) {
			String templateName = node.getMetaData().getValue("name");
			if (!resouceSequence.contains(templateName)) {
				resouceSequence.add(templateName);
			}
		}

		String resourceSeqStr = resouceSequence.stream().collect(Collectors.joining(","));
		service.setResourceOrder(resourceSeqStr);
		logger.debug(" resourceSeq for service uuid(" + service.getModelUUID() + ") : " + resourceSeqStr);
	}

	private static String CUSTOMIZATION_UUID = "customizationUUID";

	private static String getValue(Object value, List<Input> servInputs) {
		String output = null;
		if(value instanceof Map) {
			// currently this logic handles only one level of nesting.
			return ((LinkedHashMap) value).values().toArray()[0].toString();
		} else if(value instanceof GetInput) {
			String inputName = ((GetInput)value).getInputName();

			for(Input input : servInputs) {
				if(input.getName().equals(inputName)) {
					// keep both input name and default value
					// if service input does not supplies value the use default value
					String defaultValue = input.getDefault() != null ? (String) input.getDefault() : "";
					output =  inputName + "|" + defaultValue;// return default value
				}
			}

		} else {
			output = value != null ? value.toString() : "";
		}
		return output; // return property value
	}

	String getResourceInput(ToscaResourceStructure toscaResourceStructure, String resourceCustomizationUuid) throws ArtifactInstallerException {
		Map<String, String> resouceRequest = new HashMap<>();
		ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper();

		List<Input> serInput = iSdcCsarHelper.getServiceInputs();
		Optional<NodeTemplate> nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream()
				.filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid)).findFirst();
		if(nodeTemplateOpt.isPresent()) {
			NodeTemplate nodeTemplate = nodeTemplateOpt.get();
			LinkedHashMap<String, Property> resourceProperties = nodeTemplate.getProperties();

			for(String key : resourceProperties.keySet()) {
				Property property = resourceProperties.get(key);

				String value = getValue(property.getValue(), serInput);
				resouceRequest.put(key, value);
			}
		}

		try {
			ObjectMapper objectMapper = new ObjectMapper();
			String jsonStr = objectMapper.writeValueAsString(resouceRequest);

			jsonStr = jsonStr.replace("\"", "\\\"");
			logger.debug("resource request for resource customization id (" + resourceCustomizationUuid + ") : " + jsonStr);
			return jsonStr;
		} catch (JsonProcessingException e) {
			logger.error("resource input could not be deserialized for resource customization id ("
					+ resourceCustomizationUuid + ")");
			throw new ArtifactInstallerException("resource input could not be parsed", e);
		}
	}

    protected void processNetworks (ToscaResourceStructure toscaResourceStruct,
                                    Service service) throws ArtifactInstallerException {
        List <NodeTemplate> nodeTemplatesVLList = toscaResourceStruct.getSdcCsarHelper ().getServiceVlList ();

        if (nodeTemplatesVLList != null) {
            for (NodeTemplate vlNode : nodeTemplatesVLList) {
                String networkResourceModelName = vlNode.getMetaData ().getValue (SdcPropertyNames.PROPERTY_NAME_NAME);

                TempNetworkHeatTemplateLookup tempNetworkLookUp =
                                                                tempNetworkLookupRepo.findFirstBynetworkResourceModelName (networkResourceModelName);

                if (tempNetworkLookUp != null) {
                    HeatTemplate heatTemplate =
                                              heatRepo.findByArtifactUuid (tempNetworkLookUp.getHeatTemplateArtifactUuid ());
                    if (heatTemplate != null) {
                        NetworkResourceCustomization networkCustomization =
                                                                          createNetwork (vlNode,
                                                                                         toscaResourceStruct,
                                                                                         heatTemplate,
                                                                                         tempNetworkLookUp.getAicVersionMax (),
                                                                                         tempNetworkLookUp.getAicVersionMin (),
                                                                                         service);
                        service.getNetworkCustomizations ().add (networkCustomization);
                    } else {
                        throw new ArtifactInstallerException ("No HeatTemplate found for artifactUUID: "
                                                              + tempNetworkLookUp.getHeatTemplateArtifactUuid ());
                    }
                } else {
                    NetworkResourceCustomization networkCustomization = createNetwork (vlNode,
                                                                                       toscaResourceStruct,
                                                                                       null,
                                                                                       null,
                                                                                       null,
                                                                                       service);
					networkCustomization.setResourceInput(getResourceInput(toscaResourceStruct, networkCustomization.getModelCustomizationUUID()));
					service.getNetworkCustomizations().add (networkCustomization);
                    logger.debug ("No NetworkResourceName found in TempNetworkHeatTemplateLookup for "
                                  + networkResourceModelName);
                }

            }
        }
	}

	protected void processAllottedResources(ToscaResourceStructure toscaResourceStruct, Service service) throws ArtifactInstallerException {
		List<NodeTemplate> allottedResourceList = toscaResourceStruct.getSdcCsarHelper().getAllottedResources();
		
		if (allottedResourceList != null) {
			for (NodeTemplate allottedNode : allottedResourceList) {
				AllottedResourceCustomization allottedResource = createAllottedResource(allottedNode, toscaResourceStruct, service);
				allottedResource.setResourceInput(getResourceInput(toscaResourceStruct, allottedResource.getModelCustomizationUUID()));
				service.getAllottedCustomizations().add(allottedResource);
			}
		}
	}
		
	protected void processServiceProxyAndConfiguration(ToscaResourceStructure toscaResourceStruct, Service service) {
		
		List<NodeTemplate> serviceProxyResourceList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.SERVICE_PROXY);
		
		List<NodeTemplate> configurationNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CONFIGURATION);
		
		List<ServiceProxyResourceCustomization> serviceProxyList = new ArrayList<ServiceProxyResourceCustomization>();		
		List<ConfigurationResourceCustomization> configurationResourceList = new ArrayList<ConfigurationResourceCustomization>();
		
		ServiceProxyResourceCustomization serviceProxy = null;
		
		if (serviceProxyResourceList != null) {
			for (NodeTemplate spNode : serviceProxyResourceList) {
				serviceProxy = createServiceProxy(spNode, service, toscaResourceStruct);
				
				ServiceProxyResource serviceProxyResource = findExistingServiceProxyResource(serviceProxyList, serviceProxy.getServiceProxyResource().getModelUUID());
				
				if(serviceProxyResource == null){
				
				serviceProxyList.add(serviceProxy);

				for (NodeTemplate configNode : configurationNodeTemplatesList) {
										
						List<RequirementAssignment> requirementsList = toscaResourceStruct.getSdcCsarHelper().getRequirementsOf(configNode).getAll();
						for (RequirementAssignment requirement :  requirementsList) {
							if (requirement.getNodeTemplateName().equals(spNode.getName())) {
								ConfigurationResourceCustomization configurationResource = createConfiguration(configNode, toscaResourceStruct, serviceProxy);
																
								configurationResourceList.add(configurationResource);
								break;
							}
						}
				}
				
				}
	
			}
		}
		
		service.setConfigurationCustomizations(configurationResourceList);
		service.setServiceProxyCustomizations(serviceProxyList);
	}
	
	protected void processNetworkCollections(ToscaResourceStructure toscaResourceStruct, Service service) {
		
		List<NodeTemplate> networkCollectionList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CR);
		
		if (networkCollectionList != null) {
			for (NodeTemplate crNode : networkCollectionList) {	
				
				createNetworkCollection(crNode, toscaResourceStruct, service);
				collectionRepo.saveAndFlush(toscaResourceStruct.getCatalogCollectionResource());
				
				List<NetworkInstanceGroup> networkInstanceGroupList = toscaResourceStruct.getCatalogNetworkInstanceGroup();
				for(NetworkInstanceGroup networkInstanceGroup : networkInstanceGroupList){
					instanceGroupRepo.saveAndFlush(networkInstanceGroup);
				}
	
			}
		}
		service.getCollectionResourceCustomizations().add(toscaResourceStruct.getCatalogCollectionResourceCustomization());
	}


	protected void processVFResources (ToscaResourceStructure toscaResourceStruct, Service service, VfResourceStructure vfResourceStructure)
			throws Exception{
		logger.debug("processVFResources");
		
		List<NodeTemplate> vfNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceVfList();
//		String servicecategory = toscaResourceStruct.getCatalogService().getCategory();
//		String serviceType = toscaResourceStruct.getCatalogService().getServiceType();
		
		for (NodeTemplate nodeTemplate : vfNodeTemplatesList) {
			Metadata metadata = nodeTemplate.getMetaData();
			String vfCustomizationCategory = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY);
			logger.debug("VF Category is : " + vfCustomizationCategory);
			
			// Do not treat Allotted Resources as VNF resources
			if(ALLOTTED_RESOURCE.equalsIgnoreCase(vfCustomizationCategory)){
				continue;
			}

			String vfCustomizationUUID = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
			logger.debug("VFCustomizationUUID=" + vfCustomizationUUID);

			IResourceInstance vfNotificationResource = vfResourceStructure.getResourceInstance();

			// Make sure the VF ResourceCustomizationUUID from the notification and tosca
			// customizations match before comparing their VF Modules UUID's
			logger.debug("Checking if Notification VF ResourceCustomizationUUID: "
					+ vfNotificationResource.getResourceCustomizationUUID() + " matches Tosca VF Customization UUID: "
					+ vfCustomizationUUID);

			if (vfCustomizationUUID.equals(vfNotificationResource.getResourceCustomizationUUID())) {
				logger.debug("vfCustomizationUUID: " + vfCustomizationUUID
						+ " matches vfNotificationResource CustomizationUUID");				
				
				processVfModules(toscaResourceStruct, vfResourceStructure, service, nodeTemplate, vfCustomizationUUID);
			}
			else {
				logger.debug("Notification VF ResourceCustomizationUUID: "
						+ vfNotificationResource.getResourceCustomizationUUID() + " doesn't match "
						+ "Tosca VF Customization UUID: " + vfCustomizationUUID);
			}
		}
	}
	
	
	protected void processVfModules(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStructure,
			Service service, NodeTemplate nodeTemplate, String vfCustomizationUUID)
			throws Exception {
		logger.debug("processVfModules for vfCustomizationUUID: " + vfCustomizationUUID);
		
		VnfResourceCustomization vnfResource = createVnfResource(nodeTemplate, toscaResourceStruct, service);
		
		if (vfResourceStructure.getVfModuleStructure() != null && !vfResourceStructure.getVfModuleStructure().isEmpty()) {
			Set<CvnfcCustomization> existingCvnfcSet = new HashSet<CvnfcCustomization>();
			Set<VnfcCustomization> existingVnfcSet = new HashSet<VnfcCustomization>();

			for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {

				logger.debug("vfModuleStructure:" + vfModuleStructure.toString());
				List<org.onap.sdc.toscaparser.api.Group> vfGroups = toscaResourceStruct.getSdcCsarHelper()
						.getVfModulesByVf(vfCustomizationUUID);
				IVfModuleData vfMetadata = vfModuleStructure.getVfModuleMetadata();

				logger.debug("Comparing Vf_Modules_Metadata CustomizationUUID : " + vfMetadata.getVfModuleModelCustomizationUUID());

				Optional<org.onap.sdc.toscaparser.api.Group> matchingObject = vfGroups.stream()
						.peek(group -> logger.debug("To Csar Group VFModuleModelCustomizationUUID "	+ group.getMetadata().getValue("vfModuleModelCustomizationUUID")))
						.filter(group -> group.getMetadata().getValue("vfModuleModelCustomizationUUID").equals(vfMetadata.getVfModuleModelCustomizationUUID()))
						.findFirst();
				if (matchingObject.isPresent()) {
					VfModuleCustomization vfModuleCustomization = createVFModuleResource(matchingObject.get(), nodeTemplate, toscaResourceStruct, 
							                             vfResourceStructure, vfMetadata, vnfResource,service, existingCvnfcSet, existingVnfcSet);
					vfModuleCustomization.getVfModule().setVnfResources(vnfResource.getVnfResources());
				} else {
					throw new Exception("Cannot find matching VFModule Customization in Csar for Vf_Modules_Metadata: " + vfMetadata.getVfModuleModelCustomizationUUID());
				}
			}
		}

		vnfResource.setResourceInput(getResourceInput(toscaResourceStruct, vnfResource.getModelCustomizationUUID()));
		service.getVnfCustomizations().add(vnfResource);
	}

	public void processWatchdog(String distributionId, String servideUUID) {
		WatchdogServiceModVerIdLookup modVerIdLookup = new WatchdogServiceModVerIdLookup(distributionId,servideUUID);
		watchdogModVerIdLookupRepository.saveAndFlush(modVerIdLookup);
		
		WatchdogDistributionStatus distributionStatus = new WatchdogDistributionStatus(distributionId);
		watchdogDistributionStatusRepository.saveAndFlush(distributionStatus);
	}

	protected void extractHeatInformation(ToscaResourceStructure toscaResourceStruct,
			VfResourceStructure vfResourceStructure) {
		for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {

			switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
			case ASDCConfiguration.HEAT:
			case ASDCConfiguration.HEAT_NESTED:
				createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
						vfModuleArtifact);
				break;
			case ASDCConfiguration.HEAT_VOL:
				createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
						vfModuleArtifact);
				VfModuleArtifact envModuleArtifact = getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
				createHeatEnvFromArtifact(vfResourceStructure, envModuleArtifact);
				break;
			case ASDCConfiguration.HEAT_ENV:
				createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
				break;
			case ASDCConfiguration.HEAT_ARTIFACT:
				createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact,
						toscaResourceStruct);
				break;
			case ASDCConfiguration.HEAT_NET:
			case ASDCConfiguration.OTHER:
				logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT,
						vfModuleArtifact.getArtifactInfo().getArtifactType() + "(Artifact Name:"
								+ vfModuleArtifact.getArtifactInfo().getArtifactName() + ")",
						"", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
				break;
			default:
				break;

			}
		}
	}

	protected VfModuleArtifact getHeatEnvArtifactFromGeneratedArtifact(VfResourceStructure vfResourceStructure,
			VfModuleArtifact vfModuleArtifact) {
		String artifactName = vfModuleArtifact.getArtifactInfo().getArtifactName();
		artifactName = artifactName.substring(0, artifactName.indexOf('.'));
		for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
			if (moduleArtifact.getArtifactInfo().getArtifactName().contains(artifactName)
					&& moduleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
				return moduleArtifact;
			}
		}
		return null;
	}

	public String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure,
			List<String> listTypes) {
		String newFileBody = filebody;
		for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {

			if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {

				newFileBody = verifyTheFilePrefixInString(newFileBody,
						moduleArtifact.getArtifactInfo().getArtifactName());
			}
		}
		return newFileBody;
	}

	public String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {

		String needlePrefix = "file:///";
		String prefixedFilenameToVerify = needlePrefix + filenameToVerify;

		if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
			return body;
		}

		StringBuilder sb = new StringBuilder(body.length());

		int currentIndex = 0;
		int startIndex = 0;

		while (currentIndex != -1) {
			startIndex = currentIndex;
			currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);

			if (currentIndex == -1) {
				break;
			}
			// We append from the startIndex up to currentIndex (start of File
			// Name)
			sb.append(body.substring(startIndex, currentIndex));
			sb.append(filenameToVerify);

			currentIndex += prefixedFilenameToVerify.length();
		}

		sb.append(body.substring(startIndex));

		return sb.toString();
	}

	protected void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
			ToscaResourceStructure toscaResourceStruct, VfModuleArtifact vfModuleArtifact) {
		HeatTemplate heatTemplate = new HeatTemplate();
		List<String> typeList = new ArrayList<>();
		typeList.add(ASDCConfiguration.HEAT_NESTED);
		typeList.add(ASDCConfiguration.HEAT_ARTIFACT);

		heatTemplate.setTemplateBody(
				verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
		heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());

		if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
			heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
		} else {
			heatTemplate.setTimeoutMinutes(240);
		}

		heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
		heatTemplate.setVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
		heatTemplate.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());

		if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
			heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
		} else {
			heatTemplate.setArtifactChecksum(MANUAL_RECORD);
		}

		Set<HeatTemplateParam> heatParam = extractHeatTemplateParameters(
				vfModuleArtifact.getResult(), vfModuleArtifact.getArtifactInfo().getArtifactUUID());
		heatTemplate.setParameters(heatParam);	
		vfModuleArtifact.setHeatTemplate(heatTemplate);
	}

	protected void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
			VfModuleArtifact vfModuleArtifact) {
		HeatEnvironment heatEnvironment = new HeatEnvironment();
		heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
		List<String> typeList = new ArrayList<>();
		typeList.add(ASDCConfiguration.HEAT);
		typeList.add(ASDCConfiguration.HEAT_VOL);
		heatEnvironment.setEnvironment(
				verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
		heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
		heatEnvironment.setVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));	
		heatEnvironment.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());

		if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
			heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
		} else {
			heatEnvironment.setArtifactChecksum(MANUAL_RECORD);
		}		
		vfModuleArtifact.setHeatEnvironment(heatEnvironment);
	}

	protected void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
		VfModuleArtifact vfModuleArtifact, ToscaResourceStructure toscaResourceStruct) {
		
		HeatFiles heatFile = new HeatFiles();	
		heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
		heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
		heatFile.setFileBody(vfModuleArtifact.getResult());
		heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
		heatFile.setVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
		toscaResourceStruct.setHeatFilesUUID(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
		if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
			heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
		} else {
			heatFile.setArtifactChecksum(MANUAL_RECORD);
		}
		vfModuleArtifact.setHeatFiles(heatFile);
	}

	protected Service createService(ToscaResourceStructure toscaResourceStructure,
			VfResourceStructure vfResourceStructure) {

		toscaResourceStructure.getServiceMetadata();

		Metadata serviceMetadata = toscaResourceStructure.getServiceMetadata();

		Service service = new Service();

		if (serviceMetadata != null) {

			if (toscaResourceStructure.getServiceVersion() != null) {
				service.setModelVersion(toscaResourceStructure.getServiceVersion());
			}

			service.setServiceType(serviceMetadata.getValue("serviceType"));
			service.setServiceRole(serviceMetadata.getValue("serviceRole"));

			service.setDescription(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
			service.setModelName(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
			service.setModelUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			service.setEnvironmentContext(serviceMetadata.getValue("environmentContext"));

			if (vfResourceStructure != null) 
				service.setWorkloadContext(vfResourceStructure.getNotification().getWorkloadContext());
						
			service.setModelInvariantUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
			service.setCsar(toscaResourceStructure.getCatalogToscaCsar());			
		}
		
		
		toscaResourceStructure.setCatalogService(service); 
		return service;
	}
	
	protected ServiceProxyResourceCustomization createServiceProxy(NodeTemplate nodeTemplate, Service service, ToscaResourceStructure toscaResourceStructure) {

		Metadata spMetadata = nodeTemplate.getMetaData();
		
		ServiceProxyResource spResource = new ServiceProxyResource();
		
		spResource.setModelName(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
		spResource.setModelInvariantUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
		spResource.setModelUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
		spResource.setModelVersion(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
		spResource.setDescription(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));	
		
		ServiceProxyResourceCustomization spCustomizationResource = new ServiceProxyResourceCustomization();
		
		Set<ServiceProxyResourceCustomization> serviceProxyCustomizationSet = new HashSet<>();
		
		spCustomizationResource.setModelCustomizationUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		spCustomizationResource.setModelInstanceName(nodeTemplate.getName());
		spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
		spCustomizationResource.setSourceService(service);
		spCustomizationResource.setServiceProxyResource(spResource);
		spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
		spCustomizationResource.setServiceProxyResource(spResource);
		serviceProxyCustomizationSet.add(spCustomizationResource);

		toscaResourceStructure.setCatalogServiceProxyResource(spResource);
		
		toscaResourceStructure.setCatalogServiceProxyResourceCustomization(spCustomizationResource);
		
		return spCustomizationResource;
	}
	
	protected ConfigurationResourceCustomization createConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization) {

		Metadata metadata = nodeTemplate.getMetaData();
		
		ConfigurationResource configResource = new ConfigurationResource();
		
		configResource.setModelName(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
		configResource.setModelInvariantUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
		configResource.setModelUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
		configResource.setModelVersion(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
		configResource.setDescription(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
		configResource.setToscaNodeType(nodeTemplate.getType());
		
		ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
		
		Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
		
		configCustomizationResource.setModelCustomizationUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
		
		configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
		configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
		configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
		configCustomizationResource.setServiceProxyResourceCustomization(spResourceCustomization);
		configCustomizationResource.setConfigResourceCustomization(configCustomizationResource);
		configCustomizationResource.setConfigurationResource(configResource);
		configResourceCustomizationSet.add(configCustomizationResource);

		configResource.setConfigurationResourceCustomization(configResourceCustomizationSet); 	
		
		toscaResourceStructure.setCatalogConfigurationResource(configResource);
		
		toscaResourceStructure.setCatalogConfigurationResourceCustomization(configCustomizationResource);
		
		return configCustomizationResource;
	}
	
	protected ConfigurationResourceCustomization createFabricConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure) {
		
		Metadata fabricMetadata = nodeTemplate.getMetaData();
		
		ConfigurationResource configResource = new ConfigurationResource();
		
		configResource.setModelName(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
		configResource.setModelInvariantUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
		configResource.setModelUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
		configResource.setModelVersion(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
		configResource.setDescription(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
		configResource.setToscaNodeType(nodeTemplate.getType());
		
		ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
		
		Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
		
		configCustomizationResource.setModelCustomizationUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
		
		configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "function"));
		configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "role"));
		configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "type"));
		configCustomizationResource.setConfigResourceCustomization(configCustomizationResource);
		configCustomizationResource.setConfigurationResource(configResource);
		configResourceCustomizationSet.add(configCustomizationResource);

		configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);
		
		return configCustomizationResource;
	}

	protected void createToscaCsar(ToscaResourceStructure toscaResourceStructure) {
		ToscaCsar toscaCsar = new ToscaCsar();
		if (toscaResourceStructure.getToscaArtifact().getArtifactChecksum() != null) {
			toscaCsar.setArtifactChecksum(toscaResourceStructure.getToscaArtifact().getArtifactChecksum());
		} else {
			toscaCsar.setArtifactChecksum(MANUAL_RECORD);
		}
		toscaCsar.setArtifactUUID(toscaResourceStructure.getToscaArtifact().getArtifactUUID());
		toscaCsar.setName(toscaResourceStructure.getToscaArtifact().getArtifactName());
		toscaCsar.setVersion(toscaResourceStructure.getToscaArtifact().getArtifactVersion());
		toscaCsar.setDescription(toscaResourceStructure.getToscaArtifact().getArtifactDescription());
		toscaCsar.setUrl(toscaResourceStructure.getToscaArtifact().getArtifactURL());

		toscaResourceStructure.setCatalogToscaCsar(toscaCsar);
	}
	
	protected VnfcCustomization findExistingVfc(Set<VnfcCustomization> vnfcCustomizations, String customizationUUID) {
		VnfcCustomization vnfcCustomization = null;
		for(VnfcCustomization vnfcCustom : vnfcCustomizations){
			if (vnfcCustom != null && vnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
				vnfcCustomization = vnfcCustom;
			}
		}
		
		if(vnfcCustomization==null)
			vnfcCustomization = vnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
		
		return vnfcCustomization;
	}
	
	protected CvnfcCustomization findExistingCvfc(Set<CvnfcCustomization> cvnfcCustomizations, String customizationUUID) {
		CvnfcCustomization cvnfcCustomization = null;
		for(CvnfcCustomization cvnfcCustom : cvnfcCustomizations){
			if (cvnfcCustom != null && cvnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
				cvnfcCustomization = cvnfcCustom;
			}
		}
		
		if(cvnfcCustomization==null)
			cvnfcCustomization = cvnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
		
		return cvnfcCustomization;
	}

	protected  NetworkResourceCustomization createNetwork(NodeTemplate networkNodeTemplate,
			ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin,Service service) {
		
		NetworkResourceCustomization networkResourceCustomization=networkCustomizationRepo.findOneByModelCustomizationUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
				if(networkResourceCustomization==null){
			networkResourceCustomization = createNetworkResourceCustomization(networkNodeTemplate,
					toscaResourceStructure);
					
			NetworkResource networkResource = findExistingNetworkResource(service,
					networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
					if(networkResource == null)
				networkResource = createNetworkResource(networkNodeTemplate, toscaResourceStructure, heatTemplate,
						aicMax, aicMin);

					networkResource.addNetworkResourceCustomization(networkResourceCustomization);		
					networkResourceCustomization.setNetworkResource(networkResource);
				}
		return networkResourceCustomization;
	}
	
	protected  NetworkResource findExistingNetworkResource(Service service, String modelUUID) {
		NetworkResource networkResource = null;
		for(NetworkResourceCustomization networkCustom : service.getNetworkCustomizations()){
			if (networkCustom.getNetworkResource() != null
					&& networkCustom.getNetworkResource().getModelUUID().equals(modelUUID)) {
					networkResource = networkCustom.getNetworkResource();
			}
		}
		if(networkResource==null)
			networkResource = networkRepo.findResourceByModelUUID(modelUUID);
		
		return networkResource;
	}
	
	protected NetworkResourceCustomization createNetworkResourceCustomization(NodeTemplate networkNodeTemplate,
			ToscaResourceStructure toscaResourceStructure) {
		NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
		networkResourceCustomization.setModelInstanceName(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
		networkResourceCustomization.setModelCustomizationUUID(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));		

		networkResourceCustomization.setNetworkTechnology(
				testNull(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
						SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY)));
		networkResourceCustomization.setNetworkType(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE)));
		networkResourceCustomization.setNetworkRole(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE)));
		networkResourceCustomization.setNetworkScope(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE)));
		return networkResourceCustomization;
	}

	protected NetworkResource createNetworkResource(NodeTemplate networkNodeTemplate,
			ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin) {
		NetworkResource networkResource = new NetworkResource();
		String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
				networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);

		if ("true".equalsIgnoreCase(providerNetwork)) {
			networkResource.setNeutronNetworkType(PROVIDER);
		} else {
			networkResource.setNeutronNetworkType(BASIC);
		}

		networkResource.setModelName(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));

		networkResource.setModelInvariantUUID(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
		networkResource.setModelUUID(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
		networkResource.setModelVersion(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));

		networkResource.setAicVersionMax(aicMax);		
		networkResource.setAicVersionMin(aicMin);
		networkResource.setToscaNodeType(networkNodeTemplate.getType());
		networkResource.setDescription(
				testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
		networkResource.setOrchestrationMode(HEAT);	
		networkResource.setHeatTemplate(heatTemplate); 
		return networkResource;
	}
	
	protected  CollectionNetworkResourceCustomization createNetworkCollection(NodeTemplate networkNodeTemplate,
			ToscaResourceStructure toscaResourceStructure, Service service) {

		CollectionNetworkResourceCustomization collectionNetworkResourceCustomization = new CollectionNetworkResourceCustomization();

		// **** Build Object to populate Collection_Resource table
		CollectionResource collectionResource = new CollectionResource();

		collectionResource
				.setModelName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
		collectionResource.setModelInvariantUUID(
				networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
		collectionResource
				.setModelUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
		collectionResource
				.setModelVersion(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
		collectionResource
				.setDescription(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
		collectionResource.setToscaNodeType(networkNodeTemplate.getType());

		toscaResourceStructure.setCatalogCollectionResource(collectionResource);

		// **** Build object to populate Collection_Resource_Customization table
		NetworkCollectionResourceCustomization ncfc = new NetworkCollectionResourceCustomization();
		
		ncfc.setFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
				"cr_function"));
		ncfc.setRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
				"cr_role"));
		ncfc.setType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
				"cr_type"));

		ncfc.setModelInstanceName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
		ncfc.setModelCustomizationUUID(
				networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		
		Set<CollectionNetworkResourceCustomization> networkResourceCustomizationSet = new HashSet<>();
		networkResourceCustomizationSet.add(collectionNetworkResourceCustomization);

		ncfc.setNetworkResourceCustomization(networkResourceCustomizationSet);

		ncfc.setCollectionResource(collectionResource);
		toscaResourceStructure.setCatalogCollectionResourceCustomization(ncfc);
		
		//*** Build object to populate the Instance_Group table
		List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
				.getGroupsOfOriginOfNodeTemplateByToscaGroupType(networkNodeTemplate,
						"org.openecomp.groups.NetworkCollection");
		
		List<NetworkInstanceGroup> networkInstanceGroupList = new ArrayList<>();

		List<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationList = new ArrayList<CollectionResourceInstanceGroupCustomization>();

		for (Group group : groupList) { 
			
			NetworkInstanceGroup networkInstanceGroup = new NetworkInstanceGroup();
			Metadata instanceMetadata = group.getMetadata();
			networkInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
			networkInstanceGroup
					.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
			networkInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			networkInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
			networkInstanceGroup.setToscaNodeType(group.getType());
			networkInstanceGroup.setRole(SubType.SUB_INTERFACE.toString()); // Set
																			// Role
			networkInstanceGroup.setType(InstanceGroupType.L3_NETWORK); // Set
																		// type
			networkInstanceGroup.setCollectionResource(collectionResource);
		
			// ****Build object to populate
			// Collection_Resource_Instance_Group_Customization table
			CollectionResourceInstanceGroupCustomization crInstanceGroupCustomization = new CollectionResourceInstanceGroupCustomization();
			crInstanceGroupCustomization.setInstanceGroup(networkInstanceGroup);
			crInstanceGroupCustomization.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			crInstanceGroupCustomization.setModelCustomizationUUID(
					networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		
			String quantityName = instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
			String fixedQuantity = quantityName.replace("NetworkCollection", "Fixed");
			if (toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
					fixedQuantity + "_quantity") != null) {

				crInstanceGroupCustomization.setSubInterfaceNetworkQuantity(Integer.parseInt(
						toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
								fixedQuantity + "_quantity"))); 
			}
		
			crInstanceGroupCustomization.setDescription(
					toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
							instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
									+ "_network_collection_description"));
			crInstanceGroupCustomization.setFunction(
					toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
							instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
									+ "_network_collection_function"));
			crInstanceGroupCustomization.setCollectionResourceCust(ncfc);
			collectionResourceInstanceGroupCustomizationList.add(crInstanceGroupCustomization);

			networkInstanceGroup
					.setCollectionInstanceGroupCustomizations(collectionResourceInstanceGroupCustomizationList);

			networkInstanceGroupList.add(networkInstanceGroup);

		//}

		toscaResourceStructure.setCatalogNetworkInstanceGroup(networkInstanceGroupList);

		List<NodeTemplate> vlNodeList = toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplateBySdcType(networkNodeTemplate, SdcTypes.VL);
		
		List<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationList = new ArrayList<>();
		
		//*****Build object to populate the NetworkResource table
		NetworkResource networkResource = new NetworkResource();
		
		for(NodeTemplate vlNodeTemplate : vlNodeList){

			String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
					vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);

			if ("true".equalsIgnoreCase(providerNetwork)) {
				networkResource.setNeutronNetworkType(PROVIDER);
			} else {
				networkResource.setNeutronNetworkType(BASIC);
			}

			networkResource.setModelName(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));

			networkResource.setModelInvariantUUID(
					vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
			networkResource.setModelUUID(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			networkResource
					.setModelVersion(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));

			networkResource.setAicVersionMax(
					vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES));
			
			TempNetworkHeatTemplateLookup tempNetworkLookUp = tempNetworkLookupRepo.findFirstBynetworkResourceModelName(
					vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
			
			if (tempNetworkLookUp != null ) {	
					
				HeatTemplate heatTemplate = heatRepo
						.findByArtifactUuid(tempNetworkLookUp.getHeatTemplateArtifactUuid());
					networkResource.setHeatTemplate(heatTemplate);
					
					networkResource.setAicVersionMin(tempNetworkLookUp.getAicVersionMin());
					
			}

			networkResource.setToscaNodeType(vlNodeTemplate.getType());
			networkResource
					.setDescription(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
			networkResource.setOrchestrationMode(HEAT);
			
			// Build object to populate the
			// Collection_Network_Resource_Customization table
			for (NodeTemplate memberNode : group.getMemberNodes()) {
				collectionNetworkResourceCustomization.setModelInstanceName(memberNode.getName());
			}

			collectionNetworkResourceCustomization.setModelCustomizationUUID(
					vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));

			collectionNetworkResourceCustomization.setNetworkTechnology(
					toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vlNodeTemplate,
							SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY));
			collectionNetworkResourceCustomization.setNetworkType(toscaResourceStructure.getSdcCsarHelper()
					.getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE));
			collectionNetworkResourceCustomization.setNetworkRole(toscaResourceStructure.getSdcCsarHelper()
					.getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE));
			collectionNetworkResourceCustomization.setNetworkScope(toscaResourceStructure.getSdcCsarHelper()
					.getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE));
			collectionNetworkResourceCustomization.setInstanceGroup(networkInstanceGroup);
			collectionNetworkResourceCustomization.setNetworkResource(networkResource);
			collectionNetworkResourceCustomization.setNetworkResourceCustomization(ncfc);
			
			collectionNetworkResourceCustomizationList.add(collectionNetworkResourceCustomization);
		   }

		}
		
		return collectionNetworkResourceCustomization;
	}
	
	protected VnfcInstanceGroupCustomization createVNFCInstanceGroup(NodeTemplate vnfcNodeTemplate, Group group,
			VnfResourceCustomization vnfResourceCustomization) {

			Metadata instanceMetadata = group.getMetadata();
			// Populate InstanceGroup
			VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup();
			
			vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
			vfcInstanceGroup.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
			vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
			vfcInstanceGroup.setToscaNodeType(group.getType());
			vfcInstanceGroup.setRole("SUB-INTERFACE");   // Set Role
			vfcInstanceGroup.setType(InstanceGroupType.VNFC);  // Set type	
			
			//Populate VNFCInstanceGroupCustomization
			VnfcInstanceGroupCustomization vfcInstanceGroupCustom = new VnfcInstanceGroupCustomization();
			
			vfcInstanceGroupCustom.setModelCustomizationUUID(vnfResourceCustomization.getModelCustomizationUUID());
			vfcInstanceGroupCustom.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			vfcInstanceGroupCustom.setDescription(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
			vfcInstanceGroupCustom.setFunction("FUNCTION");
			vfcInstanceGroupCustom.setInstanceGroup(vfcInstanceGroup);
			vfcInstanceGroupCustom.setVnfResourceCust(vnfResourceCustomization);		
			
		return vfcInstanceGroupCustom;

	}
		
	protected VfModuleCustomization createVFModuleResource(Group group, NodeTemplate nodeTemplate,
			ToscaResourceStructure toscaResourceStructure, VfResourceStructure vfResourceStructure,
			IVfModuleData vfModuleData, VnfResourceCustomization vnfResource, Service service, Set<CvnfcCustomization> existingCvnfcSet, Set<VnfcCustomization> existingVnfcSet) {
		
		VfModuleCustomization vfModuleCustomization = findExistingVfModuleCustomization(vnfResource,
				vfModuleData.getVfModuleModelCustomizationUUID());
		if(vfModuleCustomization == null){		
			VfModule vfModule = findExistingVfModule(vnfResource,
					nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
			Metadata vfMetadata = group.getMetadata();
			if(vfModule==null)
				vfModule=createVfModule(group, toscaResourceStructure, vfModuleData, vfMetadata);
			
			vfModuleCustomization = createVfModuleCustomization(group, toscaResourceStructure, vfModule, vfModuleData);
			setHeatInformationForVfModule(toscaResourceStructure, vfResourceStructure, vfModule, vfModuleCustomization,
					vfMetadata);
			vfModuleCustomization.setVfModule(vfModule);
			vfModule.getVfModuleCustomization().add(vfModuleCustomization);
			vnfResource.getVfModuleCustomizations().add(vfModuleCustomization);
		} else {
			vfResourceStructure.setAlreadyDeployed(true);
		}
		
		//******************************************************************************************************************
		//* Extract VFC's and CVFC's then add them to VFModule
		//******************************************************************************************************************
		
		Set<VnfVfmoduleCvnfcConfigurationCustomization> vnfVfmoduleCvnfcConfigurationCustomizations = new HashSet<VnfVfmoduleCvnfcConfigurationCustomization>();		
		Set<CvnfcCustomization> cvnfcCustomizations = new HashSet<CvnfcCustomization>();
		Set<VnfcCustomization> vnfcCustomizations = new HashSet<VnfcCustomization>();

		// Extract CVFC lists
		List<NodeTemplate> cvfcList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(nodeTemplate, SdcTypes.CVFC);
						
		for(NodeTemplate cvfcTemplate : cvfcList) {
									
			CvnfcCustomization existingCvnfcCustomization = findExistingCvfc(existingCvnfcSet, cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
			
			if(existingCvnfcCustomization == null){
			
			//Extract associated VFC - Should always be just one
			List<NodeTemplate> vfcList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(cvfcTemplate, SdcTypes.VFC);
						
			for(NodeTemplate vfcTemplate : vfcList) {
				
				VnfcCustomization vnfcCustomization = new VnfcCustomization();
				VnfcCustomization existingVnfcCustomization = null;
				
				existingVnfcCustomization = findExistingVfc(existingVnfcSet, vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
					
				// Only Add Abstract VNFC's to our DB, ignore all others
				if(existingVnfcCustomization == null && vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY).equalsIgnoreCase("Abstract")){
					vnfcCustomization.setModelCustomizationUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
					vnfcCustomization.setModelInstanceName(vfcTemplate.getName());
					vnfcCustomization.setModelInvariantUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
					vnfcCustomization.setModelName(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
					vnfcCustomization.setModelUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
	
					vnfcCustomization.setModelVersion(
							testNull(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
					vnfcCustomization.setDescription(
							testNull(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
					vnfcCustomization.setToscaNodeType(testNull(vfcTemplate.getType()));
					
					vnfcCustomizations.add(vnfcCustomization);
					existingVnfcSet.add(vnfcCustomization);
				}
			
			// This check is needed incase the VFC subcategory is something other than Abstract.  In that case we want to skip adding that record to our DB.
			if(vnfcCustomization.getModelCustomizationUUID() != null){
				
				CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
				cvnfcCustomization.setModelCustomizationUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
				cvnfcCustomization.setModelInstanceName(cvfcTemplate.getName());
				cvnfcCustomization.setModelInvariantUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
				cvnfcCustomization.setModelName(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
				cvnfcCustomization.setModelUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
	
				cvnfcCustomization.setModelVersion(
						testNull(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
				cvnfcCustomization.setDescription(
						testNull(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
				cvnfcCustomization.setToscaNodeType(testNull(cvfcTemplate.getType()));
				
				if(existingVnfcCustomization != null){
					cvnfcCustomization.setVnfcCustomization(existingVnfcCustomization);
				}else{
					cvnfcCustomization.setVnfcCustomization(vnfcCustomization);
				}
				
				cvnfcCustomization.setVfModuleCustomization(vfModuleCustomization);
				cvnfcCustomization.setVnfResourceCustomization(vnfResource);					
				
				cvnfcCustomizations.add(cvnfcCustomization);
				existingCvnfcSet.add(cvnfcCustomization);
			
			//*****************************************************************************************************************************************
			//* Extract Fabric Configuration
			//*****************************************************************************************************************************************
			
			List<NodeTemplate> fabricConfigList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(nodeTemplate, SdcTypes.CONFIGURATION);
								
			for(NodeTemplate fabricTemplate : fabricConfigList) {
										
				ConfigurationResource fabricConfig = null;
				
				ConfigurationResource existingConfig = findExistingConfiguration(service, fabricTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
								
				if(existingConfig == null){
					
					ConfigurationResourceCustomization fabricConfigCustomization = createFabricConfiguration(fabricTemplate, toscaResourceStructure);
					
					fabricConfig = fabricConfigCustomization.getConfigurationResource();
					
					service.getConfigurationCustomizations().add(fabricConfigCustomization);
				}else {
					fabricConfig = existingConfig;
				}
				
				
				VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization = createVfCnvfConfigCustomization(fabricTemplate, toscaResourceStructure, 
																			   vnfResource, vfModuleCustomization, cvnfcCustomization, fabricConfig);
				
				vnfVfmoduleCvnfcConfigurationCustomizations.add(vnfVfmoduleCvnfcConfigurationCustomization);
			}
			
			}
			
		   }
			
		  }
			
		} 
		
		vfModuleCustomization.setCvnfcCustomization(cvnfcCustomizations);
		vfModuleCustomization.setVnfVfmoduleCvnfcConfigurationCustomization(vnfVfmoduleCvnfcConfigurationCustomizations);
		
		return vfModuleCustomization;
	}
	
	protected VnfVfmoduleCvnfcConfigurationCustomization createVfCnvfConfigCustomization(NodeTemplate fabricTemplate, ToscaResourceStructure toscaResourceStruct, 
            VnfResourceCustomization vnfResource, VfModuleCustomization vfModuleCustomization, CvnfcCustomization cvnfcCustomization,
            ConfigurationResource configResource) {

		Metadata fabricMetadata = fabricTemplate.getMetaData();
		
		
		VnfVfmoduleCvnfcConfigurationCustomization vfModuleToCvnfc = new VnfVfmoduleCvnfcConfigurationCustomization();
		
		vfModuleToCvnfc.setConfigurationResource(configResource);
		vfModuleToCvnfc.setCvnfcCustomization(cvnfcCustomization);
		vfModuleToCvnfc.setModelCustomizationUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		vfModuleToCvnfc.setModelInstanceName(fabricTemplate.getName());
		vfModuleToCvnfc.setVfModuleCustomization(vfModuleCustomization);
		vfModuleToCvnfc.setVnfResourceCustomization(vnfResource);
		vfModuleToCvnfc.setPolicyName(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, "name"));
		
		List<Policy> policyList = toscaResourceStruct.getSdcCsarHelper().getPoliciesOfTarget(fabricTemplate);
		
		if(policyList != null){
			for(Policy policy : policyList){
				vfModuleToCvnfc.setPolicyName(policy.getName());
			}
		}
		
		vfModuleToCvnfc.setConfigurationFunction(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
		vfModuleToCvnfc.setConfigurationRole(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
		vfModuleToCvnfc.setConfigurationType(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
		
		return vfModuleToCvnfc;
	}
		
	protected ConfigurationResource findExistingConfiguration(Service service, String modelUUID) {
		ConfigurationResource configResource = null;
		for(ConfigurationResourceCustomization configurationResourceCustom : service.getConfigurationCustomizations()){
			if (configurationResourceCustom.getConfigurationResource() != null
					&& configurationResourceCustom.getConfigurationResource().getModelUUID().equals(modelUUID)) {
				configResource = configurationResourceCustom.getConfigurationResource();
			}
		}
		if(configResource==null)
			configResource = configRepo.findResourceByModelUUID(modelUUID);
		
		return configResource;
	}
	
	protected ServiceProxyResource findExistingServiceProxyResource(List<ServiceProxyResourceCustomization> serviceProxyList, String modelUUID) {
		ServiceProxyResource serviceProxyResource = null;
		for(ServiceProxyResourceCustomization serviceProxyResourceCustom : serviceProxyList){
			if (serviceProxyResourceCustom.getServiceProxyResource() != null
					&& serviceProxyResourceCustom.getServiceProxyResource().getModelUUID().equals(modelUUID)) {
				serviceProxyResource = serviceProxyResourceCustom.getServiceProxyResource();
			}
		}
		if(serviceProxyResource==null)
			serviceProxyResource = serviceProxyRepo.findResourceByModelUUID(modelUUID);
		
		return serviceProxyResource;
	}
	
	protected VfModuleCustomization findExistingVfModuleCustomization(VnfResourceCustomization vnfResource,
			String vfModuleModelCustomizationUUID) {
		VfModuleCustomization vfModuleCustomization = null;
		for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
			if(vfModuleCustom.getModelCustomizationUUID().equalsIgnoreCase(vfModuleModelCustomizationUUID)){
				vfModuleCustomization = vfModuleCustom;
			}
		}
		if(vfModuleCustomization==null)
			vfModuleCustomization = vfModuleCustomizationRepo
					.findByModelCustomizationUUID(vfModuleModelCustomizationUUID);
		
		return vfModuleCustomization;
	}

	protected VfModule findExistingVfModule(VnfResourceCustomization vnfResource, String modelUUID) {
		VfModule vfModule = null;
		for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
			if(vfModuleCustom.getVfModule() != null && vfModuleCustom.getVfModule().getModelUUID().equals(modelUUID)){
				vfModule = vfModuleCustom.getVfModule();
			}
		}
		if(vfModule==null)
			vfModule = vfModuleRepo.findByModelUUID(modelUUID);
		
		return vfModule;
	}

	protected VfModuleCustomization createVfModuleCustomization(Group group,
			ToscaResourceStructure toscaResourceStructure, VfModule vfModule, IVfModuleData vfModuleData) {
		VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
		
		vfModuleCustomization.setModelCustomizationUUID(vfModuleData.getVfModuleModelCustomizationUUID());

		vfModuleCustomization.setVfModule(vfModule);

		String initialCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT);
		if (initialCount != null && initialCount.length() > 0) {
			vfModuleCustomization.setInitialCount(Integer.valueOf(initialCount));
		}

		vfModuleCustomization.setInitialCount(Integer.valueOf(toscaResourceStructure.getSdcCsarHelper()
				.getGroupPropertyLeafValue(group, SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT)));

		String availabilityZoneCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT);
		if (availabilityZoneCount != null && availabilityZoneCount.length() > 0) {
			vfModuleCustomization.setAvailabilityZoneCount(Integer.valueOf(availabilityZoneCount));
		}

		vfModuleCustomization.setLabel(toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_VFMODULELABEL));

		String maxInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_MAXVFMODULEINSTANCES);
		if (maxInstances != null && maxInstances.length() > 0) {
			vfModuleCustomization.setMaxInstances(Integer.valueOf(maxInstances));
		}

		String minInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_MINVFMODULEINSTANCES);
		if (minInstances != null && minInstances.length() > 0) {
			vfModuleCustomization.setMinInstances(Integer.valueOf(minInstances));
		}
		return vfModuleCustomization;
	}

	protected VfModule createVfModule(Group group, ToscaResourceStructure toscaResourceStructure,
			IVfModuleData vfModuleData, Metadata vfMetadata) {
		VfModule vfModule = new VfModule();
		String vfModuleModelUUID = vfModuleData.getVfModuleModelUUID();

		if(vfModuleModelUUID == null) {
			vfModuleModelUUID = testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
					SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
		} else if (vfModuleModelUUID.indexOf('.') > -1) {
			vfModuleModelUUID = vfModuleModelUUID.substring(0, vfModuleModelUUID.indexOf('.'));
		}

		vfModule.setModelInvariantUUID(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getMetadataPropertyValue(vfMetadata, SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)));
		vfModule.setModelName(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
				SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELNAME)));
		vfModule.setModelUUID(vfModuleModelUUID);
		vfModule.setModelVersion(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
				SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELVERSION)));
		vfModule.setDescription(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
				SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));

		String vfModuleType = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
				SdcPropertyNames.PROPERTY_NAME_VFMODULETYPE);
		if (vfModuleType != null && "Base".equalsIgnoreCase(vfModuleType)) {
			vfModule.setIsBase(true);
		} else {
			vfModule.setIsBase(false);
		}
		return vfModule;
	}

	protected void setHeatInformationForVfModule(ToscaResourceStructure toscaResourceStructure,
			VfResourceStructure vfResourceStructure, VfModule vfModule, VfModuleCustomization vfModuleCustomization,
			Metadata vfMetadata) {
		
		Optional<VfModuleStructure> matchingObject = vfResourceStructure.getVfModuleStructure().stream()
				.filter(vfModuleStruct -> vfModuleStruct.getVfModuleMetadata().getVfModuleModelUUID()
						.equalsIgnoreCase(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
								SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID)))
				.findFirst();

		if (matchingObject.isPresent()) {
			List<HeatFiles> heatFilesList = new ArrayList<>();
			List<HeatTemplate> volumeHeatChildTemplates = new ArrayList<HeatTemplate>();
			List<HeatTemplate> heatChildTemplates = new ArrayList<HeatTemplate>();
			HeatTemplate parentHeatTemplate = new HeatTemplate();
			String parentArtifactType = null;
			Set<String> artifacts = new HashSet<>(matchingObject.get().getVfModuleMetadata().getArtifacts());
			for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
				
				List<HeatTemplate> childNestedHeatTemplates = new ArrayList<HeatTemplate>();
				
				if (artifacts.contains(vfModuleArtifact.getArtifactInfo().getArtifactUUID())) {
					checkVfModuleArtifactType(vfModule, vfModuleCustomization, heatFilesList, vfModuleArtifact,
							childNestedHeatTemplates, parentHeatTemplate, vfResourceStructure);
				}
				
				if(vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)){
					parentArtifactType = identifyParentOfNestedTemplate(matchingObject.get(), vfModuleArtifact);
					
					if(!childNestedHeatTemplates.isEmpty()){
					
						if (parentArtifactType != null && parentArtifactType.equalsIgnoreCase(ASDCConfiguration.HEAT_VOL)) {
							volumeHeatChildTemplates.add(childNestedHeatTemplates.get(0));
						} else {
							heatChildTemplates.add(childNestedHeatTemplates.get(0));
						}
					}
				}
				
			}
			if(!heatFilesList.isEmpty()){
				vfModule.setHeatFiles(heatFilesList);
			}
			
			
			// Set all Child Templates related to HEAT_VOLUME
			if(!volumeHeatChildTemplates.isEmpty()){
				if(vfModule.getVolumeHeatTemplate() != null){
					vfModule.getVolumeHeatTemplate().setChildTemplates(volumeHeatChildTemplates);
				}else{
					logger.debug("VolumeHeatTemplate not set in setHeatInformationForVfModule()");
				}
			}
			
			// Set all Child Templates related to HEAT
			if(!heatChildTemplates.isEmpty()){
				if(vfModule.getModuleHeatTemplate() != null){
					vfModule.getModuleHeatTemplate().setChildTemplates(heatChildTemplates);
				}else{
					logger.debug("ModuleHeatTemplate not set in setHeatInformationForVfModule()");
				}
			}
		}
	}

	protected void checkVfModuleArtifactType(VfModule vfModule, VfModuleCustomization vfModuleCustomization,
			List<HeatFiles> heatFilesList, VfModuleArtifact vfModuleArtifact, List<HeatTemplate> nestedHeatTemplates,
			HeatTemplate parentHeatTemplate, VfResourceStructure vfResourceStructure) {
		if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT)) {
			vfModuleArtifact.incrementDeployedInDB();
			vfModule.setModuleHeatTemplate(vfModuleArtifact.getHeatTemplate());
		} else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_VOL)) {
			vfModule.setVolumeHeatTemplate(vfModuleArtifact.getHeatTemplate());
			VfModuleArtifact volVfModuleArtifact = this.getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
			vfModuleCustomization.setVolumeHeatEnv(volVfModuleArtifact.getHeatEnvironment());
			vfModuleArtifact.incrementDeployedInDB();
		} else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
			if(vfModuleArtifact.getHeatEnvironment().getName().contains("volume")) {
				vfModuleCustomization.setVolumeHeatEnv(vfModuleArtifact.getHeatEnvironment());
			} else { 
			vfModuleCustomization.setHeatEnvironment(vfModuleArtifact.getHeatEnvironment());
			}
			vfModuleArtifact.incrementDeployedInDB();
		} else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ARTIFACT)) {
			heatFilesList.add(vfModuleArtifact.getHeatFiles());							
			vfModuleArtifact.incrementDeployedInDB();
		} else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)) {
			nestedHeatTemplates.add(vfModuleArtifact.getHeatTemplate());				
			vfModuleArtifact.incrementDeployedInDB();
		}
	}

	protected VnfResourceCustomization createVnfResource(NodeTemplate vfNodeTemplate,
			ToscaResourceStructure toscaResourceStructure, Service service) {
		VnfResourceCustomization vnfResourceCustomization = vnfCustomizationRepo.findOneByModelCustomizationUUID(
				vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
		if(vnfResourceCustomization == null){		
			VnfResource vnfResource = findExistingVnfResource(service,
					vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			
			if(vnfResource==null)
				vnfResource=createVnfResource(vfNodeTemplate);
			
			vnfResourceCustomization = createVnfResourceCustomization(vfNodeTemplate, toscaResourceStructure,
					vnfResource);
			vnfResourceCustomization.setVnfResources(vnfResource);
			vnfResource.getVnfResourceCustomizations().add(vnfResourceCustomization);
			
			// Fetch VNFC Instance Group Info				
			List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
					.getGroupsOfOriginOfNodeTemplateByToscaGroupType(vfNodeTemplate,
							"org.openecomp.groups.VfcInstanceGroup");
				
			for (Group group : groupList) { 
				
					VnfcInstanceGroupCustomization vnfcInstanceGroupCustomization = createVNFCInstanceGroup(vfNodeTemplate, group, vnfResourceCustomization);
					
					vnfcInstanceGroupCustomizationRepo.saveAndFlush(vnfcInstanceGroupCustomization);				
			}			
		}
		return vnfResourceCustomization;
	}
	
	protected VnfResource findExistingVnfResource(Service service, String modelUUID) {
		VnfResource vnfResource = null;
		for(VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()){
			if (vnfResourceCustom.getVnfResources() != null
					&& vnfResourceCustom.getVnfResources().getModelUUID().equals(modelUUID)) {
				vnfResource = vnfResourceCustom.getVnfResources();
			}
		}
		if(vnfResource==null)
			vnfResource = vnfRepo.findResourceByModelUUID(modelUUID);
		
		return vnfResource;
	}

	protected VnfResourceCustomization createVnfResourceCustomization(NodeTemplate vfNodeTemplate,
			ToscaResourceStructure toscaResourceStructure, VnfResource vnfResource) {
		VnfResourceCustomization vnfResourceCustomization = new VnfResourceCustomization();
		vnfResourceCustomization.setModelCustomizationUUID(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
		vnfResourceCustomization.setModelInstanceName(vfNodeTemplate.getName());

		vnfResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
		vnfResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(vfNodeTemplate, "nf_naming_code")));
		vnfResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
		vnfResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));

		vnfResourceCustomization.setMultiStageDesign(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(vfNodeTemplate, MULTI_STAGE_DESIGN));

		vnfResourceCustomization.setVnfResources(vnfResource);
		vnfResourceCustomization.setAvailabilityZoneMaxCount(Integer.getInteger(
				vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT)));	

		CapabilityAssignments vnfCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
				.getCapabilitiesOf(vfNodeTemplate);

		if (vnfCustomizationCapability != null) {
			CapabilityAssignment capAssign = vnfCustomizationCapability.getCapabilityByName(SCALABLE);

			if (capAssign != null) {
				vnfResourceCustomization.setMinInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
						.getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
				vnfResourceCustomization.setMaxInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
						.getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
			}

		}
		
		toscaResourceStructure.setCatalogVnfResourceCustomization(vnfResourceCustomization);
		
		return vnfResourceCustomization;
	}

	protected VnfResource createVnfResource(NodeTemplate vfNodeTemplate) {
		VnfResource vnfResource = new VnfResource();
		vnfResource.setModelInvariantUUID(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
		vnfResource.setModelName(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
		vnfResource.setModelUUID(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));

		vnfResource.setModelVersion(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
		vnfResource.setDescription(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
		vnfResource.setOrchestrationMode(HEAT);
		vnfResource.setToscaNodeType(testNull(vfNodeTemplate.getType()));
		vnfResource.setAicVersionMax(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
		vnfResource.setAicVersionMin(
				testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
		vnfResource.setCategory(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY));
		vnfResource.setSubCategory(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY));
		
		return vnfResource;
	}

	protected AllottedResourceCustomization createAllottedResource(NodeTemplate nodeTemplate,
			ToscaResourceStructure toscaResourceStructure, Service service) throws ArtifactInstallerException {
		AllottedResourceCustomization allottedResourceCustomization = allottedCustomizationRepo
				.findOneByModelCustomizationUUID(
				nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
			
		if(allottedResourceCustomization == null){			
			AllottedResource allottedResource = findExistingAllottedResource(service,
					nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
			
			if(allottedResource==null)
				allottedResource=createAR(nodeTemplate);
			
			toscaResourceStructure.setAllottedResource(allottedResource);			
			allottedResourceCustomization = createAllottedResourceCustomization(nodeTemplate, toscaResourceStructure);
			allottedResourceCustomization.setAllottedResource(allottedResource);
			allottedResource.getAllotedResourceCustomization().add(allottedResourceCustomization);
		}

		allottedResourceCustomization.setResourceInput(getResourceInput(toscaResourceStructure, allottedResourceCustomization.getModelCustomizationUUID()));
		return allottedResourceCustomization;
	}
	
	protected AllottedResource findExistingAllottedResource(Service service, String modelUUID) {
		AllottedResource allottedResource = null;
		for(AllottedResourceCustomization allottedResourceCustom : service.getAllottedCustomizations()){
			if (allottedResourceCustom.getAllottedResource() != null
					&& allottedResourceCustom.getAllottedResource().getModelUUID().equals(modelUUID)) {
				allottedResource = allottedResourceCustom.getAllottedResource();
			}
		}
		if(allottedResource==null)
			allottedResource = allottedRepo.findResourceByModelUUID(modelUUID);
		
		return allottedResource;
	}
	
	protected AllottedResourceCustomization createAllottedResourceCustomization(NodeTemplate nodeTemplate,
			ToscaResourceStructure toscaResourceStructure) {
		AllottedResourceCustomization allottedResourceCustomization = new AllottedResourceCustomization();
		allottedResourceCustomization.setModelCustomizationUUID(
				testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
		allottedResourceCustomization.setModelInstanceName(nodeTemplate.getName());
		

		allottedResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
		allottedResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(nodeTemplate, "nf_naming_code")));
		allottedResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
		allottedResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
				.getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));

		List<NodeTemplate> vfcNodes = toscaResourceStructure.getSdcCsarHelper().getVfcListByVf(allottedResourceCustomization.getModelCustomizationUUID());
		
		if(vfcNodes != null){
			for(NodeTemplate vfcNode : vfcNodes){
			
				allottedResourceCustomization.setProvidingServiceModelUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_uuid"));
				allottedResourceCustomization.setProvidingServiceModelInvariantUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_invariant_uuid"));
				allottedResourceCustomization.setProvidingServiceModelName(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_name"));
			}
		}
		

		CapabilityAssignments arCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
				.getCapabilitiesOf(nodeTemplate);

		if (arCustomizationCapability != null) {
			CapabilityAssignment capAssign = arCustomizationCapability.getCapabilityByName(SCALABLE);

			if (capAssign != null) {
				allottedResourceCustomization.setMinInstances(
						Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
								capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
				allottedResourceCustomization.setMaxInstances(
						Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
								capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
			}
		}
		return allottedResourceCustomization;
	}

	protected AllottedResource createAR(NodeTemplate nodeTemplate) {
		AllottedResource allottedResource = new AllottedResource();
		allottedResource
		.setModelUUID(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
		allottedResource.setModelInvariantUUID(
				testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
		allottedResource
		.setModelName(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
		allottedResource
		.setModelVersion(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
		allottedResource.setToscaNodeType(testNull(nodeTemplate.getType()));
		allottedResource.setSubcategory(
				testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY)));
		allottedResource
		.setDescription(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
		return allottedResource;
	}

	protected Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile, String artifactUUID) {
		// Scan the payload downloadResult and extract the HeatTemplate
		// parameters
		YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
		return yamlEditor.getParameterList(artifactUUID);
	}	

	protected String testNull(Object object) {

		if (object == null) {
			return null;
		} else if (object.equals("NULL")) {
			return null;
		} else if (object instanceof Integer) {
			return object.toString();
		} else if (object instanceof String) {
			return (String) object;
		} else {
			return "Type not recognized";
		}
	}
	
	protected static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,
			VfModuleArtifact heatNestedArtifact) {

		if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null && vfModuleStructure
				.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
			for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
					.getArtifactInfo().getRelatedArtifacts()) {
				if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
					return ASDCConfiguration.HEAT;
				}

			}
		} 
		
		if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null 
				&& vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()
						.getRelatedArtifacts() != null) {
			for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL)
					.get(0).getArtifactInfo().getRelatedArtifacts()) {
				if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
					return ASDCConfiguration.HEAT_VOL;
				}
			
			}
		}
		
		// Does not belong to anything
		return null;
			
	}
	
	protected static String createVNFName(VfResourceStructure vfResourceStructure) {

		return vfResourceStructure.getNotification().getServiceName() + "/"
				+ vfResourceStructure.getResourceInstance().getResourceInstanceName();
	}

	protected static String createVfModuleName(VfModuleStructure vfModuleStructure) {
		
		return createVNFName(vfModuleStructure.getParentVfResource()) + "::"
				+ vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
	}
	
	
	protected static Timestamp getCurrentTimeStamp() {
		
		return new Timestamp(new Date().getTime());
	}

}