Containerization feature of SO
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / installer / heat / ToscaResourceInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.asdc.installer.heat;
23
24
25 import java.sql.Timestamp;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.Set;
32
33 import org.hibernate.exception.ConstraintViolationException;
34 import org.hibernate.exception.LockAcquisitionException;
35 import org.onap.sdc.api.notification.IArtifactInfo;
36 import org.onap.sdc.api.notification.IStatusData;
37 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
38 import org.onap.sdc.tosca.parser.impl.SdcTypes;
39 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
40 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
41 import org.onap.sdc.toscaparser.api.Group;
42 import org.onap.sdc.toscaparser.api.NodeTemplate;
43 import org.onap.sdc.toscaparser.api.RequirementAssignment;
44 import org.onap.sdc.toscaparser.api.elements.Metadata;
45 import org.onap.sdc.utils.DistributionStatusEnum;
46 import org.onap.so.asdc.client.ASDCConfiguration;
47 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
48 import org.onap.so.asdc.installer.ASDCElementInfo;
49 import org.onap.so.asdc.installer.BigDecimalVersion;
50 import org.onap.so.asdc.installer.IVfModuleData;
51 import org.onap.so.asdc.installer.ToscaResourceStructure;
52 import org.onap.so.asdc.installer.VfModuleArtifact;
53 import org.onap.so.asdc.installer.VfModuleStructure;
54 import org.onap.so.asdc.installer.VfResourceStructure;
55 import org.onap.so.asdc.util.YamlEditor;
56 import org.onap.so.db.catalog.beans.AllottedResource;
57 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
58 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
59 import org.onap.so.db.catalog.beans.CollectionResource;
60 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
61 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
62 import org.onap.so.db.catalog.beans.ConfigurationResource;
63 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
64 import org.onap.so.db.catalog.beans.ExternalServiceToInternalService;
65 import org.onap.so.db.catalog.beans.HeatEnvironment;
66 import org.onap.so.db.catalog.beans.HeatFiles;
67 import org.onap.so.db.catalog.beans.HeatTemplate;
68 import org.onap.so.db.catalog.beans.HeatTemplateParam;
69 import org.onap.so.db.catalog.beans.InstanceGroupType;
70 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
71 import org.onap.so.db.catalog.beans.NetworkInstanceGroup;
72 import org.onap.so.db.catalog.beans.NetworkResource;
73 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
74 import org.onap.so.db.catalog.beans.Service;
75 import org.onap.so.db.catalog.beans.ServiceProxyResource;
76 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
77 import org.onap.so.db.catalog.beans.SubType;
78 import org.onap.so.db.catalog.beans.TempNetworkHeatTemplateLookup;
79 import org.onap.so.db.catalog.beans.ToscaCsar;
80 import org.onap.so.db.catalog.beans.VFCInstanceGroup;
81 import org.onap.so.db.catalog.beans.VfModule;
82 import org.onap.so.db.catalog.beans.VfModuleCustomization;
83 import org.onap.so.db.catalog.beans.VnfResource;
84 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
85 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
86 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
87 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
88 import org.onap.so.db.catalog.data.repository.CollectionResourceCustomizationRepository;
89 import org.onap.so.db.catalog.data.repository.CollectionResourceRepository;
90 import org.onap.so.db.catalog.data.repository.ConfigurationResourceCustomizationRepository;
91 import org.onap.so.db.catalog.data.repository.ExternalServiceToInternalServiceRepository;
92 import org.onap.so.db.catalog.data.repository.HeatTemplateRepository;
93 import org.onap.so.db.catalog.data.repository.InstanceGroupRepository;
94 import org.onap.so.db.catalog.data.repository.NetworkResourceCustomizationRepository;
95 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
96 import org.onap.so.db.catalog.data.repository.ServiceProxyResourceCustomizationRepository;
97 import org.onap.so.db.catalog.data.repository.ServiceRepository;
98 import org.onap.so.db.catalog.data.repository.TempNetworkHeatTemplateRepository;
99 import org.onap.so.db.catalog.data.repository.VFModuleCustomizationRepository;
100 import org.onap.so.db.catalog.data.repository.VFModuleRepository;
101 import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
102 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
103 import org.onap.so.db.catalog.data.repository.VnfcInstanceGroupCustomizationRepository;
104 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
105 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
106 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
107 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
108 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
109 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
110 import org.onap.so.logger.MessageEnum;
111 import org.onap.so.logger.MsoLogger;
112 import org.springframework.beans.factory.annotation.Autowired;
113 import org.springframework.stereotype.Component;
114 import org.springframework.transaction.annotation.Transactional;
115
116 @Component
117 public class ToscaResourceInstaller {
118
119         protected static final String ALLOTTED_RESOURCE = "Allotted Resource";
120
121         protected static final String MULTI_STAGE_DESIGN = "multi_stage_design";
122
123         protected static final String SCALABLE = "scalable";
124
125         protected static final String BASIC = "BASIC";
126
127         protected static final String PROVIDER = "PROVIDER";
128
129         protected static final String HEAT = "HEAT";
130
131         protected static final String MANUAL_RECORD = "MANUAL_RECORD";
132
133         protected static final String MSO = "SO";
134
135
136         @Autowired
137         protected ServiceRepository serviceRepo;
138         
139         @Autowired
140         protected InstanceGroupRepository instanceGroupRepo;
141         
142         @Autowired
143         protected ServiceProxyResourceCustomizationRepository serviceProxyCustomizationRepo;
144         
145         @Autowired
146         protected CollectionResourceRepository collectionRepo;
147         
148         @Autowired
149         protected CollectionResourceCustomizationRepository collectionCustomizationRepo;
150         
151         @Autowired
152         protected ConfigurationResourceCustomizationRepository configCustomizationRepo;
153
154         @Autowired
155         protected VnfResourceRepository vnfRepo;
156
157         @Autowired
158         protected VnfCustomizationRepository vnfCustomizationRepo;
159         
160         @Autowired
161         protected VFModuleRepository vfModuleRepo;
162
163         @Autowired
164         protected VFModuleCustomizationRepository vfModuleCustomizationRepo;    
165         
166         @Autowired
167         protected VnfcInstanceGroupCustomizationRepository vnfcInstanceGroupCustomizationRepo;  
168
169         @Autowired
170         protected AllottedResourceRepository allottedRepo;
171
172         @Autowired
173         protected AllottedResourceCustomizationRepository allottedCustomizationRepo;
174
175         @Autowired
176         protected NetworkResourceRepository networkRepo;
177          
178         @Autowired
179         protected HeatTemplateRepository heatRepo;
180
181         @Autowired
182         protected NetworkResourceCustomizationRepository networkCustomizationRepo;
183
184         @Autowired
185         protected WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
186         @Autowired
187         protected WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
188         @Autowired
189         protected WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;     
190         
191         @Autowired
192         protected TempNetworkHeatTemplateRepository tempNetworkLookupRepo;
193         
194         @Autowired
195         protected ExternalServiceToInternalServiceRepository externalServiceToInternalServiceRepository;
196         
197         protected static final MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,ToscaResourceInstaller.class);
198
199         public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {
200                 boolean status = false;
201                 VfResourceStructure vfResourceStructure = vfResourceStruct;
202                 try {
203                     status = vfResourceStructure.isDeployedSuccessfully();
204                 } catch (RuntimeException e) {
205                     status = false;
206                 }
207                 try {                                   
208                         Service existingService = serviceRepo.findOneByModelUUID(vfResourceStructure.getNotification().getServiceUUID()); 
209                         if(existingService != null)
210                                 status = true;
211                         if (status) {
212                                 logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
213                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
214                                                 vfResourceStructure.getNotification().getServiceName(),
215                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
216                                                                 vfResourceStructure.getNotification().getServiceVersion()),
217                                                 vfResourceStructure.getNotification().getServiceUUID(),
218                                                 vfResourceStructure.getResourceInstance().getResourceName(), "", "");
219                                 WatchdogComponentDistributionStatus wdStatus = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
220                                 wdStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
221                                 watchdogCDStatusRepository.saveAndFlush(wdStatus);
222                         } else {                        
223                                 logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
224                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
225                                                 vfResourceStructure.getNotification().getServiceName(),
226                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
227                                                                 vfResourceStructure.getNotification().getServiceVersion()),
228                                                 vfResourceStructure.getNotification().getServiceUUID(),
229                                                 vfResourceStructure.getResourceInstance().getResourceName(), "", "");
230                         }
231                         return status;
232                 } catch (Exception e) {
233                         logger.error(MessageEnum.ASDC_ARTIFACT_CHECK_EXC, "", "", MsoLogger.ErrorCode.SchemaError,
234                                         "Exception - isResourceAlreadyDeployed");
235                         throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
236                 }
237         }
238
239         public void installTheComponentStatus(IStatusData iStatus) throws ArtifactInstallerException {
240                 logger.debug("Entering installTheComponentStatus for distributionId " + iStatus.getDistributionID()
241                                 + " and ComponentName " + iStatus.getComponentName());
242
243                 try {
244                         WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(iStatus.getDistributionID(),
245                                         iStatus.getComponentName());
246                         cdStatus.setComponentDistributionStatus(iStatus.getStatus().toString());
247                         watchdogCDStatusRepository.save(cdStatus);
248
249                 } catch (Exception e) {
250                         logger.debug("Exception caught in installTheComponentStatus " + e.getMessage());
251                         throw new ArtifactInstallerException("Exception caught in installTheComponentStatus " + e.getMessage());
252                 }
253         }
254
255         @Transactional(rollbackFor = { ArtifactInstallerException.class })
256         public void installTheResource(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStruct)
257                         throws ArtifactInstallerException {             
258                 VfResourceStructure vfResourceStructure = vfResourceStruct;
259                 extractHeatInformation(toscaResourceStruct, vfResourceStructure);       
260
261                 // PCLO: in case of deployment failure, use a string that will represent
262                 // the type of artifact that failed...
263                 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
264                 try {
265                         createToscaCsar(toscaResourceStruct);                   
266                         createService(toscaResourceStruct, vfResourceStruct);                   
267                         Service service = toscaResourceStruct.getCatalogService();                              
268                         List<NodeTemplate> vfNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceVfList();
269                         
270                 
271                         for (NodeTemplate nodeTemplate : vfNodeTemplatesList) { 
272                                 
273                                 Metadata metadata = nodeTemplate.getMetaData();         
274                                 String serviceType = toscaResourceStruct.getCatalogService().getServiceType();                  
275                                 String vfCustomizationCategory = toscaResourceStruct.getSdcCsarHelper()
276                                                 .getMetadataPropertyValue(metadata, SdcPropertyNames.PROPERTY_NAME_CATEGORY);
277                                 processVfModules(toscaResourceStruct, vfResourceStructure, service, nodeTemplate, metadata,
278                                                 vfCustomizationCategory);
279                         }
280
281                         List<NodeTemplate> allottedResourceList = toscaResourceStruct.getSdcCsarHelper().getAllottedResources();
282                         processAllottedResources(toscaResourceStruct, service, allottedResourceList);
283                         processNetworks(toscaResourceStruct, service);  
284                         // process Network Collections
285                         processNetworkCollections(toscaResourceStruct, service);
286                         // Process Service Proxy & Configuration
287                         processServiceProxyAndConfiguration(toscaResourceStruct, service);
288                         
289                         serviceRepo.save(service);
290                         
291                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
292                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
293                         watchdogCDStatusRepository.save(status);
294
295                         toscaResourceStruct.setSuccessfulDeployment();
296
297                 } catch (Exception e) {
298                         logger.debug("Exception :", e);
299                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
300                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_ERROR.name());
301                         watchdogCDStatusRepository.save(status);
302                         Throwable dbExceptionToCapture = e;
303                         while (!(dbExceptionToCapture instanceof ConstraintViolationException
304                                         || dbExceptionToCapture instanceof LockAcquisitionException)
305                                         && (dbExceptionToCapture.getCause() != null)) {
306                                 dbExceptionToCapture = dbExceptionToCapture.getCause();
307                         }
308
309                         if (dbExceptionToCapture instanceof ConstraintViolationException
310                                         || dbExceptionToCapture instanceof LockAcquisitionException) {
311                                 logger.warn(MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED,
312                                                 vfResourceStructure.getResourceInstance().getResourceName(),
313                                                 vfResourceStructure.getNotification().getServiceVersion(), "", MsoLogger.ErrorCode.DataError, "Exception - ASCDC Artifact already deployed", e);
314                         } else {
315                                 String elementToLog = (!artifactListForLogging.isEmpty()
316                                                 ? artifactListForLogging.get(artifactListForLogging.size() - 1).toString()
317                                                 : "No element listed");
318                                 logger.error(MessageEnum.ASDC_ARTIFACT_INSTALL_EXC, elementToLog, "", "", MsoLogger.ErrorCode.DataError,
319                                                 "Exception caught during installation of "
320                                                                 + vfResourceStructure.getResourceInstance().getResourceName()
321                                                                 + ". Transaction rollback",
322                                                 e);
323                                 throw new ArtifactInstallerException("Exception caught during installation of "
324                                                 + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
325                         }
326                 }
327         }
328
329         protected void processNetworks(ToscaResourceStructure toscaResourceStruct, Service service)
330                         throws ArtifactInstallerException {
331                 List<NodeTemplate> nodeTemplatesVLList = toscaResourceStruct.getSdcCsarHelper().getServiceVlList();
332
333                 if (nodeTemplatesVLList != null) {
334                         for (NodeTemplate vlNode : nodeTemplatesVLList) {
335                                 String networkResourceModelName = vlNode.getMetaData()
336                                                 .getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
337                                 
338                                 TempNetworkHeatTemplateLookup tempNetworkLookUp = tempNetworkLookupRepo.findFirstBynetworkResourceModelName(networkResourceModelName);
339                                 
340                                 if (tempNetworkLookUp != null ) {                                       
341                                                 HeatTemplate heatTemplate =  heatRepo.findByArtifactUuid(tempNetworkLookUp.getHeatTemplateArtifactUuid());
342                                                 if (heatTemplate != null ) {
343                                                 NetworkResourceCustomization networkCustomization = createNetwork(vlNode, toscaResourceStruct, heatTemplate,tempNetworkLookUp.getAicVersionMax(),
344                                                         tempNetworkLookUp.getAicVersionMin(),service);
345                                                 service.getNetworkCustomizations()
346                                                 .add(networkCustomization);
347                                         }
348                                         else{
349                                                 throw new ArtifactInstallerException(                                   
350                                                         "No HeatTemplate found for artifactUUID: "
351                                                                         + tempNetworkLookUp.getHeatTemplateArtifactUuid());
352                                         }
353                                 } else {
354                                         throw new ArtifactInstallerException(
355                                                         "No NetworkResourceName found in TempNetworkHeatTemplateLookup for "
356                                                                         + networkResourceModelName);
357                                 }                                       
358                                 
359                         }
360                 }
361         }
362
363         protected void processAllottedResources(ToscaResourceStructure toscaResourceStruct, Service service,
364                         List<NodeTemplate> allottedResourceList) {
365                 if (allottedResourceList != null) {
366                         for (NodeTemplate allottedNode : allottedResourceList) {                                                                        
367                                 service.getAllottedCustomizations()
368                                                 .add(createAllottedResource(allottedNode, toscaResourceStruct, service));                               
369                         }
370                 }
371         }
372         
373         protected void processServiceProxyAndConfiguration(ToscaResourceStructure toscaResourceStruct, Service service) {
374                 
375                 List<NodeTemplate> serviceProxyResourceList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.SERVICE_PROXY);
376                 
377                 List<NodeTemplate> configurationNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CONFIGURATION);
378                 
379                 if (serviceProxyResourceList != null) {
380                         for (NodeTemplate spNode : serviceProxyResourceList) {
381                                 createServiceProxy(spNode, service, toscaResourceStruct);
382                                 serviceProxyCustomizationRepo.saveAndFlush(toscaResourceStruct.getCatalogServiceProxyResourceCustomization());  
383
384                                 for (NodeTemplate configNode : configurationNodeTemplatesList) {
385                                         
386                                                 List<RequirementAssignment> requirementsList = toscaResourceStruct.getSdcCsarHelper().getRequirementsOf(configNode).getAll();
387                                                 for (RequirementAssignment requirement :  requirementsList) {
388                                                         if (requirement.getNodeTemplateName().equals(spNode.getName())) {
389                                                                 createConfiguration(configNode, toscaResourceStruct, toscaResourceStruct.getCatalogServiceProxyResourceCustomization());
390                                                                 configCustomizationRepo.saveAndFlush(toscaResourceStruct.getCatalogConfigurationResourceCustomization());       
391                                                                 break;
392                                                         }
393                                                 }
394                                 }
395         
396                         }
397                 }
398         }
399         
400         protected void processNetworkCollections(ToscaResourceStructure toscaResourceStruct, Service service) {
401                 
402                 List<NodeTemplate> networkCollectionList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CR);
403                 
404                 if (networkCollectionList != null) {
405                         for (NodeTemplate crNode : networkCollectionList) {     
406                                 
407                                 createNetworkCollection(crNode, toscaResourceStruct, service);
408                                 collectionRepo.saveAndFlush(toscaResourceStruct.getCatalogCollectionResource());
409                                 
410                                 List<NetworkInstanceGroup> networkInstanceGroupList = toscaResourceStruct.getCatalogNetworkInstanceGroup();
411                                 for(NetworkInstanceGroup networkInstanceGroup : networkInstanceGroupList){
412                                         instanceGroupRepo.saveAndFlush(networkInstanceGroup);
413                                 }
414         
415                         }
416                 }
417                 service.getCollectionResourceCustomizations().add(toscaResourceStruct.getCatalogCollectionResourceCustomization());
418         }
419
420         protected void processVfModules(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStructure,
421                         Service service, NodeTemplate nodeTemplate, Metadata metadata, String vfCustomizationCategory) throws Exception {
422                 if (!vfCustomizationCategory.equalsIgnoreCase(ALLOTTED_RESOURCE)) 
423                 {
424
425                         String vfCustomizationUUID = toscaResourceStruct.getSdcCsarHelper()
426                                         .getMetadataPropertyValue(metadata, SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
427                         logger.debug("vfCustomizationUUID=" + vfCustomizationUUID);                                     
428                         
429                         VnfResourceCustomization vnfResource = createVnfResource(nodeTemplate, toscaResourceStruct, service);   
430                         
431                         for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {                        
432                                 logger.debug("vfModuleStructure:" + vfModuleStructure.toString());
433                                 List<org.onap.sdc.toscaparser.api.Group> vfGroups = toscaResourceStruct
434                                                 .getSdcCsarHelper().getVfModulesByVf(vfCustomizationUUID);
435                                 IVfModuleData vfMetadata = vfModuleStructure.getVfModuleMetadata();             
436                                 Optional<org.onap.sdc.toscaparser.api.Group> matchingObject = vfGroups.stream().
437                                             filter(group -> group.getMetadata().getValue("vfModuleModelCustomizationUUID").equals(vfMetadata.getVfModuleModelCustomizationUUID())).
438                                             findFirst();
439                                 if(matchingObject.isPresent()){
440                                         VfModuleCustomization vfModuleCustomization = createVFModuleResource(matchingObject.get(), nodeTemplate, toscaResourceStruct, vfResourceStructure,vfMetadata, vnfResource);
441                                         vfModuleCustomization.getVfModule().setVnfResources(vnfResource.getVnfResources());
442                                 }else
443                                         throw (new Exception("Cannot find matching VFModule Customization"));
444                                 
445                         }
446                         service.getVnfCustomizations().add(vnfResource);
447                 }
448         }
449
450         protected void processFlexware(ToscaResourceStructure toscaResourceStruct, Service service, NodeTemplate nodeTemplate,
451                         String serviceType) {
452                 if (serviceType != null && serviceType.equalsIgnoreCase("Flexware")) {
453
454                         createVnfResource(nodeTemplate, toscaResourceStruct, service);
455                         String modelName = toscaResourceStruct.getVnfResourceCustomization().getVnfResources().getModelName();
456                         
457                         String modelVersion = BigDecimalVersion.castAndCheckNotificationVersionToString(
458                                         toscaResourceStruct.getCatalogVnfResourceCustomization().getVnfResources().getModelVersion());
459                         // check for duplicate record already in the database
460                         VnfResource vnfResource = vnfRepo.findByModelNameAndModelVersion(modelName, modelVersion);
461
462                         if (vnfResource != null) {
463                                 toscaResourceStruct.setVnfAlreadyInstalled(true);
464                         }
465
466                         vnfCustomizationRepo.saveAndFlush(toscaResourceStruct.getCatalogVnfResourceCustomization());                                    
467                 }
468         }
469
470         public void processWatchdog(String distributionId, String servideUUID) {
471                 WatchdogServiceModVerIdLookup modVerIdLookup = new WatchdogServiceModVerIdLookup(distributionId,servideUUID);
472                 watchdogModVerIdLookupRepository.saveAndFlush(modVerIdLookup);
473                 
474                 WatchdogDistributionStatus distributionStatus = new WatchdogDistributionStatus(distributionId);
475                 watchdogDistributionStatusRepository.saveAndFlush(distributionStatus);
476         }
477
478         protected void extractHeatInformation(ToscaResourceStructure toscaResourceStruct,
479                         VfResourceStructure vfResourceStructure) {
480                 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
481
482                         switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
483                         case ASDCConfiguration.HEAT:
484                         case ASDCConfiguration.HEAT_NESTED:
485                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
486                                                 vfModuleArtifact);
487                                 break;
488                         case ASDCConfiguration.HEAT_VOL:
489                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
490                                                 vfModuleArtifact);
491                                 VfModuleArtifact envModuleArtifact = getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
492                                 createHeatEnvFromArtifact(vfResourceStructure, envModuleArtifact);
493                                 break;
494                         case ASDCConfiguration.HEAT_ENV:
495                                 createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
496                                 break;
497                         case ASDCConfiguration.HEAT_ARTIFACT:
498                                 createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact,
499                                                 toscaResourceStruct);
500                                 break;
501                         case ASDCConfiguration.HEAT_NET:
502                         case ASDCConfiguration.OTHER:
503                                 logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT,
504                                                 vfModuleArtifact.getArtifactInfo().getArtifactType() + "(Artifact Name:"
505                                                                 + vfModuleArtifact.getArtifactInfo().getArtifactName() + ")",
506                                                 "", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
507                                 break;
508                         default:
509                                 break;
510
511                         }
512                 }
513         }
514
515         protected VfModuleArtifact getHeatEnvArtifactFromGeneratedArtifact(VfResourceStructure vfResourceStructure,
516                         VfModuleArtifact vfModuleArtifact) {
517                 String artifactName = vfModuleArtifact.getArtifactInfo().getArtifactName();
518                 artifactName = artifactName.substring(0, artifactName.indexOf('.'));
519                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
520                         if (moduleArtifact.getArtifactInfo().getArtifactName().contains(artifactName)
521                                         && moduleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
522                                 return moduleArtifact;
523                         }
524                 }
525                 return null;
526         }
527
528         public String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure,
529                         List<String> listTypes) {
530                 String newFileBody = filebody;
531                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
532
533                         if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
534
535                                 newFileBody = verifyTheFilePrefixInString(newFileBody,
536                                                 moduleArtifact.getArtifactInfo().getArtifactName());
537                         }
538                 }
539                 return newFileBody;
540         }
541
542         public String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
543
544                 String needlePrefix = "file:///";
545                 String prefixedFilenameToVerify = needlePrefix + filenameToVerify;
546
547                 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
548                         return body;
549                 }
550
551                 StringBuilder sb = new StringBuilder(body.length());
552
553                 int currentIndex = 0;
554                 int startIndex = 0;
555
556                 while (currentIndex != -1) {
557                         startIndex = currentIndex;
558                         currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);
559
560                         if (currentIndex == -1) {
561                                 break;
562                         }
563                         // We append from the startIndex up to currentIndex (start of File
564                         // Name)
565                         sb.append(body.substring(startIndex, currentIndex));
566                         sb.append(filenameToVerify);
567
568                         currentIndex += prefixedFilenameToVerify.length();
569                 }
570
571                 sb.append(body.substring(startIndex));
572
573                 return sb.toString();
574         }
575
576         protected void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
577                         ToscaResourceStructure toscaResourceStruct, VfModuleArtifact vfModuleArtifact) {
578                 HeatTemplate heatTemplate = new HeatTemplate();
579                 List<String> typeList = new ArrayList<>();
580                 typeList.add(ASDCConfiguration.HEAT_NESTED);
581                 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
582
583                 heatTemplate.setTemplateBody(
584                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
585                 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
586
587                 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
588                         heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
589                 } else {
590                         heatTemplate.setTimeoutMinutes(240);
591                 }
592
593                 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
594                 heatTemplate.setVersion(BigDecimalVersion
595                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
596                 heatTemplate.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
597
598                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
599                         heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
600                 } else {
601                         heatTemplate.setArtifactChecksum(MANUAL_RECORD);
602                 }
603
604                 Set<HeatTemplateParam> heatParam = extractHeatTemplateParameters(
605                                 vfModuleArtifact.getResult(), vfModuleArtifact.getArtifactInfo().getArtifactUUID());
606                 heatTemplate.setParameters(heatParam);  
607                 vfModuleArtifact.setHeatTemplate(heatTemplate);
608         }
609
610         protected void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
611                         VfModuleArtifact vfModuleArtifact) {
612                 HeatEnvironment heatEnvironment = new HeatEnvironment();
613                 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
614                 List<String> typeList = new ArrayList<>();
615                 typeList.add(ASDCConfiguration.HEAT);
616                 typeList.add(ASDCConfiguration.HEAT_VOL);
617                 heatEnvironment.setEnvironment(
618                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
619                 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
620                 heatEnvironment.setVersion(BigDecimalVersion
621                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));     
622                 heatEnvironment.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
623
624                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
625                         heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
626                 } else {
627                         heatEnvironment.setArtifactChecksum(MANUAL_RECORD);
628                 }               
629                 vfModuleArtifact.setHeatEnvironment(heatEnvironment);
630         }
631
632         protected void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
633                 VfModuleArtifact vfModuleArtifact, ToscaResourceStructure toscaResourceStruct) {
634                 
635                 HeatFiles heatFile = new HeatFiles();   
636                 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
637                 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
638                 heatFile.setFileBody(vfModuleArtifact.getResult());
639                 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
640                 heatFile.setVersion(BigDecimalVersion
641                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
642                 toscaResourceStruct.setHeatFilesUUID(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
643                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
644                         heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
645                 } else {
646                         heatFile.setArtifactChecksum(MANUAL_RECORD);
647                 }
648                 vfModuleArtifact.setHeatFiles(heatFile);
649         }
650
651         protected Service createService(ToscaResourceStructure toscaResourceStructure,
652                         VfResourceStructure vfResourceStructure) {
653
654                 toscaResourceStructure.getServiceMetadata();
655
656                 Metadata serviceMetadata = toscaResourceStructure.getServiceMetadata();
657
658                 Service service = new Service();
659
660                 if (serviceMetadata != null) {
661
662                         if (toscaResourceStructure.getServiceVersion() != null) {
663                                 service.setModelVersion(toscaResourceStructure.getServiceVersion());
664                         }
665
666                         service.setServiceType(serviceMetadata.getValue("serviceType"));
667                         service.setServiceRole(serviceMetadata.getValue("serviceRole"));
668
669                         service.setDescription(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
670                         service.setModelName(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
671                         service.setModelUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
672                         service.setEnvironmentContext(serviceMetadata.getValue("environmentContext"));
673
674                         if (vfResourceStructure != null) 
675                                 service.setWorkloadContext(vfResourceStructure.getNotification().getWorkloadContext());
676                                                 
677                         service.setModelInvariantUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
678                         service.setCsar(toscaResourceStructure.getCatalogToscaCsar());                  
679                 }
680                 
681                 
682                 toscaResourceStructure.setCatalogService(service); 
683                 return service;
684         }
685         
686         protected ServiceProxyResourceCustomization createServiceProxy(NodeTemplate nodeTemplate, Service service, ToscaResourceStructure toscaResourceStructure) {
687
688                 Metadata spMetadata = nodeTemplate.getMetaData();
689                 
690                 ServiceProxyResource spResource = new ServiceProxyResource();
691                 
692                 spResource.setModelName(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
693                 spResource.setModelInvariantUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
694                 spResource.setModelUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
695                 spResource.setModelVersion(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
696                 spResource.setDescription(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));     
697                 
698                 ServiceProxyResourceCustomization spCustomizationResource = new ServiceProxyResourceCustomization();
699                 
700                 Set<ServiceProxyResourceCustomization> serviceProxyCustomizationSet = new HashSet<>();
701                 
702                 spCustomizationResource.setModelCustomizationUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
703                 spCustomizationResource.setModelInstanceName(nodeTemplate.getName());
704                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
705                 spCustomizationResource.setSourceService(service);
706                 spCustomizationResource.setServiceProxyResource(spResource);
707                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
708                 spCustomizationResource.setServiceProxyResource(spResource);
709                 serviceProxyCustomizationSet.add(spCustomizationResource);
710
711                 
712                 spResource.setServiceProxyCustomization(serviceProxyCustomizationSet);          
713                 
714                 toscaResourceStructure.setCatalogServiceProxyResource(spResource);
715                 
716                 toscaResourceStructure.setCatalogServiceProxyResourceCustomization(spCustomizationResource);
717                 
718                 return spCustomizationResource;
719         }
720         
721         protected ConfigurationResourceCustomization createConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization) {
722
723                 Metadata metadata = nodeTemplate.getMetaData();
724                 
725                 ConfigurationResource configResource = new ConfigurationResource();
726                 
727                 configResource.setModelName(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
728                 configResource.setModelInvariantUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
729                 configResource.setModelUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
730                 configResource.setModelVersion(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
731                 configResource.setDescription(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
732                 configResource.setToscaNodeType(nodeTemplate.getType());
733                 
734                 ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
735                 
736                 Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
737                 
738                 configCustomizationResource.setModelCustomizationUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
739                 configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
740                 
741                 configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
742                 configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
743                 configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
744                 configCustomizationResource.setServiceProxyResourceCustomization(spResourceCustomization);
745                 configCustomizationResource.setConfigResourceCustomization(configCustomizationResource);
746                 configCustomizationResource.setConfigurationResource(configResource);
747                 configResourceCustomizationSet.add(configCustomizationResource);
748
749                 configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);   
750                 
751                 toscaResourceStructure.setCatalogConfigurationResource(configResource);
752                 
753                 toscaResourceStructure.setCatalogConfigurationResourceCustomization(configCustomizationResource);
754                 
755                 return configCustomizationResource;
756         }
757
758
759         protected void createToscaCsar(ToscaResourceStructure toscaResourceStructure) {
760                 ToscaCsar toscaCsar = new ToscaCsar();
761                 if (toscaResourceStructure.getToscaArtifact().getArtifactChecksum() != null) {
762                         toscaCsar.setArtifactChecksum(toscaResourceStructure.getToscaArtifact().getArtifactChecksum());
763                 } else {
764                         toscaCsar.setArtifactChecksum(MANUAL_RECORD);
765                 }
766                 toscaCsar.setArtifactUUID(toscaResourceStructure.getToscaArtifact().getArtifactUUID());
767                 toscaCsar.setName(toscaResourceStructure.getToscaArtifact().getArtifactName());
768                 toscaCsar.setVersion(toscaResourceStructure.getToscaArtifact().getArtifactVersion());
769                 toscaCsar.setDescription(toscaResourceStructure.getToscaArtifact().getArtifactDescription());
770                 toscaCsar.setUrl(toscaResourceStructure.getToscaArtifact().getArtifactURL());
771
772                 toscaResourceStructure.setCatalogToscaCsar(toscaCsar);
773         }
774
775         protected  NetworkResourceCustomization createNetwork(NodeTemplate networkNodeTemplate,
776                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin,Service service) {
777                 
778                 NetworkResourceCustomization networkResourceCustomization=networkCustomizationRepo.findOneByModelCustomizationUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
779                                 if(networkResourceCustomization==null){
780                         networkResourceCustomization = createNetworkResourceCustomization(networkNodeTemplate,
781                                         toscaResourceStructure);
782                                         
783                         NetworkResource networkResource = findExistingNetworkResource(service,
784                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
785                                         if(networkResource == null)
786                                 networkResource = createNetworkResource(networkNodeTemplate, toscaResourceStructure, heatTemplate,
787                                                 aicMax, aicMin);
788
789                                         networkResource.addNetworkResourceCustomization(networkResourceCustomization);          
790                                         networkResourceCustomization.setNetworkResource(networkResource);
791                                 }
792                 return networkResourceCustomization;
793         }
794         
795         protected  NetworkResource findExistingNetworkResource(Service service, String modelUUID) {
796                 NetworkResource networkResource = null;
797                 for(NetworkResourceCustomization networkCustom : service.getNetworkCustomizations()){
798                         if (networkCustom.getNetworkResource() != null
799                                         && networkCustom.getNetworkResource().getModelUUID().equals(modelUUID)) {
800                                         networkResource = networkCustom.getNetworkResource();
801                         }
802                 }
803                 if(networkResource==null)
804                         networkResource = networkRepo.findResourceByModelUUID(modelUUID);
805                 
806                 return networkResource;
807         }
808         
809         protected NetworkResourceCustomization createNetworkResourceCustomization(NodeTemplate networkNodeTemplate,
810                         ToscaResourceStructure toscaResourceStructure) {
811                 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
812                 networkResourceCustomization.setModelInstanceName(
813                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
814                 networkResourceCustomization.setModelCustomizationUUID(
815                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));                
816
817                 networkResourceCustomization.setNetworkTechnology(
818                                 testNull(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
819                                                 SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY)));
820                 networkResourceCustomization.setNetworkType(testNull(toscaResourceStructure.getSdcCsarHelper()
821                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE)));
822                 networkResourceCustomization.setNetworkRole(testNull(toscaResourceStructure.getSdcCsarHelper()
823                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE)));
824                 networkResourceCustomization.setNetworkScope(testNull(toscaResourceStructure.getSdcCsarHelper()
825                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE)));
826                 return networkResourceCustomization;
827         }
828
829         protected NetworkResource createNetworkResource(NodeTemplate networkNodeTemplate,
830                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin) {
831                 NetworkResource networkResource = new NetworkResource();
832                 String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
833                                 networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
834
835                 if ("true".equalsIgnoreCase(providerNetwork)) {
836                         networkResource.setNeutronNetworkType(PROVIDER);
837                 } else {
838                         networkResource.setNeutronNetworkType(BASIC);
839                 }
840
841                 networkResource.setModelName(
842                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
843
844                 networkResource.setModelInvariantUUID(
845                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
846                 networkResource.setModelUUID(
847                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
848                 networkResource.setModelVersion(
849                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
850
851                 networkResource.setAicVersionMax(aicMax);               
852                 networkResource.setAicVersionMin(aicMin);
853                 networkResource.setToscaNodeType(networkNodeTemplate.getType());
854                 networkResource.setDescription(
855                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
856                 networkResource.setOrchestrationMode(HEAT);     
857                 networkResource.setHeatTemplate(heatTemplate); 
858                 return networkResource;
859         }
860         
861         protected  CollectionNetworkResourceCustomization createNetworkCollection(NodeTemplate networkNodeTemplate,
862                         ToscaResourceStructure toscaResourceStructure, Service service) {
863
864                 CollectionNetworkResourceCustomization collectionNetworkResourceCustomization = new CollectionNetworkResourceCustomization();
865
866                 // **** Build Object to populate Collection_Resource table
867                 CollectionResource collectionResource = new CollectionResource();
868
869                 collectionResource
870                                 .setModelName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
871                 collectionResource.setModelInvariantUUID(
872                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
873                 collectionResource
874                                 .setModelUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
875                 collectionResource
876                                 .setModelVersion(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
877                 collectionResource
878                                 .setDescription(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
879                 collectionResource.setToscaNodeType(networkNodeTemplate.getType());
880
881                 toscaResourceStructure.setCatalogCollectionResource(collectionResource);
882
883                 // **** Build object to populate Collection_Resource_Customization table
884                 NetworkCollectionResourceCustomization ncfc = new NetworkCollectionResourceCustomization();
885                 
886                 ncfc.setFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
887                                 "cr_function"));
888                 ncfc.setRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
889                                 "cr_role"));
890                 ncfc.setType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
891                                 "cr_type"));
892
893                 ncfc.setModelInstanceName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
894                 ncfc.setModelCustomizationUUID(
895                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
896                 
897                 Set<CollectionNetworkResourceCustomization> networkResourceCustomizationSet = new HashSet<>();
898                 networkResourceCustomizationSet.add(collectionNetworkResourceCustomization);
899
900                 ncfc.setNetworkResourceCustomization(networkResourceCustomizationSet);
901
902                 ncfc.setCollectionResource(collectionResource);
903                 toscaResourceStructure.setCatalogCollectionResourceCustomization(ncfc);
904                 
905                 //*** Build object to populate the Instance_Group table
906                 List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
907                                 .getGroupsOfOriginOfNodeTemplateByToscaGroupType(networkNodeTemplate,
908                                                 "org.openecomp.groups.NetworkCollection");
909                 
910                 List<NetworkInstanceGroup> networkInstanceGroupList = new ArrayList<>();
911
912                 List<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationList = new ArrayList<CollectionResourceInstanceGroupCustomization>();
913
914                 for (Group group : groupList) { 
915                         
916                         NetworkInstanceGroup networkInstanceGroup = new NetworkInstanceGroup();
917                         Metadata instanceMetadata = group.getMetadata();
918                         networkInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
919                         networkInstanceGroup
920                                         .setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
921                         networkInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
922                         networkInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
923                         networkInstanceGroup.setToscaNodeType(group.getType());
924                         networkInstanceGroup.setRole(SubType.SUB_INTERFACE.toString()); // Set
925                                                                                                                                                         // Role
926                         networkInstanceGroup.setType(InstanceGroupType.L3_NETWORK); // Set
927                                                                                                                                                 // type
928                         networkInstanceGroup.setCollectionResource(collectionResource);
929                 
930                         // ****Build object to populate
931                         // Collection_Resource_Instance_Group_Customization table
932                         CollectionResourceInstanceGroupCustomization crInstanceGroupCustomization = new CollectionResourceInstanceGroupCustomization();
933                         crInstanceGroupCustomization.setInstanceGroup(networkInstanceGroup);
934                         crInstanceGroupCustomization.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
935                         crInstanceGroupCustomization.setModelCustomizationUUID(
936                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
937                 
938                         String quantityName = instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
939                         String fixedQuantity = quantityName.replace("NetworkCollection", "Fixed");
940                         if (toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
941                                         fixedQuantity + "_quantity") != null) {
942
943                                 crInstanceGroupCustomization.setSubInterfaceNetworkQuantity(Integer.parseInt(
944                                                 toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
945                                                                 fixedQuantity + "_quantity"))); 
946                         }
947                 
948                         crInstanceGroupCustomization.setDescription(
949                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
950                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
951                                                                         + "_network_collection_description"));
952                         crInstanceGroupCustomization.setFunction(
953                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
954                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
955                                                                         + "_network_collection_function"));
956                         crInstanceGroupCustomization.setCollectionResourceCust(ncfc);
957                         collectionResourceInstanceGroupCustomizationList.add(crInstanceGroupCustomization);
958
959                         networkInstanceGroup
960                                         .setCollectionInstanceGroupCustomizations(collectionResourceInstanceGroupCustomizationList);
961
962                         networkInstanceGroupList.add(networkInstanceGroup);
963
964                 //}
965
966                 toscaResourceStructure.setCatalogNetworkInstanceGroup(networkInstanceGroupList);
967
968                 List<NodeTemplate> vlNodeList = toscaResourceStructure.getSdcCsarHelper()
969                                 .getNodeTemplateBySdcType(networkNodeTemplate, SdcTypes.VL);
970                 
971                 List<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationList = new ArrayList<>();
972                 
973                 //*****Build object to populate the NetworkResource table
974                 NetworkResource networkResource = new NetworkResource();
975                 
976                 for(NodeTemplate vlNodeTemplate : vlNodeList){
977
978                         String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
979                                         vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
980
981                         if ("true".equalsIgnoreCase(providerNetwork)) {
982                                 networkResource.setNeutronNetworkType(PROVIDER);
983                         } else {
984                                 networkResource.setNeutronNetworkType(BASIC);
985                         }
986
987                         networkResource.setModelName(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
988
989                         networkResource.setModelInvariantUUID(
990                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
991                         networkResource.setModelUUID(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
992                         networkResource
993                                         .setModelVersion(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
994
995                         networkResource.setAicVersionMax(
996                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES));
997                         
998                         TempNetworkHeatTemplateLookup tempNetworkLookUp = tempNetworkLookupRepo.findFirstBynetworkResourceModelName(
999                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1000                         
1001                         if (tempNetworkLookUp != null ) {       
1002                                         
1003                                 HeatTemplate heatTemplate = heatRepo
1004                                                 .findByArtifactUuid(tempNetworkLookUp.getHeatTemplateArtifactUuid());
1005                                         networkResource.setHeatTemplate(heatTemplate);
1006                                         
1007                                         networkResource.setAicVersionMin(tempNetworkLookUp.getAicVersionMin());
1008                                         
1009                         }
1010
1011                         networkResource.setToscaNodeType(vlNodeTemplate.getType());
1012                         networkResource
1013                                         .setDescription(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1014                         networkResource.setOrchestrationMode(HEAT);
1015                         
1016                         // Build object to populate the
1017                         // Collection_Network_Resource_Customization table
1018                         for (NodeTemplate memberNode : group.getMemberNodes()) {
1019                                 collectionNetworkResourceCustomization.setModelInstanceName(memberNode.getName());
1020                         }
1021
1022                         collectionNetworkResourceCustomization.setModelCustomizationUUID(
1023                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1024
1025                         collectionNetworkResourceCustomization.setNetworkTechnology(
1026                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vlNodeTemplate,
1027                                                         SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY));
1028                         collectionNetworkResourceCustomization.setNetworkType(toscaResourceStructure.getSdcCsarHelper()
1029                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE));
1030                         collectionNetworkResourceCustomization.setNetworkRole(toscaResourceStructure.getSdcCsarHelper()
1031                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE));
1032                         collectionNetworkResourceCustomization.setNetworkScope(toscaResourceStructure.getSdcCsarHelper()
1033                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE));
1034                         collectionNetworkResourceCustomization.setInstanceGroup(networkInstanceGroup);
1035                         collectionNetworkResourceCustomization.setNetworkResource(networkResource);
1036                         collectionNetworkResourceCustomization.setNetworkResourceCustomization(ncfc);
1037                         
1038                         collectionNetworkResourceCustomizationList.add(collectionNetworkResourceCustomization);
1039                    }
1040
1041                 }
1042                 
1043                 return collectionNetworkResourceCustomization;
1044         }
1045         
1046         protected VnfcInstanceGroupCustomization createVNFCInstanceGroup(NodeTemplate vnfcNodeTemplate, Group group,
1047                         VnfResourceCustomization vnfResourceCustomization) {
1048
1049                         Metadata instanceMetadata = group.getMetadata();
1050                         // Populate InstanceGroup
1051                         VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup();
1052                         
1053                         vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1054                         vfcInstanceGroup.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1055                         vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1056                         vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1057                 vfcInstanceGroup.setToscaNodeType(group.getType());
1058                         vfcInstanceGroup.setRole("SUB-INTERFACE");   // Set Role
1059                         vfcInstanceGroup.setType(InstanceGroupType.VNFC);  // Set type  
1060                         
1061                         //Populate VNFCInstanceGroupCustomization
1062                         VnfcInstanceGroupCustomization vfcInstanceGroupCustom = new VnfcInstanceGroupCustomization();
1063                         
1064                         vfcInstanceGroupCustom.setModelCustomizationUUID(vnfResourceCustomization.getModelCustomizationUUID());
1065                         vfcInstanceGroupCustom.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1066                         vfcInstanceGroupCustom.setDescription(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1067                         vfcInstanceGroupCustom.setFunction("FUNCTION");
1068                         vfcInstanceGroupCustom.setInstanceGroup(vfcInstanceGroup);
1069                         vfcInstanceGroupCustom.setVnfResourceCust(vnfResourceCustomization);            
1070                         
1071                 return vfcInstanceGroupCustom;
1072
1073         }
1074         
1075         protected VFCInstanceGroup findExistingVnfcInstanceGroup(VnfResourceCustomization vnfResourceCustomization,
1076                         String modelUUID) {
1077                 VFCInstanceGroup vfcInstanceGroup = null;
1078                 List<VnfcInstanceGroupCustomization> vnfInstanceGroupCustomizations = vnfResourceCustomization
1079                                 .getVnfcInstanceGroupCustomizations();
1080                 if(vnfInstanceGroupCustomizations != null){
1081                         for (VnfcInstanceGroupCustomization vnfcInstanceGroupCustom : vnfResourceCustomization
1082                                         .getVnfcInstanceGroupCustomizations()) {
1083                                 if (vnfcInstanceGroupCustom.getInstanceGroup() != null
1084                                                 && vnfcInstanceGroupCustom.getInstanceGroup().getModelUUID().equals(modelUUID)) {
1085                                         vfcInstanceGroup = (VFCInstanceGroup)vnfcInstanceGroupCustom.getInstanceGroup();
1086                                 }
1087                         }
1088                 }
1089                 if (vfcInstanceGroup == null)
1090                         vfcInstanceGroup = (VFCInstanceGroup) instanceGroupRepo.findByModelUUID(modelUUID);
1091
1092                 return vfcInstanceGroup;
1093         }
1094         
1095         protected VfModuleCustomization createVFModuleResource(Group group, NodeTemplate nodeTemplate,
1096                         ToscaResourceStructure toscaResourceStructure, VfResourceStructure vfResourceStructure,
1097                         IVfModuleData vfModuleData, VnfResourceCustomization vnfResource) {
1098                 VfModuleCustomization vfModuleCustomization = findExistingVfModuleCustomization(vnfResource,
1099                                 vfModuleData.getVfModuleModelCustomizationUUID());
1100                 if(vfModuleCustomization == null){              
1101                         VfModule vfModule = findExistingVfModule(vnfResource,
1102                                         nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1103                         Metadata vfMetadata = group.getMetadata();
1104                         if(vfModule==null)
1105                                 vfModule=createVfModule(group, toscaResourceStructure, vfModuleData, vfMetadata);
1106                         
1107                         vfModuleCustomization = createVfModuleCustomzation(group, toscaResourceStructure, vfModule, vfModuleData);
1108                         setHeatInformationForVfModule(toscaResourceStructure, vfResourceStructure, vfModule, vfModuleCustomization,
1109                                         vfMetadata);
1110                         vfModuleCustomization.setVfModule(vfModule);
1111                         vfModule.getVfModuleCustomization().add(vfModuleCustomization);
1112                         vnfResource.getVfModuleCustomizations().add(vfModuleCustomization);
1113                 } else {
1114                         vfResourceStructure.setAlreadyDeployed(true);
1115                 }
1116                 
1117                 return vfModuleCustomization;
1118         }
1119         
1120         protected VfModuleCustomization findExistingVfModuleCustomization(VnfResourceCustomization vnfResource,
1121                         String vfModuleModelCustomizationUUID) {
1122                 VfModuleCustomization vfModuleCustomization = null;
1123                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1124                         if(vfModuleCustom.getModelCustomizationUUID().equalsIgnoreCase(vfModuleModelCustomizationUUID)){
1125                                 vfModuleCustomization = vfModuleCustom;
1126                         }
1127                 }
1128                 if(vfModuleCustomization==null)
1129                         vfModuleCustomization = vfModuleCustomizationRepo
1130                                         .findByModelCustomizationUUID(vfModuleModelCustomizationUUID);
1131                 
1132                 return vfModuleCustomization;
1133         }
1134
1135         protected VfModule findExistingVfModule(VnfResourceCustomization vnfResource, String modelUUID) {
1136                 VfModule vfModule = null;
1137                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1138                         if(vfModuleCustom.getVfModule() != null && vfModuleCustom.getVfModule().getModelUUID().equals(modelUUID)){
1139                                 vfModule = vfModuleCustom.getVfModule();
1140                         }
1141                 }
1142                 if(vfModule==null)
1143                         vfModule = vfModuleRepo.findByModelUUID(modelUUID);
1144                 
1145                 return vfModule;
1146         }
1147
1148         protected VfModuleCustomization createVfModuleCustomzation(Group group,
1149                         ToscaResourceStructure toscaResourceStructure, VfModule vfModule, IVfModuleData vfModuleData) {
1150                 VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
1151                 
1152                 vfModuleCustomization.setModelCustomizationUUID(vfModuleData.getVfModuleModelCustomizationUUID());
1153
1154                 vfModuleCustomization.setVfModule(vfModule);
1155
1156                 String initialCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1157                                 SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT);
1158                 if (initialCount != null && initialCount.length() > 0) {
1159                         vfModuleCustomization.setInitialCount(Integer.valueOf(initialCount));
1160                 }
1161
1162                 vfModuleCustomization.setInitialCount(Integer.valueOf(toscaResourceStructure.getSdcCsarHelper()
1163                                 .getGroupPropertyLeafValue(group, SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT)));
1164
1165                 String availabilityZoneCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1166                                 SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT);
1167                 if (availabilityZoneCount != null && availabilityZoneCount.length() > 0) {
1168                         vfModuleCustomization.setAvailabilityZoneCount(Integer.valueOf(availabilityZoneCount));
1169                 }
1170
1171                 vfModuleCustomization.setLabel(toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1172                                 SdcPropertyNames.PROPERTY_NAME_VFMODULELABEL));
1173
1174                 String maxInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1175                                 SdcPropertyNames.PROPERTY_NAME_MAXVFMODULEINSTANCES);
1176                 if (maxInstances != null && maxInstances.length() > 0) {
1177                         vfModuleCustomization.setMaxInstances(Integer.valueOf(maxInstances));
1178                 }
1179
1180                 String minInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1181                                 SdcPropertyNames.PROPERTY_NAME_MINVFMODULEINSTANCES);
1182                 if (minInstances != null && minInstances.length() > 0) {
1183                         vfModuleCustomization.setMinInstances(Integer.valueOf(minInstances));
1184                 }
1185                 return vfModuleCustomization;
1186         }
1187
1188         protected VfModule createVfModule(Group group, ToscaResourceStructure toscaResourceStructure,
1189                         IVfModuleData vfModuleData, Metadata vfMetadata) {
1190                 VfModule vfModule = new VfModule();
1191                 String vfModuleModelUUID = vfModuleData.getVfModuleModelUUID();
1192
1193                 if(vfModuleModelUUID == null) {
1194                         vfModuleModelUUID = testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1195                                         SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1196                 } else if (vfModuleModelUUID.indexOf('.') > -1) {
1197                         vfModuleModelUUID = vfModuleModelUUID.substring(0, vfModuleModelUUID.indexOf('.'));
1198                 }
1199
1200                 vfModule.setModelInvariantUUID(testNull(toscaResourceStructure.getSdcCsarHelper()
1201                                 .getMetadataPropertyValue(vfMetadata, SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)));
1202                 vfModule.setModelName(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1203                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELNAME)));
1204                 vfModule.setModelUUID(vfModuleModelUUID);
1205                 vfModule.setModelVersion(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1206                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELVERSION)));
1207                 vfModule.setDescription(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1208                                 SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1209
1210                 String vfModuleType = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1211                                 SdcPropertyNames.PROPERTY_NAME_VFMODULETYPE);
1212                 if (vfModuleType != null && "Base".equalsIgnoreCase(vfModuleType)) {
1213                         vfModule.setIsBase(true);
1214                 } else {
1215                         vfModule.setIsBase(false);
1216                 }
1217                 return vfModule;
1218         }
1219
1220         protected void setHeatInformationForVfModule(ToscaResourceStructure toscaResourceStructure,
1221                         VfResourceStructure vfResourceStructure, VfModule vfModule, VfModuleCustomization vfModuleCustomization,
1222                         Metadata vfMetadata) {
1223                 Optional<VfModuleStructure> matchingObject = vfResourceStructure.getVfModuleStructure().stream()
1224                                 .filter(vfModuleStruct -> vfModuleStruct.getVfModuleMetadata().getVfModuleModelUUID()
1225                                                 .equalsIgnoreCase(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1226                                                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID)))
1227                                 .findFirst();
1228
1229                 if (matchingObject.isPresent()) {
1230                         List<HeatFiles> heatFilesList = new ArrayList<>();
1231                         List<HeatTemplate> volumeHeatChildTemplates = new ArrayList<HeatTemplate>();
1232                         List<HeatTemplate> heatChildTemplates = new ArrayList<HeatTemplate>();
1233                         HeatTemplate parentHeatTemplate = new HeatTemplate();
1234                         String parentArtifactType = null;
1235                         Set<String> artifacts = new HashSet<>(matchingObject.get().getVfModuleMetadata().getArtifacts());
1236                         for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
1237                                 
1238                                 List<HeatTemplate> childNestedHeatTemplates = new ArrayList<HeatTemplate>();
1239                                 
1240                                 if (artifacts.contains(vfModuleArtifact.getArtifactInfo().getArtifactUUID())) {
1241                                         checkVfModuleArtifactType(vfModule, vfModuleCustomization, heatFilesList, vfModuleArtifact,
1242                                                         childNestedHeatTemplates, parentHeatTemplate, vfResourceStructure);
1243                                 }
1244                                 
1245                                 if(vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)){
1246                                         parentArtifactType = identifyParentOfNestedTemplate(matchingObject.get(), vfModuleArtifact);
1247                                         
1248                                         if(!childNestedHeatTemplates.isEmpty()){
1249                                         
1250                                                 if (parentArtifactType != null && parentArtifactType.equalsIgnoreCase(ASDCConfiguration.HEAT_VOL)) {
1251                                                         volumeHeatChildTemplates.add(childNestedHeatTemplates.get(0));
1252                                                 } else {
1253                                                         heatChildTemplates.add(childNestedHeatTemplates.get(0));
1254                                                 }
1255                                         }
1256                                 }
1257                                 
1258                         }
1259                         if(!heatFilesList.isEmpty()){
1260                                 vfModule.setHeatFiles(heatFilesList);
1261                         }
1262                         
1263                         
1264                         // Set all Child Templates related to HEAT_VOLUME
1265                         if(!volumeHeatChildTemplates.isEmpty()){
1266                                 vfModule.getVolumeHeatTemplate().setChildTemplates(volumeHeatChildTemplates);
1267                         }
1268                         
1269                         // Set all Child Templates related to HEAT
1270                         if(!heatChildTemplates.isEmpty()){
1271                                 vfModule.getVolumeHeatTemplate().setChildTemplates(heatChildTemplates);
1272                         }
1273                 }
1274         }
1275
1276         protected void checkVfModuleArtifactType(VfModule vfModule, VfModuleCustomization vfModuleCustomization,
1277                         List<HeatFiles> heatFilesList, VfModuleArtifact vfModuleArtifact, List<HeatTemplate> nestedHeatTemplates,
1278                         HeatTemplate parentHeatTemplate, VfResourceStructure vfResourceStructure) {
1279                 if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT)) {
1280                         vfModuleArtifact.incrementDeployedInDB();
1281                         vfModule.setModuleHeatTemplate(vfModuleArtifact.getHeatTemplate());
1282                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_VOL)) {
1283                         vfModule.setVolumeHeatTemplate(vfModuleArtifact.getHeatTemplate());
1284                         VfModuleArtifact volVfModuleArtifact = this.getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
1285                         vfModuleCustomization.setVolumeHeatEnv(volVfModuleArtifact.getHeatEnvironment());
1286                         vfModuleArtifact.incrementDeployedInDB();
1287                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
1288                         if(vfModuleArtifact.getHeatEnvironment().getName().contains("volume")) {
1289                                 vfModuleCustomization.setVolumeHeatEnv(vfModuleArtifact.getHeatEnvironment());
1290                         } else { 
1291                         vfModuleCustomization.setHeatEnvironment(vfModuleArtifact.getHeatEnvironment());
1292                         }
1293                         vfModuleArtifact.incrementDeployedInDB();
1294                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ARTIFACT)) {
1295                         heatFilesList.add(vfModuleArtifact.getHeatFiles());                                                     
1296                         vfModuleArtifact.incrementDeployedInDB();
1297                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)) {
1298                         nestedHeatTemplates.add(vfModuleArtifact.getHeatTemplate());                            
1299                         vfModuleArtifact.incrementDeployedInDB();
1300                 }
1301         }
1302
1303         protected VnfResourceCustomization createVnfResource(NodeTemplate vfNodeTemplate,
1304                         ToscaResourceStructure toscaResourceStructure, Service service) {
1305                 VnfResourceCustomization vnfResourceCustomization = vnfCustomizationRepo.findOneByModelCustomizationUUID(
1306                                 vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1307                 if(vnfResourceCustomization == null){           
1308                         VnfResource vnfResource = findExistingVnfResource(service,
1309                                         vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1310                         
1311                         if(vnfResource==null)
1312                                 vnfResource=createVnfResource(vfNodeTemplate);
1313                         
1314                         vnfResourceCustomization = createVnfResourceCustomization(vfNodeTemplate, toscaResourceStructure,
1315                                         vnfResource);
1316                         vnfResourceCustomization.setVnfResources(vnfResource);
1317                         vnfResource.getVnfResourceCustomizations().add(vnfResourceCustomization);
1318                         
1319                         // Fetch VNFC Instance Group Info                               
1320                         List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
1321                                         .getGroupsOfOriginOfNodeTemplateByToscaGroupType(vfNodeTemplate,
1322                                                         "org.openecomp.groups.VfcInstanceGroup");
1323                                 
1324                         for (Group group : groupList) { 
1325                                 
1326                                 VFCInstanceGroup vfcInstanceGroup = findExistingVnfcInstanceGroup(vnfResourceCustomization,
1327                                                 group.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1328                                 if(vfcInstanceGroup == null){
1329                                         VnfcInstanceGroupCustomization vnfcInstanceGroupCustomization = createVNFCInstanceGroup(
1330                                                         vfNodeTemplate, group, vnfResourceCustomization);
1331                                 
1332                                         vnfcInstanceGroupCustomizationRepo.saveAndFlush(vnfcInstanceGroupCustomization);
1333                                 }
1334                         }
1335                         
1336                 }
1337                 return vnfResourceCustomization;
1338         }
1339         
1340         protected VnfResource findExistingVnfResource(Service service, String modelUUID) {
1341                 VnfResource vnfResource = null;
1342                 for(VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()){
1343                         if (vnfResourceCustom.getVnfResources() != null
1344                                         && vnfResourceCustom.getVnfResources().getModelUUID().equals(modelUUID)) {
1345                                 vnfResource = vnfResourceCustom.getVnfResources();
1346                         }
1347                 }
1348                 if(vnfResource==null)
1349                         vnfResource = vnfRepo.findResourceByModelUUID(modelUUID);
1350                 
1351                 return vnfResource;
1352         }
1353
1354         protected VnfResourceCustomization createVnfResourceCustomization(NodeTemplate vfNodeTemplate,
1355                         ToscaResourceStructure toscaResourceStructure, VnfResource vnfResource) {
1356                 VnfResourceCustomization vnfResourceCustomization = new VnfResourceCustomization();
1357                 vnfResourceCustomization.setModelCustomizationUUID(
1358                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
1359                 vnfResourceCustomization.setModelInstanceName(vfNodeTemplate.getName());
1360
1361                 vnfResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
1362                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
1363                 vnfResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
1364                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, "nf_naming_code")));
1365                 vnfResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
1366                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
1367                 vnfResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
1368                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
1369
1370                 vnfResourceCustomization.setMultiStageDesign(toscaResourceStructure.getSdcCsarHelper()
1371                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, MULTI_STAGE_DESIGN));
1372
1373                 vnfResourceCustomization.setVnfResources(vnfResource);
1374                 vnfResourceCustomization.setAvailabilityZoneMaxCount(Integer.getInteger(
1375                                 vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT)));  
1376
1377                 CapabilityAssignments vnfCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
1378                                 .getCapabilitiesOf(vfNodeTemplate);
1379
1380                 if (vnfCustomizationCapability != null) {
1381                         CapabilityAssignment capAssign = vnfCustomizationCapability.getCapabilityByName(SCALABLE);
1382
1383                         if (capAssign != null) {
1384                                 vnfResourceCustomization.setMinInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
1385                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1386                                 vnfResourceCustomization.setMaxInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
1387                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1388                         }
1389
1390                 }
1391                 
1392                 toscaResourceStructure.setCatalogVnfResourceCustomization(vnfResourceCustomization);
1393                 
1394                 return vnfResourceCustomization;
1395         }
1396
1397         protected VnfResource createVnfResource(NodeTemplate vfNodeTemplate) {
1398                 VnfResource vnfResource = new VnfResource();
1399                 vnfResource.setModelInvariantUUID(
1400                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
1401                 vnfResource.setModelName(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1402                 vnfResource.setModelUUID(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
1403
1404                 vnfResource.setModelVersion(
1405                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1406                 vnfResource.setDescription(
1407                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1408                 vnfResource.setOrchestrationMode(HEAT);
1409                 vnfResource.setToscaNodeType(testNull(vfNodeTemplate.getType()));
1410                 vnfResource.setAicVersionMax(
1411                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1412                 vnfResource.setAicVersionMin(
1413                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1414                 
1415                 return vnfResource;
1416         }
1417
1418         protected AllottedResourceCustomization createAllottedResource(NodeTemplate nodeTemplate,
1419                         ToscaResourceStructure toscaResourceStructure, Service service) {
1420                 AllottedResourceCustomization allottedResourceCustomization = allottedCustomizationRepo
1421                                 .findOneByModelCustomizationUUID(
1422                                 nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1423                         
1424                 if(allottedResourceCustomization == null){                      
1425                         AllottedResource allottedResource = findExistingAllottedResource(service,
1426                                         nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1427                         
1428                         if(allottedResource==null)
1429                                 allottedResource=createAR(nodeTemplate);
1430                         
1431                         toscaResourceStructure.setAllottedResource(allottedResource);                   
1432                         allottedResourceCustomization = createAllottedResourceCustomization(nodeTemplate, toscaResourceStructure);
1433                         allottedResourceCustomization.setAllottedResource(allottedResource);
1434                         allottedResource.getAllotedResourceCustomization().add(allottedResourceCustomization);
1435                 }
1436                 return allottedResourceCustomization;
1437         }
1438         
1439         protected AllottedResource findExistingAllottedResource(Service service, String modelUUID) {
1440                 AllottedResource allottedResource = null;
1441                 for(AllottedResourceCustomization allottedResourceCustom : service.getAllottedCustomizations()){
1442                         if (allottedResourceCustom.getAllottedResource() != null
1443                                         && allottedResourceCustom.getAllottedResource().getModelUUID().equals(modelUUID)) {
1444                                 allottedResource = allottedResourceCustom.getAllottedResource();
1445                         }
1446                 }
1447                 if(allottedResource==null)
1448                         allottedResource = allottedRepo.findResourceByModelUUID(modelUUID);
1449                 
1450                 return allottedResource;
1451         }
1452         
1453         protected AllottedResourceCustomization createAllottedResourceCustomization(NodeTemplate nodeTemplate,
1454                         ToscaResourceStructure toscaResourceStructure) {
1455                 AllottedResourceCustomization allottedResourceCustomization = new AllottedResourceCustomization();
1456                 allottedResourceCustomization.setModelCustomizationUUID(
1457                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
1458                 allottedResourceCustomization.setModelInstanceName(nodeTemplate.getName());
1459                 
1460
1461                 allottedResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
1462                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
1463                 allottedResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
1464                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, "nf_naming_code")));
1465                 allottedResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
1466                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
1467                 allottedResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
1468                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
1469
1470                 List<NodeTemplate> vfcNodes = toscaResourceStructure.getSdcCsarHelper().getVfcListByVf(allottedResourceCustomization.getModelCustomizationUUID());
1471                 
1472                 if(vfcNodes != null){
1473                         for(NodeTemplate vfcNode : vfcNodes){
1474                         
1475                                 allottedResourceCustomization.setProvidingServiceModelUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_uuid"));
1476                                 allottedResourceCustomization.setProvidingServiceModelInvariantUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_invariant_uuid"));
1477                                 allottedResourceCustomization.setProvidingServiceModelName(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_name"));
1478                         }
1479                 }
1480                 
1481
1482                 CapabilityAssignments arCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
1483                                 .getCapabilitiesOf(nodeTemplate);
1484
1485                 if (arCustomizationCapability != null) {
1486                         CapabilityAssignment capAssign = arCustomizationCapability.getCapabilityByName(SCALABLE);
1487
1488                         if (capAssign != null) {
1489                                 allottedResourceCustomization.setMinInstances(
1490                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
1491                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1492                                 allottedResourceCustomization.setMaxInstances(
1493                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
1494                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1495                         }
1496                 }
1497                 return allottedResourceCustomization;
1498         }
1499
1500         protected AllottedResource createAR(NodeTemplate nodeTemplate) {
1501                 AllottedResource allottedResource = new AllottedResource();
1502                 allottedResource
1503                 .setModelUUID(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
1504                 allottedResource.setModelInvariantUUID(
1505                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
1506                 allottedResource
1507                 .setModelName(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1508                 allottedResource
1509                 .setModelVersion(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1510                 allottedResource.setToscaNodeType(testNull(nodeTemplate.getType()));
1511                 allottedResource.setSubcategory(
1512                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY)));
1513                 allottedResource
1514                 .setDescription(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1515                 return allottedResource;
1516         }
1517
1518         protected Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile, String artifactUUID) {
1519                 // Scan the payload downloadResult and extract the HeatTemplate
1520                 // parameters
1521                 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
1522                 return yamlEditor.getParameterList(artifactUUID);
1523         }       
1524
1525         protected String testNull(Object object) {
1526
1527                 if (object == null) {
1528                         return null;
1529                 } else if (object.equals("NULL")) {
1530                         return null;
1531                 } else if (object instanceof Integer) {
1532                         return object.toString();
1533                 } else if (object instanceof String) {
1534                         return (String) object;
1535                 } else {
1536                         return "Type not recognized";
1537                 }
1538         }
1539         
1540         protected static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,
1541                         VfModuleArtifact heatNestedArtifact) {
1542
1543                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null && vfModuleStructure
1544                                 .getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
1545                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
1546                                         .getArtifactInfo().getRelatedArtifacts()) {
1547                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
1548                                         return ASDCConfiguration.HEAT;
1549                                 }
1550
1551                         }
1552                 } 
1553                 
1554                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null 
1555                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()
1556                                                 .getRelatedArtifacts() != null) {
1557                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL)
1558                                         .get(0).getArtifactInfo().getRelatedArtifacts()) {
1559                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
1560                                         return ASDCConfiguration.HEAT_VOL;
1561                                 }
1562                         
1563                         }
1564                 }
1565                 
1566                 // Does not belong to anything
1567                 return null;
1568                         
1569         }
1570         
1571         protected static String createVNFName(VfResourceStructure vfResourceStructure) {
1572
1573                 return vfResourceStructure.getNotification().getServiceName() + "/"
1574                                 + vfResourceStructure.getResourceInstance().getResourceInstanceName();
1575         }
1576
1577         protected static String createVfModuleName(VfModuleStructure vfModuleStructure) {
1578                 
1579                 return createVNFName(vfModuleStructure.getParentVfResource()) + "::"
1580                                 + vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
1581         }
1582         
1583         
1584         protected static Timestamp getCurrentTimeStamp() {
1585                 
1586                 return new Timestamp(new Date().getTime());
1587         }
1588
1589 }