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