4cef0f10132dc830d325c7f82f287134a6d5e66c
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / installer / heat / VfResourceInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.asdc.installer.heat;
22
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.hibernate.exception.ConstraintViolationException;
29 import org.hibernate.exception.LockAcquisitionException;
30
31 import org.openecomp.sdc.api.notification.IArtifactInfo;
32 import org.openecomp.mso.asdc.client.ASDCConfiguration;
33 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
34 import org.openecomp.mso.asdc.installer.ASDCElementInfo;
35 import org.openecomp.mso.asdc.installer.BigDecimalVersion;
36 import org.openecomp.mso.asdc.installer.IVfResourceInstaller;
37 import org.openecomp.mso.asdc.installer.VfModuleArtifact;
38 import org.openecomp.mso.asdc.installer.VfModuleStructure;
39 import org.openecomp.mso.asdc.installer.VfResourceStructure;
40 import org.openecomp.mso.asdc.util.YamlEditor;
41 import org.openecomp.mso.db.catalog.CatalogDatabase;
42 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
43 import org.openecomp.mso.db.catalog.beans.HeatFiles;
44 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
45 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
46 import org.openecomp.mso.db.catalog.beans.Service;
47 import org.openecomp.mso.db.catalog.beans.VfModule;
48 import org.openecomp.mso.db.catalog.beans.VnfResource;
49 import org.openecomp.mso.logger.MessageEnum;
50 import org.openecomp.mso.logger.MsoLogger;
51
52 public class VfResourceInstaller implements IVfResourceInstaller {
53
54         private MsoLogger logger;
55
56         public VfResourceInstaller() {
57                 logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
58         }
59
60         @Override
61         public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStructure)
62                         throws ArtifactInstallerException {
63                 boolean status = false;
64
65                 try (CatalogDatabase db = new CatalogDatabase()) {
66
67                         logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "VNFResource",
68                                         VfResourceInstaller.createVNFName(vfResourceStructure),
69                                         BigDecimalVersion.castAndCheckNotificationVersionToString(
70                                                         vfResourceStructure.getNotification().getServiceVersion()), "", "");
71
72                         VnfResource vnfResource = db.getVnfResource(
73                                         VfResourceInstaller.createVNFName(vfResourceStructure),
74                                         BigDecimalVersion.castAndCheckNotificationVersionToString(
75                                                         vfResourceStructure.getNotification().getServiceVersion()));
76
77                         if (vnfResource != null) {
78                                 status = true;
79
80                         }
81
82                         if (status) {
83                                 logger.info(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED_DETAIL,
84                                                 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
85                                                 vfResourceStructure.getResourceInstance().getResourceUUID(),
86                                                 vfResourceStructure.getNotification().getServiceName(),
87                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
88                                                                 vfResourceStructure.getNotification().getServiceVersion()),
89                                                 vfResourceStructure.getNotification().getServiceUUID(),
90                                                 vfResourceStructure.getResourceInstance().getResourceName(), "", "");
91                         } else {
92                                 logger.info(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
93                                                 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
94                                                 vfResourceStructure.getResourceInstance().getResourceUUID(),
95                                                 vfResourceStructure.getNotification().getServiceName(),
96                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
97                                                                 vfResourceStructure.getNotification().getServiceVersion()),
98                                                 vfResourceStructure.getNotification().getServiceUUID(), 
99                                                 vfResourceStructure.getResourceInstance().getResourceName(),"", "");
100                         }
101
102                         return status;
103
104                 } catch (Exception e) {
105                         logger.error(MessageEnum.ASDC_ARTIFACT_CHECK_EXC, "", "", MsoLogger.ErrorCode.SchemaError, "Exception - isResourceAlreadyDeployed");
106                         throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
107                 }
108         }
109
110         @Override
111         public void installTheResource(VfResourceStructure vfResourceStructure) throws ArtifactInstallerException {
112
113                 // 1. Add the DB object list (Hashed) to be created from the HashMap
114                 // UUID
115                 // The DB objects will be stored in each VfModuleArtifact objects
116                 // Those objects could be reused by different VfModule
117
118                 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
119
120                         switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
121                         case ASDCConfiguration.HEAT:
122                         case ASDCConfiguration.HEAT_VOL:
123                         case ASDCConfiguration.HEAT_NESTED:
124                                 VfResourceInstaller.createHeatTemplateFromArtifact(vfResourceStructure, vfModuleArtifact);
125                                 break;
126                         case ASDCConfiguration.HEAT_ENV:
127                                 VfResourceInstaller.createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
128                                 break;
129                         case ASDCConfiguration.HEAT_ARTIFACT:
130                                 VfResourceInstaller.createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact);
131                                 break;
132                         case ASDCConfiguration.HEAT_NET:
133                         case ASDCConfiguration.OTHER:
134                                 logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT, vfModuleArtifact.getArtifactInfo().getArtifactType()+"(Artifact Name:"+vfModuleArtifact.getArtifactInfo().getArtifactName()+")", "", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
135                                 break;
136                         default:
137                                 break;
138
139                         }
140                 }
141                 
142                 // in case of deployment failure, use a string that will represent the type of artifact that failed...
143                 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
144                 
145                 CatalogDatabase catalogDB = new CatalogDatabase();
146                 // 2. Create the VFModules/VNFResource objects by linking them to the
147                 // objects created before and store them in Resource/module structure
148                 // Opening a DB transaction, starting from here
149                 try {
150                         
151                         VfResourceInstaller.createService(vfResourceStructure);
152                         
153                         VfResourceInstaller.createVnfResource(vfResourceStructure);
154
155                         // Add this one for logging
156                         artifactListForLogging.add(ASDCElementInfo.createElementFromVfResourceStructure(vfResourceStructure));
157                         
158                         catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
159                         catalogDB.saveService(vfResourceStructure.getCatalogService());
160                         
161                         for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {
162
163                                 // Here we set the right db structure according to the Catalog
164                                 // DB
165
166                                 // We expect only one MAIN HEAT per VFMODULE
167                                 // we can also obtain from it the Env ArtifactInfo, that's why
168                                 // we
169                                 // get the Main IArtifactInfo
170
171                                 HeatTemplate heatMainTemplate = null;
172                                 HeatEnvironment heatEnv = null;
173                                 
174                                 HeatTemplate heatVolumeTemplate = null;
175                                 HeatEnvironment heatVolumeEnv = null;
176                                 
177                                 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT)) {
178                                         IArtifactInfo mainEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT)
179                                                         .get(0).getArtifactInfo().getGeneratedArtifact();
180                                         
181                                         // MAIN HEAT
182                                         heatMainTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
183                                                         .get(ASDCConfiguration.HEAT).get(0).getCatalogObject();
184
185                                         // Add this one for logging
186                                         artifactListForLogging.add(ASDCElementInfo
187                                                         .createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo()));
188                                         
189                                         catalogDB.saveHeatTemplate(heatMainTemplate, heatMainTemplate.getParameters());
190                                         // Indicate we have deployed it in the DB
191                                         vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).incrementDeployedInDB();
192                                         
193                                         
194                                         // VOLUME HEAT
195                                         // We expect only one VOL HEAT per VFMODULE
196                                         // we can also obtain from it the Env ArtifactInfo, that's why
197                                         // we get the Volume IArtifactInfo
198                                 
199                                         if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_VOL)) {
200                                                 IArtifactInfo volEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0)
201                                                                 .getArtifactInfo().getGeneratedArtifact();
202                 
203                                                 heatVolumeTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
204                                                                 .get(ASDCConfiguration.HEAT_VOL).get(0).getCatalogObject();
205                                         
206                                                 // Add this one for logging
207                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()));
208
209                                                 catalogDB.saveHeatTemplate(heatVolumeTemplate, heatVolumeTemplate.getParameters());
210                                                 // Indicate we have deployed it in the DB
211                                                 vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).incrementDeployedInDB();
212                                                 
213                                                 if (volEnvArtifactInfo != null) {
214                                                         heatVolumeEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
215                                                                         .get(volEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
216
217                                                         // Add this one for logging
218                                                         artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(volEnvArtifactInfo));
219                                                                                                                 
220                                                         catalogDB.saveHeatEnvironment(heatVolumeEnv);
221                                                         // Indicate we have deployed it in the DB
222                                                         vfResourceStructure.getArtifactsMapByUUID().get(volEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
223                                                 }
224                                                 
225                                         }
226                                         
227                                         // NESTED HEAT
228                                         // Here we expect many HEAT_NESTED template to be there
229                                         // check first if we really have nested heat templates
230                                         if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_NESTED)) {
231                                                 for (VfModuleArtifact heatNestedArtifact : vfModuleStructure.getArtifactsMap()
232                                                                 .get(ASDCConfiguration.HEAT_NESTED)) {
233         
234                                                         // Check if this nested is well referenced by the MAIN HEAT
235                                                         String parentArtifactType = VfResourceInstaller.identifyParentOfNestedTemplate(vfModuleStructure,heatNestedArtifact);
236                                                         HeatTemplate heatNestedTemplate = (HeatTemplate) heatNestedArtifact.getCatalogObject();
237                                                         
238                                                         if (parentArtifactType != null) {
239                                                                                                                 
240                                                                 switch (parentArtifactType) {
241                                                                         case ASDCConfiguration.HEAT:
242                                                                                 
243                                                                                 // Add this one for logging
244                                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
245                                                                 
246                                                                                 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
247                                                                                 // Indicate we have deployed it in the DB
248                                                                                 heatNestedArtifact.incrementDeployedInDB();
249                                                                                 break;
250                                                                         case ASDCConfiguration.HEAT_VOL:
251                                                                                 
252                                                                                 // Add this one for logging
253                                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
254                                                                                 catalogDB.saveNestedHeatTemplate (heatVolumeTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
255                                                                                 // Indicate we have deployed it in the DB
256                                                                                 heatNestedArtifact.incrementDeployedInDB();
257                                                                                 break;
258                                                                                 
259                                                                         default:
260                                                                                 break;
261
262                                                                 }
263                                                         } else { // Assume it belongs to HEAT MAIN
264                                                                 // Add this one for logging
265                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
266                                                 
267                                                                 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
268                                                                 // Indicate we have deployed it in the DB
269                                                                 heatNestedArtifact.incrementDeployedInDB();
270                                                         }
271                                                 }
272                                         }
273                                         
274                                         if (mainEnvArtifactInfo != null) {
275                                                 heatEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
276                                                                 .get(mainEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
277
278                                                 // Add this one for logging
279                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(mainEnvArtifactInfo));
280                                                                                                 
281                                                 catalogDB.saveHeatEnvironment(heatEnv);
282                                                 // Indicate we have deployed it in the DB
283                                                 vfResourceStructure.getArtifactsMapByUUID().get(mainEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
284                                         }
285                                         
286                                 }
287                 
288                                         
289                                 // here we expect one VFModule to be there
290                                 VfResourceInstaller.createVfModule(vfModuleStructure,heatMainTemplate, heatVolumeTemplate, heatEnv, heatVolumeEnv);
291                                 VfModule vfModule = vfModuleStructure.getCatalogVfModule();
292
293                                 // Add this one for logging
294                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfModuleStructure(vfModuleStructure));
295                                 
296                                 catalogDB.saveOrUpdateVfModule(vfModule);
297
298                                 // Here we expect many HEAT_TEMPLATE files to be there
299                                 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_ARTIFACT)) {
300                                         for (VfModuleArtifact heatArtifact : vfModuleStructure.getArtifactsMap()
301                                                         .get(ASDCConfiguration.HEAT_ARTIFACT)) {
302         
303                                                 HeatFiles heatFile = (HeatFiles) heatArtifact.getCatalogObject();
304                                                                                         
305                                                 // Add this one for logging
306                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatArtifact.getArtifactInfo()));
307                                         
308                                                 
309                                                 catalogDB.saveVfModuleToHeatFiles (vfModule.getId(), heatFile);
310                                                 // Indicate we will deploy it in the DB
311                                                 heatArtifact.incrementDeployedInDB();
312                                         }
313                                 }
314
315                         }
316                         
317                         catalogDB.commit();
318                         vfResourceStructure.setSuccessfulDeployment();
319                         
320                 } catch (Exception e) {
321
322                         Throwable dbExceptionToCapture = e;
323                         while (!(dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException)
324                                         && (dbExceptionToCapture.getCause() != null)) {
325                                 dbExceptionToCapture = dbExceptionToCapture.getCause();
326                         }
327
328                         if (dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException) {
329                                 logger.warn(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED, vfResourceStructure.getResourceInstance().getResourceName(),
330                                                 vfResourceStructure.getNotification().getServiceVersion(), "", "", MsoLogger.ErrorCode.DataError, "Exception - ASCDC Artifact already deployed", e);
331                         } else {
332                                 String endEvent = "Exception caught during installation of the VFResource. Transaction rollback.";
333                                 String elementToLog = (artifactListForLogging.size() > 0 ? artifactListForLogging.get(artifactListForLogging.size()-1).toString() : "No element listed");
334                                 logger.error(MessageEnum.ASDC_ARTIFACT_INSTALL_EXC, elementToLog, "", "", MsoLogger.ErrorCode.DataError, "Exception caught during installation of the VFResource. Transaction rollback", e);
335                                 catalogDB.rollback();
336                                 throw new ArtifactInstallerException(
337                                                 "Exception caught during installation of the VFResource. Transaction rollback.", e);
338                         }
339
340                 } finally {
341                         catalogDB.close();
342                         // Debug log the whole collection...
343                         logger.debug(artifactListForLogging.toString());
344                 }
345
346         }
347         
348         private static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,VfModuleArtifact heatNestedArtifact) {
349
350                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null 
351                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
352                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
353                                         .getArtifactInfo().getRelatedArtifacts()) {
354                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
355                                         return ASDCConfiguration.HEAT;
356                                 }
357
358                         }
359                 } 
360                 
361                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null 
362                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
363                         for (IArtifactInfo unknownArtifact:vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts()) {
364                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
365                                         return ASDCConfiguration.HEAT_VOL;
366                                 }
367                         
368                         }
369                 }
370                 
371                 // Does not belong to anything
372                 return null;
373                         
374         }
375         
376         private static void createVnfResource(VfResourceStructure vfResourceStructure) {
377                 VnfResource vnfResource = new VnfResource();
378                 
379                 vnfResource.setAsdcUuid(vfResourceStructure.getResourceInstance().getResourceUUID());
380                 vnfResource.setDescription(vfResourceStructure.getNotification().getServiceDescription());
381                 
382                 vnfResource.setOrchestrationMode("HEAT");
383                 // Set the version but Version is stored into ASDC_SERVICE_MODEL_VERSION
384                 vnfResource.setVersion(BigDecimalVersion
385                                 .castAndCheckNotificationVersionToString(vfResourceStructure.getNotification().getServiceVersion()));
386                 vnfResource.setVnfType(VfResourceInstaller.createVNFName(vfResourceStructure));
387                 vnfResource.setModelVersion(BigDecimalVersion
388                                 .castAndCheckNotificationVersionToString(vfResourceStructure.getResourceInstance().getResourceVersion()));
389                 
390                 vnfResource.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID());
391                 vnfResource.setModelCustomizationName(vfResourceStructure.getResourceInstance().getResourceInstanceName());
392                 vnfResource.setModelName(vfResourceStructure.getResourceInstance().getResourceName());
393                 vnfResource.setServiceModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
394         
395                 vfResourceStructure.setCatalogVnfResource(vnfResource);
396         }
397
398         private static void createVfModule(VfModuleStructure vfModuleStructure,HeatTemplate heatMain, HeatTemplate heatVolume,HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
399                 VfModule vfModule = new VfModule();
400                 vfModule.setType(createVfModuleName(vfModuleStructure));
401                 vfModule.setAsdcUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelUUID());
402                 vfModule.setDescription(vfModuleStructure.getVfModuleMetadata().getVfModuleModelDescription());
403
404                 if (vfModuleStructure.getVfModuleMetadata().isBase()) {
405                         vfModule.setIsBase(1);
406                 } else {
407                         vfModule.setIsBase(0);
408                 }
409
410                 vfModule.setModelInvariantUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelInvariantUUID());
411                 vfModule.setModelName(vfModuleStructure.getVfModuleMetadata().getVfModuleModelName());
412                 
413                 vfModule.setVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(vfModuleStructure.getParentVfResource().getNotification().getServiceVersion()));
414                 vfModule.setModelVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(
415                                 vfModuleStructure.getVfModuleMetadata().getVfModuleModelVersion()));
416         
417                 vfModuleStructure.setCatalogVfModule(vfModule);
418                 
419                 VfResourceInstaller.createVfModuleLinks(vfModule, vfModuleStructure.getParentVfResource().getCatalogVnfResource(), heatMain,heatVolume, heatEnv,heatVolumeEnv);
420         }
421
422         private static void createVfModuleLinks(VfModule vfModule, VnfResource vnfResource, HeatTemplate heatMain,
423                         HeatTemplate heatVolume, HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
424                 
425                 if (heatMain !=null) {
426                         vfModule.setTemplateId(heatMain.getId());
427                 }
428                 if (heatEnv != null) {
429                         vfModule.setEnvironmentId(heatEnv.getId());
430                 }
431                 if (heatVolume != null) {
432                         vfModule.setVolTemplateId(heatVolume.getId());
433                 }
434                 if (heatVolumeEnv != null) {
435                         vfModule.setVolEnvironmentId(heatVolumeEnv.getId());
436                 }
437                 
438                 vfModule.setVnfResourceId(vnfResource.getId());
439
440         }
441
442         private static Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile) {
443
444                 // Scan the payload downloadResult and extract the HeatTemplate
445                 // parameters
446                 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
447                 return yamlEditor.getParameterList();
448
449         }
450
451         
452         public static String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure, List<String> listTypes) {
453                 String newFileBody = filebody;
454                 for (VfModuleArtifact moduleArtifact:vfResourceStructure.getArtifactsMapByUUID().values()) {
455                         
456                         if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
457                                 
458                                 newFileBody = verifyTheFilePrefixInString(newFileBody,moduleArtifact.getArtifactInfo().getArtifactName());
459                         }
460                 }
461                 return newFileBody;
462         }
463         
464         public static String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
465                 
466                 String needlePrefix = "file:///";
467                 String prefixedFilenameToVerify = needlePrefix+filenameToVerify;
468                 
469                 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) { 
470                         return body; 
471                 } 
472  
473                 StringBuffer sb = new StringBuffer(body.length()); 
474  
475                 int currentIndex = 0; 
476                 int startIndex = 0; 
477  
478                 while (currentIndex != -1) { 
479                         startIndex = currentIndex; 
480                         currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex); 
481  
482                         if (currentIndex == -1) { 
483                                 break; 
484                         } 
485  
486                         // We append from the startIndex up to currentIndex (start of File Name) 
487                         sb.append(body.substring(startIndex, currentIndex)); 
488                         sb.append(filenameToVerify); 
489  
490                         currentIndex += prefixedFilenameToVerify.length(); 
491                 } 
492  
493                 sb.append(body.substring(startIndex)); 
494  
495                 return sb.toString();
496         }
497         
498         private static void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
499                         VfModuleArtifact vfModuleArtifact) {
500                 HeatTemplate heatTemplate = new HeatTemplate();
501
502                 // TODO Set the label
503                 heatTemplate.setAsdcLabel("label");
504                 // Use the ResourceName of the ASDC template because the HEAT could be
505                 // reused
506                 heatTemplate.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
507                 heatTemplate.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
508                 
509                 List<String> typeList = new ArrayList<String>();
510                 typeList.add(ASDCConfiguration.HEAT_NESTED);
511                 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
512                 
513                 heatTemplate.setTemplateBody(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
514                 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
515
516                 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
517                         heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
518                 } else {
519                         heatTemplate.setTimeoutMinutes(240);
520                 }
521
522                 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
523                 heatTemplate.setVersion(BigDecimalVersion
524                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
525                 Set<HeatTemplateParam> heatParam = VfResourceInstaller
526                                 .extractHeatTemplateParameters(vfModuleArtifact.getResult());
527                 heatTemplate.setParameters(heatParam);
528
529                 vfModuleArtifact.setCatalogObject(heatTemplate);
530         }
531
532         private static void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
533                         VfModuleArtifact vfModuleArtifact) {
534                 HeatEnvironment heatEnvironment = new HeatEnvironment();
535
536                 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
537                 // TODO Set the label
538                 heatEnvironment.setAsdcLabel("Label");
539                 
540                 List<String> typeList = new ArrayList<String>();
541                 typeList.add(ASDCConfiguration.HEAT);
542                 typeList.add(ASDCConfiguration.HEAT_VOL);
543                 
544                 heatEnvironment.setEnvironment(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
545                 heatEnvironment.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
546                 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
547                 heatEnvironment.setVersion(BigDecimalVersion
548                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
549                 heatEnvironment.setAsdcResourceName(VfResourceInstaller.createVNFName(vfResourceStructure));
550
551                 vfModuleArtifact.setCatalogObject(heatEnvironment);
552                 
553         }
554
555         private static void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
556                         VfModuleArtifact vfModuleArtifact) {
557
558                 HeatFiles heatFile = new HeatFiles();
559                 // TODO Set the label
560                 heatFile.setAsdcLabel("Label");
561                 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
562                 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
563                 heatFile.setFileBody(vfModuleArtifact.getResult());
564                 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
565                 heatFile.setVersion(BigDecimalVersion
566                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
567
568                 heatFile.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
569                 vfModuleArtifact.setCatalogObject(heatFile);
570                 
571         }
572
573         private static void createService(VfResourceStructure vfResourceStructure) {
574                 
575                 Service service = new Service();
576                 service.setDescription(vfResourceStructure.getNotification().getServiceDescription());
577                 service.setServiceName(vfResourceStructure.getNotification().getServiceName());
578                 service.setServiceNameVersionId(vfResourceStructure.getNotification().getServiceUUID());
579                 service.setVersion(vfResourceStructure.getNotification().getServiceVersion());
580                 service.setModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
581                                 
582                 vfResourceStructure.setCatalogService(service);
583         }
584         
585         
586         private static String createVNFName(VfResourceStructure vfResourceStructure) {
587
588                 return vfResourceStructure.getNotification().getServiceName() + "/" + vfResourceStructure.getResourceInstance().getResourceInstanceName();
589         }
590
591         private static String createVfModuleName(VfModuleStructure vfModuleStructure) {
592                 
593                 return createVNFName(vfModuleStructure.getParentVfResource())+"::"+vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
594         }
595
596 }