9f914c586e13197df3cb8790adf6db2b17076a46
[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  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.asdc.installer.heat;
25
26
27 import java.sql.Timestamp;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.Set;
37 import java.util.stream.Collectors;
38
39 import org.hibernate.exception.ConstraintViolationException;
40 import org.hibernate.exception.LockAcquisitionException;
41 import org.onap.sdc.api.notification.IArtifactInfo;
42 import org.onap.sdc.api.notification.IResourceInstance;
43 import org.onap.sdc.api.notification.IStatusData;
44 import org.onap.sdc.tosca.parser.api.IEntityDetails;
45 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
46 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
47 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
48 import org.onap.sdc.tosca.parser.enums.SdcTypes;
49 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
50 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
51 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
52 import org.onap.sdc.toscaparser.api.Group;
53 import org.onap.sdc.toscaparser.api.NodeTemplate;
54 import org.onap.sdc.toscaparser.api.Policy;
55 import org.onap.sdc.toscaparser.api.Property;
56 import org.onap.sdc.toscaparser.api.RequirementAssignment;
57 import org.onap.sdc.toscaparser.api.RequirementAssignments;
58 import org.onap.sdc.toscaparser.api.elements.Metadata;
59 import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
60 import org.onap.sdc.toscaparser.api.functions.GetInput;
61 import org.onap.sdc.toscaparser.api.parameters.Input;
62 import org.onap.sdc.utils.DistributionStatusEnum;
63 import org.onap.so.asdc.client.ASDCConfiguration;
64 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
65 import org.onap.so.asdc.installer.ASDCElementInfo;
66 import org.onap.so.asdc.installer.BigDecimalVersion;
67 import org.onap.so.asdc.installer.IVfModuleData;
68 import org.onap.so.asdc.installer.PnfResourceStructure;
69 import org.onap.so.asdc.installer.ResourceStructure;
70 import org.onap.so.asdc.installer.ToscaResourceStructure;
71 import org.onap.so.asdc.installer.VfModuleArtifact;
72 import org.onap.so.asdc.installer.VfModuleStructure;
73 import org.onap.so.asdc.installer.VfResourceStructure;
74 import org.onap.so.asdc.util.YamlEditor;
75 import org.onap.so.db.catalog.beans.AllottedResource;
76 import org.onap.so.db.catalog.beans.AllottedResourceCustomization;
77 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
78 import org.onap.so.db.catalog.beans.CollectionResource;
79 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
80 import org.onap.so.db.catalog.beans.ConfigurationResource;
81 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
82 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
83 import org.onap.so.db.catalog.beans.CvnfcCustomization;
84 import org.onap.so.db.catalog.beans.HeatEnvironment;
85 import org.onap.so.db.catalog.beans.HeatFiles;
86 import org.onap.so.db.catalog.beans.HeatTemplate;
87 import org.onap.so.db.catalog.beans.HeatTemplateParam;
88 import org.onap.so.db.catalog.beans.InstanceGroupType;
89 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
90 import org.onap.so.db.catalog.beans.NetworkInstanceGroup;
91 import org.onap.so.db.catalog.beans.NetworkResource;
92 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
93 import org.onap.so.db.catalog.beans.PnfResource;
94 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
95 import org.onap.so.db.catalog.beans.Service;
96 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
97 import org.onap.so.db.catalog.beans.SubType;
98 import org.onap.so.db.catalog.beans.TempNetworkHeatTemplateLookup;
99 import org.onap.so.db.catalog.beans.ToscaCsar;
100 import org.onap.so.db.catalog.beans.VFCInstanceGroup;
101 import org.onap.so.db.catalog.beans.VfModule;
102 import org.onap.so.db.catalog.beans.VfModuleCustomization;
103 import org.onap.so.db.catalog.beans.VnfResource;
104 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
105 import org.onap.so.db.catalog.beans.VnfcCustomization;
106 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
107 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
108 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
109 import org.onap.so.db.catalog.data.repository.CollectionResourceCustomizationRepository;
110 import org.onap.so.db.catalog.data.repository.CollectionResourceRepository;
111 import org.onap.so.db.catalog.data.repository.ConfigurationResourceCustomizationRepository;
112 import org.onap.so.db.catalog.data.repository.ConfigurationResourceRepository;
113 import org.onap.so.db.catalog.data.repository.CvnfcCustomizationRepository;
114 import org.onap.so.db.catalog.data.repository.ExternalServiceToInternalServiceRepository;
115 import org.onap.so.db.catalog.data.repository.HeatTemplateRepository;
116 import org.onap.so.db.catalog.data.repository.InstanceGroupRepository;
117 import org.onap.so.db.catalog.data.repository.NetworkResourceCustomizationRepository;
118 import org.onap.so.db.catalog.data.repository.NetworkResourceRepository;
119 import org.onap.so.db.catalog.data.repository.PnfCustomizationRepository;
120 import org.onap.so.db.catalog.data.repository.PnfResourceRepository;
121 import org.onap.so.db.catalog.data.repository.ServiceProxyResourceCustomizationRepository;
122 import org.onap.so.db.catalog.data.repository.ServiceRepository;
123 import org.onap.so.db.catalog.data.repository.TempNetworkHeatTemplateRepository;
124 import org.onap.so.db.catalog.data.repository.VFModuleCustomizationRepository;
125 import org.onap.so.db.catalog.data.repository.VFModuleRepository;
126 import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
127 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
128 import org.onap.so.db.catalog.data.repository.VnfcCustomizationRepository;
129 import org.onap.so.db.catalog.data.repository.VnfcInstanceGroupCustomizationRepository;
130 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
131 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
132 import org.onap.so.db.request.beans.WatchdogServiceModVerIdLookup;
133 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
134 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
135 import org.onap.so.db.request.data.repository.WatchdogServiceModVerIdLookupRepository;
136 import org.onap.so.logger.ErrorCode;
137 import org.onap.so.logger.MessageEnum;
138 import org.slf4j.Logger;
139 import org.slf4j.LoggerFactory;
140 import org.springframework.beans.factory.annotation.Autowired;
141 import org.springframework.orm.ObjectOptimisticLockingFailureException;
142 import org.springframework.stereotype.Component;
143 import org.springframework.transaction.annotation.Transactional;
144
145 import com.fasterxml.jackson.core.JsonProcessingException;
146 import com.fasterxml.jackson.databind.ObjectMapper;
147
148 @Component
149 public class ToscaResourceInstaller {
150
151         protected static final String NODES_VRF_ENTRY = "org.openecomp.nodes.VRFEntry";
152
153         protected static final String VLAN_NETWORK_RECEPTOR = "org.openecomp.nodes.VLANNetworkReceptor";
154
155         protected static final String ALLOTTED_RESOURCE = "Allotted Resource";
156
157         protected static final String MULTI_STAGE_DESIGN = "multi_stage_design";
158
159         protected static final String SCALABLE = "scalable";
160
161         protected static final String BASIC = "BASIC";
162
163         protected static final String PROVIDER = "PROVIDER";
164
165         protected static final String HEAT = "HEAT";
166
167         protected static final String MANUAL_RECORD = "MANUAL_RECORD";
168
169         protected static final String MSO = "SO";
170
171         protected static final String SDNC_MODEL_NAME = "sdnc_model_name";
172
173         protected static final String SDNC_MODEL_VERSION = "sdnc_model_version";
174
175         private static String CUSTOMIZATION_UUID = "customizationUUID";
176
177
178         @Autowired
179         protected ServiceRepository serviceRepo;
180         
181         @Autowired
182         protected InstanceGroupRepository instanceGroupRepo;
183         
184         @Autowired
185         protected ServiceProxyResourceCustomizationRepository serviceProxyCustomizationRepo;
186         
187         @Autowired
188         protected CollectionResourceRepository collectionRepo;
189         
190         @Autowired
191         protected CollectionResourceCustomizationRepository collectionCustomizationRepo;
192         
193         @Autowired
194         protected ConfigurationResourceCustomizationRepository configCustomizationRepo;
195         
196         @Autowired
197         protected ConfigurationResourceRepository configRepo;
198
199         @Autowired
200         protected VnfResourceRepository vnfRepo;
201         
202         @Autowired
203         protected VFModuleRepository vfModuleRepo;
204
205         @Autowired
206         protected VFModuleCustomizationRepository vfModuleCustomizationRepo;    
207         
208         @Autowired
209         protected VnfcInstanceGroupCustomizationRepository vnfcInstanceGroupCustomizationRepo;  
210         
211         @Autowired
212         protected VnfcCustomizationRepository vnfcCustomizationRepo;
213         
214         @Autowired
215         protected CvnfcCustomizationRepository cvnfcCustomizationRepo;
216
217         @Autowired
218         protected AllottedResourceRepository allottedRepo;
219
220         @Autowired
221         protected AllottedResourceCustomizationRepository allottedCustomizationRepo;
222
223         @Autowired
224         protected NetworkResourceRepository networkRepo;
225          
226         @Autowired
227         protected HeatTemplateRepository heatRepo;
228
229         @Autowired
230         protected NetworkResourceCustomizationRepository networkCustomizationRepo;
231
232         @Autowired
233         protected WatchdogComponentDistributionStatusRepository watchdogCDStatusRepository;
234         @Autowired
235         protected WatchdogDistributionStatusRepository watchdogDistributionStatusRepository;
236         @Autowired
237         protected WatchdogServiceModVerIdLookupRepository watchdogModVerIdLookupRepository;     
238         
239         @Autowired
240         protected TempNetworkHeatTemplateRepository tempNetworkLookupRepo;
241         
242         @Autowired
243         protected ExternalServiceToInternalServiceRepository externalServiceToInternalServiceRepository;
244         
245         @Autowired
246         protected PnfResourceRepository pnfResourceRepository;
247
248         @Autowired
249         protected PnfCustomizationRepository pnfCustomizationRepository;
250
251         protected static final Logger logger = LoggerFactory.getLogger(ToscaResourceInstaller.class);
252
253         public boolean isResourceAlreadyDeployed(VfResourceStructure vfResourceStruct) throws ArtifactInstallerException {
254                 boolean status = false;
255                 VfResourceStructure vfResourceStructure = vfResourceStruct;
256                 try {
257                         status = vfResourceStructure.isDeployedSuccessfully();
258                 } catch (RuntimeException e) {
259                         status = false;
260                 }
261                 try {                                   
262                         Service existingService = serviceRepo.findOneByModelUUID(vfResourceStructure.getNotification().getServiceUUID()); 
263                         if(existingService != null)
264                                 status = true;
265                         if (status) {
266                                 logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
267                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
268                                                 vfResourceStructure.getNotification().getServiceName(),
269                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
270                                                                 vfResourceStructure.getNotification().getServiceVersion()),
271                                                 vfResourceStructure.getNotification().getServiceUUID(),
272                                                 vfResourceStructure.getResourceInstance().getResourceName(), "", "");
273                                 WatchdogComponentDistributionStatus wdStatus = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
274                                 wdStatus.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
275                                 watchdogCDStatusRepository.saveAndFlush(wdStatus);
276                         } else {                        
277                                 logger.info(vfResourceStructure.getResourceInstance().getResourceInstanceName(),
278                                                 vfResourceStructure.getResourceInstance().getResourceCustomizationUUID(),
279                                                 vfResourceStructure.getNotification().getServiceName(),
280                                                 BigDecimalVersion.castAndCheckNotificationVersionToString(
281                                                                 vfResourceStructure.getNotification().getServiceVersion()),
282                                                 vfResourceStructure.getNotification().getServiceUUID(),
283                                                 vfResourceStructure.getResourceInstance().getResourceName(), "", "");
284                         }
285                         return status;
286                 } catch (Exception e) {
287                         logger
288                                 .error("{} {} {}", MessageEnum.ASDC_ARTIFACT_CHECK_EXC.toString(), ErrorCode.SchemaError.getValue(),
289                                         "Exception - isResourceAlreadyDeployed");
290                         throw new ArtifactInstallerException("Exception caught during checking existence of the VNF Resource.", e);
291                 }
292         }
293
294         public void installTheComponentStatus(IStatusData iStatus) throws ArtifactInstallerException {
295                 logger.debug("Entering installTheComponentStatus for distributionId {} and ComponentName {}",
296                         iStatus.getDistributionID(), iStatus.getComponentName());
297
298                 try {
299                         WatchdogComponentDistributionStatus cdStatus = new WatchdogComponentDistributionStatus(iStatus.getDistributionID(),
300                                         iStatus.getComponentName());
301                         cdStatus.setComponentDistributionStatus(iStatus.getStatus().toString());
302                         watchdogCDStatusRepository.save(cdStatus);
303
304                 } catch (Exception e) {
305                         logger.debug("Exception caught in installTheComponentStatus {}", e.getMessage());
306                         throw new ArtifactInstallerException("Exception caught in installTheComponentStatus " + e.getMessage());
307                 }
308         }
309
310         @Transactional(rollbackFor = {ArtifactInstallerException.class})
311         public void installTheResource(ToscaResourceStructure toscaResourceStruct, ResourceStructure resourceStruct)
312                 throws ArtifactInstallerException {
313                 if (resourceStruct instanceof VfResourceStructure) {
314                         installTheVfResource(toscaResourceStruct, (VfResourceStructure) resourceStruct);
315                 } else if (resourceStruct instanceof PnfResourceStructure) {
316                         installPnfResource(toscaResourceStruct, (PnfResourceStructure) resourceStruct);
317                 } else {
318                         logger.warn("Unrecognized resource type");
319                 }
320         }
321
322         private void installPnfResource(ToscaResourceStructure toscaResourceStruct, PnfResourceStructure resourceStruct)
323                 throws ArtifactInstallerException {
324
325                 // PCLO: in case of deployment failure, use a string that will represent
326                 // the type of artifact that failed...
327                 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
328                 try {
329                         createToscaCsar(toscaResourceStruct);
330                         Service service = createService(toscaResourceStruct, resourceStruct);
331
332                         processResourceSequence(toscaResourceStruct, service);
333                         processPnfResources(toscaResourceStruct, service, resourceStruct);
334                         serviceRepo.save(service);
335
336                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(
337                                 resourceStruct.getNotification().getDistributionID(), MSO);
338                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
339                         watchdogCDStatusRepository.save(status);
340
341                         toscaResourceStruct.setSuccessfulDeployment();
342
343                 } catch (Exception e) {
344                         logger.debug("Exception :", e);
345                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(
346                                 resourceStruct.getNotification().getDistributionID(), MSO);
347                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_ERROR.name());
348                         watchdogCDStatusRepository.save(status);
349                         Throwable dbExceptionToCapture = e;
350                         while (!(dbExceptionToCapture instanceof ConstraintViolationException
351                                 || dbExceptionToCapture instanceof LockAcquisitionException)
352                                 && (dbExceptionToCapture.getCause() != null)) {
353                                 dbExceptionToCapture = dbExceptionToCapture.getCause();
354                         }
355
356                         if (dbExceptionToCapture instanceof ConstraintViolationException
357                                 || dbExceptionToCapture instanceof LockAcquisitionException) {
358                                 logger.warn("{} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED.toString(),
359                                         resourceStruct.getResourceInstance().getResourceName(),
360                                         resourceStruct.getNotification().getServiceVersion(), ErrorCode.DataError.getValue(),
361                                         "Exception - ASCDC Artifact already deployed", e);
362                         } else {
363                                 String elementToLog = (!artifactListForLogging.isEmpty()
364                                         ? artifactListForLogging.get(artifactListForLogging.size() - 1).toString()
365                                         : "No element listed");
366                                 logger.error("{} {} {} {}", MessageEnum.ASDC_ARTIFACT_INSTALL_EXC.toString(), elementToLog,
367                                         ErrorCode.DataError.getValue(),
368                                         "Exception caught during installation of " + resourceStruct.getResourceInstance()
369                                                 .getResourceName()
370                                                 + ". Transaction rollback", e);
371                                 throw new ArtifactInstallerException("Exception caught during installation of "
372                                         + resourceStruct.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
373                         }
374                 }
375         }
376         @Transactional(rollbackFor = { ArtifactInstallerException.class })
377         public void installTheVfResource(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStruct)
378                         throws ArtifactInstallerException {             
379                 VfResourceStructure vfResourceStructure = vfResourceStruct;
380                 extractHeatInformation(toscaResourceStruct, vfResourceStructure);       
381
382                 // PCLO: in case of deployment failure, use a string that will represent
383                 // the type of artifact that failed...
384                 List<ASDCElementInfo> artifactListForLogging = new ArrayList<>();
385                 try {
386                         createToscaCsar(toscaResourceStruct);                   
387                         Service service = createService(toscaResourceStruct, vfResourceStruct);
388                         List<NodeTemplate> vfNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceVfList();
389                         
390                 
391                         for (NodeTemplate nodeTemplate : vfNodeTemplatesList) { 
392                                 
393                                 Metadata metadata = nodeTemplate.getMetaData();         
394                                 String serviceType = toscaResourceStruct.getCatalogService().getServiceType();                  
395                                 String vfCustomizationCategory = toscaResourceStruct.getSdcCsarHelper()
396                                                 .getMetadataPropertyValue(metadata, SdcPropertyNames.PROPERTY_NAME_CATEGORY);
397                                 processVfModules(toscaResourceStruct, vfResourceStructure, service, nodeTemplate, metadata,
398                                                 vfCustomizationCategory);
399                         }
400                         
401                         processResourceSequence(toscaResourceStruct, service);
402                         processVFResources(toscaResourceStruct, service, vfResourceStructure);
403                         List<NodeTemplate> allottedResourceList = toscaResourceStruct.getSdcCsarHelper().getAllottedResources();
404                         processAllottedResources(toscaResourceStruct, service, allottedResourceList);
405                         processNetworks(toscaResourceStruct, service);  
406                         // process Network Collections
407                         processNetworkCollections(toscaResourceStruct, service);
408                         // Process Service Proxy & Configuration
409                         processServiceProxyAndConfiguration(toscaResourceStruct, service);
410                         
411                         serviceRepo.save(service);
412                         
413                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
414                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_OK.name());
415                         watchdogCDStatusRepository.save(status);
416
417                         toscaResourceStruct.setSuccessfulDeployment();
418
419                 } catch (Exception e) {
420                         logger.debug("Exception :", e);
421                         WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(vfResourceStruct.getNotification().getDistributionID(), MSO);
422                         status.setComponentDistributionStatus(DistributionStatusEnum.COMPONENT_DONE_ERROR.name());
423                         watchdogCDStatusRepository.save(status);
424                         Throwable dbExceptionToCapture = e;
425                         while (!(dbExceptionToCapture instanceof ConstraintViolationException
426                                         || dbExceptionToCapture instanceof LockAcquisitionException)
427                                         && (dbExceptionToCapture.getCause() != null)) {
428                                 dbExceptionToCapture = dbExceptionToCapture.getCause();
429                         }
430
431                         if (dbExceptionToCapture instanceof ConstraintViolationException
432                                         || dbExceptionToCapture instanceof LockAcquisitionException) {
433                                 logger.warn("{} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_ALREADY_DEPLOYED.toString(),
434                                         vfResourceStructure.getResourceInstance().getResourceName(),
435                                         vfResourceStructure.getNotification().getServiceVersion(), ErrorCode.DataError.getValue(),
436                                         "Exception - ASCDC Artifact already deployed", e);
437                         } else {
438                                 String elementToLog = (!artifactListForLogging.isEmpty()
439                                                 ? artifactListForLogging.get(artifactListForLogging.size() - 1).toString()
440                                                 : "No element listed");
441                                 logger.error("{} {} {} {}", MessageEnum.ASDC_ARTIFACT_INSTALL_EXC.toString(), elementToLog,
442                                         ErrorCode.DataError.getValue(),
443                                         "Exception caught during installation of " + vfResourceStructure.getResourceInstance().getResourceName()
444                                                 + ". Transaction rollback", e);
445                                 throw new ArtifactInstallerException("Exception caught during installation of "
446                                                 + vfResourceStructure.getResourceInstance().getResourceName() + ". Transaction rollback.", e);
447                         }
448                 }
449         }
450
451
452         List<NodeTemplate> getRequirementList(List<NodeTemplate> resultList, List<NodeTemplate> nodeTemplates,
453                                                                                                                  ISdcCsarHelper iSdcCsarHelper) {
454
455                 List<NodeTemplate> nodes = new ArrayList<NodeTemplate>();
456                 nodes.addAll(nodeTemplates);
457
458                 for (NodeTemplate nodeTemplate : nodeTemplates) {
459                         RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate);
460                         List<RequirementAssignment> reqAs = requirement.getAll();
461                         for (RequirementAssignment ra : reqAs) {
462                                 String reqNode = ra.getNodeTemplateName();
463                                 for (NodeTemplate rNode : resultList) {
464                                         if (rNode.getName().equals(reqNode)) {
465                                                 if(!resultList.contains(nodeTemplate)) {
466                                                         resultList.add(nodeTemplate);
467                                                 }
468                                                 if(nodes.contains(nodeTemplate)) {
469                                                         nodes.remove(nodeTemplate);
470                                                 }
471                                                 break;
472                                         }
473                                 }
474                         }
475                 }
476
477                 if (!nodes.isEmpty()) {
478                         getRequirementList(resultList, nodes, iSdcCsarHelper);
479                 }
480
481                 return resultList;
482         }
483
484         // This method retrieve resource sequence from csar file
485         void processResourceSequence(ToscaResourceStructure toscaResourceStructure, Service service) {
486                 List<String> resouceSequence = new ArrayList<String>();
487                 List<NodeTemplate> resultList = new ArrayList<NodeTemplate>();
488
489                 ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper();
490                 List<NodeTemplate> nodeTemplates = iSdcCsarHelper.getServiceNodeTemplates();
491                 List<NodeTemplate> nodes = new ArrayList<NodeTemplate>();
492                 nodes.addAll(nodeTemplates);
493
494                 for (NodeTemplate nodeTemplate : nodeTemplates) {
495                         RequirementAssignments requirement = iSdcCsarHelper.getRequirementsOf(nodeTemplate);
496
497                         if (requirement == null || requirement.getAll() == null || requirement.getAll().isEmpty()) {
498                                 resultList.add(nodeTemplate);
499                                 nodes.remove(nodeTemplate);
500                         }
501                 }
502
503                 resultList = getRequirementList(resultList, nodes, iSdcCsarHelper);
504
505                 for (NodeTemplate node : resultList) {
506                         String templateName = node.getMetaData().getValue("name");
507                         if (!resouceSequence.contains(templateName)) {
508                                 resouceSequence.add(templateName);
509                         }
510                 }
511
512                 String resourceSeqStr = resouceSequence.stream().collect(Collectors.joining(","));
513                 service.setResourceOrder(resourceSeqStr);
514                 logger.debug(" resourceSeq for service uuid(" + service.getModelUUID() + ") : " + resourceSeqStr);
515         }
516
517         private static String getValue(Object value, List<Input> servInputs) {
518                 String output = null;
519                 if(value instanceof Map) {
520                         // currently this logic handles only one level of nesting.
521                         return ((LinkedHashMap) value).values().toArray()[0].toString();
522                 } else if(value instanceof GetInput) {
523                         String inputName = ((GetInput)value).getInputName();
524
525                         for(Input input : servInputs) {
526                                 if(input.getName().equals(inputName)) {
527                                         // keep both input name and default value
528                                         // if service input does not supplies value the use default value
529                                         String defaultValue = input.getDefault() != null ? (String) input.getDefault().toString() : "";
530                                         output =  inputName + "|" + defaultValue;// return default value
531                                 }
532                         }
533
534                 } else {
535                         output = value != null ? value.toString() : "";
536                 }
537                 return output; // return property value
538         }
539
540         String getResourceInput(ToscaResourceStructure toscaResourceStructure, String resourceCustomizationUuid) throws ArtifactInstallerException {
541                 Map<String, String> resouceRequest = new HashMap<>();
542                 ISdcCsarHelper iSdcCsarHelper = toscaResourceStructure.getSdcCsarHelper();
543
544                 List<Input> serInput = iSdcCsarHelper.getServiceInputs();
545                 Optional<NodeTemplate> nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream()
546                                 .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid)).findFirst();
547                 if(nodeTemplateOpt.isPresent()) {
548                         NodeTemplate nodeTemplate = nodeTemplateOpt.get();
549                         LinkedHashMap<String, Property> resourceProperties = nodeTemplate.getProperties();
550
551                         for(String key : resourceProperties.keySet()) {
552                                 Property property = resourceProperties.get(key);
553
554                                 String value = getValue(property.getValue(), serInput);
555                                 resouceRequest.put(key, value);
556                         }
557                 }
558
559                 try {
560                         ObjectMapper objectMapper = new ObjectMapper();
561                         String jsonStr = objectMapper.writeValueAsString(resouceRequest);
562
563                         jsonStr = jsonStr.replace("\"", "\\\"");
564                         logger.debug("resource request for resource customization id (" + resourceCustomizationUuid + ") : " + jsonStr);
565                         return jsonStr;
566                 } catch (JsonProcessingException e) {
567                         logger.error("resource input could not be deserialized for resource customization id ("
568                                         + resourceCustomizationUuid + ")");
569                         throw new ArtifactInstallerException("resource input could not be parsed", e);
570                 }
571         }
572
573         protected void processNetworks (ToscaResourceStructure toscaResourceStruct,
574                                                                         Service service) throws ArtifactInstallerException {
575                 List <NodeTemplate> nodeTemplatesVLList = toscaResourceStruct.getSdcCsarHelper ().getServiceVlList ();
576
577                 if (nodeTemplatesVLList != null) {
578                         for (NodeTemplate vlNode : nodeTemplatesVLList) {
579                                 String networkResourceModelName = vlNode.getMetaData ().getValue (SdcPropertyNames.PROPERTY_NAME_NAME);
580
581                                 TempNetworkHeatTemplateLookup tempNetworkLookUp =
582                                                                                                                                 tempNetworkLookupRepo.findFirstBynetworkResourceModelName (networkResourceModelName);
583
584                                 if (tempNetworkLookUp != null) {
585                                         HeatTemplate heatTemplate =
586                                                                                           heatRepo.findByArtifactUuid (tempNetworkLookUp.getHeatTemplateArtifactUuid ());
587                                         if (heatTemplate != null) {
588                                                 NetworkResourceCustomization networkCustomization =
589                                                                                                                                                   createNetwork (vlNode,
590                                                                                                                                                                                  toscaResourceStruct,
591                                                                                                                                                                                  heatTemplate,
592                                                                                                                                                                                  tempNetworkLookUp.getAicVersionMax (),
593                                                                                                                                                                                  tempNetworkLookUp.getAicVersionMin (),
594                                                                                                                                                                                  service);
595                                                 service.getNetworkCustomizations ().add (networkCustomization);
596                                         } else {
597                                                 throw new ArtifactInstallerException ("No HeatTemplate found for artifactUUID: "
598                                                                                                                           + tempNetworkLookUp.getHeatTemplateArtifactUuid ());
599                                         }
600                                 } else {
601                                         NetworkResourceCustomization networkCustomization = createNetwork (vlNode,
602                                                                                                                                                                            toscaResourceStruct,
603                                                                                                                                                                            null,
604                                                                                                                                                                            null,
605                                                                                                                                                                            null,
606                                                                                                                                                                            service);
607                                         service.getNetworkCustomizations().add (networkCustomization);
608                                         logger.debug ("No NetworkResourceName found in TempNetworkHeatTemplateLookup for "
609                                                                         + networkResourceModelName);
610                                 }                                       
611                                 
612                         }
613                 }
614         }
615
616         protected void processAllottedResources(ToscaResourceStructure toscaResourceStruct, Service service,
617                         List<NodeTemplate> allottedResourceList) {
618                 if (allottedResourceList != null) {
619                         for (NodeTemplate allottedNode : allottedResourceList) {                                                                        
620                                 service.getAllottedCustomizations()
621                                                 .add(createAllottedResource(allottedNode, toscaResourceStruct, service));                               
622                         }
623                 }
624         }
625         
626         
627         protected ConfigurationResource getConfigurationResource(NodeTemplate nodeTemplate) {
628                 Metadata metadata = nodeTemplate.getMetaData();
629                 ConfigurationResource configResource = new ConfigurationResource();
630                 configResource.setModelName(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
631                 configResource.setModelInvariantUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
632                 configResource.setModelUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
633                 configResource.setModelVersion(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
634                 configResource.setDescription(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
635                 configResource.setToscaNodeType(nodeTemplate.getType());
636                 return configResource;
637         }
638         
639         protected ConfigurationResourceCustomization getConfigurationResourceCustomization(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure, 
640                         ServiceProxyResourceCustomization spResourceCustomization ) {
641                 Metadata metadata = nodeTemplate.getMetaData();
642                 
643                 ConfigurationResource configResource = getConfigurationResource(nodeTemplate);
644                 
645                 ConfigurationResourceCustomization configCustomizationResource = new ConfigurationResourceCustomization();
646                 
647                 Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
648                 
649                 configCustomizationResource.setModelCustomizationUUID(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
650                 configCustomizationResource.setModelInstanceName(nodeTemplate.getName());
651                 
652                 configCustomizationResource.setNfFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION));
653                 configCustomizationResource.setNfRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE));
654                 configCustomizationResource.setNfType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE));
655                 configCustomizationResource.setServiceProxyResourceCustomizationUUID(spResourceCustomization.getModelCustomizationUUID());
656         
657                 configCustomizationResource.setConfigurationResource(configResource);
658                 configResourceCustomizationSet.add(configCustomizationResource);
659
660                 configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);   
661                 return configCustomizationResource;
662         }
663         
664         
665         protected Optional<ConfigurationResourceCustomization> getVnrNodeTemplate(List<NodeTemplate> configurationNodeTemplatesList,  
666                         ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization) {
667                 Optional<ConfigurationResourceCustomization> configurationResourceCust = Optional.empty();
668                 for (NodeTemplate nodeTemplate : configurationNodeTemplatesList) {
669                         StatefulEntityType entityType = nodeTemplate.getTypeDefinition();
670                         String type = entityType.getType();
671                         
672                         if(VLAN_NETWORK_RECEPTOR.equals(type)) {
673                                 configurationResourceCust= Optional.of(getConfigurationResourceCustomization(nodeTemplate, 
674                                                 toscaResourceStructure,spResourceCustomization));
675                                 break;
676                         }
677                 }
678                 
679                 return configurationResourceCust;
680         }
681                 
682         protected void processServiceProxyAndConfiguration(ToscaResourceStructure toscaResourceStruct, Service service) {
683                 
684                 List<NodeTemplate> serviceProxyResourceList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.SERVICE_PROXY);
685                 
686                 List<NodeTemplate> configurationNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CONFIGURATION);
687                 
688                 List<ServiceProxyResourceCustomization> serviceProxyList = new ArrayList<ServiceProxyResourceCustomization>();          
689                 List<ConfigurationResourceCustomization> configurationResourceList = new ArrayList<ConfigurationResourceCustomization>();
690                 
691                 ServiceProxyResourceCustomization serviceProxy = null;
692                 
693                 if (serviceProxyResourceList != null) {
694                         for (NodeTemplate spNode : serviceProxyResourceList) {
695                                 serviceProxy = createServiceProxy(spNode, service, toscaResourceStruct);                                                                
696                                 serviceProxyList.add(serviceProxy);
697                                 Optional<ConfigurationResourceCustomization> vnrResourceCustomization = getVnrNodeTemplate(configurationNodeTemplatesList,toscaResourceStruct,serviceProxy);
698                                 
699                                 for (NodeTemplate configNode : configurationNodeTemplatesList) {
700                                                                                 
701                                                 List<RequirementAssignment> requirementsList = toscaResourceStruct.getSdcCsarHelper().getRequirementsOf(configNode).getAll();
702                                                 for (RequirementAssignment requirement :  requirementsList) {
703                                                         if (requirement.getNodeTemplateName().equals(spNode.getName())) {
704                                                                 ConfigurationResourceCustomization configurationResource = createConfiguration(configNode, toscaResourceStruct, serviceProxy, vnrResourceCustomization);
705                                                                 
706                                                                 Optional<ConfigurationResourceCustomization> matchingObject = configurationResourceList.stream()
707                                                                                 .filter(configurationResourceCustomization -> configNode.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID).equals(configurationResource.getModelCustomizationUUID()))
708                                                                                 .findFirst();
709                                                                 if(!matchingObject.isPresent()){                                                                                                                                        
710                                                                         configurationResourceList.add(configurationResource);
711                                                                 }
712                                                                 
713                                                                 break;
714                                                         }
715                                                 }
716                                 }
717         
718                         }
719                 }
720                 
721                 service.setConfigurationCustomizations(configurationResourceList);
722                 service.setServiceProxyCustomizations(serviceProxyList);
723         }
724         
725         protected void processNetworkCollections(ToscaResourceStructure toscaResourceStruct, Service service) {
726                 
727                 List<NodeTemplate> networkCollectionList = toscaResourceStruct.getSdcCsarHelper().getServiceNodeTemplateBySdcType(SdcTypes.CR);
728                 
729                 if (networkCollectionList != null) {
730                         for (NodeTemplate crNode : networkCollectionList) {     
731                                 
732                                 createNetworkCollection(crNode, toscaResourceStruct, service);
733                                 collectionRepo.saveAndFlush(toscaResourceStruct.getCatalogCollectionResource());
734                                 
735                                 List<NetworkInstanceGroup> networkInstanceGroupList = toscaResourceStruct.getCatalogNetworkInstanceGroup();
736                                 for(NetworkInstanceGroup networkInstanceGroup : networkInstanceGroupList){
737                                         instanceGroupRepo.saveAndFlush(networkInstanceGroup);
738                                 }
739         
740                         }
741                 }
742                 service.getCollectionResourceCustomizations().add(toscaResourceStruct.getCatalogCollectionResourceCustomization());
743         }
744
745
746         protected void processVFResources (ToscaResourceStructure toscaResourceStruct, Service service, VfResourceStructure vfResourceStructure)
747                         throws Exception{
748                 logger.debug("processVFResources");
749                 
750                 List<NodeTemplate> vfNodeTemplatesList = toscaResourceStruct.getSdcCsarHelper().getServiceVfList();
751 //              String servicecategory = toscaResourceStruct.getCatalogService().getCategory();
752 //              String serviceType = toscaResourceStruct.getCatalogService().getServiceType();
753                 
754                 for (NodeTemplate nodeTemplate : vfNodeTemplatesList) {
755                         Metadata metadata = nodeTemplate.getMetaData();
756                         String vfCustomizationCategory = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY);
757                         logger.debug("VF Category is : " + vfCustomizationCategory);
758                         
759                         // Do not treat Allotted Resources as VNF resources
760                         if(ALLOTTED_RESOURCE.equalsIgnoreCase(vfCustomizationCategory)){
761                                 continue;
762                         }
763
764                         String vfCustomizationUUID = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
765                         logger.debug("VFCustomizationUUID=" + vfCustomizationUUID);
766
767                         IResourceInstance vfNotificationResource = vfResourceStructure.getResourceInstance();
768
769                         // Make sure the VF ResourceCustomizationUUID from the notification and tosca
770                         // customizations match before comparing their VF Modules UUID's
771                         logger.debug("Checking if Notification VF ResourceCustomizationUUID: "
772                                         + vfNotificationResource.getResourceCustomizationUUID() + " matches Tosca VF Customization UUID: "
773                                         + vfCustomizationUUID);
774
775                         if (vfCustomizationUUID.equals(vfNotificationResource.getResourceCustomizationUUID())) {
776                                 logger.debug("vfCustomizationUUID: " + vfCustomizationUUID
777                                                 + " matches vfNotificationResource CustomizationUUID");                         
778                                 
779                                 processVfModules(toscaResourceStruct, vfResourceStructure, service, nodeTemplate, metadata,
780                                                 vfCustomizationCategory);
781                         } else {
782                                 logger.debug("Notification VF ResourceCustomizationUUID: "
783                                                 + vfNotificationResource.getResourceCustomizationUUID() + " doesn't match "
784                                                 + "Tosca VF Customization UUID: " + vfCustomizationUUID);
785                         }
786                 }
787         }
788         
789         /**
790          * This is used to process the PNF specific resource, including resource and resource_customization. {@link
791          * IEntityDetails} based API is used to retrieve information. Please check {@link ISdcCsarHelper} for details.
792          */
793         protected void processPnfResources(ToscaResourceStructure toscaResourceStruct, Service service,
794                 PnfResourceStructure resourceStructure) throws Exception {
795                 logger.info("Processing PNF resource: {}", resourceStructure.getResourceInstance().getResourceUUID());
796
797                 ISdcCsarHelper sdcCsarHelper = toscaResourceStruct.getSdcCsarHelper();
798                 EntityQuery entityQuery = EntityQuery.newBuilder(SdcTypes.PNF).build();
799                 TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build();
800
801                 List<IEntityDetails> entityDetailsList = sdcCsarHelper.getEntity(entityQuery, topologyTemplateQuery, false);
802                 for (IEntityDetails entityDetails : entityDetailsList) {
803                         Metadata metadata = entityDetails.getMetadata();
804                         String customizationUUID = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
805                         String modelUuid = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID);
806                         String notifCustomizationUUID = resourceStructure.getResourceInstance().getResourceCustomizationUUID();
807                         if (customizationUUID != null && customizationUUID.equals(notifCustomizationUUID)) {
808                                 logger.info("Resource customization UUID: {} is the same as notified resource customizationUUID: {}",
809                                         customizationUUID, notifCustomizationUUID);
810
811                                 if (checkExistingPnfResourceCutomization(customizationUUID)) {
812                                         logger.info("Resource customization UUID: {} already deployed", customizationUUID);
813                                 } else {
814                                         PnfResource pnfResource = findExistingPnfResource(service, modelUuid);
815                                         if (pnfResource == null) {
816                                                 pnfResource = createPnfResource(entityDetails);
817                                         }
818                                         PnfResourceCustomization pnfResourceCustomization = createPnfResourceCustomization(entityDetails,
819                                                 pnfResource);
820                                         pnfResource.getPnfResourceCustomizations().add(pnfResourceCustomization);
821                                         toscaResourceStruct.setPnfResourceCustomization(pnfResourceCustomization);
822                                         service.getPnfCustomizations().add(pnfResourceCustomization);
823                                 }
824                         } else {
825                                 logger
826                                         .warn("Resource customization UUID: {} is NOT the same as notified resource customizationUUID: {}",
827                                                 customizationUUID, notifCustomizationUUID);
828                         }
829                 }
830         }
831
832         private PnfResource findExistingPnfResource(Service service, String modelUuid) {
833                 PnfResource pnfResource = null;
834                 for (PnfResourceCustomization pnfResourceCustomization : service.getPnfCustomizations()) {
835                         if (pnfResourceCustomization.getPnfResources() != null && pnfResourceCustomization.getPnfResources()
836                                 .getModelUUID().equals(modelUuid)) {
837                                 pnfResource = pnfResourceCustomization.getPnfResources();
838                         }
839                 }
840                 if (pnfResource == null) {
841                         pnfResource = pnfResourceRepository.findById(modelUuid).orElse(pnfResource);
842                 }
843                 return pnfResource;
844         }
845
846         private boolean checkExistingPnfResourceCutomization(String customizationUUID) {
847                 return pnfCustomizationRepository.findById(customizationUUID).isPresent();
848         }
849
850         /**
851          * Construct the {@link PnfResource} from {@link IEntityDetails} object.
852          */
853         private PnfResource createPnfResource(IEntityDetails entity) {
854                 PnfResource pnfResource = new PnfResource();
855                 Metadata metadata = entity.getMetadata();
856                 pnfResource.setModelInvariantUUID(
857                         testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
858                 pnfResource.setModelName(testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
859                 pnfResource.setModelUUID(testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
860                 pnfResource.setModelVersion(
861                         testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
862                 pnfResource.setDescription(
863                         testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
864                 pnfResource.setCategory(testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY)));
865                 pnfResource.setSubCategory(testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY)));
866                 pnfResource.setToscaNodeType(entity.getToscaType());
867                 return pnfResource;
868         }
869
870         /**
871          * Construct the {@link PnfResourceCustomization} from {@link IEntityDetails} object.
872          */
873         private PnfResourceCustomization createPnfResourceCustomization(IEntityDetails entityDetails,
874                 PnfResource pnfResource) {
875
876                 PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
877                 Metadata metadata = entityDetails.getMetadata();
878                 Map<String, Property> properties = entityDetails.getProperties();
879                 pnfResourceCustomization
880                         .setModelCustomizationUUID(testNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
881                 pnfResourceCustomization.setModelInstanceName(entityDetails.getName());
882                 pnfResourceCustomization
883                         .setNfFunction(getStringValue(properties.get(SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
884                 pnfResourceCustomization.setNfNamingCode(getStringValue(properties.get(SdcPropertyNames.PROPERTY_NAME_NFCODE)));
885                 pnfResourceCustomization.setNfRole(getStringValue(properties.get(SdcPropertyNames.PROPERTY_NAME_NFROLE)));
886                 pnfResourceCustomization.setNfType(getStringValue(properties.get(SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
887                 pnfResourceCustomization.setMultiStageDesign(getStringValue(properties.get(MULTI_STAGE_DESIGN)));
888                 pnfResourceCustomization.setBlueprintName(getStringValue(properties.get(SDNC_MODEL_NAME)));
889                 pnfResourceCustomization.setBlueprintVersion(getStringValue(properties.get(SDNC_MODEL_VERSION)));
890
891                 pnfResourceCustomization.setPnfResources(pnfResource);
892
893                 return pnfResourceCustomization;
894         }
895
896         /**
897          * Get value from {@link Property} and cast to String value. Return empty String if property is null value.
898          */
899         private String getStringValue(Property property) {
900                 if (null == property) {
901                         return "";
902                 }
903                 Object value = property.getValue();
904                 return String.valueOf(value);
905         }
906         
907         protected void processVfModules(ToscaResourceStructure toscaResourceStruct, VfResourceStructure vfResourceStructure,
908                         Service service, NodeTemplate nodeTemplate, Metadata metadata, String vfCustomizationCategory) throws Exception {
909                 
910                 logger.debug("VF Category is : " + vfCustomizationCategory);
911                 
912                 if(vfResourceStructure.getVfModuleStructure() != null && !vfResourceStructure.getVfModuleStructure().isEmpty())
913                 {
914
915                         String vfCustomizationUUID = toscaResourceStruct.getSdcCsarHelper()
916                                         .getMetadataPropertyValue(metadata, SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
917                         logger.debug("VFCustomizationUUID=" + vfCustomizationUUID);     
918                         
919                         IResourceInstance vfNotificationResource = vfResourceStructure.getResourceInstance();           
920                         
921                         // Make sure the VF ResourceCustomizationUUID from the notification and tosca customizations match before comparing their VF Modules UUID's
922                         logger.debug("Checking if Notification VF ResourceCustomizationUUID: " + vfNotificationResource.getResourceCustomizationUUID() + 
923                                                            " matches Tosca VF Customization UUID: " +  vfCustomizationUUID);
924                         
925                         if(vfCustomizationUUID.equals(vfNotificationResource.getResourceCustomizationUUID())){
926                                 
927                                 logger.debug("vfCustomizationUUID: " + vfCustomizationUUID + " matches vfNotificationResource CustomizationUUID");
928                         
929                                 VnfResourceCustomization vnfResource = createVnfResource(nodeTemplate, toscaResourceStruct, service);
930                                 
931                                 Set<CvnfcCustomization> existingCvnfcSet = new HashSet<CvnfcCustomization>(); 
932                                 Set<VnfcCustomization> existingVnfcSet = new HashSet<VnfcCustomization>();
933                                                                 
934                                 for (VfModuleStructure vfModuleStructure : vfResourceStructure.getVfModuleStructure()) {
935                                         
936                                         logger.debug("vfModuleStructure:" + vfModuleStructure.toString());
937                                         List<org.onap.sdc.toscaparser.api.Group> vfGroups = toscaResourceStruct
938                                                         .getSdcCsarHelper().getVfModulesByVf(vfCustomizationUUID);
939                                         IVfModuleData vfMetadata = vfModuleStructure.getVfModuleMetadata();                                             
940                                         
941                                         logger.debug("Comparing Vf_Modules_Metadata CustomizationUUID : " + vfMetadata.getVfModuleModelCustomizationUUID());
942                                         
943                                         Optional<org.onap.sdc.toscaparser.api.Group> matchingObject = vfGroups.stream()
944                                                         .peek(group -> logger.debug("To Csar Group VFModuleModelCustomizationUUID " + group.getMetadata().getValue("vfModuleModelCustomizationUUID")))
945                                                         .filter(group -> group.getMetadata().getValue("vfModuleModelCustomizationUUID").equals(vfMetadata.getVfModuleModelCustomizationUUID()))
946                                                         .findFirst();
947                                         if(matchingObject.isPresent()){
948                                                 VfModuleCustomization vfModuleCustomization = createVFModuleResource(matchingObject.get(), nodeTemplate, toscaResourceStruct, 
949                                                                                                                                                                                          vfResourceStructure,vfMetadata, vnfResource, service, existingCvnfcSet, existingVnfcSet);
950                                                 vfModuleCustomization.getVfModule().setVnfResources(vnfResource.getVnfResources());
951                                         }else
952                                                 throw new Exception("Cannot find matching VFModule Customization in Csar for Vf_Modules_Metadata: " + vfMetadata.getVfModuleModelCustomizationUUID());
953                                         
954                                 }
955                                 
956                                 
957                                 // Check for VNFC Instance Group info and add it if there is
958                                 List<Group> groupList = toscaResourceStruct.getSdcCsarHelper()
959                                                 .getGroupsOfOriginOfNodeTemplateByToscaGroupType(nodeTemplate,
960                                                                 "org.openecomp.groups.VfcInstanceGroup");
961                                         
962                                 for (Group group : groupList) { 
963                                                 VnfcInstanceGroupCustomization vnfcInstanceGroupCustomization = createVNFCInstanceGroup(nodeTemplate, group, vnfResource, toscaResourceStruct);
964                                                 vnfcInstanceGroupCustomizationRepo.saveAndFlush(vnfcInstanceGroupCustomization);                                
965                                 }                       
966                                 
967                                 
968                                 service.getVnfCustomizations().add(vnfResource);
969                         } else{
970                                 logger.debug("Notification VF ResourceCustomizationUUID: " + vfNotificationResource.getResourceCustomizationUUID() + " doesn't match " +
971                                                          "Tosca VF Customization UUID: " +  vfCustomizationUUID);
972                         }
973                 }
974         }
975
976         public void processWatchdog(String distributionId, String servideUUID, Optional<String> distributionNotification,
977                         String consumerId) {
978                 WatchdogServiceModVerIdLookup modVerIdLookup = new WatchdogServiceModVerIdLookup(distributionId, servideUUID,
979                                 distributionNotification, consumerId);
980                 watchdogModVerIdLookupRepository.saveAndFlush(modVerIdLookup);
981                 
982                 try{
983                 
984                         WatchdogDistributionStatus distributionStatus = new WatchdogDistributionStatus(distributionId);
985                         watchdogDistributionStatusRepository.saveAndFlush(distributionStatus);
986                         
987                 } catch(ObjectOptimisticLockingFailureException e){
988                         logger.debug("ObjectOptimisticLockingFailureException in processWatchdog : " + e.toString());
989                         throw e;
990                 }
991         }
992         
993         protected void extractHeatInformation(ToscaResourceStructure toscaResourceStruct,
994                         VfResourceStructure vfResourceStructure) {
995                 for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
996
997                         switch (vfModuleArtifact.getArtifactInfo().getArtifactType()) {
998                         case ASDCConfiguration.HEAT:
999                         case ASDCConfiguration.HEAT_NESTED:
1000                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
1001                                                 vfModuleArtifact);
1002                                 break;
1003                         case ASDCConfiguration.HEAT_VOL:
1004                                 createHeatTemplateFromArtifact(vfResourceStructure, toscaResourceStruct,
1005                                                 vfModuleArtifact);
1006                                 VfModuleArtifact envModuleArtifact = getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
1007                                 createHeatEnvFromArtifact(vfResourceStructure, envModuleArtifact);
1008                                 break;
1009                         case ASDCConfiguration.HEAT_ENV:
1010                                 createHeatEnvFromArtifact(vfResourceStructure, vfModuleArtifact);
1011                                 break;
1012                         case ASDCConfiguration.HEAT_ARTIFACT:
1013                                 createHeatFileFromArtifact(vfResourceStructure, vfModuleArtifact,
1014                                                 toscaResourceStruct);
1015                                 break;
1016                         case ASDCConfiguration.HEAT_NET:
1017                         case ASDCConfiguration.OTHER:
1018                                 logger.warn("{} {} {} {}", MessageEnum.ASDC_ARTIFACT_TYPE_NOT_SUPPORT.toString(),
1019                                         vfModuleArtifact.getArtifactInfo().getArtifactType() + "(Artifact Name:" + vfModuleArtifact.getArtifactInfo()
1020                                                 .getArtifactName() + ")", ErrorCode.DataError.getValue(), "Artifact type not supported");
1021                                 break;
1022                         default:
1023                                 break;
1024
1025                         }
1026                 }
1027         }
1028
1029         protected VfModuleArtifact getHeatEnvArtifactFromGeneratedArtifact(VfResourceStructure vfResourceStructure,
1030                         VfModuleArtifact vfModuleArtifact) {
1031                 String artifactName = vfModuleArtifact.getArtifactInfo().getArtifactName();
1032                 artifactName = artifactName.substring(0, artifactName.indexOf('.'));
1033                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
1034                         if (moduleArtifact.getArtifactInfo().getArtifactName().contains(artifactName)
1035                                         && moduleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
1036                                 return moduleArtifact;
1037                         }
1038                 }
1039                 return null;
1040         }
1041
1042         public String verifyTheFilePrefixInArtifacts(String filebody, VfResourceStructure vfResourceStructure,
1043                         List<String> listTypes) {
1044                 String newFileBody = filebody;
1045                 for (VfModuleArtifact moduleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
1046
1047                         if (listTypes.contains(moduleArtifact.getArtifactInfo().getArtifactType())) {
1048
1049                                 newFileBody = verifyTheFilePrefixInString(newFileBody,
1050                                                 moduleArtifact.getArtifactInfo().getArtifactName());
1051                         }
1052                 }
1053                 return newFileBody;
1054         }
1055
1056         public String verifyTheFilePrefixInString(final String body, final String filenameToVerify) {
1057
1058                 String needlePrefix = "file:///";
1059                 String prefixedFilenameToVerify = needlePrefix + filenameToVerify;
1060
1061                 if ((body == null) || (body.length() == 0) || (filenameToVerify == null) || (filenameToVerify.length() == 0)) {
1062                         return body;
1063                 }
1064
1065                 StringBuilder sb = new StringBuilder(body.length());
1066
1067                 int currentIndex = 0;
1068                 int startIndex = 0;
1069
1070                 while (currentIndex != -1) {
1071                         startIndex = currentIndex;
1072                         currentIndex = body.indexOf(prefixedFilenameToVerify, startIndex);
1073
1074                         if (currentIndex == -1) {
1075                                 break;
1076                         }
1077                         // We append from the startIndex up to currentIndex (start of File
1078                         // Name)
1079                         sb.append(body.substring(startIndex, currentIndex));
1080                         sb.append(filenameToVerify);
1081
1082                         currentIndex += prefixedFilenameToVerify.length();
1083                 }
1084
1085                 sb.append(body.substring(startIndex));
1086
1087                 return sb.toString();
1088         }
1089
1090         protected void createHeatTemplateFromArtifact(VfResourceStructure vfResourceStructure,
1091                         ToscaResourceStructure toscaResourceStruct, VfModuleArtifact vfModuleArtifact) {
1092                 HeatTemplate heatTemplate = new HeatTemplate();
1093                 List<String> typeList = new ArrayList<>();
1094                 typeList.add(ASDCConfiguration.HEAT_NESTED);
1095                 typeList.add(ASDCConfiguration.HEAT_ARTIFACT);
1096
1097                 heatTemplate.setTemplateBody(
1098                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
1099                 heatTemplate.setTemplateName(vfModuleArtifact.getArtifactInfo().getArtifactName());
1100
1101                 if (vfModuleArtifact.getArtifactInfo().getArtifactTimeout() != null) {
1102                         heatTemplate.setTimeoutMinutes(vfModuleArtifact.getArtifactInfo().getArtifactTimeout());
1103                 } else {
1104                         heatTemplate.setTimeoutMinutes(240);
1105                 }
1106
1107                 heatTemplate.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
1108                 heatTemplate.setVersion(BigDecimalVersion
1109                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
1110                 heatTemplate.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
1111
1112                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
1113                         heatTemplate.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
1114                 } else {
1115                         heatTemplate.setArtifactChecksum(MANUAL_RECORD);
1116                 }
1117
1118                 Set<HeatTemplateParam> heatParam = extractHeatTemplateParameters(
1119                                 vfModuleArtifact.getResult(), vfModuleArtifact.getArtifactInfo().getArtifactUUID());
1120                 heatTemplate.setParameters(heatParam);  
1121                 vfModuleArtifact.setHeatTemplate(heatTemplate);
1122         }
1123
1124         protected void createHeatEnvFromArtifact(VfResourceStructure vfResourceStructure,
1125                         VfModuleArtifact vfModuleArtifact) {
1126                 HeatEnvironment heatEnvironment = new HeatEnvironment();
1127                 heatEnvironment.setName(vfModuleArtifact.getArtifactInfo().getArtifactName());
1128                 List<String> typeList = new ArrayList<>();
1129                 typeList.add(ASDCConfiguration.HEAT);
1130                 typeList.add(ASDCConfiguration.HEAT_VOL);
1131                 heatEnvironment.setEnvironment(
1132                                 verifyTheFilePrefixInArtifacts(vfModuleArtifact.getResult(), vfResourceStructure, typeList));
1133                 heatEnvironment.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
1134                 heatEnvironment.setVersion(BigDecimalVersion
1135                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));     
1136                 heatEnvironment.setArtifactUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
1137
1138                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
1139                         heatEnvironment.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
1140                 } else {
1141                         heatEnvironment.setArtifactChecksum(MANUAL_RECORD);
1142                 }               
1143                 vfModuleArtifact.setHeatEnvironment(heatEnvironment);
1144         }
1145
1146         protected void createHeatFileFromArtifact(VfResourceStructure vfResourceStructure,
1147                 VfModuleArtifact vfModuleArtifact, ToscaResourceStructure toscaResourceStruct) {
1148                 
1149                 HeatFiles heatFile = new HeatFiles();   
1150                 heatFile.setAsdcUuid(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
1151                 heatFile.setDescription(vfModuleArtifact.getArtifactInfo().getArtifactDescription());
1152                 heatFile.setFileBody(vfModuleArtifact.getResult());
1153                 heatFile.setFileName(vfModuleArtifact.getArtifactInfo().getArtifactName());
1154                 heatFile.setVersion(BigDecimalVersion
1155                                 .castAndCheckNotificationVersionToString(vfModuleArtifact.getArtifactInfo().getArtifactVersion()));
1156                 toscaResourceStruct.setHeatFilesUUID(vfModuleArtifact.getArtifactInfo().getArtifactUUID());
1157                 if (vfModuleArtifact.getArtifactInfo().getArtifactChecksum() != null) {
1158                         heatFile.setArtifactChecksum(vfModuleArtifact.getArtifactInfo().getArtifactChecksum());
1159                 } else {
1160                         heatFile.setArtifactChecksum(MANUAL_RECORD);
1161                 }
1162                 vfModuleArtifact.setHeatFiles(heatFile);
1163         }
1164
1165         protected Service createService(ToscaResourceStructure toscaResourceStructure,
1166                         ResourceStructure resourceStructure) {
1167
1168                 Metadata serviceMetadata = toscaResourceStructure.getServiceMetadata();
1169
1170                 Service service = new Service();
1171
1172                 if (serviceMetadata != null) {
1173
1174                         if (toscaResourceStructure.getServiceVersion() != null) {
1175                                 service.setModelVersion(toscaResourceStructure.getServiceVersion());
1176                         }
1177
1178                         service.setServiceType(serviceMetadata.getValue("serviceType"));
1179                         service.setServiceRole(serviceMetadata.getValue("serviceRole"));
1180                         service.setCategory(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY));
1181
1182                         service.setDescription(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1183                         service.setModelName(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1184                         service.setModelUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1185                         service.setEnvironmentContext(serviceMetadata.getValue("environmentContext"));
1186
1187                         if (resourceStructure != null) 
1188                                 service.setWorkloadContext(resourceStructure.getNotification().getWorkloadContext());
1189                                                 
1190                         service.setModelInvariantUUID(serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1191                         service.setCsar(toscaResourceStructure.getCatalogToscaCsar());                  
1192                 }
1193                 
1194                 
1195                 toscaResourceStructure.setCatalogService(service); 
1196                 return service;
1197         }
1198         
1199         protected ServiceProxyResourceCustomization createServiceProxy(NodeTemplate nodeTemplate, Service service, ToscaResourceStructure toscaResourceStructure) {
1200
1201                 Metadata spMetadata = nodeTemplate.getMetaData();
1202                 
1203                 ServiceProxyResourceCustomization spCustomizationResource = new ServiceProxyResourceCustomization();
1204                 
1205                 Set<ServiceProxyResourceCustomization> serviceProxyCustomizationSet = new HashSet<>();
1206                 
1207                 spCustomizationResource.setModelName(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1208                 spCustomizationResource.setModelInvariantUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1209                 spCustomizationResource.setModelUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1210                 spCustomizationResource.setModelVersion(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1211                 spCustomizationResource.setDescription(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));        
1212                                 
1213                 spCustomizationResource.setModelCustomizationUUID(spMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1214                 spCustomizationResource.setModelInstanceName(nodeTemplate.getName());
1215                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
1216                 
1217                 String sourceServiceUUID = spMetadata.getValue("sourceModelUuid");
1218                 
1219                 Service sourceService = serviceRepo.findOneByModelUUID(sourceServiceUUID);      
1220                 
1221                 spCustomizationResource.setSourceService(sourceService);
1222                 spCustomizationResource.setToscaNodeType(nodeTemplate.getType());
1223                 serviceProxyCustomizationSet.add(spCustomizationResource);
1224
1225                 
1226                 toscaResourceStructure.setCatalogServiceProxyResourceCustomization(spCustomizationResource);
1227                 
1228                 return spCustomizationResource;
1229         }
1230         
1231         protected ConfigurationResourceCustomization createConfiguration(NodeTemplate nodeTemplate, 
1232                         ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization,
1233                         Optional<ConfigurationResourceCustomization> vnrResourceCustomization) {
1234
1235                 ConfigurationResourceCustomization configCustomizationResource = getConfigurationResourceCustomization(nodeTemplate, 
1236                                 toscaResourceStructure,spResourceCustomization);
1237                 
1238                 ConfigurationResource configResource = getConfigurationResource(nodeTemplate);
1239                 
1240                 Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
1241                 
1242                 StatefulEntityType entityType = nodeTemplate.getTypeDefinition();
1243                 String type = entityType.getType();
1244                 
1245                 if(NODES_VRF_ENTRY.equals(type)) {
1246                         configCustomizationResource.setConfigResourceCustomization(vnrResourceCustomization.orElse(null));
1247                 }
1248                         
1249                 configCustomizationResource.setConfigurationResource(configResource);
1250                 
1251                 configResourceCustomizationSet.add(configCustomizationResource);
1252
1253                 configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);   
1254                 
1255                 toscaResourceStructure.setCatalogConfigurationResource(configResource);
1256                 
1257                 toscaResourceStructure.setCatalogConfigurationResourceCustomization(configCustomizationResource);
1258                 
1259                 return configCustomizationResource;
1260         }
1261         
1262         protected ConfigurationResource createFabricConfiguration(NodeTemplate nodeTemplate, ToscaResourceStructure toscaResourceStructure) {
1263                 
1264                 Metadata fabricMetadata = nodeTemplate.getMetaData();
1265                 
1266                 ConfigurationResource configResource = new ConfigurationResource();
1267                 
1268                 configResource.setModelName(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1269                 configResource.setModelInvariantUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1270                 configResource.setModelUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1271                 configResource.setModelVersion(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1272                 configResource.setDescription(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1273                 configResource.setToscaNodeType(nodeTemplate.getType());
1274                 
1275                 return configResource;
1276         }
1277
1278         protected void createToscaCsar(ToscaResourceStructure toscaResourceStructure) {
1279                 ToscaCsar toscaCsar = new ToscaCsar();
1280                 if (toscaResourceStructure.getToscaArtifact().getArtifactChecksum() != null) {
1281                         toscaCsar.setArtifactChecksum(toscaResourceStructure.getToscaArtifact().getArtifactChecksum());
1282                 } else {
1283                         toscaCsar.setArtifactChecksum(MANUAL_RECORD);
1284                 }
1285                 toscaCsar.setArtifactUUID(toscaResourceStructure.getToscaArtifact().getArtifactUUID());
1286                 toscaCsar.setName(toscaResourceStructure.getToscaArtifact().getArtifactName());
1287                 toscaCsar.setVersion(toscaResourceStructure.getToscaArtifact().getArtifactVersion());
1288                 toscaCsar.setDescription(toscaResourceStructure.getToscaArtifact().getArtifactDescription());
1289                 toscaCsar.setUrl(toscaResourceStructure.getToscaArtifact().getArtifactURL());
1290
1291                 toscaResourceStructure.setCatalogToscaCsar(toscaCsar);
1292         }
1293         
1294         protected VnfcCustomization findExistingVfc(Set<VnfcCustomization> vnfcCustomizations, String customizationUUID) {
1295                 VnfcCustomization vnfcCustomization = null;
1296                 for(VnfcCustomization vnfcCustom : vnfcCustomizations){
1297                         if (vnfcCustom != null && vnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
1298                                 vnfcCustomization = vnfcCustom;
1299                         }
1300                 }
1301                 
1302                 if(vnfcCustomization==null)
1303                         vnfcCustomization = vnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
1304                         //vnfcCustomization = new VnfcCustomization();
1305                 
1306                 return vnfcCustomization;
1307         }
1308         
1309         protected CvnfcCustomization findExistingCvfc(Set<CvnfcCustomization> cvnfcCustomizations, String customizationUUID) {
1310                 CvnfcCustomization cvnfcCustomization = null;
1311                 for(CvnfcCustomization cvnfcCustom : cvnfcCustomizations){
1312                         if (cvnfcCustom != null && cvnfcCustom.getModelCustomizationUUID().equals(customizationUUID)) {
1313                                 cvnfcCustomization = cvnfcCustom;
1314                         }
1315                 }
1316                 
1317                 if(cvnfcCustomization==null)
1318                         cvnfcCustomization = cvnfcCustomizationRepo.findOneByModelCustomizationUUID(customizationUUID);
1319                 
1320                 return cvnfcCustomization;
1321         }
1322
1323         protected  NetworkResourceCustomization createNetwork(NodeTemplate networkNodeTemplate,
1324                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin,Service service) {
1325                 
1326                 NetworkResourceCustomization networkResourceCustomization=networkCustomizationRepo.findOneByModelCustomizationUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1327                                 
1328                 boolean networkUUIDsMatch = true;
1329                 // Check to make sure the NetworkResourceUUID on the Customization record matches the NetworkResourceUUID from the distribution.  
1330                 // If not we'll update the Customization record with latest from the distribution
1331                 if(networkResourceCustomization != null){
1332                         String existingNetworkModelUUID = networkResourceCustomization.getNetworkResource().getModelUUID();
1333                         String latestNetworkModelUUID = networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID);
1334                         
1335                         if(!existingNetworkModelUUID.equals(latestNetworkModelUUID)){
1336                                 networkUUIDsMatch = false;
1337                         }
1338                 
1339                 }
1340
1341                 if (networkResourceCustomization!=null && !networkUUIDsMatch){
1342                         
1343                         NetworkResource networkResource = createNetworkResource(networkNodeTemplate, toscaResourceStructure, heatTemplate,
1344                                         aicMax, aicMin);
1345                         
1346                         networkResourceCustomization.setNetworkResource(networkResource);                       
1347                                         
1348                         networkCustomizationRepo.saveAndFlush(networkResourceCustomization);
1349                         
1350                 }
1351                 else if(networkResourceCustomization==null){
1352                         networkResourceCustomization = createNetworkResourceCustomization(networkNodeTemplate,
1353                                         toscaResourceStructure);
1354                                         
1355                         NetworkResource networkResource = findExistingNetworkResource(service,
1356                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1357                                         if(networkResource == null)
1358                                 networkResource = createNetworkResource(networkNodeTemplate, toscaResourceStructure, heatTemplate,
1359                                                 aicMax, aicMin);
1360
1361                                         networkResource.addNetworkResourceCustomization(networkResourceCustomization);          
1362                                         networkResourceCustomization.setNetworkResource(networkResource);
1363                 }
1364                 
1365                 return networkResourceCustomization;
1366         }
1367         
1368         protected  NetworkResource findExistingNetworkResource(Service service, String modelUUID) {
1369                 NetworkResource networkResource = null;
1370                 for(NetworkResourceCustomization networkCustom : service.getNetworkCustomizations()){
1371                         if (networkCustom.getNetworkResource() != null
1372                                         && networkCustom.getNetworkResource().getModelUUID().equals(modelUUID)) {
1373                                         networkResource = networkCustom.getNetworkResource();
1374                         }
1375                 }
1376                 if(networkResource==null)
1377                         networkResource = networkRepo.findResourceByModelUUID(modelUUID);
1378                 
1379                 return networkResource;
1380         }
1381         
1382         protected NetworkResourceCustomization createNetworkResourceCustomization(NodeTemplate networkNodeTemplate,
1383                         ToscaResourceStructure toscaResourceStructure) {
1384                 NetworkResourceCustomization networkResourceCustomization = new NetworkResourceCustomization();
1385                 networkResourceCustomization.setModelInstanceName(
1386                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1387                 networkResourceCustomization.setModelCustomizationUUID(
1388                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));                
1389
1390                 networkResourceCustomization.setNetworkTechnology(
1391                                 testNull(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1392                                                 SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY)));
1393                 networkResourceCustomization.setNetworkType(testNull(toscaResourceStructure.getSdcCsarHelper()
1394                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE)));
1395                 networkResourceCustomization.setNetworkRole(testNull(toscaResourceStructure.getSdcCsarHelper()
1396                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE)));
1397                 networkResourceCustomization.setNetworkScope(testNull(toscaResourceStructure.getSdcCsarHelper()
1398                                 .getNodeTemplatePropertyLeafValue(networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE)));
1399                 return networkResourceCustomization;
1400         }
1401
1402         protected NetworkResource createNetworkResource(NodeTemplate networkNodeTemplate,
1403                         ToscaResourceStructure toscaResourceStructure, HeatTemplate heatTemplate, String aicMax, String aicMin) {
1404                 NetworkResource networkResource = new NetworkResource();
1405                 String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
1406                                 networkNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
1407
1408                 if ("true".equalsIgnoreCase(providerNetwork)) {
1409                         networkResource.setNeutronNetworkType(PROVIDER);
1410                 } else {
1411                         networkResource.setNeutronNetworkType(BASIC);
1412                 }
1413
1414                 networkResource.setModelName(
1415                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
1416
1417                 networkResource.setModelInvariantUUID(
1418                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
1419                 networkResource.setModelUUID(
1420                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
1421                 networkResource.setModelVersion(
1422                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1423
1424                 networkResource.setAicVersionMax(aicMax);               
1425                 networkResource.setAicVersionMin(aicMin);
1426                 networkResource.setToscaNodeType(networkNodeTemplate.getType());
1427                 networkResource.setDescription(
1428                                 testNull(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1429                 networkResource.setOrchestrationMode(HEAT);     
1430                 networkResource.setHeatTemplate(heatTemplate); 
1431                 return networkResource;
1432         }
1433         
1434         protected  CollectionNetworkResourceCustomization createNetworkCollection(NodeTemplate networkNodeTemplate,
1435                         ToscaResourceStructure toscaResourceStructure, Service service) {
1436
1437                 CollectionNetworkResourceCustomization collectionNetworkResourceCustomization = new CollectionNetworkResourceCustomization();
1438
1439                 // **** Build Object to populate Collection_Resource table
1440                 CollectionResource collectionResource = new CollectionResource();
1441
1442                 collectionResource
1443                                 .setModelName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1444                 collectionResource.setModelInvariantUUID(
1445                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1446                 collectionResource
1447                                 .setModelUUID(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1448                 collectionResource
1449                                 .setModelVersion(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1450                 collectionResource
1451                                 .setDescription(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1452                 collectionResource.setToscaNodeType(networkNodeTemplate.getType());
1453
1454                 toscaResourceStructure.setCatalogCollectionResource(collectionResource);
1455
1456                 // **** Build object to populate Collection_Resource_Customization table
1457                 NetworkCollectionResourceCustomization ncfc = new NetworkCollectionResourceCustomization();
1458                 
1459                 ncfc.setFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1460                                 "cr_function"));
1461                 ncfc.setRole(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1462                                 "cr_role"));
1463                 ncfc.setType(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1464                                 "cr_type"));
1465
1466                 ncfc.setModelInstanceName(networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1467                 ncfc.setModelCustomizationUUID(
1468                                 networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1469                 
1470                 Set<CollectionNetworkResourceCustomization> networkResourceCustomizationSet = new HashSet<>();
1471                 networkResourceCustomizationSet.add(collectionNetworkResourceCustomization);
1472
1473                 ncfc.setNetworkResourceCustomization(networkResourceCustomizationSet);
1474
1475                 ncfc.setCollectionResource(collectionResource);
1476                 toscaResourceStructure.setCatalogCollectionResourceCustomization(ncfc);
1477                 
1478                 //*** Build object to populate the Instance_Group table
1479                 List<Group> groupList = toscaResourceStructure.getSdcCsarHelper()
1480                                 .getGroupsOfOriginOfNodeTemplateByToscaGroupType(networkNodeTemplate,
1481                                                 "org.openecomp.groups.NetworkCollection");
1482                 
1483                 List<NetworkInstanceGroup> networkInstanceGroupList = new ArrayList<>();
1484
1485                 List<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationList = new ArrayList<CollectionResourceInstanceGroupCustomization>();
1486
1487                 for (Group group : groupList) { 
1488                         
1489                         NetworkInstanceGroup networkInstanceGroup = new NetworkInstanceGroup();
1490                         Metadata instanceMetadata = group.getMetadata();
1491                         networkInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1492                         networkInstanceGroup
1493                                         .setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1494                         networkInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1495                         networkInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1496                         networkInstanceGroup.setToscaNodeType(group.getType());
1497                         networkInstanceGroup.setRole(SubType.SUB_INTERFACE.toString()); // Set
1498                                                                                                                                                         // Role
1499                         networkInstanceGroup.setType(InstanceGroupType.L3_NETWORK); // Set
1500                                                                                                                                                 // type
1501                         networkInstanceGroup.setCollectionResource(collectionResource);
1502                 
1503                         // ****Build object to populate
1504                         // Collection_Resource_Instance_Group_Customization table
1505                         CollectionResourceInstanceGroupCustomization crInstanceGroupCustomization = new CollectionResourceInstanceGroupCustomization();
1506                         crInstanceGroupCustomization.setInstanceGroup(networkInstanceGroup);
1507                         crInstanceGroupCustomization.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1508                         crInstanceGroupCustomization.setModelCustomizationUUID(
1509                                         networkNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1510                 
1511                         // Loop through the template policy to find the subinterface_network_quantity property name.  Then extract the value for it.
1512                         List<Policy> policyList = toscaResourceStructure.getSdcCsarHelper().getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(networkNodeTemplate, "org.openecomp.policies.scaling.Fixed");
1513                         
1514                         if(policyList != null){
1515                                 for(Policy policy : policyList){
1516                                         for(String policyNetworkCollection : policy.getTargets()){
1517                                                 
1518                                                 if(policyNetworkCollection.equalsIgnoreCase(group.getName())){
1519                                                 
1520                                                         Map<String, Object> propMap = policy.getPolicyProperties();
1521                                         
1522                                                         if(propMap.get("quantity") != null){
1523                                                                                                                                 
1524                                                                 String quantity = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate, getPropertyInput(propMap.get("quantity").toString())); 
1525                                         
1526                                                                 if(quantity != null){
1527                                                                         crInstanceGroupCustomization.setSubInterfaceNetworkQuantity(Integer.parseInt(quantity));
1528                                                                 }
1529                                                                                                 
1530                                                         }
1531                                         
1532                                            }
1533                                         }
1534                                 }       
1535                         }
1536                                         
1537                         crInstanceGroupCustomization.setDescription(
1538                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1539                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
1540                                                                         + "_network_collection_description"));
1541                         crInstanceGroupCustomization.setFunction(
1542                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(networkNodeTemplate,
1543                                                         instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME)
1544                                                                         + "_network_collection_function"));
1545                         crInstanceGroupCustomization.setCollectionResourceCust(ncfc);
1546                         collectionResourceInstanceGroupCustomizationList.add(crInstanceGroupCustomization);
1547
1548                         networkInstanceGroup
1549                                         .setCollectionInstanceGroupCustomizations(collectionResourceInstanceGroupCustomizationList);
1550
1551                         networkInstanceGroupList.add(networkInstanceGroup);
1552
1553
1554                 toscaResourceStructure.setCatalogNetworkInstanceGroup(networkInstanceGroupList);
1555
1556                 List<NodeTemplate> vlNodeList = toscaResourceStructure.getSdcCsarHelper()
1557                                 .getNodeTemplateBySdcType(networkNodeTemplate, SdcTypes.VL);
1558                 
1559                 List<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationList = new ArrayList<>();
1560                 
1561                 //*****Build object to populate the NetworkResource table
1562                 NetworkResource networkResource = new NetworkResource();
1563                 
1564                 for(NodeTemplate vlNodeTemplate : vlNodeList){
1565
1566                         String providerNetwork = toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(
1567                                         vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK);
1568
1569                         if ("true".equalsIgnoreCase(providerNetwork)) {
1570                                 networkResource.setNeutronNetworkType(PROVIDER);
1571                         } else {
1572                                 networkResource.setNeutronNetworkType(BASIC);
1573                         }
1574
1575                         networkResource.setModelName(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1576
1577                         networkResource.setModelInvariantUUID(
1578                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1579                         networkResource.setModelUUID(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1580                         networkResource
1581                                         .setModelVersion(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1582
1583                         networkResource.setAicVersionMax(
1584                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES));
1585                         
1586                         TempNetworkHeatTemplateLookup tempNetworkLookUp = tempNetworkLookupRepo.findFirstBynetworkResourceModelName(
1587                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1588                         
1589                         if (tempNetworkLookUp != null ) {       
1590                                         
1591                                 HeatTemplate heatTemplate = heatRepo
1592                                                 .findByArtifactUuid(tempNetworkLookUp.getHeatTemplateArtifactUuid());
1593                                         networkResource.setHeatTemplate(heatTemplate);
1594                                         
1595                                         networkResource.setAicVersionMin(tempNetworkLookUp.getAicVersionMin());
1596                                         
1597                         }
1598
1599                         networkResource.setToscaNodeType(vlNodeTemplate.getType());
1600                         networkResource
1601                                         .setDescription(vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1602                         networkResource.setOrchestrationMode(HEAT);
1603                         
1604                         // Build object to populate the
1605                         // Collection_Network_Resource_Customization table
1606                         for (NodeTemplate memberNode : group.getMemberNodes()) {
1607                                 collectionNetworkResourceCustomization.setModelInstanceName(memberNode.getName());
1608                         }
1609
1610                         collectionNetworkResourceCustomization.setModelCustomizationUUID(
1611                                         vlNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1612
1613                         collectionNetworkResourceCustomization.setNetworkTechnology(
1614                                         toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vlNodeTemplate,
1615                                                         SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY));
1616                         collectionNetworkResourceCustomization.setNetworkType(toscaResourceStructure.getSdcCsarHelper()
1617                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE));
1618                         collectionNetworkResourceCustomization.setNetworkRole(toscaResourceStructure.getSdcCsarHelper()
1619                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE));
1620                         collectionNetworkResourceCustomization.setNetworkScope(toscaResourceStructure.getSdcCsarHelper()
1621                                         .getNodeTemplatePropertyLeafValue(vlNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE));
1622                         collectionNetworkResourceCustomization.setInstanceGroup(networkInstanceGroup);
1623                         collectionNetworkResourceCustomization.setNetworkResource(networkResource);
1624                         collectionNetworkResourceCustomization.setNetworkResourceCustomization(ncfc);
1625                         
1626                         collectionNetworkResourceCustomizationList.add(collectionNetworkResourceCustomization);
1627                    }
1628
1629                 }
1630                 
1631                 return collectionNetworkResourceCustomization;
1632         }
1633         
1634         protected VnfcInstanceGroupCustomization createVNFCInstanceGroup(NodeTemplate vnfcNodeTemplate, Group group,
1635                         VnfResourceCustomization vnfResourceCustomization, ToscaResourceStructure toscaResourceStructure) {
1636
1637                         Metadata instanceMetadata = group.getMetadata();
1638                         // Populate InstanceGroup
1639                         VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup();
1640                         
1641                         vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1642                         vfcInstanceGroup.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1643                         vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1644                         vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
1645                         vfcInstanceGroup.setToscaNodeType(group.getType());
1646                         vfcInstanceGroup.setRole("SUB-INTERFACE");   // Set Role
1647                         vfcInstanceGroup.setType(InstanceGroupType.VNFC);  // Set type  
1648                         
1649                         //Populate VNFCInstanceGroupCustomization
1650                         VnfcInstanceGroupCustomization vfcInstanceGroupCustom = new VnfcInstanceGroupCustomization();
1651                         
1652                         vfcInstanceGroupCustom.setVnfResourceCust(vnfResourceCustomization);
1653                         vnfResourceCustomization.getVnfcInstanceGroupCustomizations().add(vfcInstanceGroupCustom);
1654                         
1655                         vfcInstanceGroupCustom.setInstanceGroup(vfcInstanceGroup);                      
1656                         vfcInstanceGroup.getVnfcInstanceGroupCustomizations().add(vfcInstanceGroupCustom);
1657                         
1658                         vfcInstanceGroupCustom.setDescription(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
1659                                                 
1660                         String getInputName = null;
1661                         String groupProperty = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group, "vfc_instance_group_function"); 
1662                         if (groupProperty != null) { 
1663                         int getInputIndex = groupProperty.indexOf("{get_input="); 
1664                                 if (getInputIndex > -1) { 
1665                                         getInputName = groupProperty.substring(getInputIndex+11, groupProperty.length()-1); 
1666                                 } 
1667                         }
1668                         vfcInstanceGroupCustom.setFunction(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vnfcNodeTemplate, getInputName));
1669                         vfcInstanceGroupCustom.setInstanceGroup(vfcInstanceGroup);
1670                         
1671                         
1672                 return vfcInstanceGroupCustom;
1673
1674         }
1675                 
1676         protected VfModuleCustomization createVFModuleResource(Group group, NodeTemplate vfTemplate,
1677                         ToscaResourceStructure toscaResourceStructure, VfResourceStructure vfResourceStructure,
1678                         IVfModuleData vfModuleData, VnfResourceCustomization vnfResource, Service service,
1679                         Set<CvnfcCustomization> existingCvnfcSet, Set<VnfcCustomization> existingVnfcSet) {
1680
1681                 VfModuleCustomization vfModuleCustomization = findExistingVfModuleCustomization(vnfResource,
1682                                 vfModuleData.getVfModuleModelCustomizationUUID());
1683                 if (vfModuleCustomization == null) {
1684                         VfModule vfModule = findExistingVfModule(vnfResource,
1685                                         vfTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1686                         Metadata vfMetadata = group.getMetadata();
1687                         if (vfModule == null)
1688                                 vfModule = createVfModule(group, toscaResourceStructure, vfModuleData, vfMetadata);
1689
1690                         vfModuleCustomization = createVfModuleCustomization(group, toscaResourceStructure, vfModule, vfModuleData);
1691                         vfModuleCustomization.setVnfCustomization(vnfResource);
1692                         setHeatInformationForVfModule(toscaResourceStructure, vfResourceStructure, vfModule, vfModuleCustomization,
1693                                         vfMetadata);
1694                         vfModuleCustomization.setVfModule(vfModule);
1695                         vfModule.getVfModuleCustomization().add(vfModuleCustomization);
1696                         vnfResource.getVfModuleCustomizations().add(vfModuleCustomization);
1697                 } else {
1698                         vfResourceStructure.setAlreadyDeployed(true);
1699                 }
1700
1701                 // ******************************************************************************************************************
1702                 // * Extract VFC's and CVFC's then add them to VFModule
1703                 // ******************************************************************************************************************
1704
1705                 Set<CvnfcConfigurationCustomization> cvnfcConfigurationCustomizations = new HashSet<CvnfcConfigurationCustomization>();
1706                 Set<CvnfcCustomization> cvnfcCustomizations = new HashSet<CvnfcCustomization>();
1707                 Set<VnfcCustomization> vnfcCustomizations = new HashSet<VnfcCustomization>();
1708
1709                 // Only set the CVNFC if this vfModule group is a member of it.
1710                 List<NodeTemplate> groupMembers = toscaResourceStructure.getSdcCsarHelper().getMembersOfVfModule(vfTemplate,
1711                                 group);
1712                 String vfModuleMemberName = null;
1713
1714                 for (NodeTemplate node : groupMembers) {
1715                         vfModuleMemberName = node.getName();
1716                 }
1717
1718                 // Extract CVFC lists
1719                 List<NodeTemplate> cvfcList = toscaResourceStructure.getSdcCsarHelper().getNodeTemplateBySdcType(vfTemplate,
1720                                 SdcTypes.CVFC);
1721
1722                 for (NodeTemplate cvfcTemplate : cvfcList) {
1723                         boolean cvnfcVfModuleNameMatch = false;
1724                         
1725                         for(NodeTemplate node : groupMembers){          
1726                                 vfModuleMemberName = node.getName();
1727                                 
1728                                 if(vfModuleMemberName.equalsIgnoreCase(cvfcTemplate.getName())){
1729                                         cvnfcVfModuleNameMatch = true;
1730                                         break;
1731                                 }
1732                         }               
1733                         
1734                         if(vfModuleMemberName != null && cvnfcVfModuleNameMatch){
1735
1736                                 // Extract associated VFC - Should always be just one
1737                                 List<NodeTemplate> vfcList = toscaResourceStructure.getSdcCsarHelper()
1738                                                 .getNodeTemplateBySdcType(cvfcTemplate, SdcTypes.VFC);
1739
1740                                 for (NodeTemplate vfcTemplate : vfcList) {
1741
1742                                         VnfcCustomization vnfcCustomization = new VnfcCustomization();
1743                                         VnfcCustomization existingVnfcCustomization = null;
1744
1745                                         existingVnfcCustomization = findExistingVfc(existingVnfcSet,
1746                                                         vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1747
1748                                         if (existingVnfcCustomization == null) {
1749                                                 vnfcCustomization = new VnfcCustomization();
1750                                         } else {
1751                                                 vnfcCustomization = existingVnfcCustomization;
1752                                         }
1753
1754                                         // Only Add Abstract VNFC's to our DB, ignore all others
1755                                         if (existingVnfcCustomization == null && vfcTemplate.getMetaData()
1756                                                         .getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY).equalsIgnoreCase("Abstract")) {
1757                                                 vnfcCustomization.setModelCustomizationUUID(
1758                                                                 vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1759                                                 vnfcCustomization.setModelInstanceName(vfcTemplate.getName());
1760                                                 vnfcCustomization.setModelInvariantUUID(
1761                                                                 vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1762                                                 vnfcCustomization
1763                                                                 .setModelName(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1764                                                 vnfcCustomization
1765                                                                 .setModelUUID(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1766
1767                                                 vnfcCustomization.setModelVersion(
1768                                                                 testNull(vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1769                                                 vnfcCustomization.setDescription(testNull(
1770                                                                 vfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1771                                                 vnfcCustomization.setToscaNodeType(testNull(vfcTemplate.getType()));
1772
1773                                                 vnfcCustomizations.add(vnfcCustomization);
1774                                                 existingVnfcSet.add(vnfcCustomization);
1775                                         }
1776
1777                                         // This check is needed incase the VFC subcategory is
1778                                         // something other than Abstract. In that case we want to
1779                                         // skip adding that record to our DB.
1780                                         if (vnfcCustomization.getModelCustomizationUUID() != null) {
1781                                                 CvnfcCustomization cvnfcCustomization = new CvnfcCustomization();
1782                                                 cvnfcCustomization.setModelCustomizationUUID(
1783                                                                 cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1784                                                 cvnfcCustomization.setModelInstanceName(cvfcTemplate.getName());
1785                                                 cvnfcCustomization.setModelInvariantUUID(
1786                                                                 cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
1787                                                 cvnfcCustomization
1788                                                                 .setModelName(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
1789                                                 cvnfcCustomization
1790                                                                 .setModelUUID(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1791
1792                                                 cvnfcCustomization.setModelVersion(
1793                                                                 testNull(cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
1794                                                 cvnfcCustomization.setDescription(testNull(
1795                                                                 cvfcTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1796                                                 cvnfcCustomization.setToscaNodeType(testNull(cvfcTemplate.getType()));
1797
1798                                                 if (existingVnfcCustomization != null) {
1799                                                         cvnfcCustomization.setVnfcCustomization(existingVnfcCustomization);
1800                                                 } else {
1801                                                         cvnfcCustomization.setVnfcCustomization(vnfcCustomization);
1802                                                 }
1803
1804                                                 cvnfcCustomization.setNfcFunction(toscaResourceStructure.getSdcCsarHelper()
1805                                                                 .getNodeTemplatePropertyLeafValue(cvfcTemplate, "nfc_function"));
1806                                                 cvnfcCustomization.setNfcNamingCode(toscaResourceStructure.getSdcCsarHelper()
1807                                                                 .getNodeTemplatePropertyLeafValue(cvfcTemplate, "nfc_naming_code"));
1808                                                 cvnfcCustomization.setVfModuleCustomization(vfModuleCustomization);
1809
1810                                                 cvnfcCustomizations.add(cvnfcCustomization);
1811                                                 existingCvnfcSet.add(cvnfcCustomization);
1812
1813                                                 // *****************************************************************************************************************************************
1814                                                 // * Extract Fabric Configuration
1815                                                 // *****************************************************************************************************************************************
1816
1817                                                 List<NodeTemplate> fabricConfigList = toscaResourceStructure.getSdcCsarHelper()
1818                                                                 .getNodeTemplateBySdcType(vfTemplate, SdcTypes.CONFIGURATION);
1819
1820                                                 for (NodeTemplate fabricTemplate : fabricConfigList) {
1821
1822                                                         ConfigurationResource fabricConfig = null;
1823
1824                                                         ConfigurationResource existingConfig = findExistingConfiguration(service,
1825                                                                         fabricTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
1826
1827                                                         if (existingConfig == null) {
1828
1829                                                                 fabricConfig = createFabricConfiguration(fabricTemplate, toscaResourceStructure);
1830
1831                                                         } else {
1832                                                                 fabricConfig = existingConfig;
1833                                                         }
1834
1835                                                         CvnfcConfigurationCustomization cvnfcConfigurationCustomization = createCvnfcConfigurationCustomization(
1836                                                                         fabricTemplate, toscaResourceStructure, vnfResource, vfModuleCustomization,
1837                                                                         cvnfcCustomization, fabricConfig, vfTemplate, vfModuleMemberName);
1838                                                         cvnfcConfigurationCustomizations.add(cvnfcConfigurationCustomization);
1839                                                         
1840                                                         fabricConfig.setCvnfcConfigurationCustomization(cvnfcConfigurationCustomizations);
1841                                                 }
1842                                                 cvnfcCustomization.setCvnfcConfigurationCustomization(cvnfcConfigurationCustomizations);
1843                                         }
1844
1845                                 }
1846
1847                         }
1848                 }
1849                 vfModuleCustomization.setCvnfcCustomization(cvnfcCustomizations);
1850
1851                 return vfModuleCustomization;
1852         }
1853         
1854         protected CvnfcConfigurationCustomization createCvnfcConfigurationCustomization(NodeTemplate fabricTemplate, ToscaResourceStructure toscaResourceStruct, 
1855             VnfResourceCustomization vnfResource, VfModuleCustomization vfModuleCustomization, CvnfcCustomization cvnfcCustomization,
1856             ConfigurationResource configResource, NodeTemplate vfTemplate, String vfModuleMemberName) {
1857
1858                 Metadata fabricMetadata = fabricTemplate.getMetaData(); 
1859                                 
1860                 CvnfcConfigurationCustomization cvnfcConfigurationCustomization = new CvnfcConfigurationCustomization();
1861                 
1862                 cvnfcConfigurationCustomization.setConfigurationResource(configResource);
1863                 
1864                 cvnfcConfigurationCustomization.setCvnfcCustomization(cvnfcCustomization);
1865                 
1866                 cvnfcConfigurationCustomization.setModelCustomizationUUID(fabricMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
1867                 cvnfcConfigurationCustomization.setModelInstanceName(fabricTemplate.getName());
1868                 
1869                 List<Policy> policyList = toscaResourceStruct.getSdcCsarHelper().getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(vfTemplate, "org.openecomp.policies.External");
1870                 
1871                 if(policyList != null){
1872                         for(Policy policy : policyList){
1873                                 
1874                                 for(String policyCvfcTarget : policy.getTargets()){
1875                                         
1876                                         if(policyCvfcTarget.equalsIgnoreCase(vfModuleMemberName)){
1877                                 
1878                                                 Map<String, Object> propMap = policy.getPolicyProperties();
1879
1880                                                 if(propMap.get("type").toString().equalsIgnoreCase("Fabric Policy")){
1881                                                         cvnfcConfigurationCustomization.setPolicyName(propMap.get("name").toString());
1882                                                 }
1883                                         }
1884                                 }
1885                         }                       
1886                 }
1887                 
1888                 cvnfcConfigurationCustomization.setConfigurationFunction(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, "function"));
1889                 cvnfcConfigurationCustomization.setConfigurationRole(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, "role"));
1890                 cvnfcConfigurationCustomization.setConfigurationType(toscaResourceStruct.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(fabricTemplate, "type"));
1891                         
1892                 return cvnfcConfigurationCustomization;
1893         }
1894                 
1895         protected ConfigurationResource findExistingConfiguration(Service service, String modelUUID) {
1896                 ConfigurationResource configResource = null;
1897                 for(ConfigurationResourceCustomization configurationResourceCustom : service.getConfigurationCustomizations()){
1898                         if (configurationResourceCustom.getConfigurationResource() != null
1899                                         && configurationResourceCustom.getConfigurationResource().getModelUUID().equals(modelUUID)) {
1900                                 configResource = configurationResourceCustom.getConfigurationResource();
1901                         }
1902                 }
1903                 
1904                 return configResource;
1905         }
1906                 
1907         protected VfModuleCustomization findExistingVfModuleCustomization(VnfResourceCustomization vnfResource,
1908                         String vfModuleModelCustomizationUUID) {
1909                 VfModuleCustomization vfModuleCustomization = null;
1910                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1911                         if(vfModuleCustom.getModelCustomizationUUID().equalsIgnoreCase(vfModuleModelCustomizationUUID)){
1912                                 vfModuleCustomization = vfModuleCustom;
1913                         }
1914                 }
1915                 return vfModuleCustomization;
1916         }
1917
1918         protected VfModule findExistingVfModule(VnfResourceCustomization vnfResource, String modelUUID) {
1919                 VfModule vfModule = null;
1920                 for(VfModuleCustomization vfModuleCustom : vnfResource.getVfModuleCustomizations()){
1921                         if(vfModuleCustom.getVfModule() != null && vfModuleCustom.getVfModule().getModelUUID().equals(modelUUID)){
1922                                 vfModule = vfModuleCustom.getVfModule();
1923                         }
1924                 }
1925                 if(vfModule==null)
1926                         vfModule = vfModuleRepo.findByModelUUID(modelUUID);
1927                 
1928                 return vfModule;
1929         }
1930
1931         protected VfModuleCustomization createVfModuleCustomization(Group group,
1932                         ToscaResourceStructure toscaResourceStructure, VfModule vfModule, IVfModuleData vfModuleData) {
1933                 VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
1934                 
1935                 vfModuleCustomization.setModelCustomizationUUID(vfModuleData.getVfModuleModelCustomizationUUID());
1936
1937                 vfModuleCustomization.setVfModule(vfModule);
1938
1939                 String initialCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1940                                 SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT);
1941                 if (initialCount != null && initialCount.length() > 0) {
1942                         vfModuleCustomization.setInitialCount(Integer.valueOf(initialCount));
1943                 }
1944
1945                 vfModuleCustomization.setInitialCount(Integer.valueOf(toscaResourceStructure.getSdcCsarHelper()
1946                                 .getGroupPropertyLeafValue(group, SdcPropertyNames.PROPERTY_NAME_INITIALCOUNT)));
1947
1948                 String availabilityZoneCount = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1949                                 SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT);
1950                 if (availabilityZoneCount != null && availabilityZoneCount.length() > 0) {
1951                         vfModuleCustomization.setAvailabilityZoneCount(Integer.valueOf(availabilityZoneCount));
1952                 }
1953
1954                 vfModuleCustomization.setLabel(toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1955                                 SdcPropertyNames.PROPERTY_NAME_VFMODULELABEL));
1956
1957                 String maxInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1958                                 SdcPropertyNames.PROPERTY_NAME_MAXVFMODULEINSTANCES);
1959                 if (maxInstances != null && maxInstances.length() > 0) {
1960                         vfModuleCustomization.setMaxInstances(Integer.valueOf(maxInstances));
1961                 }
1962
1963                 String minInstances = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1964                                 SdcPropertyNames.PROPERTY_NAME_MINVFMODULEINSTANCES);
1965                 if (minInstances != null && minInstances.length() > 0) {
1966                         vfModuleCustomization.setMinInstances(Integer.valueOf(minInstances));
1967                 }
1968                 return vfModuleCustomization;
1969         }
1970
1971         protected VfModule createVfModule(Group group, ToscaResourceStructure toscaResourceStructure,
1972                         IVfModuleData vfModuleData, Metadata vfMetadata) {
1973                 VfModule vfModule = new VfModule();
1974                 String vfModuleModelUUID = vfModuleData.getVfModuleModelUUID();
1975
1976                 if(vfModuleModelUUID == null) {
1977                         vfModuleModelUUID = testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1978                                         SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID));
1979                 } else if (vfModuleModelUUID.indexOf('.') > -1) {
1980                         vfModuleModelUUID = vfModuleModelUUID.substring(0, vfModuleModelUUID.indexOf('.'));
1981                 }
1982
1983                 vfModule.setModelInvariantUUID(testNull(toscaResourceStructure.getSdcCsarHelper()
1984                                 .getMetadataPropertyValue(vfMetadata, SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELINVARIANTUUID)));
1985                 vfModule.setModelName(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1986                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELNAME)));
1987                 vfModule.setModelUUID(vfModuleModelUUID);
1988                 vfModule.setModelVersion(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1989                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELVERSION)));
1990                 vfModule.setDescription(testNull(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
1991                                 SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
1992
1993                 String vfModuleType = toscaResourceStructure.getSdcCsarHelper().getGroupPropertyLeafValue(group,
1994                                 SdcPropertyNames.PROPERTY_NAME_VFMODULETYPE);
1995                 if (vfModuleType != null && "Base".equalsIgnoreCase(vfModuleType)) {
1996                         vfModule.setIsBase(true);
1997                 } else {
1998                         vfModule.setIsBase(false);
1999                 }
2000                 return vfModule;
2001         }
2002
2003         protected void setHeatInformationForVfModule(ToscaResourceStructure toscaResourceStructure,
2004                         VfResourceStructure vfResourceStructure, VfModule vfModule, VfModuleCustomization vfModuleCustomization,
2005                         Metadata vfMetadata) {
2006                                 
2007                 Optional<VfModuleStructure> matchingObject = vfResourceStructure.getVfModuleStructure().stream()
2008                                 .filter(vfModuleStruct -> vfModuleStruct.getVfModuleMetadata().getVfModuleModelUUID()
2009                                                 .equalsIgnoreCase(toscaResourceStructure.getSdcCsarHelper().getMetadataPropertyValue(vfMetadata,
2010                                                                 SdcPropertyNames.PROPERTY_NAME_VFMODULEMODELUUID)))
2011                                 .findFirst();
2012
2013                 if (matchingObject.isPresent()) {
2014                         List<HeatFiles> heatFilesList = new ArrayList<>();
2015                         List<HeatTemplate> volumeHeatChildTemplates = new ArrayList<HeatTemplate>();
2016                         List<HeatTemplate> heatChildTemplates = new ArrayList<HeatTemplate>();
2017                         HeatTemplate parentHeatTemplate = new HeatTemplate();
2018                         String parentArtifactType = null;
2019                         Set<String> artifacts = new HashSet<>(matchingObject.get().getVfModuleMetadata().getArtifacts());
2020                         for (VfModuleArtifact vfModuleArtifact : vfResourceStructure.getArtifactsMapByUUID().values()) {
2021                                 
2022                                 List<HeatTemplate> childNestedHeatTemplates = new ArrayList<HeatTemplate>();
2023                                 
2024                                 if (artifacts.contains(vfModuleArtifact.getArtifactInfo().getArtifactUUID())) {
2025                                         checkVfModuleArtifactType(vfModule, vfModuleCustomization, heatFilesList, vfModuleArtifact,
2026                                                         childNestedHeatTemplates, parentHeatTemplate, vfResourceStructure);
2027                                 }
2028                                 
2029                                 if(vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)){
2030                                         parentArtifactType = identifyParentOfNestedTemplate(matchingObject.get(), vfModuleArtifact);
2031                                         
2032                                         if(!childNestedHeatTemplates.isEmpty()){
2033                                         
2034                                                 if (parentArtifactType != null && parentArtifactType.equalsIgnoreCase(ASDCConfiguration.HEAT_VOL)) {
2035                                                         volumeHeatChildTemplates.add(childNestedHeatTemplates.get(0));
2036                                                 } else {
2037                                                         heatChildTemplates.add(childNestedHeatTemplates.get(0));
2038                                                 }
2039                                         }
2040                                 }
2041                                 
2042                         }
2043                         if(!heatFilesList.isEmpty()){
2044                                 vfModule.setHeatFiles(heatFilesList);
2045                         }
2046                         
2047                         
2048                         // Set all Child Templates related to HEAT_VOLUME
2049                         if(!volumeHeatChildTemplates.isEmpty()){
2050                                 if(vfModule.getVolumeHeatTemplate() != null){
2051                                         vfModule.getVolumeHeatTemplate().setChildTemplates(volumeHeatChildTemplates);
2052                                 }else{
2053                                         logger.debug("VolumeHeatTemplate not set in setHeatInformationForVfModule()");
2054                                 }
2055                         }
2056                         
2057                         // Set all Child Templates related to HEAT
2058                         if(!heatChildTemplates.isEmpty()){
2059                                 if(vfModule.getModuleHeatTemplate() != null){
2060                                         vfModule.getModuleHeatTemplate().setChildTemplates(heatChildTemplates);
2061                                 }else{
2062                                         logger.debug("ModuleHeatTemplate not set in setHeatInformationForVfModule()");
2063                                 }
2064                         }
2065                 }
2066         }
2067
2068         protected void checkVfModuleArtifactType(VfModule vfModule, VfModuleCustomization vfModuleCustomization,
2069                         List<HeatFiles> heatFilesList, VfModuleArtifact vfModuleArtifact, List<HeatTemplate> nestedHeatTemplates,
2070                         HeatTemplate parentHeatTemplate, VfResourceStructure vfResourceStructure) {
2071                 if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT)) {
2072                         vfModuleArtifact.incrementDeployedInDB();
2073                         vfModule.setModuleHeatTemplate(vfModuleArtifact.getHeatTemplate());
2074                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_VOL)) {
2075                         vfModule.setVolumeHeatTemplate(vfModuleArtifact.getHeatTemplate());
2076                         VfModuleArtifact volVfModuleArtifact = this.getHeatEnvArtifactFromGeneratedArtifact(vfResourceStructure, vfModuleArtifact);
2077                         vfModuleCustomization.setVolumeHeatEnv(volVfModuleArtifact.getHeatEnvironment());
2078                         vfModuleArtifact.incrementDeployedInDB();
2079                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ENV)) {
2080                         if(vfModuleArtifact.getHeatEnvironment().getName().contains("volume")) {
2081                                 vfModuleCustomization.setVolumeHeatEnv(vfModuleArtifact.getHeatEnvironment());
2082                         } else { 
2083                         vfModuleCustomization.setHeatEnvironment(vfModuleArtifact.getHeatEnvironment());
2084                         }
2085                         vfModuleArtifact.incrementDeployedInDB();
2086                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_ARTIFACT)) {
2087                         heatFilesList.add(vfModuleArtifact.getHeatFiles());                                                     
2088                         vfModuleArtifact.incrementDeployedInDB();
2089                 } else if (vfModuleArtifact.getArtifactInfo().getArtifactType().equals(ASDCConfiguration.HEAT_NESTED)) {
2090                         nestedHeatTemplates.add(vfModuleArtifact.getHeatTemplate());                            
2091                         vfModuleArtifact.incrementDeployedInDB();
2092                 }
2093         }
2094
2095         protected VnfResourceCustomization createVnfResource(NodeTemplate vfNodeTemplate,
2096                         ToscaResourceStructure toscaResourceStructure, Service service) {
2097                 VnfResourceCustomization vnfResourceCustomization = null;
2098                 if(vnfResourceCustomization == null){
2099                         VnfResource vnfResource = findExistingVnfResource(service,
2100                                         vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
2101                         
2102                         if(vnfResource==null)
2103                                 vnfResource=createVnfResource(vfNodeTemplate);
2104                         
2105                         vnfResourceCustomization = createVnfResourceCustomization(vfNodeTemplate, toscaResourceStructure,
2106                                         vnfResource);
2107                         vnfResourceCustomization.setVnfResources(vnfResource);
2108                         vnfResourceCustomization.setService(service);
2109                         vnfResource.getVnfResourceCustomizations().add(vnfResourceCustomization);
2110                         
2111                 }
2112                 return vnfResourceCustomization;
2113         }
2114         
2115         protected VnfResource findExistingVnfResource(Service service, String modelUUID) {
2116                 VnfResource vnfResource = null;
2117                 for(VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()){
2118                         if (vnfResourceCustom.getVnfResources() != null
2119                                         && vnfResourceCustom.getVnfResources().getModelUUID().equals(modelUUID)) {
2120                                 vnfResource = vnfResourceCustom.getVnfResources();
2121                         }
2122                 }
2123                 if(vnfResource==null)
2124                         vnfResource = vnfRepo.findResourceByModelUUID(modelUUID);
2125                 
2126                 return vnfResource;
2127         }
2128
2129         protected VnfResourceCustomization createVnfResourceCustomization(NodeTemplate vfNodeTemplate,
2130                         ToscaResourceStructure toscaResourceStructure, VnfResource vnfResource) {
2131                 VnfResourceCustomization vnfResourceCustomization = new VnfResourceCustomization();
2132                 vnfResourceCustomization.setModelCustomizationUUID(
2133                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
2134                 vnfResourceCustomization.setModelInstanceName(vfNodeTemplate.getName());
2135
2136                 vnfResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
2137                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
2138                 vnfResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
2139                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, "nf_naming_code")));
2140                 vnfResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
2141                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
2142                 vnfResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
2143                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
2144
2145                 vnfResourceCustomization.setMultiStageDesign(toscaResourceStructure.getSdcCsarHelper()
2146                                 .getNodeTemplatePropertyLeafValue(vfNodeTemplate, MULTI_STAGE_DESIGN));
2147
2148                 vnfResourceCustomization.setBlueprintName(testNull(toscaResourceStructure.getSdcCsarHelper()
2149                         .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SDNC_MODEL_NAME)));
2150
2151                 vnfResourceCustomization.setBlueprintVersion(testNull(toscaResourceStructure.getSdcCsarHelper()
2152                         .getNodeTemplatePropertyLeafValue(vfNodeTemplate, SDNC_MODEL_VERSION)));
2153
2154                 vnfResourceCustomization.setVnfResources(vnfResource);
2155                 vnfResourceCustomization.setAvailabilityZoneMaxCount(Integer.getInteger(
2156                                 vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_AVAILABILITYZONECOUNT)));  
2157
2158                 CapabilityAssignments vnfCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
2159                                 .getCapabilitiesOf(vfNodeTemplate);
2160
2161                 if (vnfCustomizationCapability != null) {
2162                         CapabilityAssignment capAssign = vnfCustomizationCapability.getCapabilityByName(SCALABLE);
2163
2164                         if (capAssign != null) {
2165                                 vnfResourceCustomization.setMinInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
2166                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
2167                                 vnfResourceCustomization.setMaxInstances(Integer.getInteger(toscaResourceStructure.getSdcCsarHelper()
2168                                                 .getCapabilityPropertyLeafValue(capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
2169                         }
2170
2171                 }
2172                 
2173                 toscaResourceStructure.setCatalogVnfResourceCustomization(vnfResourceCustomization);
2174                 
2175                 return vnfResourceCustomization;
2176         }
2177
2178         protected VnfResource createVnfResource(NodeTemplate vfNodeTemplate) {
2179                 VnfResource vnfResource = new VnfResource();
2180                 vnfResource.setModelInvariantUUID(
2181                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
2182                 vnfResource.setModelName(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
2183                 vnfResource.setModelUUID(testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
2184
2185                 vnfResource.setModelVersion(
2186                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
2187                 vnfResource.setDescription(
2188                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION)));
2189                 vnfResource.setOrchestrationMode(HEAT);
2190                 vnfResource.setToscaNodeType(testNull(vfNodeTemplate.getType()));
2191                 vnfResource.setAicVersionMax(
2192                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
2193                 vnfResource.setAicVersionMin(
2194                                 testNull(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
2195                 vnfResource.setCategory(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY));
2196                 vnfResource.setSubCategory(vfNodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY));
2197                 
2198                 return vnfResource;
2199         }
2200
2201         protected AllottedResourceCustomization createAllottedResource(NodeTemplate nodeTemplate,
2202                         ToscaResourceStructure toscaResourceStructure, Service service) {
2203                 AllottedResourceCustomization allottedResourceCustomization = allottedCustomizationRepo
2204                                 .findOneByModelCustomizationUUID(
2205                                 nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID));
2206                         
2207                 if(allottedResourceCustomization == null){                      
2208                         AllottedResource allottedResource = findExistingAllottedResource(service,
2209                                         nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
2210                         
2211                         if(allottedResource==null)
2212                                 allottedResource=createAR(nodeTemplate);
2213                         
2214                         toscaResourceStructure.setAllottedResource(allottedResource);                   
2215                         allottedResourceCustomization = createAllottedResourceCustomization(nodeTemplate, toscaResourceStructure);
2216                         allottedResourceCustomization.setAllottedResource(allottedResource);
2217                         allottedResource.getAllotedResourceCustomization().add(allottedResourceCustomization);
2218                 }
2219                 return allottedResourceCustomization;
2220         }
2221         
2222         protected AllottedResource findExistingAllottedResource(Service service, String modelUUID) {
2223                 AllottedResource allottedResource = null;
2224                 for(AllottedResourceCustomization allottedResourceCustom : service.getAllottedCustomizations()){
2225                         if (allottedResourceCustom.getAllottedResource() != null
2226                                         && allottedResourceCustom.getAllottedResource().getModelUUID().equals(modelUUID)) {
2227                                 allottedResource = allottedResourceCustom.getAllottedResource();
2228                         }
2229                 }
2230                 if(allottedResource==null)
2231                         allottedResource = allottedRepo.findResourceByModelUUID(modelUUID);
2232                 
2233                 return allottedResource;
2234         }
2235         
2236         protected AllottedResourceCustomization createAllottedResourceCustomization(NodeTemplate nodeTemplate,
2237                         ToscaResourceStructure toscaResourceStructure) {
2238                 AllottedResourceCustomization allottedResourceCustomization = new AllottedResourceCustomization();
2239                 allottedResourceCustomization.setModelCustomizationUUID(
2240                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)));
2241                 allottedResourceCustomization.setModelInstanceName(nodeTemplate.getName());
2242                 
2243
2244                 allottedResourceCustomization.setNfFunction(testNull(toscaResourceStructure.getSdcCsarHelper()
2245                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFFUNCTION)));
2246                 allottedResourceCustomization.setNfNamingCode(testNull(toscaResourceStructure.getSdcCsarHelper()
2247                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, "nf_naming_code")));
2248                 allottedResourceCustomization.setNfRole(testNull(toscaResourceStructure.getSdcCsarHelper()
2249                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFROLE)));
2250                 allottedResourceCustomization.setNfType(testNull(toscaResourceStructure.getSdcCsarHelper()
2251                                 .getNodeTemplatePropertyLeafValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NFTYPE)));
2252
2253                 List<NodeTemplate> vfcNodes = toscaResourceStructure.getSdcCsarHelper().getVfcListByVf(allottedResourceCustomization.getModelCustomizationUUID());
2254                 
2255                 if(vfcNodes != null){
2256                         for(NodeTemplate vfcNode : vfcNodes){
2257                         
2258                                 allottedResourceCustomization.setProvidingServiceModelUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_uuid"));
2259                                 allottedResourceCustomization.setProvidingServiceModelInvariantUUID(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_invariant_uuid"));
2260                                 allottedResourceCustomization.setProvidingServiceModelName(toscaResourceStructure.getSdcCsarHelper().getNodeTemplatePropertyLeafValue(vfcNode, "providing_service_name"));
2261                         }
2262                 }
2263                 
2264
2265                 CapabilityAssignments arCustomizationCapability = toscaResourceStructure.getSdcCsarHelper()
2266                                 .getCapabilitiesOf(nodeTemplate);
2267
2268                 if (arCustomizationCapability != null) {
2269                         CapabilityAssignment capAssign = arCustomizationCapability.getCapabilityByName(SCALABLE);
2270
2271                         if (capAssign != null) {
2272                                 allottedResourceCustomization.setMinInstances(
2273                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
2274                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MININSTANCES)));
2275                                 allottedResourceCustomization.setMaxInstances(
2276                                                 Integer.getInteger(toscaResourceStructure.getSdcCsarHelper().getCapabilityPropertyLeafValue(
2277                                                                 capAssign, SdcPropertyNames.PROPERTY_NAME_MAXINSTANCES)));
2278                         }
2279                 }
2280                 return allottedResourceCustomization;
2281         }
2282
2283         protected AllottedResource createAR(NodeTemplate nodeTemplate) {
2284                 AllottedResource allottedResource = new AllottedResource();
2285                 allottedResource
2286                 .setModelUUID(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_UUID)));
2287                 allottedResource.setModelInvariantUUID(
2288                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID)));
2289                 allottedResource
2290                 .setModelName(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME)));
2291                 allottedResource
2292                 .setModelVersion(testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_VERSION)));
2293                 allottedResource.setToscaNodeType(testNull(nodeTemplate.getType()));
2294                 allottedResource.setSubcategory(
2295                                 testNull(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_SUBCATEGORY)));
2296                 allottedResource
2297                 .setDescription(nodeTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
2298                 return allottedResource;
2299         }
2300
2301         protected Set<HeatTemplateParam> extractHeatTemplateParameters(String yamlFile, String artifactUUID) {
2302                 // Scan the payload downloadResult and extract the HeatTemplate
2303                 // parameters
2304                 YamlEditor yamlEditor = new YamlEditor(yamlFile.getBytes());
2305                 return yamlEditor.getParameterList(artifactUUID);
2306         }       
2307
2308         protected String testNull(Object object) {
2309
2310                 if (object == null) {
2311                         return null;
2312                 } else if (object.equals("NULL")) {
2313                         return null;
2314                 } else if (object instanceof Integer) {
2315                         return object.toString();
2316                 } else if (object instanceof String) {
2317                         return (String) object;
2318                 } else {
2319                         return "Type not recognized";
2320                 }
2321         }
2322         
2323         protected static String identifyParentOfNestedTemplate(VfModuleStructure vfModuleStructure,
2324                         VfModuleArtifact heatNestedArtifact) {
2325
2326                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT) != null && vfModuleStructure
2327                                 .getArtifactsMap().get(ASDCConfiguration.HEAT).get(0).getArtifactInfo().getRelatedArtifacts() != null) {
2328                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT).get(0)
2329                                         .getArtifactInfo().getRelatedArtifacts()) {
2330                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
2331                                         return ASDCConfiguration.HEAT;
2332                                 }
2333
2334                         }
2335                 } 
2336                 
2337                 if (vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL) != null 
2338                                 && vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL).get(0).getArtifactInfo()
2339                                                 .getRelatedArtifacts() != null) {
2340                         for (IArtifactInfo unknownArtifact : vfModuleStructure.getArtifactsMap().get(ASDCConfiguration.HEAT_VOL)
2341                                         .get(0).getArtifactInfo().getRelatedArtifacts()) {
2342                                 if (heatNestedArtifact.getArtifactInfo().getArtifactUUID().equals(unknownArtifact.getArtifactUUID())) {
2343                                         return ASDCConfiguration.HEAT_VOL;
2344                                 }
2345                         
2346                         }
2347                 }
2348                 
2349                 // Does not belong to anything
2350                 return null;
2351                         
2352         }
2353         
2354         protected static String createVNFName(VfResourceStructure vfResourceStructure) {
2355
2356                 return vfResourceStructure.getNotification().getServiceName() + "/"
2357                                 + vfResourceStructure.getResourceInstance().getResourceInstanceName();
2358         }
2359
2360         protected static String createVfModuleName(VfModuleStructure vfModuleStructure) {
2361                 
2362                 return createVNFName(vfModuleStructure.getParentVfResource()) + "::"
2363                                 + vfModuleStructure.getVfModuleMetadata().getVfModuleModelName();
2364         }
2365         
2366         protected String getPropertyInput(String propertyName){
2367         
2368                 String inputName = new String();
2369                 
2370                 if (propertyName != null) { 
2371                         int getInputIndex = propertyName.indexOf("{get_input="); 
2372                         if (getInputIndex > -1) { 
2373                                 inputName = propertyName.substring(getInputIndex+11, propertyName.length()-1); 
2374                         } 
2375                 }
2376                 
2377                 return inputName;
2378         }
2379         
2380         
2381         protected static Timestamp getCurrentTimeStamp() {
2382                 
2383                 return new Timestamp(new Date().getTime());
2384         }
2385
2386 }
2387