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