aboutsummaryrefslogtreecommitdiffstats
path: root/asdc-controller/src/main/java/org/openecomp/mso/asdc/installer/heat/VfResourceInstaller.java
blob: e2ed1a3d79eb38412a85e35ba54e78243e760945 (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
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.asdc.installer.heat;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.LockAcquisitionException;
import org.openecomp.sdc.api.notification.IArtifactInfo;

import org.openecomp.mso.asdc.client.ASDCConfiguration;
import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
import org.openecomp.mso.asdc.installer.ASDCElementInfo;
import org.openecomp.mso.asdc.installer.BigDecimalVersion;
import org.openecomp.mso.asdc.installer.IVfResourceInstaller;
import org.openecomp.mso.asdc.installer.VfModuleArtifact;
import org.openecomp.mso.asdc.installer.VfModuleStructure;
import org.openecomp.mso.asdc.installer.VfResourceStructure;
import org.openecomp.mso.asdc.util.YamlEditor;
import org.openecomp.mso.db.catalog.CatalogDatabase;
import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
import org.openecomp.mso.db.catalog.beans.HeatFiles;
import org.openecomp.mso.db.catalog.beans.HeatTemplate;
import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
import org.openecomp.mso.db.catalog.beans.Service;
import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
import org.openecomp.mso.db.catalog.beans.VfModule;
import org.openecomp.mso.db.catalog.beans.VnfResource;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;

public class VfResourceInstaller implements IVfResourceInstaller {

	private MsoLogger logger;

	public VfResourceInstaller() {
		logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
	}

	@Override
	public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct)
			throws ArtifactInstallerException {

		boolean status = false;
		VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;

        try(CatalogDatabase db = new CatalogDatabase()) {

			String resourceType = vfResourceStruct.getResourceInstance().getResourceType();
			String category = vfResourceStruct.getResourceInstance().getCategory();

			// Check for duplicate VF Module that is not an Allotted Resource
			if(resourceType.equals("VF") && !category.equalsIgnoreCase("Allotted Resource")){
				logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "VNFResource",
					VfResourceInstaller.createVNFName(vfResourceStructure),
					BigDecimalVersion.castAndCheckNotificationVersionToString(
							vfResourceStructure.getNotification().getServiceVersion()), "", "");

				VnfResource vnfResource = db.getVnfResource(
					VfResourceInstaller.createVNFName(vfResourceStructure),
					BigDecimalVersion.castAndCheckNotificationVersionToString(
							vfResourceStructure.getNotification().getServiceVersion()));

			if (vnfResource != null) {
				status = true;

			}

			}

			// Check dup for VF Allotted Resource
			if(resourceType.equals("VF") && category.equalsIgnoreCase("Allotted Resource")){
				logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "AllottedResource",
						vfResourceStruct.getResourceInstance().getResourceInstanceName(),
						BigDecimalVersion.castAndCheckNotificationVersionToString(
								vfResourceStructure.getNotification().getServiceVersion()), "", "");

				List<AllottedResourceCustomization> allottedResources = db.getAllAllottedResourcesByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());

				if(allottedResources != null && allottedResources.size() > 0){
					for(AllottedResourceCustomization allottedResource : allottedResources){

						String existingAllottedResource = allottedResource.getModelCustomizationUuid();
						String notificationAllottedResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();

						if(existingAllottedResource.equals(notificationAllottedResource)){
							status=true;
							break;
						}
					}

				}
			}

			// Check Network for duplicates
			if(resourceType.equals("VL")){
				logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "NetworkResource",
						vfResourceStruct.getResourceInstance().getResourceInstanceName(),
						BigDecimalVersion.castAndCheckNotificationVersionToString(
								vfResourceStructure.getNotification().getServiceVersion()), "", "");

				List<NetworkResourceCustomization> networkResources = db.getAllNetworksByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());

				if(networkResources != null && networkResources.size() > 0){
					for(NetworkResourceCustomization networkResource : networkResources){

						String existingNetworkResource = networkResource.getModelCustomizationUuid();
						String notificationNetworkResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();

						if(existingNetworkResource.equals(notificationNetworkResource)){
							status=true;
							break;
						}
					}

				}
			}

			if (status) {
				logger.info(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED_DETAIL,
						vfResourceStructure.getResourceInstance().getResourceInstanceName(),
						vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
						vfResourceStructure.getNotification().getServiceName(),
						BigDecimalVersion.castAndCheckNotificationVersionToString(
								vfResourceStructure.getNotification().getServiceVersion()),
						vfResourceStructure.getNotification().getServiceUUID(),
						vfResourceStructure.getResourceInstance().getResourceName(),"", "");
			} else {
				logger.info(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
						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);
		}
	}

	@Override
	public void installTheResource(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {

		// 1. Add the DB object list (Hashed) to be created from the HashMap
		// UUID
		// The DB objects will be stored in each VfModuleArtifact objects
		// Those objects could be reused by different VfModule

		VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;

		for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {

			switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
			case ASDCConfiguration.HEAT:
			case ASDCConfiguration.HEAT_VOL:
			case ASDCConfiguration.HEAT_NESTED:
				VfResourceInstaller.createHeatTemplateFromArtifact(vfResourceStructure, vfModuleArtifact);
				break;
			case ASDCConfiguration.HEAT_ENV:
				VfResourceInstaller.createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
				break;
			case ASDCConfiguration.HEAT_ARTIFACT:
				VfResourceInstaller.createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact);
				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;

			}
		}

		// in case of deployment failure, use a string that will represent the type of artifact that failed...
		List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();

		CatalogDatabase catalogDB = new CatalogDatabase();
		// 2. Create the VFModules/VNFResource objects by linking them to the
		// objects created before and store them in Resource/module structure
		// Opening a DB transaction, starting from here
		try {

			VfResourceInstaller.createService(vfResourceStructure);

			String resourceType = vfResourceStructure.getResourceInstance().getResourceType();
			String resourceCategory = vfResourceStructure.getResourceInstance().getCategory();

			if(resourceType.equals("VF")){

				if(resourceCategory.equalsIgnoreCase("Allotted Resource")){
					VfResourceInstaller.createAllottedResourceCustomization(vfResourceStructure);
					catalogDB.saveAllottedResourceCustomization(vfResourceStructure.getCatalogResourceCustomization());
				} else {
					VfResourceInstaller.createVnfResource(vfResourceStructure);
					catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
				}
			}

			if(resourceType.equals("VL")){
				VfResourceInstaller.createNetworkResourceCustomization(vfResourceStructure);
				catalogDB.saveNetworkResourceCustomization(vfResourceStructure.getCatalogNetworkResourceCustomization());
			}

			// Add this one for logging
			artifactListForLogging.add(ASDCElementInfo.createElementFromVfResourceStructure(vfResourceStructure));

			//catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
			catalogDB.saveService(vfResourceStructure.getCatalogService());

			// Now that the service has been added we can populate the Service_to_AllottedResources table
			if(resourceType.equals("VF") && resourceCategory.equalsIgnoreCase("Allotted Resource")){
				catalogDB.saveServiceToAllottedResources(vfResourceStructure.getCatalogServiceToAllottedResources());
			}

			// Now that the service has been added we can populate the Service_to_Network table
			if(resourceType.equals("VL")){
				catalogDB.saveServiceToNetworks(vfResourceStructure.getCatalogServiceToNetworks());
			}

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

				// Here we set the right db structure according to the Catalog
				// DB

				// We expect only one MAIN HEAT per VFMODULE
				// we can also obtain from it the Env ArtifactInfo, that's why
				// we
				// get the Main IArtifactInfo

				HeatTemplate heatMainTemplate = null;
				HeatEnvironment heatEnv = null;

				HeatTemplate heatVolumeTemplate = null;
				HeatEnvironment heatVolumeEnv = null;

				if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT)) {
					IArtifactInfo mainEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT)
							.get(0).getArtifactInfo().getGeneratedArtifact();

					// MAIN HEAT
					heatMainTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
							.get(ASDCConfiguration.HEAT).get(0).getCatalogObject();

					// Add this one for logging
					artifactListForLogging.add(ASDCElementInfo
							.createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo()));

					catalogDB.saveHeatTemplate(heatMainTemplate, heatMainTemplate.getParameters());
					// Indicate we have deployed it in the DB
					vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).incrementDeployedInDB();


					// VOLUME HEAT
					// We expect only one VOL HEAT per VFMODULE
					// we can also obtain from it the Env ArtifactInfo, that's why
					// we get the Volume IArtifactInfo

					if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_VOL)) {
						IArtifactInfo volEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0)
								.getArtifactInfo().getGeneratedArtifact();

						heatVolumeTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
								.get(ASDCConfiguration.HEAT_VOL).get(0).getCatalogObject();

						// Add this one for logging
						artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()));

						catalogDB.saveHeatTemplate(heatVolumeTemplate, heatVolumeTemplate.getParameters());
						// Indicate we have deployed it in the DB
						vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).incrementDeployedInDB();

						if (volEnvArtifactInfo != null) {
							heatVolumeEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
									.get(volEnvArtifactInfo.getArtifactUUID()).getCatalogObject();

							// Add this one for logging
							artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(volEnvArtifactInfo));

							catalogDB.saveHeatEnvironment(heatVolumeEnv);
							// Indicate we have deployed it in the DB
							vfResourceStructure.getArtifactsMapByUUID().get(volEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
						}

					}

					// NESTED HEAT
					// Here we expect many HEAT_NESTED template to be there
					// check first if we really have nested heat templates
					if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_NESTED)) {
						for (VfModuleArtifact heatNestedArtifact : vfModuleStructure.getArtifactsMap()
								.get(ASDCConfiguration.HEAT_NESTED)) {

							// Check if this nested is well referenced by the MAIN HEAT
							String parentArtifactType = VfResourceInstaller.identifyParentOfNestedTemplate(vfModuleStructure,heatNestedArtifact);
							HeatTemplate heatNestedTemplate = (HeatTemplate) heatNestedArtifact.getCatalogObject();

							if (parentArtifactType != null) {

								switch (parentArtifactType) {
									case ASDCConfiguration.HEAT:

										// Add this one for logging
										artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));

										catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
										// Indicate we have deployed it in the DB
										heatNestedArtifact.incrementDeployedInDB();
										break;
									case ASDCConfiguration.HEAT_VOL:

										// Add this one for logging
										artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
										catalogDB.saveNestedHeatTemplate (heatVolumeTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
										// Indicate we have deployed it in the DB
										heatNestedArtifact.incrementDeployedInDB();
										break;

									default:
										break;

								}
							} else { // Assume it belongs to HEAT MAIN
								// Add this one for logging
								artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));

								catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
								// Indicate we have deployed it in the DB
								heatNestedArtifact.incrementDeployedInDB();
							}
						}
					}

					if (mainEnvArtifactInfo != null) {
						heatEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
								.get(mainEnvArtifactInfo.getArtifactUUID()).getCatalogObject();

						// Add this one for logging
						artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(mainEnvArtifactInfo));

						catalogDB.saveHeatEnvironment(heatEnv);
						// Indicate we have deployed it in the DB
						vfResourceStructure.getArtifactsMapByUUID().get(mainEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
					}

				}


				// here we expect one VFModule to be there
				VfResourceInstaller.createVfModule(vfModuleStructure,heatMainTemplate, heatVolumeTemplate, heatEnv, heatVolumeEnv);
				VfModule vfModule = vfModuleStructure.getCatalogVfModule();

				// Add this one for logging
				artifactListForLogging.add(ASDCElementInfo.createElementFromVfModuleStructure(vfModuleStructure));

				catalogDB.saveOrUpdateVfModule(vfModule);

				// Here we expect many HEAT_TEMPLATE files to be there
				if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_ARTIFACT)) {
					for (VfModuleArtifact heatArtifact : vfModuleStructure.getArtifactsMap()
							.get(ASDCConfiguration.HEAT_ARTIFACT)) {

						HeatFiles heatFile = (HeatFiles) heatArtifact.getCatalogObject();

						// Add this one for logging
						artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatArtifact.getArtifactInfo()));


						catalogDB.saveVfModuleToHeatFiles (vfModule.getId(), heatFile);
						// Indicate we will deploy it in the DB
						heatArtifact.incrementDeployedInDB();
					}
				}

			}

			catalogDB.commit();
			vfResourceStructure.setSuccessfulDeployment();

		} catch (Exception e) {

			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 endEvent = "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.";
				String elementToLog = (artifactListForLogging.size() > 0 ? 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);
				catalogDB.rollback();
				throw new ArtifactInstallerException(
						"Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
			}

		} finally {
			catalogDB.close();
			// Debug log the whole collection...
			logger.debug(artifactListForLogging.toString());
		}

	}

	private 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;

	}

	private static void createVnfResource(VfResourceStructure vfResourceStructure) {
		VnfResource vnfResource = new VnfResource();

		vnfResource.setAsdcUuid(vfResourceStructure.getResourceInstance().getResourceUUID());
		vnfResource.setDescription(vfResourceStructure.getNotification().getServiceDescription());

		vnfResource.setOrchestrationMode("HEAT");
		// Set the version but Version is stored into ASDC_SERVICE_MODEL_VERSION
		vnfResource.setVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfResourceStructure.getNotification().getServiceVersion()));
		vnfResource.setVnfType(VfResourceInstaller.createVNFName(vfResourceStructure));
		vnfResource.setModelVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfResourceStructure.getResourceInstance().getResourceVersion()));

		vnfResource.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID());
		vnfResource.setModelCustomizationName(vfResourceStructure.getResourceInstance().getResourceInstanceName());
		vnfResource.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID());
		vnfResource.setModelName(vfResourceStructure.getResourceInstance().getResourceName());
		vnfResource.setServiceModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
		//vnfResource.setCreated(getCurrentTimeStamp());

		vfResourceStructure.setCatalogVnfResource(vnfResource);
	}

	private static void createNetworkResourceCustomization(VfResourceStructure vfResourceStructure) {
		NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();

		networkResourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
		networkResourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
		networkResourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
		networkResourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
		networkResourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
		networkResourceCustomization.setModelVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
		//networkResourceCustomization.setCreated(getCurrentTimeStamp());

		vfResourceStructure.setCatalogNetworkResourceCustomization(networkResourceCustomization);

		ServiceToNetworks serviceNetworks = new ServiceToNetworks();
		serviceNetworks.setNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
		serviceNetworks.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());

		vfResourceStructure.setCatalogServiceToNetworks(serviceNetworks);

	}

	private static void createAllottedResourceCustomization(VfResourceStructure vfResourceStructure) {
		AllottedResourceCustomization resourceCustomization = new AllottedResourceCustomization();

		resourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
		resourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
		resourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
		resourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
		resourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
		resourceCustomization.setVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
		//resourceCustomization.setCreated(getCurrentTimeStamp());

		vfResourceStructure.setCatalogResourceCustomization(resourceCustomization);

		ServiceToAllottedResources serviceAllottedResources = new ServiceToAllottedResources();
		serviceAllottedResources.setArModelCustomizationUuid(resourceCustomization.getModelCustomizationUuid());
		serviceAllottedResources.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());

		vfResourceStructure.setCatalogServiceToAllottedResources(serviceAllottedResources);

	}

	private static void createVfModule(VfModuleStructure vfModuleStructure,HeatTemplate heatMain, HeatTemplate heatVolume,HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
		VfModule vfModule = new VfModule();
		vfModule.setType(createVfModuleName(vfModuleStructure));
		vfModule.setAsdcUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelUUID());
		vfModule.setDescription(vfModuleStructure.getVfModuleMetadata().getVfModuleModelDescription());

		if (vfModuleStructure.getVfModuleMetadata().isBase()) {
			vfModule.setIsBase(1);
		} else {
			vfModule.setIsBase(0);
		}

		vfModule.setModelCustomizationUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelCustomizationUUID());
		vfModule.setModelInvariantUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelInvariantUUID());
		vfModule.setModelName(vfModuleStructure.getVfModuleMetadata().getVfModuleModelName());

		vfModule.setVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(vfModuleStructure.getParentVfResource().getNotification().getServiceVersion()));
		vfModule.setModelVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(
				vfModuleStructure.getVfModuleMetadata().getVfModuleModelVersion()));

		Map<String,String> map = vfModuleStructure.getVfModuleMetadata().getProperties();

		if(map != null){

			if(map.get("vf_module_label") != null){
				vfModule.setLabel(map.get("vf_module_label"));
			}
			if(map.get("initial_count") != null && map.get("initial_count").length() > 0){
				vfModule.setInitialCount(Integer.parseInt(map.get("initial_count")));
			}
			if(map.get("min_vf_module_instances") != null && map.get("min_vf_module_instances").length() > 0){
				vfModule.setMinInstances(Integer.parseInt(map.get("min_vf_module_instances")));
			}
			if(map.get("max_vf_module_instances") != null && map.get("max_vf_module_instances").length() > 0){
				vfModule.setMaxInstances(Integer.parseInt(map.get("max_vf_module_instances")));
			}

		}

		vfModuleStructure.setCatalogVfModule(vfModule);

		VfResourceInstaller.createVfModuleLinks(vfModule, vfModuleStructure.getParentVfResource().getCatalogVnfResource(), heatMain,heatVolume, heatEnv,heatVolumeEnv);
	}

	private static void createVfModuleLinks(VfModule vfModule, VnfResource vnfResource, HeatTemplate heatMain,
			HeatTemplate heatVolume, HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {

		if (heatMain !=null) {
			vfModule.setTemplateId(heatMain.getId());
		}
		if (heatEnv != null) {
			vfModule.setEnvironmentId(heatEnv.getId());
		}
		if (heatVolume != null) {
			vfModule.setVolTemplateId(heatVolume.getId());
		}
		if (heatVolumeEnv != null) {
			vfModule.setVolEnvironmentId(heatVolumeEnv.getId());
		}

		vfModule.setVnfResourceId(vnfResource.getId());

	}

	private static Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile) {

		// Scan the payload downloadResult and extract the HeatTemplate
		// parameters
		YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
		return yamlEditor.getParameterList();

	}


	public static 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 static 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;
		}

		StringBuffer sb = new StringBuffer(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();
	}

	private static void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
			VfModuleArtifact vfModuleArtifact) {
		HeatTemplate heatTemplate = new HeatTemplate();

		// TODO Set the label
		heatTemplate.setAsdcLabel("label");
		// Use the ResourceName of the ASDC template because the HEAT could be
		// reused
		heatTemplate.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
		heatTemplate.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());

		List<String> typeList = new ArrayList<String>();
		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()));

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

		Set<HeatTemplateParam> heatParam = VfResourceInstaller
				.extractHeatTemplateParameters(vfModuleArtifact.getResult());
		heatTemplate.setParameters(heatParam);

		vfModuleArtifact.setCatalogObject(heatTemplate);
	}

	private static void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
			VfModuleArtifact vfModuleArtifact) {
		HeatEnvironment heatEnvironment = new HeatEnvironment();

		heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
		// TODO Set the label
		heatEnvironment.setAsdcLabel("Label");

		List<String> typeList = new ArrayList<String>();
		typeList.add(ASDCConfiguration.HEAT);
		typeList.add(ASDCConfiguration.HEAT_VOL);

		heatEnvironment.setEnvironment(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
		heatEnvironment.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
		heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
		heatEnvironment.setVersion(BigDecimalVersion
				.castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
		heatEnvironment.setAsdcResourceName(VfResourceInstaller.createVNFName(vfResourceStructure));

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

		vfModuleArtifact.setCatalogObject(heatEnvironment);

	}

	private static void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
			VfModuleArtifact vfModuleArtifact) {

		HeatFiles heatFile = new HeatFiles();
		// TODO Set the label
		heatFile.setAsdcLabel("Label");
		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()));

		heatFile.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());

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

		vfModuleArtifact.setCatalogObject(heatFile);

	}

	private static void createService(VfResourceStructure vfResourceStructure) {

		Service service = new Service();
		service.setDescription(vfResourceStructure.getNotification().getServiceDescription());
		service.setServiceName(vfResourceStructure.getNotification().getServiceName());
		service.setServiceNameVersionId(vfResourceStructure.getNotification().getServiceUUID());
		service.setVersion(vfResourceStructure.getNotification().getServiceVersion());
		service.setModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());

		vfResourceStructure.setCatalogService(service);
	}


	private static String createVNFName(VfResourceStructure vfResourceStructure) {

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

	private static String createVfModuleName(VfModuleStructure vfModuleStructure) {

		return createVNFName(vfModuleStructure.getParentVfResource())+"::"+vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
	}


	private static Timestamp getCurrentTimeStamp() {

		return new Timestamp(new Date().getTime());
	}


}