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