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