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