[MSO-8] Update the maven dependency
[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 import java.sql.Timestamp;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.hibernate.exception.ConstraintViolationException;
33 import org.hibernate.exception.LockAcquisitionException;
34 import org.openecomp.sdc.api.notification.IArtifactInfo;
35
36 import org.openecomp.mso.asdc.client.ASDCConfiguration;
37 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
38 import org.openecomp.mso.asdc.installer.ASDCElementInfo;
39 import org.openecomp.mso.asdc.installer.BigDecimalVersion;
40 import org.openecomp.mso.asdc.installer.IVfResourceInstaller;
41 import org.openecomp.mso.asdc.installer.VfModuleArtifact;
42 import org.openecomp.mso.asdc.installer.VfModuleStructure;
43 import org.openecomp.mso.asdc.installer.VfResourceStructure;
44 import org.openecomp.mso.asdc.util.YamlEditor;
45 import org.openecomp.mso.db.catalog.CatalogDatabase;
46 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
47 import org.openecomp.mso.db.catalog.beans.HeatFiles;
48 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
49 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
50 import org.openecomp.mso.db.catalog.beans.NetworkResourceCustomization;
51 import org.openecomp.mso.db.catalog.beans.AllottedResourceCustomization;
52 import org.openecomp.mso.db.catalog.beans.Service;
53 import org.openecomp.mso.db.catalog.beans.ServiceToAllottedResources;
54 import org.openecomp.mso.db.catalog.beans.ServiceToNetworks;
55 import org.openecomp.mso.db.catalog.beans.VfModule;
56 import org.openecomp.mso.db.catalog.beans.VnfResource;
57 import org.openecomp.mso.logger.MessageEnum;
58 import org.openecomp.mso.logger.MsoLogger;
59
60 public class VfResourceInstaller implements IVfResourceInstaller {
61
62         private MsoLogger logger;
63
64         public VfResourceInstaller() {
65                 logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC);
66         }
67
68         @Override
69         public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct)
70                         throws ArtifactInstallerException {
71
72                 CatalogDatabase db = new CatalogDatabase();
73                 boolean status = false;
74                 VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;
75
76         try {
77
78                         String resourceType = vfResourceStruct.getResourceInstance().getResourceType();
79                         String category = vfResourceStruct.getResourceInstance().getCategory();
80
81                         // Check for duplicate VF Module that is not an Allotted Resource
82                         if(resourceType.equals("VF") && !category.equalsIgnoreCase("Allotted Resource")){
83                                 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "VNFResource",
84                                         VfResourceInstaller.createVNFName(vfResourceStructure),
85                                         BigDecimalVersion.castAndCheckNotificationVersionToString(
86                                                         vfResourceStructure.getNotification().getServiceVersion()), "", "");
87
88                                 VnfResource vnfResource = db.getVnfResource(
89                                         VfResourceInstaller.createVNFName(vfResourceStructure),
90                                         BigDecimalVersion.castAndCheckNotificationVersionToString(
91                                                         vfResourceStructure.getNotification().getServiceVersion()));
92
93                         if (vnfResource != null) {
94                                 status = true;
95
96                         }
97
98                         }
99
100                         // Check dup for VF Allotted Resource
101                         if(resourceType.equals("VF") && category.equalsIgnoreCase("Allotted Resource")){
102                                 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "AllottedResource",
103                                                 vfResourceStruct.getResourceInstance().getResourceInstanceName(),
104                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
105                                                                 vfResourceStructure.getNotification().getServiceVersion()), "", "");
106
107                                 List<AllottedResourceCustomization> allottedResources = db.getAllAllottedResourcesByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());
108
109                                 if(allottedResources != null && allottedResources.size() > 0){
110                                         for(AllottedResourceCustomization allottedResource : allottedResources){
111
112                                                 String existingAllottedResource = allottedResource.getModelCustomizationUuid();
113                                                 String notificationAllottedResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();
114
115                                                 if(existingAllottedResource.equals(notificationAllottedResource)){
116                                                         status=true;
117                                                         break;
118                                                 }
119                                         }
120
121                                 }
122                         }
123
124                         // Check Network for duplicates
125                         if(resourceType.equals("VL")){
126                                 logger.info(MessageEnum.ASDC_CHECK_HEAT_TEMPLATE, "NetworkResource",
127                                                 vfResourceStruct.getResourceInstance().getResourceInstanceName(),
128                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
129                                                                 vfResourceStructure.getNotification().getServiceVersion()), "", "");
130
131                                 List<NetworkResourceCustomization> networkResources = db.getAllNetworksByServiceModelUuid(vfResourceStruct.getNotification().getServiceUUID());
132
133                                 if(networkResources != null && networkResources.size() > 0){
134                                         for(NetworkResourceCustomization networkResource : networkResources){
135
136                                                 String existingNetworkResource = networkResource.getModelCustomizationUuid();
137                                                 String notificationNetworkResource = vfResourceStruct.getResourceInstance().getResourceCustomizationUUID();
138
139                                                 if(existingNetworkResource.equals(notificationNetworkResource)){
140                                                         status=true;
141                                                         break;
142                                                 }
143                                         }
144
145                                 }
146                         }
147
148                         if (status) {
149                                 logger.info(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED_DETAIL,
150                                                 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
151                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
152                                                 vfResourceStructure.getNotification().getServiceName(),
153                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
154                                                                 vfResourceStructure.getNotification().getServiceVersion()),
155                                                 vfResourceStructure.getNotification().getServiceUUID(),
156                                                 vfResourceStructure.getResourceInstance().getResourceName(),"", "");
157                         } else {
158                                 logger.info(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
159                                                 vfResourceStructure.getResourceInstance().getResourceInstanceName(),
160                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
161                                                 vfResourceStructure.getNotification().getServiceName(),
162                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
163                                                                 vfResourceStructure.getNotification().getServiceVersion()),
164                                                 vfResourceStructure.getNotification().getServiceUUID(),
165                                                 vfResourceStructure.getResourceInstance().getResourceName(),"", "");
166                         }
167
168                         return status;
169
170                 } catch (Exception e) {
171                         logger.error(MessageEnum.ASDC_ARTIFACT_CHECK_EXC, "", "", MsoLogger.ErrorCode.SchemaError, "Exception - isResourceAlreadyDeployed");
172                         throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
173                 }
174         }
175
176         @Override
177         public void installTheResource(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {
178
179                 // 1. Add the DB object list (Hashed) to be created from the HashMap
180                 // UUID
181                 // The DB objects will be stored in each VfModuleArtifact objects
182                 // Those objects could be reused by different VfModule
183
184                 VfResourceStructure vfResourceStructure = (VfResourceStructure)vfResourceStruct;
185
186                 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
187
188                         switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
189                         case ASDCConfiguration.HEAT:
190                         case ASDCConfiguration.HEAT_VOL:
191                         case ASDCConfiguration.HEAT_NESTED:
192                                 VfResourceInstaller.createHeatTemplateFromArtifact(vfResourceStructure, vfModuleArtifact);
193                                 break;
194                         case ASDCConfiguration.HEAT_ENV:
195                                 VfResourceInstaller.createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
196                                 break;
197                         case ASDCConfiguration.HEAT_ARTIFACT:
198                                 VfResourceInstaller.createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact);
199                                 break;
200                         case ASDCConfiguration.HEAT_NET:
201                         case ASDCConfiguration.OTHER:
202                                 logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT, vfModuleArtifact.getArtifactInfo().getArtifactType()+"(Artifact Name:"+vfModuleArtifact.getArtifactInfo().getArtifactName()+")", "", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
203                                 break;
204                         default:
205                                 break;
206
207                         }
208                 }
209
210                 // in case of deployment failure, use a string that will represent the type of artifact that failed...
211                 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
212
213                 CatalogDatabase catalogDB = new CatalogDatabase();
214                 // 2. Create the VFModules/VNFResource objects by linking them to the
215                 // objects created before and store them in Resource/module structure
216                 // Opening a DB transaction, starting from here
217                 try {
218
219                         VfResourceInstaller.createService(vfResourceStructure);
220
221                         String resourceType = vfResourceStructure.getResourceInstance().getResourceType();
222                         String resourceCategory = vfResourceStructure.getResourceInstance().getCategory();
223
224                         if(resourceType.equals("VF")){
225
226                                 if(resourceCategory.equalsIgnoreCase("Allotted Resource")){
227                                         VfResourceInstaller.createAllottedResourceCustomization(vfResourceStructure);
228                                         catalogDB.saveAllottedResourceCustomization(vfResourceStructure.getCatalogResourceCustomization());
229                                 } else {
230                                         VfResourceInstaller.createVnfResource(vfResourceStructure);
231                                         catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
232                                 }
233                         }
234
235                         if(resourceType.equals("VL")){
236                                 VfResourceInstaller.createNetworkResourceCustomization(vfResourceStructure);
237                                 catalogDB.saveNetworkResourceCustomization(vfResourceStructure.getCatalogNetworkResourceCustomization());
238                         }
239
240                         // Add this one for logging
241                         artifactListForLogging.add(ASDCElementInfo.createElementFromVfResourceStructure(vfResourceStructure));
242
243                         //catalogDB.saveOrUpdateVnfResource(vfResourceStructure.getCatalogVnfResource());
244                         catalogDB.saveService(vfResourceStructure.getCatalogService());
245
246                         // Now that the service has been added we can populate the Service_to_AllottedResources table
247                         if(resourceType.equals("VF") && resourceCategory.equalsIgnoreCase("Allotted Resource")){
248                                 catalogDB.saveServiceToAllottedResources(vfResourceStructure.getCatalogServiceToAllottedResources());
249                         }
250
251                         // Now that the service has been added we can populate the Service_to_Network table
252                         if(resourceType.equals("VL")){
253                                 catalogDB.saveServiceToNetworks(vfResourceStructure.getCatalogServiceToNetworks());
254                         }
255
256                         for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {
257
258                                 // Here we set the right db structure according to the Catalog
259                                 // DB
260
261                                 // We expect only one MAIN HEAT per VFMODULE
262                                 // we can also obtain from it the Env ArtifactInfo, that's why
263                                 // we
264                                 // get the Main IArtifactInfo
265
266                                 HeatTemplate heatMainTemplate = null;
267                                 HeatEnvironment heatEnv = null;
268
269                                 HeatTemplate heatVolumeTemplate = null;
270                                 HeatEnvironment heatVolumeEnv = null;
271
272                                 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT)) {
273                                         IArtifactInfo mainEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT)
274                                                         .get(0).getArtifactInfo().getGeneratedArtifact();
275
276                                         // MAIN HEAT
277                                         heatMainTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
278                                                         .get(ASDCConfiguration.HEAT).get(0).getCatalogObject();
279
280                                         // Add this one for logging
281                                         artifactListForLogging.add(ASDCElementInfo
282                                                         .createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo()));
283
284                                         catalogDB.saveHeatTemplate(heatMainTemplate, heatMainTemplate.getParameters());
285                                         // Indicate we have deployed it in the DB
286                                         vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).incrementDeployedInDB();
287
288
289                                         // VOLUME HEAT
290                                         // We expect only one VOL HEAT per VFMODULE
291                                         // we can also obtain from it the Env ArtifactInfo, that's why
292                                         // we get the Volume IArtifactInfo
293
294                                         if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_VOL)) {
295                                                 IArtifactInfo volEnvArtifactInfo = vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0)
296                                                                 .getArtifactInfo().getGeneratedArtifact();
297
298                                                 heatVolumeTemplate = (HeatTemplate) vfModuleStructure.getArtifactsMap()
299                                                                 .get(ASDCConfiguration.HEAT_VOL).get(0).getCatalogObject();
300
301                                                 // Add this one for logging
302                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()));
303
304                                                 catalogDB.saveHeatTemplate(heatVolumeTemplate, heatVolumeTemplate.getParameters());
305                                                 // Indicate we have deployed it in the DB
306                                                 vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).incrementDeployedInDB();
307
308                                                 if (volEnvArtifactInfo != null) {
309                                                         heatVolumeEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
310                                                                         .get(volEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
311
312                                                         // Add this one for logging
313                                                         artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(volEnvArtifactInfo));
314
315                                                         catalogDB.saveHeatEnvironment(heatVolumeEnv);
316                                                         // Indicate we have deployed it in the DB
317                                                         vfResourceStructure.getArtifactsMapByUUID().get(volEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
318                                                 }
319
320                                         }
321
322                                         // NESTED HEAT
323                                         // Here we expect many HEAT_NESTED template to be there
324                                         // check first if we really have nested heat templates
325                                         if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_NESTED)) {
326                                                 for (VfModuleArtifact heatNestedArtifact : vfModuleStructure.getArtifactsMap()
327                                                                 .get(ASDCConfiguration.HEAT_NESTED)) {
328
329                                                         // Check if this nested is well referenced by the MAIN HEAT
330                                                         String parentArtifactType = VfResourceInstaller.identifyParentOfNestedTemplate(vfModuleStructure,heatNestedArtifact);
331                                                         HeatTemplate heatNestedTemplate = (HeatTemplate) heatNestedArtifact.getCatalogObject();
332
333                                                         if (parentArtifactType != null) {
334
335                                                                 switch (parentArtifactType) {
336                                                                         case ASDCConfiguration.HEAT:
337
338                                                                                 // Add this one for logging
339                                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
340
341                                                                                 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
342                                                                                 // Indicate we have deployed it in the DB
343                                                                                 heatNestedArtifact.incrementDeployedInDB();
344                                                                                 break;
345                                                                         case ASDCConfiguration.HEAT_VOL:
346
347                                                                                 // Add this one for logging
348                                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
349                                                                                 catalogDB.saveNestedHeatTemplate (heatVolumeTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
350                                                                                 // Indicate we have deployed it in the DB
351                                                                                 heatNestedArtifact.incrementDeployedInDB();
352                                                                                 break;
353
354                                                                         default:
355                                                                                 break;
356
357                                                                 }
358                                                         } else { // Assume it belongs to HEAT MAIN
359                                                                 // Add this one for logging
360                                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatNestedArtifact.getArtifactInfo()));
361
362                                                                 catalogDB.saveNestedHeatTemplate (heatMainTemplate.getId(), heatNestedTemplate, heatNestedTemplate.getTemplateName());
363                                                                 // Indicate we have deployed it in the DB
364                                                                 heatNestedArtifact.incrementDeployedInDB();
365                                                         }
366                                                 }
367                                         }
368
369                                         if (mainEnvArtifactInfo != null) {
370                                                 heatEnv = (HeatEnvironment) vfResourceStructure.getArtifactsMapByUUID()
371                                                                 .get(mainEnvArtifactInfo.getArtifactUUID()).getCatalogObject();
372
373                                                 // Add this one for logging
374                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(mainEnvArtifactInfo));
375
376                                                 catalogDB.saveHeatEnvironment(heatEnv);
377                                                 // Indicate we have deployed it in the DB
378                                                 vfResourceStructure.getArtifactsMapByUUID().get(mainEnvArtifactInfo.getArtifactUUID()).incrementDeployedInDB();
379                                         }
380
381                                 }
382
383
384                                 // here we expect one VFModule to be there
385                                 VfResourceInstaller.createVfModule(vfModuleStructure,heatMainTemplate, heatVolumeTemplate, heatEnv, heatVolumeEnv);
386                                 VfModule vfModule = vfModuleStructure.getCatalogVfModule();
387
388                                 // Add this one for logging
389                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfModuleStructure(vfModuleStructure));
390
391                                 catalogDB.saveOrUpdateVfModule(vfModule);
392
393                                 // Here we expect many HEAT_TEMPLATE files to be there
394                                 if (vfModuleStructure.getArtifactsMap().containsKey(ASDCConfiguration.HEAT_ARTIFACT)) {
395                                         for (VfModuleArtifact heatArtifact : vfModuleStructure.getArtifactsMap()
396                                                         .get(ASDCConfiguration.HEAT_ARTIFACT)) {
397
398                                                 HeatFiles heatFile = (HeatFiles) heatArtifact.getCatalogObject();
399
400                                                 // Add this one for logging
401                                                 artifactListForLogging.add(ASDCElementInfo.createElementFromVfArtifactInfo(heatArtifact.getArtifactInfo()));
402
403
404                                                 catalogDB.saveVfModuleToHeatFiles (vfModule.getId(), heatFile);
405                                                 // Indicate we will deploy it in the DB
406                                                 heatArtifact.incrementDeployedInDB();
407                                         }
408                                 }
409
410                         }
411
412                         catalogDB.commit();
413                         vfResourceStructure.setSuccessfulDeployment();
414
415                 } catch (Exception e) {
416
417                         Throwable dbExceptionToCapture = e;
418                         while (!(dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException)
419                                         && (dbExceptionToCapture.getCause() != null)) {
420                                 dbExceptionToCapture = dbExceptionToCapture.getCause();
421                         }
422
423                         if (dbExceptionToCapture instanceof ConstraintViolationException || dbExceptionToCapture instanceof LockAcquisitionException) {
424                                 logger.warn(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED, vfResourceStructure.getResourceInstance().getResourceName(),
425                                                 vfResourceStructure.getNotification().getServiceVersion(), "", "", MsoLogger.ErrorCode.DataError, "Exception - ASCDC Artifact already deployed", e);
426                         } else {
427                                 String endEvent = "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.";
428                                 String elementToLog = (artifactListForLogging.size() > 0 ? artifactListForLogging.get(artifactListForLogging.size()-1).toString() : "No element listed");
429                                 logger.error(MessageEnum.ASDC_ARTIFACT_INSTALL_EXC, elementToLog, "", "", MsoLogger.ErrorCode.DataError, "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback", e);
430                                 catalogDB.rollback();
431                                 throw new ArtifactInstallerException(
432                                                 "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
433                         }
434
435                 } finally {
436                         catalogDB.close();
437                         // Debug log the whole collection...
438                         logger.debug(artifactListForLogging.toString());
439                 }
440
441         }
442
443         private static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,VfModuleArtifact heatNestedArtifact) {
444
445                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null
446                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
447                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
448                                         .getArtifactInfo().getRelatedArtifacts()) {
449                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
450                                         return ASDCConfiguration.HEAT;
451                                 }
452
453                         }
454                 }
455
456                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null
457                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
458                         for (IArtifactInfo unknownArtifact:vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo().getRelatedArtifacts()) {
459                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
460                                         return ASDCConfiguration.HEAT_VOL;
461                                 }
462
463                         }
464                 }
465
466                 // Does not belong to anything
467                 return null;
468
469         }
470
471         private static void createVnfResource(VfResourceStructure vfResourceStructure) {
472                 VnfResource vnfResource = new VnfResource();
473
474                 vnfResource.setAsdcUuid(vfResourceStructure.getResourceInstance().getResourceUUID());
475                 vnfResource.setDescription(vfResourceStructure.getNotification().getServiceDescription());
476
477                 vnfResource.setOrchestrationMode("HEAT");
478                 // Set the version but Version is stored into ASDC_SERVICE_MODEL_VERSION
479                 vnfResource.setVersion(BigDecimalVersion
480                                 .castAndCheckNotificationVersionToString(vfResourceStructure.getNotification().getServiceVersion()));
481                 vnfResource.setVnfType(VfResourceInstaller.createVNFName(vfResourceStructure));
482                 vnfResource.setModelVersion(BigDecimalVersion
483                                 .castAndCheckNotificationVersionToString(vfResourceStructure.getResourceInstance().getResourceVersion()));
484
485                 vnfResource.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID());
486                 vnfResource.setModelCustomizationName(vfResourceStructure.getResourceInstance().getResourceInstanceName());
487                 vnfResource.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID());
488                 vnfResource.setModelName(vfResourceStructure.getResourceInstance().getResourceName());
489                 vnfResource.setServiceModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
490                 //vnfResource.setCreated(getCurrentTimeStamp());
491
492                 vfResourceStructure.setCatalogVnfResource(vnfResource);
493         }
494
495         private static void createNetworkResourceCustomization(VfResourceStructure vfResourceStructure) {
496                 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
497
498                 networkResourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
499                 networkResourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
500                 networkResourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
501                 networkResourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
502                 networkResourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
503                 networkResourceCustomization.setModelVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
504                 //networkResourceCustomization.setCreated(getCurrentTimeStamp());
505
506                 vfResourceStructure.setCatalogNetworkResourceCustomization(networkResourceCustomization);
507
508                 ServiceToNetworks serviceNetworks = new ServiceToNetworks();
509                 serviceNetworks.setNetworkModelCustomizationUuid(networkResourceCustomization.getModelCustomizationUuid());
510                 serviceNetworks.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());
511
512                 vfResourceStructure.setCatalogServiceToNetworks(serviceNetworks);
513
514         }
515
516         private static void createAllottedResourceCustomization(VfResourceStructure vfResourceStructure) {
517                 AllottedResourceCustomization resourceCustomization = new AllottedResourceCustomization();
518
519                 resourceCustomization.setModelCustomizationUuid(vfResourceStructure.getResourceInstance().getResourceCustomizationUUID().trim());
520                 resourceCustomization.setModelName(vfResourceStructure.getResourceInstance().getResourceName().trim());
521                 resourceCustomization.setModelInstanceName(vfResourceStructure.getResourceInstance().getResourceInstanceName().trim());
522                 resourceCustomization.setModelInvariantUuid(vfResourceStructure.getResourceInstance().getResourceInvariantUUID().trim());
523                 resourceCustomization.setModelUuid(vfResourceStructure.getResourceInstance().getResourceUUID().trim());
524                 resourceCustomization.setVersion(vfResourceStructure.getResourceInstance().getResourceVersion().trim());
525                 //resourceCustomization.setCreated(getCurrentTimeStamp());
526
527                 vfResourceStructure.setCatalogResourceCustomization(resourceCustomization);
528
529                 ServiceToAllottedResources serviceAllottedResources = new ServiceToAllottedResources();
530                 serviceAllottedResources.setArModelCustomizationUuid(resourceCustomization.getModelCustomizationUuid());
531                 serviceAllottedResources.setServiceModelUuid(vfResourceStructure.getNotification().getServiceUUID());
532
533                 vfResourceStructure.setCatalogServiceToAllottedResources(serviceAllottedResources);
534
535         }
536
537         private static void createVfModule(VfModuleStructure vfModuleStructure,HeatTemplate heatMain, HeatTemplate heatVolume,HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
538                 VfModule vfModule = new VfModule();
539                 vfModule.setType(createVfModuleName(vfModuleStructure));
540                 vfModule.setAsdcUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelUUID());
541                 vfModule.setDescription(vfModuleStructure.getVfModuleMetadata().getVfModuleModelDescription());
542
543                 if (vfModuleStructure.getVfModuleMetadata().isBase()) {
544                         vfModule.setIsBase(1);
545                 } else {
546                         vfModule.setIsBase(0);
547                 }
548
549                 vfModule.setModelCustomizationUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelCustomizationUUID());
550                 vfModule.setModelInvariantUuid(vfModuleStructure.getVfModuleMetadata().getVfModuleModelInvariantUUID());
551                 vfModule.setModelName(vfModuleStructure.getVfModuleMetadata().getVfModuleModelName());
552
553                 vfModule.setVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(vfModuleStructure.getParentVfResource().getNotification().getServiceVersion()));
554                 vfModule.setModelVersion(BigDecimalVersion.castAndCheckNotificationVersionToString(
555                                 vfModuleStructure.getVfModuleMetadata().getVfModuleModelVersion()));
556
557                 Map<String,String> map = vfModuleStructure.getVfModuleMetadata().getProperties();
558
559                 if(map != null){
560
561                         if(map.get("vf_module_label") != null){
562                                 vfModule.setLabel(map.get("vf_module_label"));
563                         }
564                         if(map.get("initial_count") != null && map.get("initial_count").length() > 0){
565                                 vfModule.setInitialCount(Integer.parseInt(map.get("initial_count")));
566                         }
567                         if(map.get("min_vf_module_instances") != null && map.get("min_vf_module_instances").length() > 0){
568                                 vfModule.setMinInstances(Integer.parseInt(map.get("min_vf_module_instances")));
569                         }
570                         if(map.get("max_vf_module_instances") != null && map.get("max_vf_module_instances").length() > 0){
571                                 vfModule.setMaxInstances(Integer.parseInt(map.get("max_vf_module_instances")));
572                         }
573
574                 }
575
576                 vfModuleStructure.setCatalogVfModule(vfModule);
577
578                 VfResourceInstaller.createVfModuleLinks(vfModule, vfModuleStructure.getParentVfResource().getCatalogVnfResource(), heatMain,heatVolume, heatEnv,heatVolumeEnv);
579         }
580
581         private static void createVfModuleLinks(VfModule vfModule, VnfResource vnfResource, HeatTemplate heatMain,
582                         HeatTemplate heatVolume, HeatEnvironment heatEnv, HeatEnvironment heatVolumeEnv) {
583
584                 if (heatMain !=null) {
585                         vfModule.setTemplateId(heatMain.getId());
586                 }
587                 if (heatEnv != null) {
588                         vfModule.setEnvironmentId(heatEnv.getId());
589                 }
590                 if (heatVolume != null) {
591                         vfModule.setVolTemplateId(heatVolume.getId());
592                 }
593                 if (heatVolumeEnv != null) {
594                         vfModule.setVolEnvironmentId(heatVolumeEnv.getId());
595                 }
596
597                 vfModule.setVnfResourceId(vnfResource.getId());
598
599         }
600
601         private static Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile) {
602
603                 // Scan the payload downloadResult and extract the HeatTemplate
604                 // parameters
605                 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
606                 return yamlEditor.getParameterList();
607
608         }
609
610
611         public static String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure, List<String> listTypes) {
612                 String newFileBody = filebody;
613                 for (VfModuleArtifact moduleArtifact:vfResourceStructure.getArtifactsMapByUUID().values()) {
614
615                         if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
616
617                                 newFileBody = verifyTheFilePrefixInString(newFileBody,moduleArtifact.getArtifactInfo().getArtifactName());
618                         }
619                 }
620                 return newFileBody;
621         }
622
623         public static String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
624
625                 String needlePrefix = "file:///";
626                 String prefixedFilenameToVerify = needlePrefix+filenameToVerify;
627
628                 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
629                         return body;
630                 }
631
632                 StringBuffer sb = new StringBuffer(body.length());
633
634                 int currentIndex = 0;
635                 int startIndex = 0;
636
637                 while (currentIndex != -1) {
638                         startIndex = currentIndex;
639                         currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);
640
641                         if (currentIndex == -1) {
642                                 break;
643                         }
644
645                         // We append from the startIndex up to currentIndex (start of File Name)
646                         sb.append(body.substring(startIndex, currentIndex));
647                         sb.append(filenameToVerify);
648
649                         currentIndex += prefixedFilenameToVerify.length();
650                 }
651
652                 sb.append(body.substring(startIndex));
653
654                 return sb.toString();
655         }
656
657         private static void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
658                         VfModuleArtifact vfModuleArtifact) {
659                 HeatTemplate heatTemplate = new HeatTemplate();
660
661                 // TODO Set the label
662                 heatTemplate.setAsdcLabel("label");
663                 // Use the ResourceName of the ASDC template because the HEAT could be
664                 // reused
665                 heatTemplate.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
666                 heatTemplate.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
667
668                 List<String> typeList = new ArrayList<String>();
669                 typeList.add(ASDCConfiguration.HEAT_NESTED);
670                 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
671
672                 heatTemplate.setTemplateBody(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
673                 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
674
675                 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
676                         heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
677                 } else {
678                         heatTemplate.setTimeoutMinutes(240);
679                 }
680
681                 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
682                 heatTemplate.setVersion(BigDecimalVersion
683                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
684
685                 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
686                         heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
687                 } else {
688                         heatTemplate.setArtifactChecksum("MANUAL_RECORD");
689                 }
690
691                 Set<HeatTemplateParam> heatParam = VfResourceInstaller
692                                 .extractHeatTemplateParameters(vfModuleArtifact.getResult());
693                 heatTemplate.setParameters(heatParam);
694
695                 vfModuleArtifact.setCatalogObject(heatTemplate);
696         }
697
698         private static void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
699                         VfModuleArtifact vfModuleArtifact) {
700                 HeatEnvironment heatEnvironment = new HeatEnvironment();
701
702                 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
703                 // TODO Set the label
704                 heatEnvironment.setAsdcLabel("Label");
705
706                 List<String> typeList = new ArrayList<String>();
707                 typeList.add(ASDCConfiguration.HEAT);
708                 typeList.add(ASDCConfiguration.HEAT_VOL);
709
710                 heatEnvironment.setEnvironment(verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(),vfResourceStructure,typeList));
711                 heatEnvironment.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
712                 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
713                 heatEnvironment.setVersion(BigDecimalVersion
714                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
715                 heatEnvironment.setAsdcResourceName(VfResourceInstaller.createVNFName(vfResourceStructure));
716
717                 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
718                         heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
719                 } else{
720                         heatEnvironment.setArtifactChecksum("MANUAL_RECORD");
721                 }
722
723                 vfModuleArtifact.setCatalogObject(heatEnvironment);
724
725         }
726
727         private static void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
728                         VfModuleArtifact vfModuleArtifact) {
729
730                 HeatFiles heatFile = new HeatFiles();
731                 // TODO Set the label
732                 heatFile.setAsdcLabel("Label");
733                 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
734                 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
735                 heatFile.setFileBody(vfModuleArtifact.getResult());
736                 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
737                 heatFile.setVersion(BigDecimalVersion
738                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
739
740                 heatFile.setAsdcResourceName(vfResourceStructure.getResourceInstance().getResourceName());
741
742                 if(vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null){
743                         heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
744                 } else {
745                         heatFile.setArtifactChecksum("MANUAL_RECORD");
746                 }
747
748                 vfModuleArtifact.setCatalogObject(heatFile);
749
750         }
751
752         private static void createService(VfResourceStructure vfResourceStructure) {
753
754                 Service service = new Service();
755                 service.setDescription(vfResourceStructure.getNotification().getServiceDescription());
756                 service.setServiceName(vfResourceStructure.getNotification().getServiceName());
757                 service.setServiceNameVersionId(vfResourceStructure.getNotification().getServiceUUID());
758                 service.setVersion(vfResourceStructure.getNotification().getServiceVersion());
759                 service.setModelInvariantUUID(vfResourceStructure.getNotification().getServiceInvariantUUID());
760
761                 vfResourceStructure.setCatalogService(service);
762         }
763
764
765         private static String createVNFName(VfResourceStructure vfResourceStructure) {
766
767                 return vfResourceStructure.getNotification().getServiceName() + "/" + vfResourceStructure.getResourceInstance().getResourceInstanceName();
768         }
769
770         private static String createVfModuleName(VfModuleStructure vfModuleStructure) {
771
772                 return createVNFName(vfModuleStructure.getParentVfResource())+"::"+vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
773         }
774
775
776         private static Timestamp getCurrentTimeStamp() {
777
778                 return new Timestamp(new Date().getTime());
779         }
780
781
782 }