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