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