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