970cb0bf50b8d3e994c24a9447eb76a7a32fbfc7
[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 VFModuleMetadata 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 for VF Module Metadata: " + vfMetadata.getVfModuleModelCustomizationUUID());
503                                         
504                                 }
505                                 service.getVnfCustomizations().add(vnfResource);
506                         }
507                 }
508         }
509
510         public void processWatchdog(String distributionId, String servideUUID) {
511                 WatchdogServiceModVerIdLookup modVerIdLookup = new WatchdogServiceModVerIdLookup(distributionId,servideUUID);
512                 watchdogModVerIdLookupRepository.saveAndFlush(modVerIdLookup);
513                 
514                 WatchdogDistributionStatus distributionStatus = new WatchdogDistributionStatus(distributionId);
515                 watchdogDistributionStatusRepository.saveAndFlush(distributionStatus);
516         }
517
518         protected void extractHeatInformation(ToscaResourceStructure toscaResourceStruct,
519                         VfResourceStructure vfResourceStructure) {
520                 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
521
522                         switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
523                         case ASDCConfiguration.HEAT:
524                         case ASDCConfiguration.HEAT_NESTED:
525                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
526                                                 vfModuleArtifact);
527                                 break;
528                         case ASDCConfiguration.HEAT_VOL:
529                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
530                                                 vfModuleArtifact);
531                                 VfModuleArtifact envModuleArtifact = getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
532                                 createHeatEnvFromArtifact(vfResourceStructure, envModuleArtifact);
533                                 break;
534                         case ASDCConfiguration.HEAT_ENV:
535                                 createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
536                                 break;
537                         case ASDCConfiguration.HEAT_ARTIFACT:
538                                 createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact,
539                                                 toscaResourceStruct);
540                                 break;
541                         case ASDCConfiguration.HEAT_NET:
542                         case ASDCConfiguration.OTHER:
543                                 logger.warn(MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT,
544                                                 vfModuleArtifact.getArtifactInfo().getArtifactType() + "(Artifact Name:"
545                                                                 + vfModuleArtifact.getArtifactInfo().getArtifactName() + ")",
546                                                 "", "", MsoLogger.ErrorCode.DataError, "Artifact type not supported");
547                                 break;
548                         default:
549                                 break;
550
551                         }
552                 }
553         }
554
555         protected VfModuleArtifact getHeatEnvArtifactFromGeneratedArtifact(VfResourceStructure vfResourceStructure,
556                         VfModuleArtifact vfModuleArtifact) {
557                 String artifactName = vfModuleArtifact.getArtifactInfo().getArtifactName();
558                 artifactName = artifactName.substring(0, artifactName.indexOf('.'));
559                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
560                         if (moduleArtifact.getArtifactInfo().getArtifactName().contains(artifactName)
561                                         && moduleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
562                                 return moduleArtifact;
563                         }
564                 }
565                 return null;
566         }
567
568         public String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure,
569                         List<String> listTypes) {
570                 String newFileBody = filebody;
571                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
572
573                         if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
574
575                                 newFileBody = verifyTheFilePrefixInString(newFileBody,
576                                                 moduleArtifact.getArtifactInfo().getArtifactName());
577                         }
578                 }
579                 return newFileBody;
580         }
581
582         public String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
583
584                 String needlePrefix = "file:///";
585                 String prefixedFilenameToVerify = needlePrefix + filenameToVerify;
586
587                 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
588                         return body;
589                 }
590
591                 StringBuilder sb = new StringBuilder(body.length());
592
593                 int currentIndex = 0;
594                 int startIndex = 0;
595
596                 while (currentIndex != -1) {
597                         startIndex = currentIndex;
598                         currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);
599
600                         if (currentIndex == -1) {
601                                 break;
602                         }
603                         // We append from the startIndex up to currentIndex (start of File
604                         // Name)
605                         sb.append(body.substring(startIndex, currentIndex));
606                         sb.append(filenameToVerify);
607
608                         currentIndex += prefixedFilenameToVerify.length();
609                 }
610
611                 sb.append(body.substring(startIndex));
612
613                 return sb.toString();
614         }
615
616         protected void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
617                         ToscaResourceStructure toscaResourceStruct, VfModuleArtifact vfModuleArtifact) {
618                 HeatTemplate heatTemplate = new HeatTemplate();
619                 List<String> typeList = new ArrayList<>();
620                 typeList.add(ASDCConfiguration.HEAT_NESTED);
621                 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
622
623                 heatTemplate.setTemplateBody(
624                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
625                 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
626
627                 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
628                         heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
629                 } else {
630                         heatTemplate.setTimeoutMinutes(240);
631                 }
632
633                 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
634                 heatTemplate.setVersion(BigDecimalVersion
635                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
636                 heatTemplate.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
637
638                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
639                         heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
640                 } else {
641                         heatTemplate.setArtifactChecksum(MANUAL_RECORD);
642                 }
643
644                 Set<HeatTemplateParam> heatParam = extractHeatTemplateParameters(
645                                 vfModuleArtifact.getResult(), vfModuleArtifact.getArtifactInfo().getArtifactUUID());
646                 heatTemplate.setParameters(heatParam);  
647                 vfModuleArtifact.setHeatTemplate(heatTemplate);
648         }
649
650         protected void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
651                         VfModuleArtifact vfModuleArtifact) {
652                 HeatEnvironment heatEnvironment = new HeatEnvironment();
653                 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
654                 List<String> typeList = new ArrayList<>();
655                 typeList.add(ASDCConfiguration.HEAT);
656                 typeList.add(ASDCConfiguration.HEAT_VOL);
657                 heatEnvironment.setEnvironment(
658                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
659                 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
660                 heatEnvironment.setVersion(BigDecimalVersion
661                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));     
662                 heatEnvironment.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
663
664                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
665                         heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
666                 } else {
667                         heatEnvironment.setArtifactChecksum(MANUAL_RECORD);
668                 }               
669                 vfModuleArtifact.setHeatEnvironment(heatEnvironment);
670         }
671
672         protected void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
673                 VfModuleArtifact vfModuleArtifact, ToscaResourceStructure toscaResourceStruct) {
674                 
675                 HeatFiles heatFile = new HeatFiles();   
676                 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
677                 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
678                 heatFile.setFileBody(vfModuleArtifact.getResult());
679                 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
680                 heatFile.setVersion(BigDecimalVersion
681                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
682                 toscaResourceStruct.setHeatFilesUUID(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
683                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
684                         heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
685                 } else {
686                         heatFile.setArtifactChecksum(MANUAL_RECORD);
687                 }
688                 vfModuleArtifact.setHeatFiles(heatFile);
689         }
690
691         protected Service createService(ToscaResourceStructure toscaResourceStructure,
692                         VfResourceStructure vfResourceStructure) {
693
694                 toscaResourceStructure.getServiceMetadata();
695
696                 Metadata serviceMetadata = toscaResourceStructure.getServiceMetadata();
697
698                 Service service = new Service();
699
700                 if (serviceMetadata != null) {
701
702                         if (toscaResourceStructure.getServiceVersion() != null) {
703                                 service.setModelVersion(toscaResourceStructure.getServiceVersion());
704                         }
705
706                         service.setServiceType(serviceMetadata.getValue("serviceType"));
707                         service.setServiceRole(serviceMetadata.getValue("serviceRole"));
708
709                         service.setDescription(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
710                         service.setModelName(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
711                         service.setModelUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
712                         service.setEnvironmentContext(serviceMetadata.getValue("environmentContext"));
713
714                         if (vfResourceStructure != null) 
715                                 service.setWorkloadContext(vfResourceStructure.getNotification().getWorkloadContext());
716                                                 
717                         service.setModelInvariantUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
718                         service.setCsar(toscaResourceStructure.getCatalogToscaCsar());                  
719                 }
720                 
721                 
722                 toscaResourceStructure.setCatalogService(service); 
723                 return service;
724         }
725         
726         protected ServiceProxyResourceCustomization createServiceProxy(NodeTemplate nodeTemplate, Service service, ToscaResourceStructure toscaResourceStructure) {
727
728                 Metadata spMetadata = nodeTemplate.getMetaData();
729                 
730                 ServiceProxyResource spResource = new ServiceProxyResource();
731                 
732                 spResource.setModelName(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
733                 spResource.setModelInvariantUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
734                 spResource.setModelUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
735                 spResource.setModelVersion(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
736                 spResource.setDescription(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));     
737                 
738                 ServiceProxyResourceCustomization spCustomizationResource = new ServiceProxyResourceCustomization();
739                 
740                 Set<ServiceProxyResourceCustomization> serviceProxyCustomizationSet = new HashSet<>();
741                 
742                 spCustomizationResource.setModelCustomizationUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
743                 spCustomizationResource.setModelInstanceName(nodeTemplate.getName());
744                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
745                 spCustomizationResource.setSourceService(service);
746                 spCustomizationResource.setServiceProxyResource(spResource);
747                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
748                 spCustomizationResource.setServiceProxyResource(spResource);
749                 serviceProxyCustomizationSet.add(spCustomizationResource);
750
751                 toscaResourceStructure.setCatalogServiceProxyResource(spResource);
752                 
753                 toscaResourceStructure.setCatalogServiceProxyResourceCustomization(spCustomizationResource);
754                 
755                 return spCustomizationResource;
756         }
757         
758         protected ConfigurationResourceCustomization createConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization) {
759
760                 Metadata metadata = nodeTemplate.getMetaData();
761                 
762                 ConfigurationResource configResource = new ConfigurationResource();
763                 
764                 configResource.setModelName(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
765                 configResource.setModelInvariantUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
766                 configResource.setModelUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
767                 configResource.setModelVersion(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
768                 configResource.setDescription(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
769                 configResource.setToscaNodeType(nodeTemplate.getType());
770                 
771                 ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
772                 
773                 Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
774                 
775                 configCustomizationResource.setModelCustomizationUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
776                 configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
777                 
778                 configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
779                 configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
780                 configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
781                 configCustomizationResource.setServiceProxyResourceCustomization(spResourceCustomization);
782                 configCustomizationResource.setConfigResourceCustomization(configCustomizationResource);
783                 configCustomizationResource.setConfigurationResource(configResource);
784                 configResourceCustomizationSet.add(configCustomizationResource);
785
786                 configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);   
787                 
788                 toscaResourceStructure.setCatalogConfigurationResource(configResource);
789                 
790                 toscaResourceStructure.setCatalogConfigurationResourceCustomization(configCustomizationResource);
791                 
792                 return configCustomizationResource;
793         }
794         
795         protected ConfigurationResourceCustomization createFabricConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure) {
796                 
797                 Metadata fabricMetadata = nodeTemplate.getMetaData();
798                 
799                 ConfigurationResource configResource = new ConfigurationResource();
800                 
801                 configResource.setModelName(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
802                 configResource.setModelInvariantUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
803                 configResource.setModelUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
804                 configResource.setModelVersion(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
805                 configResource.setDescription(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
806                 configResource.setToscaNodeType(nodeTemplate.getType());
807                 
808                 ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
809                 
810                 Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
811                 
812                 configCustomizationResource.setModelCustomizationUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
813                 configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
814                 
815                 configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "function"));
816                 configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "role"));
817                 configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, "type"));
818                 configCustomizationResource.setConfigResourceCustomization(configCustomizationResource);
819                 configCustomizationResource.setConfigurationResource(configResource);
820                 configResourceCustomizationSet.add(configCustomizationResource);
821
822                 configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);
823                 
824                 return configCustomizationResource;
825         }
826
827         protected void createToscaCsar(ToscaResourceStructure toscaResourceStructure) {
828                 ToscaCsar toscaCsar = new ToscaCsar();
829                 if (toscaResourceStructure.getToscaArtifact().getArtifactChecksum() != null) {
830                         toscaCsar.setArtifactChecksum(toscaResourceStructure.getToscaArtifact().getArtifactChecksum());
831                 } else {
832                         toscaCsar.setArtifactChecksum(MANUAL_RECORD);
833                 }
834                 toscaCsar.setArtifactUUID(toscaResourceStructure.getToscaArtifact().getArtifactUUID());
835                 toscaCsar.setName(toscaResourceStructure.getToscaArtifact().getArtifactName());
836                 toscaCsar.setVersion(toscaResourceStructure.getToscaArtifact().getArtifactVersion());
837                 toscaCsar.setDescription(toscaResourceStructure.getToscaArtifact().getArtifactDescription());
838                 toscaCsar.setUrl(toscaResourceStructure.getToscaArtifact().getArtifactURL());
839
840                 toscaResourceStructure.setCatalogToscaCsar(toscaCsar);
841         }
842         
843         protected VnfcCustomization findExistingVfc(Set<VnfcCustomization> vnfcCustomizations, String customizationUUID) {
844                 VnfcCustomization vnfcCustomization = null;
845                 for(VnfcCustomization vnfcCustom : vnfcCustomizations){
846                         if (vnfcCustom != null && vnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
847                                 vnfcCustomization = vnfcCustom;
848                         }
849                 }
850                 
851                 if(vnfcCustomization==null)
852                         vnfcCustomization = vnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
853                 
854                 return vnfcCustomization;
855         }
856         
857         protected CvnfcCustomization findExistingCvfc(Set<CvnfcCustomization> cvnfcCustomizations, String customizationUUID) {
858                 CvnfcCustomization cvnfcCustomization = null;
859                 for(CvnfcCustomization cvnfcCustom : cvnfcCustomizations){
860                         if (cvnfcCustom != null && cvnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
861                                 cvnfcCustomization = cvnfcCustom;
862                         }
863                 }
864                 
865                 if(cvnfcCustomization==null)
866                         cvnfcCustomization = cvnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
867                 
868                 return cvnfcCustomization;
869         }
870
871         protected  NetworkResourceCustomization createNetwork(NodeTemplate networkNodeTemplate,
872                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin,Service service) {
873                 
874                 NetworkResourceCustomization networkResourceCustomization=networkCustomizationRepo.findOneByModelCustomizationUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
875                                 if(networkResourceCustomization==null){
876                         networkResourceCustomization = createNetworkResourceCustomization(networkNodeTemplate,
877                                         toscaResourceStructure);
878                                         
879                         NetworkResource networkResource = findExistingNetworkResource(service,
880                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
881                                         if(networkResource == null)
882                                 networkResource = createNetworkResource(networkNodeTemplate, toscaResourceStructure, heatTemplate,
883                                                 aicMax, aicMin);
884
885                                         networkResource.addNetworkResourceCustomization(networkResourceCustomization);          
886                                         networkResourceCustomization.setNetworkResource(networkResource);
887                                 }
888                 return networkResourceCustomization;
889         }
890         
891         protected  NetworkResource findExistingNetworkResource(Service service, String modelUUID) {
892                 NetworkResource networkResource = null;
893                 for(NetworkResourceCustomization networkCustom : service.getNetworkCustomizations()){
894                         if (networkCustom.getNetworkResource() != null
895                                         && networkCustom.getNetworkResource().getModelUUID().equals(modelUUID)) {
896                                         networkResource = networkCustom.getNetworkResource();
897                         }
898                 }
899                 if(networkResource==null)
900                         networkResource = networkRepo.findResourceByModelUUID(modelUUID);
901                 
902                 return networkResource;
903         }
904         
905         protected NetworkResourceCustomization createNetworkResourceCustomization(NodeTemplate networkNodeTemplate,
906                         ToscaResourceStructure toscaResourceStructure) {
907                 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
908                 networkResourceCustomization.setModelInstanceName(
909                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
910                 networkResourceCustomization.setModelCustomizationUUID(
911                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));                
912
913                 networkResourceCustomization.setNetworkTechnology(
914                                 testNull(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
915                                                 SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY)));
916                 networkResourceCustomization.setNetworkType(testNull(toscaResourceStructure.getSdcCsarHelper()
917                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE)));
918                 networkResourceCustomization.setNetworkRole(testNull(toscaResourceStructure.getSdcCsarHelper()
919                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE)));
920                 networkResourceCustomization.setNetworkScope(testNull(toscaResourceStructure.getSdcCsarHelper()
921                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE)));
922                 return networkResourceCustomization;
923         }
924
925         protected NetworkResource createNetworkResource(NodeTemplate networkNodeTemplate,
926                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin) {
927                 NetworkResource networkResource = new NetworkResource();
928                 String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
929                                 networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
930
931                 if ("true".equalsIgnoreCase(providerNetwork)) {
932                         networkResource.setNeutronNetworkType(PROVIDER);
933                 } else {
934                         networkResource.setNeutronNetworkType(BASIC);
935                 }
936
937                 networkResource.setModelName(
938                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
939
940                 networkResource.setModelInvariantUUID(
941                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
942                 networkResource.setModelUUID(
943                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
944                 networkResource.setModelVersion(
945                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
946
947                 networkResource.setAicVersionMax(aicMax);               
948                 networkResource.setAicVersionMin(aicMin);
949                 networkResource.setToscaNodeType(networkNodeTemplate.getType());
950                 networkResource.setDescription(
951                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
952                 networkResource.setOrchestrationMode(HEAT);     
953                 networkResource.setHeatTemplate(heatTemplate); 
954                 return networkResource;
955         }
956         
957         protected  CollectionNetworkResourceCustomization createNetworkCollection(NodeTemplate networkNodeTemplate,
958                         ToscaResourceStructure toscaResourceStructure, Service service) {
959
960                 CollectionNetworkResourceCustomization collectionNetworkResourceCustomization = new CollectionNetworkResourceCustomization();
961
962                 // **** Build Object to populate Collection_Resource table
963                 CollectionResource collectionResource = new CollectionResource();
964
965                 collectionResource
966                                 .setModelName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
967                 collectionResource.setModelInvariantUUID(
968                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
969                 collectionResource
970                                 .setModelUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
971                 collectionResource
972                                 .setModelVersion(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
973                 collectionResource
974                                 .setDescription(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
975                 collectionResource.setToscaNodeType(networkNodeTemplate.getType());
976
977                 toscaResourceStructure.setCatalogCollectionResource(collectionResource);
978
979                 // **** Build object to populate Collection_Resource_Customization table
980                 NetworkCollectionResourceCustomization ncfc = new NetworkCollectionResourceCustomization();
981                 
982                 ncfc.setFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
983                                 "cr_function"));
984                 ncfc.setRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
985                                 "cr_role"));
986                 ncfc.setType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
987                                 "cr_type"));
988
989                 ncfc.setModelInstanceName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
990                 ncfc.setModelCustomizationUUID(
991                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
992                 
993                 Set<CollectionNetworkResourceCustomization> networkResourceCustomizationSet = new HashSet<>();
994                 networkResourceCustomizationSet.add(collectionNetworkResourceCustomization);
995
996                 ncfc.setNetworkResourceCustomization(networkResourceCustomizationSet);
997
998                 ncfc.setCollectionResource(collectionResource);
999                 toscaResourceStructure.setCatalogCollectionResourceCustomization(ncfc);
1000                 
1001                 //*** Build object to populate the Instance_Group table
1002                 List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
1003                                 .getGroupsOfOriginOfNodeTemplateByToscaGroupType(networkNodeTemplate,
1004                                                 "org.openecomp.groups.NetworkCollection");
1005                 
1006                 List<NetworkInstanceGroup> networkInstanceGroupList = new ArrayList<>();
1007
1008                 List<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationList = new ArrayList<CollectionResourceInstanceGroupCustomization>();
1009
1010                 for (Group group : groupList) { 
1011                         
1012                         NetworkInstanceGroup networkInstanceGroup = new NetworkInstanceGroup();
1013                         Metadata instanceMetadata = group.getMetadata();
1014                         networkInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1015                         networkInstanceGroup
1016                                         .setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1017                         networkInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1018                         networkInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1019                         networkInstanceGroup.setToscaNodeType(group.getType());
1020                         networkInstanceGroup.setRole(SubType.SUB_INTERFACE.toString()); // Set
1021                                                                                                                                                         // Role
1022                         networkInstanceGroup.setType(InstanceGroupType.L3_NETWORK); // Set
1023                                                                                                                                                 // type
1024                         networkInstanceGroup.setCollectionResource(collectionResource);
1025                 
1026                         // ****Build object to populate
1027                         // Collection_Resource_Instance_Group_Customization table
1028                         CollectionResourceInstanceGroupCustomization crInstanceGroupCustomization = new CollectionResourceInstanceGroupCustomization();
1029                         crInstanceGroupCustomization.setInstanceGroup(networkInstanceGroup);
1030                         crInstanceGroupCustomization.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1031                         crInstanceGroupCustomization.setModelCustomizationUUID(
1032                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1033                 
1034                         String quantityName = instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME);
1035                         String fixedQuantity = quantityName.replace("NetworkCollection", "Fixed");
1036                         if (toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1037                                         fixedQuantity + "_quantity") != null) {
1038
1039                                 crInstanceGroupCustomization.setSubInterfaceNetworkQuantity(Integer.parseInt(
1040                                                 toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1041                                                                 fixedQuantity + "_quantity"))); 
1042                         }
1043                 
1044                         crInstanceGroupCustomization.setDescription(
1045                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1046                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
1047                                                                         + "_network_collection_description"));
1048                         crInstanceGroupCustomization.setFunction(
1049                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1050                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
1051                                                                         + "_network_collection_function"));
1052                         crInstanceGroupCustomization.setCollectionResourceCust(ncfc);
1053                         collectionResourceInstanceGroupCustomizationList.add(crInstanceGroupCustomization);
1054
1055                         networkInstanceGroup
1056                                         .setCollectionInstanceGroupCustomizations(collectionResourceInstanceGroupCustomizationList);
1057
1058                         networkInstanceGroupList.add(networkInstanceGroup);
1059
1060                 //}
1061
1062                 toscaResourceStructure.setCatalogNetworkInstanceGroup(networkInstanceGroupList);
1063
1064                 List<NodeTemplate> vlNodeList = toscaResourceStructure.getSdcCsarHelper()
1065                                 .getNodeTemplateBySdcType(networkNodeTemplate, SdcTypes.VL);
1066                 
1067                 List<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationList = new ArrayList<>();
1068                 
1069                 //*****Build object to populate the NetworkResource table
1070                 NetworkResource networkResource = new NetworkResource();
1071                 
1072                 for(NodeTemplate vlNodeTemplate : vlNodeList){
1073
1074                         String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
1075                                         vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
1076
1077                         if ("true".equalsIgnoreCase(providerNetwork)) {
1078                                 networkResource.setNeutronNetworkType(PROVIDER);
1079                         } else {
1080                                 networkResource.setNeutronNetworkType(BASIC);
1081                         }
1082
1083                         networkResource.setModelName(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1084
1085                         networkResource.setModelInvariantUUID(
1086                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1087                         networkResource.setModelUUID(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1088                         networkResource
1089                                         .setModelVersion(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1090
1091                         networkResource.setAicVersionMax(
1092                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES));
1093                         
1094                         TempNetworkHeatTemplateLookup tempNetworkLookUp = tempNetworkLookupRepo.findFirstBynetworkResourceModelName(
1095                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1096                         
1097                         if (tempNetworkLookUp != null ) {       
1098                                         
1099                                 HeatTemplate heatTemplate = heatRepo
1100                                                 .findByArtifactUuid(tempNetworkLookUp.getHeatTemplateArtifactUuid());
1101                                         networkResource.setHeatTemplate(heatTemplate);
1102                                         
1103                                         networkResource.setAicVersionMin(tempNetworkLookUp.getAicVersionMin());
1104                                         
1105                         }
1106
1107                         networkResource.setToscaNodeType(vlNodeTemplate.getType());
1108                         networkResource
1109                                         .setDescription(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1110                         networkResource.setOrchestrationMode(HEAT);
1111                         
1112                         // Build object to populate the
1113                         // Collection_Network_Resource_Customization table
1114                         for (NodeTemplate memberNode : group.getMemberNodes()) {
1115                                 collectionNetworkResourceCustomization.setModelInstanceName(memberNode.getName());
1116                         }
1117
1118                         collectionNetworkResourceCustomization.setModelCustomizationUUID(
1119                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1120
1121                         collectionNetworkResourceCustomization.setNetworkTechnology(
1122                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vlNodeTemplate,
1123                                                         SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY));
1124                         collectionNetworkResourceCustomization.setNetworkType(toscaResourceStructure.getSdcCsarHelper()
1125                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE));
1126                         collectionNetworkResourceCustomization.setNetworkRole(toscaResourceStructure.getSdcCsarHelper()
1127                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE));
1128                         collectionNetworkResourceCustomization.setNetworkScope(toscaResourceStructure.getSdcCsarHelper()
1129                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE));
1130                         collectionNetworkResourceCustomization.setInstanceGroup(networkInstanceGroup);
1131                         collectionNetworkResourceCustomization.setNetworkResource(networkResource);
1132                         collectionNetworkResourceCustomization.setNetworkResourceCustomization(ncfc);
1133                         
1134                         collectionNetworkResourceCustomizationList.add(collectionNetworkResourceCustomization);
1135                    }
1136
1137                 }
1138                 
1139                 return collectionNetworkResourceCustomization;
1140         }
1141         
1142         protected VnfcInstanceGroupCustomization createVNFCInstanceGroup(NodeTemplate vnfcNodeTemplate, Group group,
1143                         VnfResourceCustomization vnfResourceCustomization) {
1144
1145                         Metadata instanceMetadata = group.getMetadata();
1146                         // Populate InstanceGroup
1147                         VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup();
1148                         
1149                         vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1150                         vfcInstanceGroup.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1151                         vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1152                         vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1153                 vfcInstanceGroup.setToscaNodeType(group.getType());
1154                         vfcInstanceGroup.setRole("SUB-INTERFACE");   // Set Role
1155                         vfcInstanceGroup.setType(InstanceGroupType.VNFC);  // Set type  
1156                         
1157                         //Populate VNFCInstanceGroupCustomization
1158                         VnfcInstanceGroupCustomization vfcInstanceGroupCustom = new VnfcInstanceGroupCustomization();
1159                         
1160                         vfcInstanceGroupCustom.setModelCustomizationUUID(vnfResourceCustomization.getModelCustomizationUUID());
1161                         vfcInstanceGroupCustom.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1162                         vfcInstanceGroupCustom.setDescription(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1163                         vfcInstanceGroupCustom.setFunction("FUNCTION");
1164                         vfcInstanceGroupCustom.setInstanceGroup(vfcInstanceGroup);
1165                         vfcInstanceGroupCustom.setVnfResourceCust(vnfResourceCustomization);            
1166                         
1167                 return vfcInstanceGroupCustom;
1168
1169         }
1170         
1171         protected VFCInstanceGroup findExistingVnfcInstanceGroup(VnfResourceCustomization vnfResourceCustomization,
1172                         String modelUUID) {
1173                 VFCInstanceGroup vfcInstanceGroup = null;
1174                 List<VnfcInstanceGroupCustomization> vnfInstanceGroupCustomizations = vnfResourceCustomization
1175                                 .getVnfcInstanceGroupCustomizations();
1176                 if(vnfInstanceGroupCustomizations != null){
1177                         for (VnfcInstanceGroupCustomization vnfcInstanceGroupCustom : vnfResourceCustomization
1178                                         .getVnfcInstanceGroupCustomizations()) {
1179                                 if (vnfcInstanceGroupCustom.getInstanceGroup() != null
1180                                                 && vnfcInstanceGroupCustom.getInstanceGroup().getModelUUID().equals(modelUUID)) {
1181                                         vfcInstanceGroup = (VFCInstanceGroup)vnfcInstanceGroupCustom.getInstanceGroup();
1182                                 }
1183                         }
1184                 }
1185                 if (vfcInstanceGroup == null)
1186                         vfcInstanceGroup = (VFCInstanceGroup) instanceGroupRepo.findByModelUUID(modelUUID);
1187
1188                 return vfcInstanceGroup;
1189         }
1190         
1191         protected VfModuleCustomization createVFModuleResource(Group group, NodeTemplate nodeTemplate,
1192                         ToscaResourceStructure toscaResourceStructure, VfResourceStructure vfResourceStructure,
1193                         IVfModuleData vfModuleData, VnfResourceCustomization vnfResource, Service service, Set<CvnfcCustomization> existingCvnfcSet, Set<VnfcCustomization> existingVnfcSet) {
1194                 
1195                 VfModuleCustomization vfModuleCustomization = findExistingVfModuleCustomization(vnfResource,
1196                                 vfModuleData.getVfModuleModelCustomizationUUID());
1197                 if(vfModuleCustomization == null){              
1198                         VfModule vfModule = findExistingVfModule(vnfResource,
1199                                         nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1200                         Metadata vfMetadata = group.getMetadata();
1201                         if(vfModule==null)
1202                                 vfModule=createVfModule(group, toscaResourceStructure, vfModuleData, vfMetadata);
1203                         
1204                         vfModuleCustomization = createVfModuleCustomzation(group, toscaResourceStructure, vfModule, vfModuleData);
1205                         setHeatInformationForVfModule(toscaResourceStructure, vfResourceStructure, vfModule, vfModuleCustomization,
1206                                         vfMetadata);
1207                         vfModuleCustomization.setVfModule(vfModule);
1208                         vfModule.getVfModuleCustomization().add(vfModuleCustomization);
1209                         vnfResource.getVfModuleCustomizations().add(vfModuleCustomization);
1210                 } else {
1211                         vfResourceStructure.setAlreadyDeployed(true);
1212                 }
1213                 
1214                 //******************************************************************************************************************
1215                 //* Extract VFC's and CVFC's then add them to VFModule
1216                 //******************************************************************************************************************
1217                 
1218                 Set<VnfVfmoduleCvnfcConfigurationCustomization> vnfVfmoduleCvnfcConfigurationCustomizations = new HashSet<VnfVfmoduleCvnfcConfigurationCustomization>();                
1219                 Set<CvnfcCustomization> cvnfcCustomizations = new HashSet<CvnfcCustomization>();
1220                 Set<VnfcCustomization> vnfcCustomizations = new HashSet<VnfcCustomization>();
1221
1222                 // Extract CVFC lists
1223                 List<NodeTemplate> cvfcList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(nodeTemplate, SdcTypes.CVFC);
1224                                                 
1225                 for(NodeTemplate cvfcTemplate : cvfcList) {
1226                                                                         
1227                         CvnfcCustomization existingCvnfcCustomization = findExistingCvfc(existingCvnfcSet, cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1228                         
1229                         if(existingCvnfcCustomization == null){
1230                         
1231                         //Extract associated VFC - Should always be just one
1232                         List<NodeTemplate> vfcList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(cvfcTemplate, SdcTypes.VFC);
1233                                                 
1234                         for(NodeTemplate vfcTemplate : vfcList) {
1235                                 
1236                                 VnfcCustomization vnfcCustomization = new VnfcCustomization();
1237                                 VnfcCustomization existingVnfcCustomization = null;
1238                                 
1239                                 existingVnfcCustomization = findExistingVfc(existingVnfcSet, vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1240                                         
1241                                 // Only Add Abstract VNFC's to our DB, ignore all others
1242                                 if(existingVnfcCustomization == null && vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY).equalsIgnoreCase("Abstract")){
1243                                         vnfcCustomization.setModelCustomizationUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1244                                         vnfcCustomization.setModelInstanceName(vfcTemplate.getName());
1245                                         vnfcCustomization.setModelInvariantUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1246                                         vnfcCustomization.setModelName(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1247                                         vnfcCustomization.setModelUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1248         
1249                                         vnfcCustomization.setModelVersion(
1250                                                         testNull(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1251                                         vnfcCustomization.setDescription(
1252                                                         testNull(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1253                                         vnfcCustomization.setToscaNodeType(testNull(vfcTemplate.getType()));
1254                                         
1255                                         vnfcCustomizations.add(vnfcCustomization);
1256                                         existingVnfcSet.add(vnfcCustomization);
1257                                 }
1258                         
1259                         // 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.
1260                         if(vnfcCustomization.getModelCustomizationUUID() != null){
1261                                 
1262                                 CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
1263                                 cvnfcCustomization.setModelCustomizationUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1264                                 cvnfcCustomization.setModelInstanceName(cvfcTemplate.getName());
1265                                 cvnfcCustomization.setModelInvariantUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1266                                 cvnfcCustomization.setModelName(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1267                                 cvnfcCustomization.setModelUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1268         
1269                                 cvnfcCustomization.setModelVersion(
1270                                                 testNull(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1271                                 cvnfcCustomization.setDescription(
1272                                                 testNull(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1273                                 cvnfcCustomization.setToscaNodeType(testNull(cvfcTemplate.getType()));
1274                                 
1275                                 if(existingVnfcCustomization != null){
1276                                         cvnfcCustomization.setVnfcCustomization(existingVnfcCustomization);
1277                                 }else{
1278                                         cvnfcCustomization.setVnfcCustomization(vnfcCustomization);
1279                                 }
1280                                 
1281                                 cvnfcCustomization.setVfModuleCustomization(vfModuleCustomization);
1282                                 cvnfcCustomization.setVnfResourceCustomization(vnfResource);                                    
1283                                 
1284                                 cvnfcCustomizations.add(cvnfcCustomization);
1285                                 existingCvnfcSet.add(cvnfcCustomization);
1286                         
1287                         //*****************************************************************************************************************************************
1288                         //* Extract Fabric Configuration
1289                         //*****************************************************************************************************************************************
1290                         
1291                         List<NodeTemplate> fabricConfigList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(nodeTemplate, SdcTypes.CONFIGURATION);
1292                                                                 
1293                         for(NodeTemplate fabricTemplate : fabricConfigList) {
1294                                                                                 
1295                                 ConfigurationResource fabricConfig = null;
1296                                 
1297                                 ConfigurationResource existingConfig = findExistingConfiguration(service, fabricTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1298                                                                 
1299                                 if(existingConfig == null){
1300                                         
1301                                         ConfigurationResourceCustomization fabricConfigCustomization = createFabricConfiguration(fabricTemplate, toscaResourceStructure);
1302                                         
1303                                         fabricConfig = fabricConfigCustomization.getConfigurationResource();
1304                                         
1305                                         service.getConfigurationCustomizations().add(fabricConfigCustomization);
1306                                 }else {
1307                                         fabricConfig = existingConfig;
1308                                 }
1309                                 
1310                                 
1311                                 VnfVfmoduleCvnfcConfigurationCustomization vnfVfmoduleCvnfcConfigurationCustomization = createVfCnvfConfigCustomization(fabricTemplate, toscaResourceStructure, 
1312                                                                                                                                                            vnfResource, vfModuleCustomization, cvnfcCustomization, fabricConfig);
1313                                 
1314                                 vnfVfmoduleCvnfcConfigurationCustomizations.add(vnfVfmoduleCvnfcConfigurationCustomization);
1315                         }
1316                         
1317                         }
1318                         
1319                    }
1320                         
1321                   }
1322                         
1323                 } 
1324                 
1325                 vfModuleCustomization.setCvnfcCustomization(cvnfcCustomizations);
1326                 vfModuleCustomization.setVnfVfmoduleCvnfcConfigurationCustomization(vnfVfmoduleCvnfcConfigurationCustomizations);
1327                 
1328                 return vfModuleCustomization;
1329         }
1330         
1331         protected VnfVfmoduleCvnfcConfigurationCustomization createVfCnvfConfigCustomization(NodeTemplate fabricTemplate, ToscaResourceStructure toscaResourceStruct, 
1332             VnfResourceCustomization vnfResource, VfModuleCustomization vfModuleCustomization, CvnfcCustomization cvnfcCustomization,
1333             ConfigurationResource configResource) {
1334
1335                 Metadata fabricMetadata = fabricTemplate.getMetaData();
1336                 
1337                 
1338                 VnfVfmoduleCvnfcConfigurationCustomization vfModuleToCvnfc = new VnfVfmoduleCvnfcConfigurationCustomization();
1339                 
1340                 vfModuleToCvnfc.setConfigurationResource(configResource);
1341                 vfModuleToCvnfc.setCvnfcCustomization(cvnfcCustomization);
1342                 vfModuleToCvnfc.setModelCustomizationUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1343                 vfModuleToCvnfc.setModelInstanceName(fabricTemplate.getName());
1344                 vfModuleToCvnfc.setVfModuleCustomization(vfModuleCustomization);
1345                 vfModuleToCvnfc.setVnfResourceCustomization(vnfResource);
1346                 vfModuleToCvnfc.setPolicyName(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, "name"));
1347                 
1348                 List<Policy> policyList = toscaResourceStruct.getSdcCsarHelper().getPoliciesOfTarget(fabricTemplate);
1349                 
1350                 if(policyList != null){
1351                         for(Policy policy : policyList){
1352                                 vfModuleToCvnfc.setPolicyName(policy.getName());
1353                         }
1354                 }
1355                 
1356                 vfModuleToCvnfc.setConfigurationFunction(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
1357                 vfModuleToCvnfc.setConfigurationRole(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
1358                 vfModuleToCvnfc.setConfigurationType(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
1359                 
1360                 return vfModuleToCvnfc;
1361         }
1362                 
1363         protected ConfigurationResource findExistingConfiguration(Service service, String modelUUID) {
1364                 ConfigurationResource configResource = null;
1365                 for(ConfigurationResourceCustomization configurationResourceCustom : service.getConfigurationCustomizations()){
1366                         if (configurationResourceCustom.getConfigurationResource() != null
1367                                         && configurationResourceCustom.getConfigurationResource().getModelUUID().equals(modelUUID)) {
1368                                 configResource = configurationResourceCustom.getConfigurationResource();
1369                         }
1370                 }
1371                 if(configResource==null)
1372                         configResource = configRepo.findResourceByModelUUID(modelUUID);
1373                 
1374                 return configResource;
1375         }
1376         
1377         protected ServiceProxyResource findExistingServiceProxyResource(List<ServiceProxyResourceCustomization> serviceProxyList, String modelUUID) {
1378                 ServiceProxyResource serviceProxyResource = null;
1379                 for(ServiceProxyResourceCustomization serviceProxyResourceCustom : serviceProxyList){
1380                         if (serviceProxyResourceCustom.getServiceProxyResource() != null
1381                                         && serviceProxyResourceCustom.getServiceProxyResource().getModelUUID().equals(modelUUID)) {
1382                                 serviceProxyResource = serviceProxyResourceCustom.getServiceProxyResource();
1383                         }
1384                 }
1385                 if(serviceProxyResource==null)
1386                         serviceProxyResource = serviceProxyRepo.findResourceByModelUUID(modelUUID);
1387                 
1388                 return serviceProxyResource;
1389         }
1390         
1391         protected VfModuleCustomization findExistingVfModuleCustomization(VnfResourceCustomization vnfResource,
1392                         String vfModuleModelCustomizationUUID) {
1393                 VfModuleCustomization vfModuleCustomization = null;
1394                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1395                         if(vfModuleCustom.getModelCustomizationUUID().equalsIgnoreCase(vfModuleModelCustomizationUUID)){
1396                                 vfModuleCustomization = vfModuleCustom;
1397                         }
1398                 }
1399                 if(vfModuleCustomization==null)
1400                         vfModuleCustomization = vfModuleCustomizationRepo
1401                                         .findByModelCustomizationUUID(vfModuleModelCustomizationUUID);
1402                 
1403                 return vfModuleCustomization;
1404         }
1405
1406         protected VfModule findExistingVfModule(VnfResourceCustomization vnfResource, String modelUUID) {
1407                 VfModule vfModule = null;
1408                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1409                         if(vfModuleCustom.getVfModule() != null && vfModuleCustom.getVfModule().getModelUUID().equals(modelUUID)){
1410                                 vfModule = vfModuleCustom.getVfModule();
1411                         }
1412                 }
1413                 if(vfModule==null)
1414                         vfModule = vfModuleRepo.findByModelUUID(modelUUID);
1415                 
1416                 return vfModule;
1417         }
1418
1419         protected VfModuleCustomization createVfModuleCustomzation(Group group,
1420                         ToscaResourceStructure toscaResourceStructure, VfModule vfModule, IVfModuleData vfModuleData) {
1421                 VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
1422                 
1423                 vfModuleCustomization.setModelCustomizationUUID(vfModuleData.getVfModuleModelCustomizationUUID());
1424
1425                 vfModuleCustomization.setVfModule(vfModule);
1426
1427                 String initialCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1428                                 SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT);
1429                 if (initialCount != null && initialCount.length() > 0) {
1430                         vfModuleCustomization.setInitialCount(Integer.valueOf(initialCount));
1431                 }
1432
1433                 vfModuleCustomization.setInitialCount(Integer.valueOf(toscaResourceStructure.getSdcCsarHelper()
1434                                 .getGroupPropertyLeafValue(group, SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT)));
1435
1436                 String availabilityZoneCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1437                                 SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT);
1438                 if (availabilityZoneCount != null && availabilityZoneCount.length() > 0) {
1439                         vfModuleCustomization.setAvailabilityZoneCount(Integer.valueOf(availabilityZoneCount));
1440                 }
1441
1442                 vfModuleCustomization.setLabel(toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1443                                 SdcPropertyNames.PROPERTY_NAME_VFMODULELABEL));
1444
1445                 String maxInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1446                                 SdcPropertyNames.PROPERTY_NAME_MAXVFMODULEINSTANCES);
1447                 if (maxInstances != null && maxInstances.length() > 0) {
1448                         vfModuleCustomization.setMaxInstances(Integer.valueOf(maxInstances));
1449                 }
1450
1451                 String minInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1452                                 SdcPropertyNames.PROPERTY_NAME_MINVFMODULEINSTANCES);
1453                 if (minInstances != null && minInstances.length() > 0) {
1454                         vfModuleCustomization.setMinInstances(Integer.valueOf(minInstances));
1455                 }
1456                 return vfModuleCustomization;
1457         }
1458
1459         protected VfModule createVfModule(Group group, ToscaResourceStructure toscaResourceStructure,
1460                         IVfModuleData vfModuleData, Metadata vfMetadata) {
1461                 VfModule vfModule = new VfModule();
1462                 String vfModuleModelUUID = vfModuleData.getVfModuleModelUUID();
1463
1464                 if(vfModuleModelUUID == null) {
1465                         vfModuleModelUUID = testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1466                                         SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1467                 } else if (vfModuleModelUUID.indexOf('.') > -1) {
1468                         vfModuleModelUUID = vfModuleModelUUID.substring(0, vfModuleModelUUID.indexOf('.'));
1469                 }
1470
1471                 vfModule.setModelInvariantUUID(testNull(toscaResourceStructure.getSdcCsarHelper()
1472                                 .getMetadataPropertyValue(vfMetadata, SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)));
1473                 vfModule.setModelName(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1474                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELNAME)));
1475                 vfModule.setModelUUID(vfModuleModelUUID);
1476                 vfModule.setModelVersion(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1477                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELVERSION)));
1478                 vfModule.setDescription(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1479                                 SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1480
1481                 String vfModuleType = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1482                                 SdcPropertyNames.PROPERTY_NAME_VFMODULETYPE);
1483                 if (vfModuleType != null && "Base".equalsIgnoreCase(vfModuleType)) {
1484                         vfModule.setIsBase(true);
1485                 } else {
1486                         vfModule.setIsBase(false);
1487                 }
1488                 return vfModule;
1489         }
1490
1491         protected void setHeatInformationForVfModule(ToscaResourceStructure toscaResourceStructure,
1492                         VfResourceStructure vfResourceStructure, VfModule vfModule, VfModuleCustomization vfModuleCustomization,
1493                         Metadata vfMetadata) {
1494                 
1495                 Optional<VfModuleStructure> matchingObject = vfResourceStructure.getVfModuleStructure().stream()
1496                                 .filter(vfModuleStruct -> vfModuleStruct.getVfModuleMetadata().getVfModuleModelUUID()
1497                                                 .equalsIgnoreCase(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1498                                                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID)))
1499                                 .findFirst();
1500
1501                 if (matchingObject.isPresent()) {
1502                         List<HeatFiles> heatFilesList = new ArrayList<>();
1503                         List<HeatTemplate> volumeHeatChildTemplates = new ArrayList<HeatTemplate>();
1504                         List<HeatTemplate> heatChildTemplates = new ArrayList<HeatTemplate>();
1505                         HeatTemplate parentHeatTemplate = new HeatTemplate();
1506                         String parentArtifactType = null;
1507                         Set<String> artifacts = new HashSet<>(matchingObject.get().getVfModuleMetadata().getArtifacts());
1508                         for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
1509                                 
1510                                 List<HeatTemplate> childNestedHeatTemplates = new ArrayList<HeatTemplate>();
1511                                 
1512                                 if (artifacts.contains(vfModuleArtifact.getArtifactInfo().getArtifactUUID())) {
1513                                         checkVfModuleArtifactType(vfModule, vfModuleCustomization, heatFilesList, vfModuleArtifact,
1514                                                         childNestedHeatTemplates, parentHeatTemplate, vfResourceStructure);
1515                                 }
1516                                 
1517                                 if(vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)){
1518                                         parentArtifactType = identifyParentOfNestedTemplate(matchingObject.get(), vfModuleArtifact);
1519                                         
1520                                         if(!childNestedHeatTemplates.isEmpty()){
1521                                         
1522                                                 if (parentArtifactType != null && parentArtifactType.equalsIgnoreCase(ASDCConfiguration.HEAT_VOL)) {
1523                                                         volumeHeatChildTemplates.add(childNestedHeatTemplates.get(0));
1524                                                 } else {
1525                                                         heatChildTemplates.add(childNestedHeatTemplates.get(0));
1526                                                 }
1527                                         }
1528                                 }
1529                                 
1530                         }
1531                         if(!heatFilesList.isEmpty()){
1532                                 vfModule.setHeatFiles(heatFilesList);
1533                         }
1534                         
1535                         
1536                         // Set all Child Templates related to HEAT_VOLUME
1537                         if(!volumeHeatChildTemplates.isEmpty()){
1538                                 if(vfModule.getVolumeHeatTemplate() != null){
1539                                         vfModule.getVolumeHeatTemplate().setChildTemplates(volumeHeatChildTemplates);
1540                                 }else{
1541                                         logger.debug("VolumeHeatTemplate not set in setHeatInformationForVfModule()");
1542                                 }
1543                         }
1544                         
1545                         // Set all Child Templates related to HEAT
1546                         if(!heatChildTemplates.isEmpty()){
1547                                 if(vfModule.getModuleHeatTemplate() != null){
1548                                         vfModule.getModuleHeatTemplate().setChildTemplates(heatChildTemplates);
1549                                 }else{
1550                                         logger.debug("ModuleHeatTemplate not set in setHeatInformationForVfModule()");
1551                                 }
1552                         }
1553                 }
1554         }
1555
1556         protected void checkVfModuleArtifactType(VfModule vfModule, VfModuleCustomization vfModuleCustomization,
1557                         List<HeatFiles> heatFilesList, VfModuleArtifact vfModuleArtifact, List<HeatTemplate> nestedHeatTemplates,
1558                         HeatTemplate parentHeatTemplate, VfResourceStructure vfResourceStructure) {
1559                 if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT)) {
1560                         vfModuleArtifact.incrementDeployedInDB();
1561                         vfModule.setModuleHeatTemplate(vfModuleArtifact.getHeatTemplate());
1562                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_VOL)) {
1563                         vfModule.setVolumeHeatTemplate(vfModuleArtifact.getHeatTemplate());
1564                         VfModuleArtifact volVfModuleArtifact = this.getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
1565                         vfModuleCustomization.setVolumeHeatEnv(volVfModuleArtifact.getHeatEnvironment());
1566                         vfModuleArtifact.incrementDeployedInDB();
1567                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
1568                         if(vfModuleArtifact.getHeatEnvironment().getName().contains("volume")) {
1569                                 vfModuleCustomization.setVolumeHeatEnv(vfModuleArtifact.getHeatEnvironment());
1570                         } else { 
1571                         vfModuleCustomization.setHeatEnvironment(vfModuleArtifact.getHeatEnvironment());
1572                         }
1573                         vfModuleArtifact.incrementDeployedInDB();
1574                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ARTIFACT)) {
1575                         heatFilesList.add(vfModuleArtifact.getHeatFiles());                                                     
1576                         vfModuleArtifact.incrementDeployedInDB();
1577                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)) {
1578                         nestedHeatTemplates.add(vfModuleArtifact.getHeatTemplate());                            
1579                         vfModuleArtifact.incrementDeployedInDB();
1580                 }
1581         }
1582
1583         protected VnfResourceCustomization createVnfResource(NodeTemplate vfNodeTemplate,
1584                         ToscaResourceStructure toscaResourceStructure, Service service) {
1585                 VnfResourceCustomization vnfResourceCustomization = vnfCustomizationRepo.findOneByModelCustomizationUUID(
1586                                 vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1587                 if(vnfResourceCustomization == null){           
1588                         VnfResource vnfResource = findExistingVnfResource(service,
1589                                         vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1590                         
1591                         if(vnfResource==null)
1592                                 vnfResource=createVnfResource(vfNodeTemplate);
1593                         
1594                         vnfResourceCustomization = createVnfResourceCustomization(vfNodeTemplate, toscaResourceStructure,
1595                                         vnfResource);
1596                         vnfResourceCustomization.setVnfResources(vnfResource);
1597                         vnfResource.getVnfResourceCustomizations().add(vnfResourceCustomization);
1598                         
1599                         // Fetch VNFC Instance Group Info                               
1600                         List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
1601                                         .getGroupsOfOriginOfNodeTemplateByToscaGroupType(vfNodeTemplate,
1602                                                         "org.openecomp.groups.VfcInstanceGroup");
1603                                 
1604                         for (Group group : groupList) { 
1605                                 
1606                                 VFCInstanceGroup vfcInstanceGroup = findExistingVnfcInstanceGroup(vnfResourceCustomization,
1607                                                 group.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1608                                 if(vfcInstanceGroup == null){
1609                                         VnfcInstanceGroupCustomization vnfcInstanceGroupCustomization = createVNFCInstanceGroup(
1610                                                         vfNodeTemplate, group, vnfResourceCustomization);
1611                                 
1612                                         vnfcInstanceGroupCustomizationRepo.saveAndFlush(vnfcInstanceGroupCustomization);
1613                                 }
1614                         }
1615                         
1616                 }
1617                 return vnfResourceCustomization;
1618         }
1619         
1620         protected VnfResource findExistingVnfResource(Service service, String modelUUID) {
1621                 VnfResource vnfResource = null;
1622                 for(VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()){
1623                         if (vnfResourceCustom.getVnfResources() != null
1624                                         && vnfResourceCustom.getVnfResources().getModelUUID().equals(modelUUID)) {
1625                                 vnfResource = vnfResourceCustom.getVnfResources();
1626                         }
1627                 }
1628                 if(vnfResource==null)
1629                         vnfResource = vnfRepo.findResourceByModelUUID(modelUUID);
1630                 
1631                 return vnfResource;
1632         }
1633
1634         protected VnfResourceCustomization createVnfResourceCustomization(NodeTemplate vfNodeTemplate,
1635                         ToscaResourceStructure toscaResourceStructure, VnfResource vnfResource) {
1636                 VnfResourceCustomization vnfResourceCustomization = new VnfResourceCustomization();
1637                 vnfResourceCustomization.setModelCustomizationUUID(
1638                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
1639                 vnfResourceCustomization.setModelInstanceName(vfNodeTemplate.getName());
1640
1641                 vnfResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
1642                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
1643                 vnfResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
1644                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, "nf_naming_code")));
1645                 vnfResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
1646                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
1647                 vnfResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
1648                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
1649
1650                 vnfResourceCustomization.setMultiStageDesign(toscaResourceStructure.getSdcCsarHelper()
1651                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, MULTI_STAGE_DESIGN));
1652
1653                 vnfResourceCustomization.setVnfResources(vnfResource);
1654                 vnfResourceCustomization.setAvailabilityZoneMaxCount(Integer.getInteger(
1655                                 vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT)));  
1656
1657                 CapabilityAssignments vnfCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
1658                                 .getCapabilitiesOf(vfNodeTemplate);
1659
1660                 if (vnfCustomizationCapability != null) {
1661                         CapabilityAssignment capAssign = vnfCustomizationCapability.getCapabilityByName(SCALABLE);
1662
1663                         if (capAssign != null) {
1664                                 vnfResourceCustomization.setMinInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
1665                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1666                                 vnfResourceCustomization.setMaxInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
1667                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1668                         }
1669
1670                 }
1671                 
1672                 toscaResourceStructure.setCatalogVnfResourceCustomization(vnfResourceCustomization);
1673                 
1674                 return vnfResourceCustomization;
1675         }
1676
1677         protected VnfResource createVnfResource(NodeTemplate vfNodeTemplate) {
1678                 VnfResource vnfResource = new VnfResource();
1679                 vnfResource.setModelInvariantUUID(
1680                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
1681                 vnfResource.setModelName(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1682                 vnfResource.setModelUUID(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
1683
1684                 vnfResource.setModelVersion(
1685                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1686                 vnfResource.setDescription(
1687                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1688                 vnfResource.setOrchestrationMode(HEAT);
1689                 vnfResource.setToscaNodeType(testNull(vfNodeTemplate.getType()));
1690                 vnfResource.setAicVersionMax(
1691                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1692                 vnfResource.setAicVersionMin(
1693                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1694                 
1695                 return vnfResource;
1696         }
1697
1698         protected AllottedResourceCustomization createAllottedResource(NodeTemplate nodeTemplate,
1699                         ToscaResourceStructure toscaResourceStructure, Service service) {
1700                 AllottedResourceCustomization allottedResourceCustomization = allottedCustomizationRepo
1701                                 .findOneByModelCustomizationUUID(
1702                                 nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1703                         
1704                 if(allottedResourceCustomization == null){                      
1705                         AllottedResource allottedResource = findExistingAllottedResource(service,
1706                                         nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1707                         
1708                         if(allottedResource==null)
1709                                 allottedResource=createAR(nodeTemplate);
1710                         
1711                         toscaResourceStructure.setAllottedResource(allottedResource);                   
1712                         allottedResourceCustomization = createAllottedResourceCustomization(nodeTemplate, toscaResourceStructure);
1713                         allottedResourceCustomization.setAllottedResource(allottedResource);
1714                         allottedResource.getAllotedResourceCustomization().add(allottedResourceCustomization);
1715                 }
1716                 return allottedResourceCustomization;
1717         }
1718         
1719         protected AllottedResource findExistingAllottedResource(Service service, String modelUUID) {
1720                 AllottedResource allottedResource = null;
1721                 for(AllottedResourceCustomization allottedResourceCustom : service.getAllottedCustomizations()){
1722                         if (allottedResourceCustom.getAllottedResource() != null
1723                                         && allottedResourceCustom.getAllottedResource().getModelUUID().equals(modelUUID)) {
1724                                 allottedResource = allottedResourceCustom.getAllottedResource();
1725                         }
1726                 }
1727                 if(allottedResource==null)
1728                         allottedResource = allottedRepo.findResourceByModelUUID(modelUUID);
1729                 
1730                 return allottedResource;
1731         }
1732         
1733         protected AllottedResourceCustomization createAllottedResourceCustomization(NodeTemplate nodeTemplate,
1734                         ToscaResourceStructure toscaResourceStructure) {
1735                 AllottedResourceCustomization allottedResourceCustomization = new AllottedResourceCustomization();
1736                 allottedResourceCustomization.setModelCustomizationUUID(
1737                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
1738                 allottedResourceCustomization.setModelInstanceName(nodeTemplate.getName());
1739                 
1740
1741                 allottedResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
1742                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
1743                 allottedResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
1744                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, "nf_naming_code")));
1745                 allottedResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
1746                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
1747                 allottedResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
1748                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
1749
1750                 List<NodeTemplate> vfcNodes = toscaResourceStructure.getSdcCsarHelper().getVfcListByVf(allottedResourceCustomization.getModelCustomizationUUID());
1751                 
1752                 if(vfcNodes != null){
1753                         for(NodeTemplate vfcNode : vfcNodes){
1754                         
1755                                 allottedResourceCustomization.setProvidingServiceModelUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_uuid"));
1756                                 allottedResourceCustomization.setProvidingServiceModelInvariantUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_invariant_uuid"));
1757                                 allottedResourceCustomization.setProvidingServiceModelName(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_name"));
1758                         }
1759                 }
1760                 
1761
1762                 CapabilityAssignments arCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
1763                                 .getCapabilitiesOf(nodeTemplate);
1764
1765                 if (arCustomizationCapability != null) {
1766                         CapabilityAssignment capAssign = arCustomizationCapability.getCapabilityByName(SCALABLE);
1767
1768                         if (capAssign != null) {
1769                                 allottedResourceCustomization.setMinInstances(
1770                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
1771                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
1772                                 allottedResourceCustomization.setMaxInstances(
1773                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
1774                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
1775                         }
1776                 }
1777                 return allottedResourceCustomization;
1778         }
1779
1780         protected AllottedResource createAR(NodeTemplate nodeTemplate) {
1781                 AllottedResource allottedResource = new AllottedResource();
1782                 allottedResource
1783                 .setModelUUID(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
1784                 allottedResource.setModelInvariantUUID(
1785                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
1786                 allottedResource
1787                 .setModelName(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1788                 allottedResource
1789                 .setModelVersion(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1790                 allottedResource.setToscaNodeType(testNull(nodeTemplate.getType()));
1791                 allottedResource.setSubcategory(
1792                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY)));
1793                 allottedResource
1794                 .setDescription(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1795                 return allottedResource;
1796         }
1797
1798         protected Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile, String artifactUUID) {
1799                 // Scan the payload downloadResult and extract the HeatTemplate
1800                 // parameters
1801                 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
1802                 return yamlEditor.getParameterList(artifactUUID);
1803         }       
1804
1805         protected String testNull(Object object) {
1806
1807                 if (object == null) {
1808                         return null;
1809                 } else if (object.equals("NULL")) {
1810                         return null;
1811                 } else if (object instanceof Integer) {
1812                         return object.toString();
1813                 } else if (object instanceof String) {
1814                         return (String) object;
1815                 } else {
1816                         return "Type not recognized";
1817                 }
1818         }
1819         
1820         protected static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,
1821                         VfModuleArtifact heatNestedArtifact) {
1822
1823                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null && vfModuleStructure
1824                                 .getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
1825                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
1826                                         .getArtifactInfo().getRelatedArtifacts()) {
1827                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
1828                                         return ASDCConfiguration.HEAT;
1829                                 }
1830
1831                         }
1832                 } 
1833                 
1834                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null 
1835                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()
1836                                                 .getRelatedArtifacts() != null) {
1837                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL)
1838                                         .get(0).getArtifactInfo().getRelatedArtifacts()) {
1839                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
1840                                         return ASDCConfiguration.HEAT_VOL;
1841                                 }
1842                         
1843                         }
1844                 }
1845                 
1846                 // Does not belong to anything
1847                 return null;
1848                         
1849         }
1850         
1851         protected static String createVNFName(VfResourceStructure vfResourceStructure) {
1852
1853                 return vfResourceStructure.getNotification().getServiceName() + "/"
1854                                 + vfResourceStructure.getResourceInstance().getResourceInstanceName();
1855         }
1856
1857         protected static String createVfModuleName(VfModuleStructure vfModuleStructure) {
1858                 
1859                 return createVNFName(vfModuleStructure.getParentVfResource()) + "::"
1860                                 + vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
1861         }
1862         
1863         
1864         protected static Timestamp getCurrentTimeStamp() {
1865                 
1866                 return new Timestamp(new Date().getTime());
1867         }
1868
1869 }