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