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