diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services')
6 files changed, 1369 insertions, 1608 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/ManualVspDataCollectionService.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/ManualVspDataCollectionService.java index dc60419973..f50c1339b3 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/ManualVspDataCollectionService.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/ManualVspDataCollectionService.java @@ -12,374 +12,355 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ - + */ package org.openecomp.sdc.vendorsoftwareproduct.services; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.openecomp.core.utilities.json.JsonUtil; -import org.openecomp.sdc.generator.datatypes.tosca.*; +import org.openecomp.sdc.generator.datatypes.tosca.ComputeFlavor; +import org.openecomp.sdc.generator.datatypes.tosca.DeploymentFlavorModel; +import org.openecomp.sdc.generator.datatypes.tosca.LicenseFlavor; +import org.openecomp.sdc.generator.datatypes.tosca.MultiFlavorVfcImage; +import org.openecomp.sdc.generator.datatypes.tosca.VendorInfo; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity; import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupModel; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory; -import org.openecomp.sdc.vendorsoftwareproduct.dao.*; -import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*; -import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentComputeAssociation; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic; import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.compute.Compute; import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.image.ImageDetails; import org.openecomp.sdc.versioning.dao.types.Version; -import java.util.*; - - public class ManualVspDataCollectionService { - private static final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao = - VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(); - private static final DeploymentFlavorDao deploymentFlavorDao = - DeploymentFlavorDaoFactory.getInstance().createInterface(); - private static final ComputeDao computeDao = - ComputeDaoFactory.getInstance().createInterface(); - private static final ImageDao imageDao = - ImageDaoFactory.getInstance().createInterface(); - private static final ComponentDao componentDao = - ComponentDaoFactory.getInstance().createInterface(); - - private static final NicDao nicDao = - NicDaoFactory.getInstance().createInterface(); - private static final VendorLicenseFacade vendorLicenseFacade = - VendorLicenseFacadeFactory.getInstance().createInterface(); - - private final Logger log = LoggerFactory.getLogger(this.getClass().getName()); - - /** - * Gets vendor name for the vsp. - * - * @param vspId the vsp id - * @param version the version - * @return the release vendor name - */ - public Optional<String> getReleaseVendor(String vspId, Version version) { - String vendorName = null; - VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version)); - if (Objects.nonNull(vspDetails)) { - vendorName = vspDetails.getVendorName(); - } - return Optional.ofNullable(vendorName); - } - - /** - * Gets the deployment flavor data for manually onboarded VSPs. - * - * @param vspId the vsp id - * @param version the version - * @return the allowed flavors - */ - public Map<String, DeploymentFlavorModel> getAllowedFlavors(String vspId, Version version) { - Map<String, DeploymentFlavorModel> allowedFlavors = new HashMap<>(); - Collection<DeploymentFlavorEntity> deploymentFlavorEntities = - deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); - if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) { - for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { - DeploymentFlavor deploymentFlavorCompositionData = - deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + private static final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao = VendorSoftwareProductInfoDaoFactory.getInstance() + .createInterface(); + private static final DeploymentFlavorDao deploymentFlavorDao = DeploymentFlavorDaoFactory.getInstance().createInterface(); + private static final ComputeDao computeDao = ComputeDaoFactory.getInstance().createInterface(); + private static final ImageDao imageDao = ImageDaoFactory.getInstance().createInterface(); + private static final ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface(); + private static final NicDao nicDao = NicDaoFactory.getInstance().createInterface(); + private static final VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory.getInstance().createInterface(); + private final Logger log = LoggerFactory.getLogger(this.getClass().getName()); + /** + * Gets vendor name for the vsp. + * + * @param vspId the vsp id + * @param version the version + * @return the release vendor name + */ + public Optional<String> getReleaseVendor(String vspId, Version version) { + String vendorName = null; VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version)); - String vspVlmId; - Version vlmVersion; if (Objects.nonNull(vspDetails)) { - vspVlmId = vspDetails.getVendorId(); - vlmVersion = vspDetails.getVlmVersion(); - if (StringUtils.isNotEmpty(vspVlmId)) { - DeploymentFlavorModel deploymentFlavorModel = new DeploymentFlavorModel(); - String featureGroupId = deploymentFlavorCompositionData.getFeatureGroupId(); - if (Objects.isNull(featureGroupId)) { - //No feature group associated with deployment flavor. So excluding this deployment - // flavor for Tosca model - continue; - } - //Gather and set License flavor info - LicenseFlavor licenseFlavor = getLicenseFlavor(featureGroupId); - deploymentFlavorModel.setLicense_flavor(licenseFlavor); - //Get sp_part_number - Optional<String> partNumber = getPartNumber(vspVlmId, vlmVersion, featureGroupId); - partNumber.ifPresent(deploymentFlavorModel::setSp_part_number); - //Gather and set Vendor Info - String vendorModel = deploymentFlavorCompositionData.getModel(); - Optional<VendorInfo> vendorInfo = getVendorInfo(vspVlmId, vendorModel, vlmVersion, - featureGroupId); - vendorInfo.ifPresent(deploymentFlavorModel::setVendor_info); - //Gather and set Compute info - List<ComponentComputeAssociation> componentComputeAssociations = - deploymentFlavorCompositionData.getComponentComputeAssociations(); - if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { - for (ComponentComputeAssociation componentComputeAssociation : - componentComputeAssociations) { - String componentId = componentComputeAssociation.getComponentId(); - String computeFlavorId = componentComputeAssociation.getComputeFlavorId(); - Optional<ComputeFlavor> computeFlavor = - getComputeFlavor(vspId, version, componentId, computeFlavorId); - computeFlavor.ifPresent(deploymentFlavorModel::setCompute_flavor); - } - } - partNumber.ifPresent(spPartNumber -> allowedFlavors.put(spPartNumber, - deploymentFlavorModel)); - } + vendorName = vspDetails.getVendorName(); } - } + return Optional.ofNullable(vendorName); } - return allowedFlavors; - } - /** - * Gets the component image data for manually onboarded VSPs. - * - * @param vspId the vsp id - * @param version the version - * @return the vsp component images - */ - public Map<String, List<MultiFlavorVfcImage>> getVspComponentImages(String vspId, - Version version) { - Map<String, List<MultiFlavorVfcImage>> vspComponentImages = new HashMap<>(); - Collection<DeploymentFlavorEntity> deploymentFlavorEntities = - deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); - for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { - DeploymentFlavor deploymentFlavorCompositionData = - deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + /** + * Gets the deployment flavor data for manually onboarded VSPs. + * + * @param vspId the vsp id + * @param version the version + * @return the allowed flavors + */ + public Map<String, DeploymentFlavorModel> getAllowedFlavors(String vspId, Version version) { + Map<String, DeploymentFlavorModel> allowedFlavors = new HashMap<>(); + Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); + if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) { + for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { + DeploymentFlavor deploymentFlavorCompositionData = deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + VspDetails vspDetails = vendorSoftwareProductInfoDao.get(new VspDetails(vspId, version)); + String vspVlmId; + Version vlmVersion; + if (Objects.nonNull(vspDetails)) { + vspVlmId = vspDetails.getVendorId(); + vlmVersion = vspDetails.getVlmVersion(); + if (StringUtils.isNotEmpty(vspVlmId)) { + DeploymentFlavorModel deploymentFlavorModel = new DeploymentFlavorModel(); + String featureGroupId = deploymentFlavorCompositionData.getFeatureGroupId(); + if (Objects.isNull(featureGroupId)) { + //No feature group associated with deployment flavor. So excluding this deployment - List<ComponentComputeAssociation> componentComputeAssociations = - deploymentFlavorCompositionData.getComponentComputeAssociations(); - if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { - for (ComponentComputeAssociation componentComputeAssociation : - componentComputeAssociations) { - String componentId = componentComputeAssociation.getComponentId(); - List<MultiFlavorVfcImage> componentImages = - getComponentImages(vspId, version, componentId); - if (CollectionUtils.isNotEmpty(componentImages)) { - vspComponentImages.put(componentId, componentImages); - } + // flavor for Tosca model + continue; + } + //Gather and set License flavor info + LicenseFlavor licenseFlavor = getLicenseFlavor(featureGroupId); + deploymentFlavorModel.setLicense_flavor(licenseFlavor); + //Get sp_part_number + Optional<String> partNumber = getPartNumber(vspVlmId, vlmVersion, featureGroupId); + partNumber.ifPresent(deploymentFlavorModel::setSp_part_number); + //Gather and set Vendor Info + String vendorModel = deploymentFlavorCompositionData.getModel(); + Optional<VendorInfo> vendorInfo = getVendorInfo(vspVlmId, vendorModel, vlmVersion, featureGroupId); + vendorInfo.ifPresent(deploymentFlavorModel::setVendor_info); + //Gather and set Compute info + List<ComponentComputeAssociation> componentComputeAssociations = deploymentFlavorCompositionData + .getComponentComputeAssociations(); + if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { + for (ComponentComputeAssociation componentComputeAssociation : componentComputeAssociations) { + String componentId = componentComputeAssociation.getComponentId(); + String computeFlavorId = componentComputeAssociation.getComputeFlavorId(); + Optional<ComputeFlavor> computeFlavor = getComputeFlavor(vspId, version, componentId, computeFlavorId); + computeFlavor.ifPresent(deploymentFlavorModel::setCompute_flavor); + } + } + partNumber.ifPresent(spPartNumber -> allowedFlavors.put(spPartNumber, deploymentFlavorModel)); + } + } + } } - } + return allowedFlavors; } - return vspComponentImages; - } - /** - * Gets the component data for manually onboarded VSPs. - * - * @param vspId the vsp id - * @param version the version - * @return the vsp components - */ - public Map<String, String> getVspComponents(String vspId, Version version) { - Map<String, String> componentIdNameMap = new HashMap<>(); - Collection<DeploymentFlavorEntity> deploymentFlavorEntities = - deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); - for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { - DeploymentFlavor deploymentFlavorCompositionData = - deploymentFlavorEntity.getDeploymentFlavorCompositionData(); - - List<ComponentComputeAssociation> componentComputeAssociations = - deploymentFlavorCompositionData.getComponentComputeAssociations(); - if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { - for (ComponentComputeAssociation componentComputeAssociation : - componentComputeAssociations) { - String componentId = componentComputeAssociation.getComponentId(); - Optional<String> componentName = getComponentName(vspId, version, componentId); - componentName.ifPresent(name -> componentIdNameMap.put(componentId, name)); + /** + * Gets the component image data for manually onboarded VSPs. + * + * @param vspId the vsp id + * @param version the version + * @return the vsp component images + */ + public Map<String, List<MultiFlavorVfcImage>> getVspComponentImages(String vspId, Version version) { + Map<String, List<MultiFlavorVfcImage>> vspComponentImages = new HashMap<>(); + Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); + for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { + DeploymentFlavor deploymentFlavorCompositionData = deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + List<ComponentComputeAssociation> componentComputeAssociations = deploymentFlavorCompositionData.getComponentComputeAssociations(); + if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { + for (ComponentComputeAssociation componentComputeAssociation : componentComputeAssociations) { + String componentId = componentComputeAssociation.getComponentId(); + List<MultiFlavorVfcImage> componentImages = getComponentImages(vspId, version, componentId); + if (CollectionUtils.isNotEmpty(componentImages)) { + vspComponentImages.put(componentId, componentImages); + } + } + } } - } + return vspComponentImages; } - return componentIdNameMap; - } - /** - * Gets the NIC data for manually onboarded VSPs. - * - * @param vspId the vsp id - * @param version the version - * @return the vsp component nics - */ - public Map<String, List<Nic>> getVspComponentNics(String vspId, Version version) { - Map<String, List<Nic>> vspComponentNics = new HashMap<>(); - Collection<DeploymentFlavorEntity> deploymentFlavorEntities = - deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); - if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) { - for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { - DeploymentFlavor deploymentFlavorCompositionData = - deploymentFlavorEntity.getDeploymentFlavorCompositionData(); - if (Objects.nonNull(deploymentFlavorCompositionData)) { - List<ComponentComputeAssociation> componentComputeAssociations = - deploymentFlavorCompositionData.getComponentComputeAssociations(); - if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { - for (ComponentComputeAssociation componentComputeAssociation : - componentComputeAssociations) { - String componentId = componentComputeAssociation.getComponentId(); - List<Nic> componentNics = getComponentNics(vspId, version, componentId); - if (CollectionUtils.isNotEmpty(componentNics)) { - vspComponentNics.put(componentId, componentNics); - } + /** + * Gets the component data for manually onboarded VSPs. + * + * @param vspId the vsp id + * @param version the version + * @return the vsp components + */ + public Map<String, String> getVspComponents(String vspId, Version version) { + Map<String, String> componentIdNameMap = new HashMap<>(); + Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); + for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { + DeploymentFlavor deploymentFlavorCompositionData = deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + List<ComponentComputeAssociation> componentComputeAssociations = deploymentFlavorCompositionData.getComponentComputeAssociations(); + if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { + for (ComponentComputeAssociation componentComputeAssociation : componentComputeAssociations) { + String componentId = componentComputeAssociation.getComponentId(); + Optional<String> componentName = getComponentName(vspId, version, componentId); + componentName.ifPresent(name -> componentIdNameMap.put(componentId, name)); + } } - } } - } + return componentIdNameMap; } - return vspComponentNics; - } - private List<Nic> getComponentNics(String vspId, Version version, String componentId) { - List<Nic> componentNics = new ArrayList<>(); - Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null)); - if (Objects.nonNull(nics)) { - for (NicEntity nic : nics) { - String nicId = nic.getId(); - NicEntity nicEntity = nicDao.get(new NicEntity(vspId, version, componentId, nicId)); - if (Objects.nonNull(nicEntity) - && Objects.nonNull(nicEntity.getCompositionData())) { - componentNics.add(nicEntity.getNicCompositionData()); + /** + * Gets the NIC data for manually onboarded VSPs. + * + * @param vspId the vsp id + * @param version the version + * @return the vsp component nics + */ + public Map<String, List<Nic>> getVspComponentNics(String vspId, Version version) { + Map<String, List<Nic>> vspComponentNics = new HashMap<>(); + Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deploymentFlavorDao.list(new DeploymentFlavorEntity(vspId, version, null)); + if (CollectionUtils.isNotEmpty(deploymentFlavorEntities)) { + for (DeploymentFlavorEntity deploymentFlavorEntity : deploymentFlavorEntities) { + DeploymentFlavor deploymentFlavorCompositionData = deploymentFlavorEntity.getDeploymentFlavorCompositionData(); + if (Objects.nonNull(deploymentFlavorCompositionData)) { + List<ComponentComputeAssociation> componentComputeAssociations = deploymentFlavorCompositionData + .getComponentComputeAssociations(); + if (CollectionUtils.isNotEmpty(componentComputeAssociations)) { + for (ComponentComputeAssociation componentComputeAssociation : componentComputeAssociations) { + String componentId = componentComputeAssociation.getComponentId(); + List<Nic> componentNics = getComponentNics(vspId, version, componentId); + if (CollectionUtils.isNotEmpty(componentNics)) { + vspComponentNics.put(componentId, componentNics); + } + } + } + } + } } - } + return vspComponentNics; } - return componentNics; - } - private LicenseFlavor getLicenseFlavor(String featureGroupId) { - LicenseFlavor licenseFlavor = new LicenseFlavor(); - licenseFlavor.setFeature_group_uuid(featureGroupId); - return licenseFlavor; - } + private List<Nic> getComponentNics(String vspId, Version version, String componentId) { + List<Nic> componentNics = new ArrayList<>(); + Collection<NicEntity> nics = nicDao.list(new NicEntity(vspId, version, componentId, null)); + if (Objects.nonNull(nics)) { + for (NicEntity nic : nics) { + String nicId = nic.getId(); + NicEntity nicEntity = nicDao.get(new NicEntity(vspId, version, componentId, nicId)); + if (Objects.nonNull(nicEntity) && Objects.nonNull(nicEntity.getCompositionData())) { + componentNics.add(nicEntity.getNicCompositionData()); + } + } + } + return componentNics; + } - private Optional<String> getPartNumber(String vlmId, Version version, - String featureGroupId) { - FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId); - if (Objects.nonNull(featureGroup)) { - return Optional.ofNullable(featureGroup.getFeatureGroup().getPartNumber()); + private LicenseFlavor getLicenseFlavor(String featureGroupId) { + LicenseFlavor licenseFlavor = new LicenseFlavor(); + licenseFlavor.setFeature_group_uuid(featureGroupId); + return licenseFlavor; } - return Optional.empty(); - } - private Optional<VendorInfo> getVendorInfo(String vlmId, String vendorModel, Version version, - String featureGroupId) { - VendorInfo vendorInfo = null; - FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId); - if (Objects.nonNull(featureGroup)) { - //Process Feature group to get Manufacturer ref no. - String manufacturerReferenceNumber = featureGroup.getEntityManufacturerReferenceNumber(); - vendorInfo = new VendorInfo(); - vendorInfo.setVendor_model(vendorModel); - if (Objects.nonNull(manufacturerReferenceNumber)) { - vendorInfo.setManufacturer_reference_number(manufacturerReferenceNumber); - } + private Optional<String> getPartNumber(String vlmId, Version version, String featureGroupId) { + FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId); + if (Objects.nonNull(featureGroup)) { + return Optional.ofNullable(featureGroup.getFeatureGroup().getPartNumber()); + } + return Optional.empty(); } - return Optional.ofNullable(vendorInfo); - } - private Optional<ComputeFlavor> getComputeFlavor(String vspId, Version version, - String componentId, String computeFlavorId) { - ComputeFlavor computeFlavor = null; - ComputeEntity computeQuestionnaire; - try { - computeQuestionnaire = computeDao.getQuestionnaireData(vspId, version, componentId, - computeFlavorId); - } catch (Exception ex) { - log.warn("Failed to get QuestionnaireData from computeDao," + - " initializing computeQuestionnaire to null", ex); - computeQuestionnaire = null; + private Optional<VendorInfo> getVendorInfo(String vlmId, String vendorModel, Version version, String featureGroupId) { + VendorInfo vendorInfo = null; + FeatureGroupModel featureGroup = getFeatureGroup(vlmId, version, featureGroupId); + if (Objects.nonNull(featureGroup)) { + //Process Feature group to get Manufacturer ref no. + String manufacturerReferenceNumber = featureGroup.getEntityManufacturerReferenceNumber(); + vendorInfo = new VendorInfo(); + vendorInfo.setVendor_model(vendorModel); + if (Objects.nonNull(manufacturerReferenceNumber)) { + vendorInfo.setManufacturer_reference_number(manufacturerReferenceNumber); + } + } + return Optional.ofNullable(vendorInfo); } - if (Objects.nonNull(computeQuestionnaire)) { - String computeQuestionnaireData = computeQuestionnaire.getQuestionnaireData(); - if (Objects.nonNull(computeQuestionnaireData)) { - Compute compute; + + private Optional<ComputeFlavor> getComputeFlavor(String vspId, Version version, String componentId, String computeFlavorId) { + ComputeFlavor computeFlavor = null; + ComputeEntity computeQuestionnaire; try { - compute = JsonUtil.json2Object(computeQuestionnaireData, Compute.class); + computeQuestionnaire = computeDao.getQuestionnaireData(vspId, version, componentId, computeFlavorId); } catch (Exception ex) { - log.warn("Failed to convert json value to compute object," + - "initializing compute to null", ex); - compute = null; + log.warn("Failed to get QuestionnaireData from computeDao," + " initializing computeQuestionnaire to null", ex); + computeQuestionnaire = null; } - if (compute != null && Objects.nonNull(compute.getVmSizing())) { - computeFlavor = new ComputeFlavor(); - if (Objects.nonNull(compute.getVmSizing().getNumOfCPUs())) { - computeFlavor.setNum_cpus(compute.getVmSizing().getNumOfCPUs()); - } - if (Objects.nonNull(compute.getVmSizing().getFileSystemSizeGB())) { - computeFlavor.setDisk_size(compute.getVmSizing().getFileSystemSizeGB() + " GB"); - } - if (Objects.nonNull(compute.getVmSizing().getMemoryRAM())) { - computeFlavor.setMem_size(compute.getVmSizing().getMemoryRAM()); - } + if (Objects.nonNull(computeQuestionnaire)) { + String computeQuestionnaireData = computeQuestionnaire.getQuestionnaireData(); + if (Objects.nonNull(computeQuestionnaireData)) { + Compute compute; + try { + compute = JsonUtil.json2Object(computeQuestionnaireData, Compute.class); + } catch (Exception ex) { + log.warn("Failed to convert json value to compute object," + "initializing compute to null", ex); + compute = null; + } + if (compute != null && Objects.nonNull(compute.getVmSizing())) { + computeFlavor = new ComputeFlavor(); + if (Objects.nonNull(compute.getVmSizing().getNumOfCPUs())) { + computeFlavor.setNum_cpus(compute.getVmSizing().getNumOfCPUs()); + } + if (Objects.nonNull(compute.getVmSizing().getFileSystemSizeGB())) { + computeFlavor.setDisk_size(compute.getVmSizing().getFileSystemSizeGB() + " GB"); + } + if (Objects.nonNull(compute.getVmSizing().getMemoryRAM())) { + computeFlavor.setMem_size(compute.getVmSizing().getMemoryRAM()); + } + } + } } - } + return Optional.ofNullable(computeFlavor); } - return Optional.ofNullable(computeFlavor); - } - private FeatureGroupModel getFeatureGroup(String vlmId, Version version, String featureGroupId) { - FeatureGroupEntity fgInput = new FeatureGroupEntity(); - fgInput.setVendorLicenseModelId(vlmId); - fgInput.setVersion(version); - fgInput.setId(featureGroupId); - return vendorLicenseFacade.getFeatureGroupModel(fgInput); - } - - private Optional<String> getComponentName(String vspId, Version version, String componentId) { + private FeatureGroupModel getFeatureGroup(String vlmId, Version version, String featureGroupId) { + FeatureGroupEntity fgInput = new FeatureGroupEntity(); + fgInput.setVendorLicenseModelId(vlmId); + fgInput.setVersion(version); + fgInput.setId(featureGroupId); + return vendorLicenseFacade.getFeatureGroupModel(fgInput); + } - ComponentEntity componentEntity = - componentDao.get(new ComponentEntity(vspId, version, componentId)); - if (Objects.nonNull(componentEntity) - && Objects.nonNull(componentEntity.getComponentCompositionData())) { - ComponentData componentCompositionData = componentEntity.getComponentCompositionData(); - return Optional.ofNullable(componentCompositionData.getDisplayName()); + private Optional<String> getComponentName(String vspId, Version version, String componentId) { + ComponentEntity componentEntity = componentDao.get(new ComponentEntity(vspId, version, componentId)); + if (Objects.nonNull(componentEntity) && Objects.nonNull(componentEntity.getComponentCompositionData())) { + ComponentData componentCompositionData = componentEntity.getComponentCompositionData(); + return Optional.ofNullable(componentCompositionData.getDisplayName()); + } + return Optional.empty(); } - return Optional.empty(); - } - private List<MultiFlavorVfcImage> getComponentImages(String vspId, Version version, - String componentId) { - List<MultiFlavorVfcImage> multiFlavorVfcImages = new ArrayList<>(); - MultiFlavorVfcImage multiFlavorVfcImage; - Collection<ImageEntity> componentImages = - imageDao.list(new ImageEntity(vspId, version, componentId, null)); - if (Objects.nonNull(componentImages)) { - for (ImageEntity componentImage : componentImages) { - ImageEntity imageEntity = imageDao.get(componentImage); - ImageEntity imageQuestionnaireDataEntity = imageDao.getQuestionnaireData(vspId, version, - componentId, componentImage.getId()); - Image imageCompositionData = imageEntity.getImageCompositionData(); - if (Objects.nonNull(imageEntity) - && Objects.nonNull(imageQuestionnaireDataEntity) - && Objects.nonNull(imageCompositionData)) { - ImageDetails imageDetails; - try { - imageDetails = JsonUtil.json2Object(imageQuestionnaireDataEntity - .getQuestionnaireData(), ImageDetails.class); - } catch (Exception ex) { - log.warn("Failed to convert json value to ImageDetails object," + - "initializing imageDetails to null", ex); - imageDetails = null; - } - if (Objects.nonNull(imageDetails) - && Objects.nonNull(imageDetails.getVersion())) { - //Image version is used as a key for the image block - //So excluding the population if questionnaire data is absent or invalid - multiFlavorVfcImage = new MultiFlavorVfcImage(); - multiFlavorVfcImage.setSoftware_version(imageDetails.getVersion()); - if (Objects.nonNull(imageCompositionData.getFileName())) { - multiFlavorVfcImage.setFile_name(imageCompositionData.getFileName()); - } - if (Objects.nonNull(imageDetails.getMd5())) { - multiFlavorVfcImage.setFile_hash(imageDetails.getMd5()); + private List<MultiFlavorVfcImage> getComponentImages(String vspId, Version version, String componentId) { + List<MultiFlavorVfcImage> multiFlavorVfcImages = new ArrayList<>(); + MultiFlavorVfcImage multiFlavorVfcImage; + Collection<ImageEntity> componentImages = imageDao.list(new ImageEntity(vspId, version, componentId, null)); + if (Objects.nonNull(componentImages)) { + for (ImageEntity componentImage : componentImages) { + ImageEntity imageEntity = imageDao.get(componentImage); + ImageEntity imageQuestionnaireDataEntity = imageDao.getQuestionnaireData(vspId, version, componentId, componentImage.getId()); + Image imageCompositionData = imageEntity.getImageCompositionData(); + if (Objects.nonNull(imageEntity) && Objects.nonNull(imageQuestionnaireDataEntity) && Objects.nonNull(imageCompositionData)) { + ImageDetails imageDetails; + try { + imageDetails = JsonUtil.json2Object(imageQuestionnaireDataEntity.getQuestionnaireData(), ImageDetails.class); + } catch (Exception ex) { + log.warn("Failed to convert json value to ImageDetails object," + "initializing imageDetails to null", ex); + imageDetails = null; + } + if (Objects.nonNull(imageDetails) && Objects.nonNull(imageDetails.getVersion())) { + //Image version is used as a key for the image block + + //So excluding the population if questionnaire data is absent or invalid + multiFlavorVfcImage = new MultiFlavorVfcImage(); + multiFlavorVfcImage.setSoftware_version(imageDetails.getVersion()); + if (Objects.nonNull(imageCompositionData.getFileName())) { + multiFlavorVfcImage.setFile_name(imageCompositionData.getFileName()); + } + if (Objects.nonNull(imageDetails.getMd5())) { + multiFlavorVfcImage.setFile_hash(imageDetails.getMd5()); + } + multiFlavorVfcImage.setFile_hash_type("md5"); + multiFlavorVfcImages.add(multiFlavorVfcImage); + } + } } - multiFlavorVfcImage.setFile_hash_type("md5"); - multiFlavorVfcImages.add(multiFlavorVfcImage); - } } - } + return multiFlavorVfcImages; } - return multiFlavorVfcImages; - } } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java index 00781c1512..15289025b1 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java @@ -13,10 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorsoftwareproduct.services.impl.composition; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.onap.sdc.tosca.datatypes.model.NodeTemplate; @@ -27,7 +33,11 @@ import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -import org.openecomp.sdc.tosca.datatypes.*; +import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType; +import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; +import org.openecomp.sdc.tosca.datatypes.ToscaNodeType; +import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.errors.ToscaInvalidEntryNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstituteNodeTemplateErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaMissingSubstitutionMappingForReqCapErrorBuilder; @@ -35,427 +45,343 @@ import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor; -import org.openecomp.sdc.vendorsoftwareproduct.types.composition.*; - -import java.util.*; -import java.util.stream.Collectors; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ExtractCompositionDataContext; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic; public class CompositionDataExtractorImpl implements CompositionDataExtractor { - protected static Logger logger; - private static ToscaAnalyzerService toscaAnalyzerService; - static { - logger = LoggerFactory.getLogger(CompositionDataExtractorImpl.class); - toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); - } + protected static Logger logger; + private static ToscaAnalyzerService toscaAnalyzerService; - /** - * Extract service composition data composition data. - * - * @param toscaServiceModel the tosca service model - * @return the composition data - */ - public CompositionData extractServiceCompositionData(ToscaServiceModel toscaServiceModel) { - ExtractCompositionDataContext context = new ExtractCompositionDataContext(); - String entryDefinitionServiceTemplateFileName = - toscaServiceModel.getEntryDefinitionServiceTemplate(); - ServiceTemplate entryDefinitionServiceTemplate = - toscaServiceModel.getServiceTemplates().get(entryDefinitionServiceTemplateFileName); - extractServiceCompositionData(entryDefinitionServiceTemplateFileName, - entryDefinitionServiceTemplate, toscaServiceModel, context); - - CompositionData compositionData = new CompositionData(); - compositionData.setNetworks(context.getNetworks()); - compositionData.setComponents(context.getComponents()); - return compositionData; - } - - private void extractServiceCompositionData(String serviceTemplateFileName, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - ExtractCompositionDataContext context) { - if (context.getHandledServiceTemplates().contains(serviceTemplateFileName)) { - return; + static { + logger = LoggerFactory.getLogger(CompositionDataExtractorImpl.class); + toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); } - context.addNetworks(extractNetworks(serviceTemplate, toscaServiceModel)); - extractComponents(serviceTemplate, toscaServiceModel, context); - handleSubstitution(serviceTemplate, toscaServiceModel, context); - context.addHandledServiceTemplates(serviceTemplateFileName); - } - private void handleSubstitution(ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - ExtractCompositionDataContext context) { - Map<String, NodeTemplate> substitutableNodeTemplates = - toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplate); - - if (substitutableNodeTemplates != null) { - for (String substitutableNodeTemplateId : substitutableNodeTemplates.keySet()) { - handleSubstitutableNodeTemplate(serviceTemplate, toscaServiceModel, - substitutableNodeTemplateId, - substitutableNodeTemplates.get(substitutableNodeTemplateId), context); - } + /** + * Extract service composition data composition data. + * + * @param toscaServiceModel the tosca service model + * @return the composition data + */ + public CompositionData extractServiceCompositionData(ToscaServiceModel toscaServiceModel) { + ExtractCompositionDataContext context = new ExtractCompositionDataContext(); + String entryDefinitionServiceTemplateFileName = toscaServiceModel.getEntryDefinitionServiceTemplate(); + ServiceTemplate entryDefinitionServiceTemplate = toscaServiceModel.getServiceTemplates().get(entryDefinitionServiceTemplateFileName); + extractServiceCompositionData(entryDefinitionServiceTemplateFileName, entryDefinitionServiceTemplate, toscaServiceModel, context); + CompositionData compositionData = new CompositionData(); + compositionData.setNetworks(context.getNetworks()); + compositionData.setComponents(context.getComponents()); + return compositionData; } - } - private void handleSubstitutableNodeTemplate(ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - String substitutableNodeTemplateId, - NodeTemplate substitutableNodeTemplate, - ExtractCompositionDataContext context) { - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - Optional<String> substituteServiceTemplateFileName = toscaAnalyzerService - .getSubstituteServiceTemplateName(substitutableNodeTemplateId, substitutableNodeTemplate); - if (!substituteServiceTemplateFileName.isPresent()) { - throw new CoreException( - new ToscaInvalidSubstituteNodeTemplateErrorBuilder(substitutableNodeTemplateId).build()); - } - if (context.getHandledServiceTemplates().contains(substituteServiceTemplateFileName.get())) { - //each substitution is should be handled once, and will get the connection to the upper - // service level according to the first one which was processed - return; - } - - ServiceTemplate substituteServiceTemplate = - toscaServiceModel.getServiceTemplates().get(substituteServiceTemplateFileName.get()); - extractServiceCompositionData(substituteServiceTemplateFileName.get(), - substituteServiceTemplate, toscaServiceModel, context); - - List<Map<String, RequirementAssignment>> substitutableRequirements = - substitutableNodeTemplate.getRequirements(); - - if (CollectionUtils.isEmpty(substitutableRequirements)) { - return; + private void extractServiceCompositionData(String serviceTemplateFileName, ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel, + ExtractCompositionDataContext context) { + if (context.getHandledServiceTemplates().contains(serviceTemplateFileName)) { + return; + } + context.addNetworks(extractNetworks(serviceTemplate, toscaServiceModel)); + extractComponents(serviceTemplate, toscaServiceModel, context); + handleSubstitution(serviceTemplate, toscaServiceModel, context); + context.addHandledServiceTemplates(serviceTemplateFileName); } - for (Map<String, RequirementAssignment> substitutableReq : substitutableRequirements) { - substitutableReq.keySet().stream().filter(reqId -> { - RequirementAssignment reqAssignment = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)), - RequirementAssignment.class); - return isLinkToNetworkRequirementAssignment(reqAssignment); - }).forEach(reqId -> { - RequirementAssignment linkToNetworkRequirement = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)), - RequirementAssignment.class); - String connectedNodeId = linkToNetworkRequirement.getNode(); - Optional<NodeTemplate> connectedNodeTemplate = - toscaAnalyzerService.getNodeTemplateById(serviceTemplate, connectedNodeId); - - if (connectedNodeTemplate.isPresent() && toscaAnalyzerService - .isTypeOf(connectedNodeTemplate.get(), ToscaNodeType.NATIVE_NETWORK, - serviceTemplate, toscaServiceModel)) { - Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService - .getSubstitutionMappedNodeTemplateByExposedReq( - substituteServiceTemplateFileName.get(), substituteServiceTemplate, reqId); - if (!mappedNodeTemplate.isPresent()) { - throw new CoreException(new ToscaMissingSubstitutionMappingForReqCapErrorBuilder( - ToscaMissingSubstitutionMappingForReqCapErrorBuilder.MappingExposedEntry - .REQUIREMENT, connectedNodeId).build()); - } - - if (toscaAnalyzerService.isTypeOf(mappedNodeTemplate.get().getValue(), - ToscaNodeType.NATIVE_NETWORK_PORT, serviceTemplate, - toscaServiceModel)) { - Nic port = context.getNics().get(mappedNodeTemplate.get().getKey()); - if (port != null) { - port.setNetworkName(connectedNodeId); - } else { - logger.warn( - "Different ports define for the same component which is used in different " - + "substitution service templates."); + private void handleSubstitution(ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel, ExtractCompositionDataContext context) { + Map<String, NodeTemplate> substitutableNodeTemplates = toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplate); + if (substitutableNodeTemplates != null) { + for (String substitutableNodeTemplateId : substitutableNodeTemplates.keySet()) { + handleSubstitutableNodeTemplate(serviceTemplate, toscaServiceModel, substitutableNodeTemplateId, + substitutableNodeTemplates.get(substitutableNodeTemplateId), context); } - } - } else if (!connectedNodeTemplate.isPresent()) { - throw new CoreException( - new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", connectedNodeId).build()); } - }); } - } - private boolean isLinkToNetworkRequirementAssignment(RequirementAssignment requirement) { - return toscaAnalyzerService.isDesiredRequirementAssignment(requirement, - ToscaCapabilityType.NATIVE_NETWORK_LINKABLE, null, - ToscaRelationshipType.NATIVE_NETWORK_LINK_TO); - } - - - private void connectPortToNetwork(Nic port, NodeTemplate portNodeTemplate) { - List<RequirementAssignment> linkRequirementsToNetwork = - toscaAnalyzerService.getRequirements(portNodeTemplate, ToscaConstants.LINK_REQUIREMENT_ID); + private void handleSubstitutableNodeTemplate(ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel, + String substitutableNodeTemplateId, NodeTemplate substitutableNodeTemplate, + ExtractCompositionDataContext context) { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + Optional<String> substituteServiceTemplateFileName = toscaAnalyzerService + .getSubstituteServiceTemplateName(substitutableNodeTemplateId, substitutableNodeTemplate); + if (!substituteServiceTemplateFileName.isPresent()) { + throw new CoreException(new ToscaInvalidSubstituteNodeTemplateErrorBuilder(substitutableNodeTemplateId).build()); + } + if (context.getHandledServiceTemplates().contains(substituteServiceTemplateFileName.get())) { + //each substitution is should be handled once, and will get the connection to the upper - //port is connected to one network - for (RequirementAssignment linkRequirementToNetwork : linkRequirementsToNetwork) { - port.setNetworkName(linkRequirementToNetwork.getNode()); + // service level according to the first one which was processed + return; + } + ServiceTemplate substituteServiceTemplate = toscaServiceModel.getServiceTemplates().get(substituteServiceTemplateFileName.get()); + extractServiceCompositionData(substituteServiceTemplateFileName.get(), substituteServiceTemplate, toscaServiceModel, context); + List<Map<String, RequirementAssignment>> substitutableRequirements = substitutableNodeTemplate.getRequirements(); + if (CollectionUtils.isEmpty(substitutableRequirements)) { + return; + } + for (Map<String, RequirementAssignment> substitutableReq : substitutableRequirements) { + substitutableReq.keySet().stream().filter(reqId -> { + RequirementAssignment reqAssignment = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)), RequirementAssignment.class); + return isLinkToNetworkRequirementAssignment(reqAssignment); + }).forEach(reqId -> { + RequirementAssignment linkToNetworkRequirement = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)), RequirementAssignment.class); + String connectedNodeId = linkToNetworkRequirement.getNode(); + Optional<NodeTemplate> connectedNodeTemplate = toscaAnalyzerService.getNodeTemplateById(serviceTemplate, connectedNodeId); + if (connectedNodeTemplate.isPresent() && toscaAnalyzerService + .isTypeOf(connectedNodeTemplate.get(), ToscaNodeType.NATIVE_NETWORK, serviceTemplate, toscaServiceModel)) { + Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService + .getSubstitutionMappedNodeTemplateByExposedReq(substituteServiceTemplateFileName.get(), substituteServiceTemplate, reqId); + if (!mappedNodeTemplate.isPresent()) { + throw new CoreException(new ToscaMissingSubstitutionMappingForReqCapErrorBuilder( + ToscaMissingSubstitutionMappingForReqCapErrorBuilder.MappingExposedEntry.REQUIREMENT, connectedNodeId).build()); + } + if (toscaAnalyzerService + .isTypeOf(mappedNodeTemplate.get().getValue(), ToscaNodeType.NATIVE_NETWORK_PORT, serviceTemplate, toscaServiceModel)) { + Nic port = context.getNics().get(mappedNodeTemplate.get().getKey()); + if (port != null) { + port.setNetworkName(connectedNodeId); + } else { + logger.warn( + "Different ports define for the same component which is used in different " + "substitution service templates."); + } + } + } else if (!connectedNodeTemplate.isPresent()) { + throw new CoreException(new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", connectedNodeId).build()); + } + }); + } } - } - /* - return Map with key - compute node template id, value - list of connected port node template id - */ - private Map<String, List<String>> getComputeToPortsConnection( - Map<String, NodeTemplate> portNodeTemplates) { - Map<String, List<String>> computeToPortConnection = new HashMap<>(); - if (MapUtils.isEmpty(portNodeTemplates)) { - return computeToPortConnection; + private boolean isLinkToNetworkRequirementAssignment(RequirementAssignment requirement) { + return toscaAnalyzerService.isDesiredRequirementAssignment(requirement, ToscaCapabilityType.NATIVE_NETWORK_LINKABLE, null, + ToscaRelationshipType.NATIVE_NETWORK_LINK_TO); } - for (String portId : portNodeTemplates.keySet()) { - List<RequirementAssignment> bindingRequirementsToCompute = toscaAnalyzerService - .getRequirements(portNodeTemplates.get(portId), ToscaConstants.BINDING_REQUIREMENT_ID); - for (RequirementAssignment bindingRequirementToCompute : bindingRequirementsToCompute) { - computeToPortConnection - .putIfAbsent(bindingRequirementToCompute.getNode(), new ArrayList<>()); - computeToPortConnection.get(bindingRequirementToCompute.getNode()).add(portId); - } - } - return computeToPortConnection; - } - private void extractComponents(ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - ExtractCompositionDataContext context) { - Map<String, NodeTemplate> computeNodeTemplates = toscaAnalyzerService - .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_COMPUTE, - toscaServiceModel); - if (MapUtils.isEmpty(computeNodeTemplates)) { - return; + private void connectPortToNetwork(Nic port, NodeTemplate portNodeTemplate) { + List<RequirementAssignment> linkRequirementsToNetwork = toscaAnalyzerService + .getRequirements(portNodeTemplate, ToscaConstants.LINK_REQUIREMENT_ID); + //port is connected to one network + for (RequirementAssignment linkRequirementToNetwork : linkRequirementsToNetwork) { + port.setNetworkName(linkRequirementToNetwork.getNode()); + } } - Map<String, List<String>> imageNodeTemplates = getComponentImages(computeNodeTemplates, - toscaServiceModel); - Map<String, List<String>> computeFlavorNodeTemplates = - getComponentComputeFlavor(computeNodeTemplates, toscaServiceModel); - Map<String, NodeTemplate> portNodeTemplates = toscaAnalyzerService - .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK_PORT, - toscaServiceModel); - Map<String, List<String>> computeToPortsConnection = - getComputeToPortsConnection(portNodeTemplates); - Map<String, List<String>> computesGroupedByType = - getNodeTemplatesGroupedByType(computeNodeTemplates); - computesGroupedByType.keySet() - .stream() - .filter(nodeType -> - !context.getCreatedComponents().contains(nodeType)) - .forEach(nodeType -> extractComponent(serviceTemplate, computeToPortsConnection, - computesGroupedByType, imageNodeTemplates, computeFlavorNodeTemplates, nodeType, - context)); - } - - private Map<String,List<String>> getComponentImages(Map<String, NodeTemplate> - computeNodeTemplates, - ToscaServiceModel toscaServiceModel) { - return getComponentProperty(ToscaConstants.COMPUTE_IMAGE, computeNodeTemplates, toscaServiceModel); - } - - private Map<String,List<String>> getComponentComputeFlavor(Map<String, NodeTemplate> - computeNodeTemplates, - ToscaServiceModel toscaServiceModel) { - return getComponentProperty(ToscaConstants.COMPUTE_FLAVOR, computeNodeTemplates, toscaServiceModel); - } - - private Map<String, List<String>> getComponentProperty(String propertyName, - Map<String, NodeTemplate> computeNodeTemplates, - ToscaServiceModel toscaServiceModel) { - Map<String,List<String>> componentPropertyValues = new HashMap<>(); - for (String component : computeNodeTemplates.keySet()) { - List<String> computes = new ArrayList<>(); - Map<String,Object> properties = computeNodeTemplates.get(component).getProperties(); - - if(MapUtils.isEmpty(properties)){ - continue; - } - - List<Object> computesList = properties.entrySet() - .stream() - .filter(map -> map.getKey().equals(propertyName)) - .map(Map.Entry::getValue) - .collect(Collectors.toList()); - for (Object obj : computesList) { - if (obj instanceof String) { - computes.add((String) obj); - } else { - Map<String, String> objMap = new ObjectMapper().convertValue(obj, Map.class); - computes.add(getInputs(toscaServiceModel, objMap.get("get_input"))); + /* + return Map with key - compute node template id, value - list of connected port node template id + */ + private Map<String, List<String>> getComputeToPortsConnection(Map<String, NodeTemplate> portNodeTemplates) { + Map<String, List<String>> computeToPortConnection = new HashMap<>(); + if (MapUtils.isEmpty(portNodeTemplates)) { + return computeToPortConnection; } - } - componentPropertyValues.put(component,computes); + for (String portId : portNodeTemplates.keySet()) { + List<RequirementAssignment> bindingRequirementsToCompute = toscaAnalyzerService + .getRequirements(portNodeTemplates.get(portId), ToscaConstants.BINDING_REQUIREMENT_ID); + for (RequirementAssignment bindingRequirementToCompute : bindingRequirementsToCompute) { + computeToPortConnection.putIfAbsent(bindingRequirementToCompute.getNode(), new ArrayList<>()); + computeToPortConnection.get(bindingRequirementToCompute.getNode()).add(portId); + } + } + return computeToPortConnection; } - return componentPropertyValues; - } - - private String getInputs(ToscaServiceModel toscaServiceModel, String inputValue) { - String mainTemplate = toscaServiceModel.getEntryDefinitionServiceTemplate(); - List<ServiceTemplate> toscaServiceTemplates = toscaServiceModel.getServiceTemplates().entrySet() - .stream() - .filter(map -> map.getKey().equals(mainTemplate)) - .map(Map.Entry::getValue) - .collect(Collectors.toList()); - ServiceTemplate serviceTemplate = toscaServiceTemplates.get(0); - if (Objects.nonNull(serviceTemplate.getTopology_template()) - && MapUtils.isNotEmpty(serviceTemplate.getTopology_template().getInputs())) { - for (Map.Entry<String, ParameterDefinition> inputEntry : serviceTemplate - .getTopology_template().getInputs().entrySet()) { - if (inputEntry.getKey().equals(inputValue)) { - String value; - try { - value= (String) inputEntry.getValue().get_default(); - } catch (Exception e) { - logger.debug(e.getMessage(), e); - value = inputEntry.getValue().get_default().toString(); - } - return value; + private void extractComponents(ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel, ExtractCompositionDataContext context) { + Map<String, NodeTemplate> computeNodeTemplates = toscaAnalyzerService + .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_COMPUTE, toscaServiceModel); + if (MapUtils.isEmpty(computeNodeTemplates)) { + return; } - } + Map<String, List<String>> imageNodeTemplates = getComponentImages(computeNodeTemplates, toscaServiceModel); + Map<String, List<String>> computeFlavorNodeTemplates = getComponentComputeFlavor(computeNodeTemplates, toscaServiceModel); + Map<String, NodeTemplate> portNodeTemplates = toscaAnalyzerService + .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK_PORT, toscaServiceModel); + Map<String, List<String>> computeToPortsConnection = getComputeToPortsConnection(portNodeTemplates); + Map<String, List<String>> computesGroupedByType = getNodeTemplatesGroupedByType(computeNodeTemplates); + computesGroupedByType.keySet().stream().filter(nodeType -> !context.getCreatedComponents().contains(nodeType)).forEach( + nodeType -> extractComponent(serviceTemplate, computeToPortsConnection, computesGroupedByType, imageNodeTemplates, + computeFlavorNodeTemplates, nodeType, context)); } - return inputValue; - } - - private void extractComponent(ServiceTemplate serviceTemplate, - Map<String, List<String>> computeToPortsConnection, - Map<String, List<String>> computesGroupedByType, - Map<String, List<String>> imageList, - Map<String, List<String>> computeFlavorNodeTemplates, - String computeNodeType, - ExtractCompositionDataContext context) { - ComponentData component = new ComponentData(); - component.setName(computeNodeType); - component.setDisplayName(getComponentDisplayName(component.getName())); - Component componentModel = new Component(); - componentModel.setData(component); - - String computeId = computesGroupedByType.get(computeNodeType).get(0); - List<String> connectedPortIds = computeToPortsConnection.get(computeId); - List<String> images = imageList.get(computeId); - List<String> computeFlavors = computeFlavorNodeTemplates.get(computeId); - if (CollectionUtils.isNotEmpty(connectedPortIds)) { - componentModel.setNics(new ArrayList<>()); - componentModel.setImages(new ArrayList<>()); - componentModel.setCompute(new ArrayList<>()); - - connectedPortIds.forEach(portId -> { - Nic port = extractPort(serviceTemplate, portId); - componentModel.getNics().add(port); - context.addNic(portId, port); - }); - - if (CollectionUtils.isNotEmpty(images)) { - images.forEach(image -> { - Image img = new Image(image); - componentModel.getImages().add(img); - context.addImage(image, img); - }); - } - - if (CollectionUtils.isNotEmpty(computeFlavors)) { - computeFlavors.forEach(flavor -> { - ComputeData computeFlavor = new ComputeData(flavor); - componentModel.getCompute().add(computeFlavor); - context.addCompute(flavor, computeFlavor); - }); - } + private Map<String, List<String>> getComponentImages(Map<String, NodeTemplate> computeNodeTemplates, ToscaServiceModel toscaServiceModel) { + return getComponentProperty(ToscaConstants.COMPUTE_IMAGE, computeNodeTemplates, toscaServiceModel); } - context.addComponent(componentModel); - context.getCreatedComponents().add(computeNodeType); - } - private Nic extractPort(ServiceTemplate serviceTemplate, String portNodeTemplateId) { - Optional<NodeTemplate> portNodeTemplate = - toscaAnalyzerService.getNodeTemplateById(serviceTemplate, portNodeTemplateId); - if (portNodeTemplate.isPresent()) { - Nic port = new Nic(); - port.setName(portNodeTemplateId); - connectPortToNetwork(port, portNodeTemplate.get()); - return port; - } else { - throw new CoreException( - new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", portNodeTemplateId).build()); + private Map<String, List<String>> getComponentComputeFlavor(Map<String, NodeTemplate> computeNodeTemplates, ToscaServiceModel toscaServiceModel) { + return getComponentProperty(ToscaConstants.COMPUTE_FLAVOR, computeNodeTemplates, toscaServiceModel); } - } - private Map<String, List<String>> getNodeTemplatesGroupedByType( - Map<String, NodeTemplate> nodeTemplates) { - Map<String, List<String>> nodeTemplatesGrouped = - new HashMap<>(); //key - node type, value - list of node ids with this type - for (String nodeId : nodeTemplates.keySet()) { - String nodeType = nodeTemplates.get(nodeId).getType(); - nodeTemplatesGrouped.putIfAbsent(nodeType, new ArrayList<>()); - nodeTemplatesGrouped.get(nodeType).add(nodeId); + private Map<String, List<String>> getComponentProperty(String propertyName, Map<String, NodeTemplate> computeNodeTemplates, + ToscaServiceModel toscaServiceModel) { + Map<String, List<String>> componentPropertyValues = new HashMap<>(); + for (String component : computeNodeTemplates.keySet()) { + List<String> computes = new ArrayList<>(); + Map<String, Object> properties = computeNodeTemplates.get(component).getProperties(); + if (MapUtils.isEmpty(properties)) { + continue; + } + List<Object> computesList = properties.entrySet().stream().filter(map -> map.getKey().equals(propertyName)).map(Map.Entry::getValue) + .collect(Collectors.toList()); + for (Object obj : computesList) { + if (obj instanceof String) { + computes.add((String) obj); + } else { + Map<String, String> objMap = new ObjectMapper().convertValue(obj, Map.class); + computes.add(getInputs(toscaServiceModel, objMap.get("get_input"))); + } + } + componentPropertyValues.put(component, computes); + } + return componentPropertyValues; } - return nodeTemplatesGrouped; - } - private List<Network> extractNetworks(ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel) { - List<Network> networks = new ArrayList<>(); - Map<String, NodeTemplate> networkNodeTemplates = toscaAnalyzerService - .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK, - toscaServiceModel); - if (MapUtils.isEmpty(networkNodeTemplates)) { - return networks; - } - for (String networkId : networkNodeTemplates.keySet()) { - Network network = new Network(); - network.setName(networkId); - Optional<Boolean> networkDhcpValue = - getNetworkDhcpValue(serviceTemplate, networkNodeTemplates.get(networkId)); - network.setDhcp(networkDhcpValue.orElse(true)); - networks.add(network); + private String getInputs(ToscaServiceModel toscaServiceModel, String inputValue) { + String mainTemplate = toscaServiceModel.getEntryDefinitionServiceTemplate(); + List<ServiceTemplate> toscaServiceTemplates = toscaServiceModel.getServiceTemplates().entrySet().stream() + .filter(map -> map.getKey().equals(mainTemplate)).map(Map.Entry::getValue).collect(Collectors.toList()); + ServiceTemplate serviceTemplate = toscaServiceTemplates.get(0); + if (Objects.nonNull(serviceTemplate.getTopology_template()) && MapUtils.isNotEmpty(serviceTemplate.getTopology_template().getInputs())) { + for (Map.Entry<String, ParameterDefinition> inputEntry : serviceTemplate.getTopology_template().getInputs().entrySet()) { + if (inputEntry.getKey().equals(inputValue)) { + String value; + try { + value = (String) inputEntry.getValue().get_default(); + } catch (Exception e) { + logger.debug(e.getMessage(), e); + value = inputEntry.getValue().get_default().toString(); + } + return value; + } + } + } + return inputValue; } - return networks; - } - //dhcp default value is true - private Optional<Boolean> getNetworkDhcpValue(ServiceTemplate serviceTemplate, - NodeTemplate networkNodeTemplate) { - if (networkNodeTemplate == null) { - return Optional.empty(); - } - if (networkNodeTemplate.getProperties() == null - || networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME) - == null) { - return Optional.of(true); + private void extractComponent(ServiceTemplate serviceTemplate, Map<String, List<String>> computeToPortsConnection, + Map<String, List<String>> computesGroupedByType, Map<String, List<String>> imageList, + Map<String, List<String>> computeFlavorNodeTemplates, String computeNodeType, + ExtractCompositionDataContext context) { + ComponentData component = new ComponentData(); + component.setName(computeNodeType); + component.setDisplayName(getComponentDisplayName(component.getName())); + Component componentModel = new Component(); + componentModel.setData(component); + String computeId = computesGroupedByType.get(computeNodeType).get(0); + List<String> connectedPortIds = computeToPortsConnection.get(computeId); + List<String> images = imageList.get(computeId); + List<String> computeFlavors = computeFlavorNodeTemplates.get(computeId); + if (CollectionUtils.isNotEmpty(connectedPortIds)) { + componentModel.setNics(new ArrayList<>()); + componentModel.setImages(new ArrayList<>()); + componentModel.setCompute(new ArrayList<>()); + connectedPortIds.forEach(portId -> { + Nic port = extractPort(serviceTemplate, portId); + componentModel.getNics().add(port); + context.addNic(portId, port); + }); + if (CollectionUtils.isNotEmpty(images)) { + images.forEach(image -> { + Image img = new Image(image); + componentModel.getImages().add(img); + context.addImage(image, img); + }); + } + if (CollectionUtils.isNotEmpty(computeFlavors)) { + computeFlavors.forEach(flavor -> { + ComputeData computeFlavor = new ComputeData(flavor); + componentModel.getCompute().add(computeFlavor); + context.addCompute(flavor, computeFlavor); + }); + } + } + context.addComponent(componentModel); + context.getCreatedComponents().add(computeNodeType); } - Object dhcp = - networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME); - if (dhcp instanceof String) { - return Optional.of(Boolean.valueOf((String) dhcp)); - } else if (dhcp instanceof Boolean) { - return Optional.of((Boolean) dhcp); - } else if (dhcp instanceof Map) { - String inputParameterName = - (String) ((Map) dhcp).get(ToscaFunctions.GET_INPUT.getFunctionName()); - if (inputParameterName != null) { - ParameterDefinition inputParameterDefinition = - serviceTemplate.getTopology_template().getInputs().get(inputParameterName); - if (inputParameterDefinition != null) { - if (inputParameterDefinition.get_default() != null) { - return Optional.of(Boolean.valueOf(inputParameterDefinition.get_default().toString())); - } + private Nic extractPort(ServiceTemplate serviceTemplate, String portNodeTemplateId) { + Optional<NodeTemplate> portNodeTemplate = toscaAnalyzerService.getNodeTemplateById(serviceTemplate, portNodeTemplateId); + if (portNodeTemplate.isPresent()) { + Nic port = new Nic(); + port.setName(portNodeTemplateId); + connectPortToNetwork(port, portNodeTemplate.get()); + return port; } else { - throw new CoreException( - new ToscaInvalidEntryNotFoundErrorBuilder("Input Parameter", inputParameterName) - .build()); + throw new CoreException(new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", portNodeTemplateId).build()); } - } } - return Optional.of(true); - } - @Override - public String getComponentDisplayName(String componentName) { - if (componentName == null) { - return null; + private Map<String, List<String>> getNodeTemplatesGroupedByType(Map<String, NodeTemplate> nodeTemplates) { + Map<String, List<String>> nodeTemplatesGrouped = + new HashMap<>(); //key - node type, value - list of node ids with this type + for (String nodeId : nodeTemplates.keySet()) { + String nodeType = nodeTemplates.get(nodeId).getType(); + nodeTemplatesGrouped.putIfAbsent(nodeType, new ArrayList<>()); + nodeTemplatesGrouped.get(nodeType).add(nodeId); + } + return nodeTemplatesGrouped; } - String delimiterChar = "."; - if (componentName.contains(delimiterChar)) { - return componentName.substring(componentName.lastIndexOf(delimiterChar) + 1); + + private List<Network> extractNetworks(ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { + List<Network> networks = new ArrayList<>(); + Map<String, NodeTemplate> networkNodeTemplates = toscaAnalyzerService + .getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK, toscaServiceModel); + if (MapUtils.isEmpty(networkNodeTemplates)) { + return networks; + } + for (String networkId : networkNodeTemplates.keySet()) { + Network network = new Network(); + network.setName(networkId); + Optional<Boolean> networkDhcpValue = getNetworkDhcpValue(serviceTemplate, networkNodeTemplates.get(networkId)); + network.setDhcp(networkDhcpValue.orElse(true)); + networks.add(network); + } + return networks; } - return componentName; - } + //dhcp default value is true + private Optional<Boolean> getNetworkDhcpValue(ServiceTemplate serviceTemplate, NodeTemplate networkNodeTemplate) { + if (networkNodeTemplate == null) { + return Optional.empty(); + } + if (networkNodeTemplate.getProperties() == null + || networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME) == null) { + return Optional.of(true); + } + Object dhcp = networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME); + if (dhcp instanceof String) { + return Optional.of(Boolean.valueOf((String) dhcp)); + } else if (dhcp instanceof Boolean) { + return Optional.of((Boolean) dhcp); + } else if (dhcp instanceof Map) { + String inputParameterName = (String) ((Map) dhcp).get(ToscaFunctions.GET_INPUT.getFunctionName()); + if (inputParameterName != null) { + ParameterDefinition inputParameterDefinition = serviceTemplate.getTopology_template().getInputs().get(inputParameterName); + if (inputParameterDefinition != null) { + if (inputParameterDefinition.get_default() != null) { + return Optional.of(Boolean.valueOf(inputParameterDefinition.get_default().toString())); + } + } else { + throw new CoreException(new ToscaInvalidEntryNotFoundErrorBuilder("Input Parameter", inputParameterName).build()); + } + } + } + return Optional.of(true); + } + @Override + public String getComponentDisplayName(String componentName) { + if (componentName == null) { + return null; + } + String delimiterChar = "."; + if (componentName.contains(delimiterChar)) { + return componentName.substring(componentName.lastIndexOf(delimiterChar) + 1); + } + return componentName; + } } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java index 951cfc7d1f..258b058df7 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; import static org.openecomp.sdc.tosca.csar.CSARConstants.ARTIFACTS_FOLDER; @@ -63,7 +62,6 @@ import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; public class ETSIServiceImpl implements ETSIService { private static final Logger LOGGER = LoggerFactory.getLogger(ETSIServiceImpl.class); - private final NonManoConfiguration nonManoConfiguration; public ETSIServiceImpl() { @@ -99,25 +97,19 @@ public class ETSIServiceImpl implements ETSIService { .forEach(manifestNonManoSourceEntry -> { final NonManoFolderType nonManoFolderType = nonManoKeyFolderMapping.get(manifestNonManoSourceEntry.getKey()); final List<String> nonManoFileList = manifestNonManoSourceEntry.getValue(); - final Map<String, Path> actualFromToPathMap = nonManoFileList.stream() - .map(nonManoFilePath -> { - final Path normalizedFilePath = resolveNonManoFilePath(originalManifestPath, nonManoFilePath); - final Optional<Path> changedPath = updateNonManoPathInHandler(handler, nonManoFolderType, normalizedFilePath); - if (changedPath.isPresent()) { - final Map<String, Path> fromAndToPathMap = new HashMap<>(); - fromAndToPathMap.put(nonManoFilePath, Paths.get(ARTIFACTS_FOLDER).resolve(changedPath.get())); - return fromAndToPathMap; - } - return null; - }) - .filter(Objects::nonNull) - .collect(Collectors.toMap( - fromToPathEntry -> fromToPathEntry.keySet().iterator().next(), - fromToPathEntry -> fromToPathEntry.values().iterator().next() - )); + final Map<String, Path> actualFromToPathMap = nonManoFileList.stream().map(nonManoFilePath -> { + final Path normalizedFilePath = resolveNonManoFilePath(originalManifestPath, nonManoFilePath); + final Optional<Path> changedPath = updateNonManoPathInHandler(handler, nonManoFolderType, normalizedFilePath); + if (changedPath.isPresent()) { + final Map<String, Path> fromAndToPathMap = new HashMap<>(); + fromAndToPathMap.put(nonManoFilePath, Paths.get(ARTIFACTS_FOLDER).resolve(changedPath.get())); + return fromAndToPathMap; + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toMap(fromToPathEntry -> fromToPathEntry.keySet().iterator().next(), + fromToPathEntry -> fromToPathEntry.values().iterator().next())); fromToPathMap.putAll(actualFromToPathMap); }); - return MapUtils.isEmpty(fromToPathMap) ? Optional.empty() : Optional.of(fromToPathMap); } @@ -125,7 +117,7 @@ public class ETSIServiceImpl implements ETSIService { * Resolves the non mano file path based on the original manifest path of the onboarded package. * * @param originalManifestPath The original path from the onboarded package manifest - * @param nonManoFilePath The non mano file path defined in the manifest + * @param nonManoFilePath The non mano file path defined in the manifest * @return The resolved and normalized non mano path. */ private Path resolveNonManoFilePath(final Path originalManifestPath, final String nonManoFilePath) { @@ -135,8 +127,8 @@ public class ETSIServiceImpl implements ETSIService { /** * Updates the non mano file path in the package file handler based on the non mano type. * - * @param handler The package file handler - * @param nonManoFolderType The Non Mano type of the file to update + * @param handler The package file handler + * @param nonManoFolderType The Non Mano type of the file to update * @param nonManoOriginalFilePath The Non Mano file original path * @return The new file path if it was updated in the package file handler, otherwise empty. */ @@ -144,23 +136,22 @@ public class ETSIServiceImpl implements ETSIService { final Path nonManoOriginalFilePath) { final Path fixedSourcePath = fixNonManoPath(nonManoOriginalFilePath); if (handler.containsFile(fixedSourcePath.toString())) { - final Path newNonManoPath = Paths.get(nonManoFolderType.getType(), nonManoFolderType.getLocation() - , fixedSourcePath.getFileName().toString()); + final Path newNonManoPath = Paths + .get(nonManoFolderType.getType(), nonManoFolderType.getLocation(), fixedSourcePath.getFileName().toString()); if (!handler.containsFile(newNonManoPath.toString())) { handler.addFile(newNonManoPath.toString(), handler.remove(fixedSourcePath.toString())); return Optional.of(newNonManoPath); } } - return Optional.empty(); } /** * Fix the original non mano file path to the ONAP package file path. - * - * Non mano artifacts that were inside the {@link org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} path - * are not moved when parsed to ONAP package, but the Manifest declaration can still have the {@link - * org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} reference in it. If so, that reference is removed. + * <p> + * Non mano artifacts that were inside the {@link org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} path are not moved when parsed to + * ONAP package, but the Manifest declaration can still have the {@link org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} reference in + * it. If so, that reference is removed. * * @param nonManoOriginalFilePath The original non mano file path * @return The non mano fixed path to ONAP package structure. @@ -174,28 +165,22 @@ public class ETSIServiceImpl implements ETSIService { if (nonManoOriginalFilePath.startsWith(relativeArtifactsPath)) { return relativeArtifactsPath.relativize(nonManoOriginalFilePath); } - return nonManoOriginalFilePath; } @Override - public void updateMainDescriptorPaths(final ToscaServiceModel toscaServiceModel, - final Map<String, Path> fromToMovedArtifactMap) { - final ServiceTemplate entryDefinition = toscaServiceModel.getServiceTemplates() - .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + public void updateMainDescriptorPaths(final ToscaServiceModel toscaServiceModel, final Map<String, Path> fromToMovedArtifactMap) { + final ServiceTemplate entryDefinition = toscaServiceModel.getServiceTemplates().get(toscaServiceModel.getEntryDefinitionServiceTemplate()); final YamlUtil yamlUtil = new YamlUtil(); final String[] entryDefinitionYaml = {yamlUtil.objectToYaml(entryDefinition)}; - fromToMovedArtifactMap.forEach((fromPath, toPath) -> entryDefinitionYaml[0] = entryDefinitionYaml[0] - .replaceAll(fromPath, toPath.toString())); - - toscaServiceModel.addServiceTemplate(toscaServiceModel.getEntryDefinitionServiceTemplate() - , yamlUtil.yamlToObject(entryDefinitionYaml[0], ServiceTemplate.class)); + fromToMovedArtifactMap.forEach((fromPath, toPath) -> entryDefinitionYaml[0] = entryDefinitionYaml[0].replaceAll(fromPath, toPath.toString())); + toscaServiceModel.addServiceTemplate(toscaServiceModel.getEntryDefinitionServiceTemplate(), + yamlUtil.yamlToObject(entryDefinitionYaml[0], ServiceTemplate.class)); } private boolean hasMetaMandatoryEntries(final ToscaMetadata toscaMetadata) { final Map<String, String> metaDataEntries = toscaMetadata.getMetaEntries(); - return metaDataEntries.containsKey(ENTRY_DEFINITIONS.getName()) && metaDataEntries - .containsKey(ETSI_ENTRY_MANIFEST.getName()) + return metaDataEntries.containsKey(ENTRY_DEFINITIONS.getName()) && metaDataEntries.containsKey(ETSI_ENTRY_MANIFEST.getName()) && metaDataEntries.containsKey(ETSI_ENTRY_CHANGE_LOG.getName()); } @@ -204,23 +189,21 @@ public class ETSIServiceImpl implements ETSIService { try { Map<String, String> metadata = getManifest(handler).getMetadata(); if (metadata.containsKey(COMPATIBLE_SPECIFICATION_VERSIONS.getToken())) { - return Arrays.asList(metadata.get(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()).split(",")) - .stream().map(Semver::new).max((v1, v2) -> v1.compareTo(v2)) - .orElse(new Semver(ETSI_VERSION_2_6_1)); + return Arrays.asList(metadata.get(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()).split(",")).stream().map(Semver::new) + .max((v1, v2) -> v1.compareTo(v2)).orElse(new Semver(ETSI_VERSION_2_6_1)); } } catch (Exception ex) { LOGGER.error("An error occurred while getting highest compatible version from manifest file", ex); } return new Semver(ETSI_VERSION_2_6_1); - } @Override public boolean hasCnfEnhancements(final FileContentHandler fileContentHandler) throws IOException { final Manifest manifest = loadManifest(fileContentHandler); return manifest.getNonManoSources().entrySet().stream() - .filter(manifestNonManoSourceEntry -> NonManoArtifactType.ONAP_CNF_HELM.getType() - .equalsIgnoreCase(manifestNonManoSourceEntry.getKey())).findFirst().isPresent(); + .filter(manifestNonManoSourceEntry -> NonManoArtifactType.ONAP_CNF_HELM.getType().equalsIgnoreCase(manifestNonManoSourceEntry.getKey())) + .findFirst().isPresent(); } private Manifest loadManifest(final FileContentHandler handler) throws IOException { @@ -248,9 +231,10 @@ public class ETSIServiceImpl implements ETSIService { public ResourceTypeEnum getResourceType(Manifest manifest) { // Valid manifest should contain whether vnf or pnf related metadata data exclusively in SOL004 standard, + // validation of manifest done during package upload stage - if (manifest != null && !manifest.getMetadata().isEmpty() - && MANIFEST_PNF_METADATA.stream().anyMatch(e -> manifest.getMetadata().containsKey(e))) { + if (manifest != null && !manifest.getMetadata().isEmpty() && MANIFEST_PNF_METADATA.stream() + .anyMatch(e -> manifest.getMetadata().containsKey(e))) { return ResourceTypeEnum.PNF; } // VNF is default resource type @@ -280,11 +264,9 @@ public class ETSIServiceImpl implements ETSIService { private ToscaMetadata getMetadata(FileContentHandler handler) throws IOException { ToscaMetadata metadata; if (handler.containsFile(TOSCA_META_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata - .parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata - .parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); } else { throw new IOException("TOSCA.meta file not found!"); } @@ -293,8 +275,7 @@ public class ETSIServiceImpl implements ETSIService { private ToscaMetadata getOriginalMetadata(final FileContentHandler handler) throws IOException { if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { - return OnboardingToscaMetadata - .parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); + return OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); } else { throw new IOException(String.format("%s file not found", TOSCA_META_ORIG_PATH_FILE_NAME)); } @@ -307,7 +288,6 @@ public class ETSIServiceImpl implements ETSIService { } else { io = handler.getFileContentAsStream(manifestLocation); } - if (io == null) { throw new IOException("Manifest file not found!"); } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/CandidateServiceImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/CandidateServiceImpl.java index 7c11fb65ab..7ba8732091 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/CandidateServiceImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/CandidateServiceImpl.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule; import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; @@ -72,578 +71,491 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CandidateServiceImpl implements CandidateService { - private static final Logger logger = LoggerFactory.getLogger(CandidateServiceImpl.class); - private CandidateServiceValidator candidateServiceValidator = new CandidateServiceValidator(); - private ManifestCreator manifestCreator; - private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao; - - public CandidateServiceImpl(ManifestCreator manifestCreator, - OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao) { - this.manifestCreator = manifestCreator; - this.orchestrationTemplateCandidateDao = orchestrationTemplateCandidateDao; - } - - public CandidateServiceImpl() { - } - - @Override - public Optional<ErrorMessage> validateNonEmptyFileToUpload(InputStream fileToUpload, - String fileSuffix) { - String errorMessage = - getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), - fileSuffix); - - if (Objects.isNull(fileToUpload)) { - return Optional.of(new ErrorMessage(ErrorLevel.ERROR, - errorMessage)); - } else { - try { - int available = fileToUpload.available(); - if (available == 0) { - return Optional.of(new ErrorMessage(ErrorLevel.ERROR, - errorMessage)); + + private static final Logger logger = LoggerFactory.getLogger(CandidateServiceImpl.class); + private CandidateServiceValidator candidateServiceValidator = new CandidateServiceValidator(); + private ManifestCreator manifestCreator; + private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao; + + public CandidateServiceImpl(ManifestCreator manifestCreator, OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao) { + this.manifestCreator = manifestCreator; + this.orchestrationTemplateCandidateDao = orchestrationTemplateCandidateDao; + } + + public CandidateServiceImpl() { + } + + @Override + public Optional<ErrorMessage> validateNonEmptyFileToUpload(InputStream fileToUpload, String fileSuffix) { + String errorMessage = getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), fileSuffix); + if (Objects.isNull(fileToUpload)) { + return Optional.of(new ErrorMessage(ErrorLevel.ERROR, errorMessage)); + } else { + try { + int available = fileToUpload.available(); + if (available == 0) { + return Optional.of(new ErrorMessage(ErrorLevel.ERROR, errorMessage)); + } + } catch (IOException e) { + logger.debug(e.getMessage(), e); + return Optional.of(new ErrorMessage(ErrorLevel.ERROR, errorMessage)); + } } - } catch (IOException e) { - logger.debug(e.getMessage(), e); - return Optional.of(new ErrorMessage(ErrorLevel.ERROR, - errorMessage)); - } - } - return Optional.empty(); - } - - @Override - public Optional<ErrorMessage> validateRawZipData(String fileSuffix, - byte[] uploadedFileData) { - if (Objects.isNull(uploadedFileData)) { - return Optional.of(new ErrorMessage(ErrorLevel.ERROR, - getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), - fileSuffix))); - } - return Optional.empty(); - } - - private String heatStructureTreeToFileDataStructure(HeatStructureTree tree, - FileContentHandler zipContentMap, - Map<String, List<ErrorMessage>> uploadErrors, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { - FilesDataStructure structure = new FilesDataStructure(); - Set<String> usedEnvFiles = new HashSet<>(); - addHeatsToFileDataStructure(tree, usedEnvFiles, structure, uploadErrors, - analyzedZipHeatFiles); - handleOtherResources(tree, usedEnvFiles, structure); - FilesDataStructure fileDataStructureFromManifest = - createFileDataStructureFromManifest(zipContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)); - List<String> structureArtifacts = structure.getArtifacts(); - structureArtifacts.addAll(fileDataStructureFromManifest.getArtifacts().stream().filter - (artifact -> isNotStrctureArtifact(structureArtifacts, artifact)) - .collect(Collectors.toList())); - handleArtifactsFromTree(tree, structure); - - return JsonUtil.object2Json(structure); - } - - private boolean isNotStrctureArtifact(List<String> structureArtifacts, String artifact) { - return !structureArtifacts.contains(artifact); - } - - @Override - public OrchestrationTemplateCandidateData createCandidateDataEntity( - CandidateDataEntityTo candidateDataEntityTo, InputStream zipFileManifest, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { - FileContentHandler zipContentMap = candidateDataEntityTo.getContentMap(); - FilesDataStructure filesDataStructure; - String dataStructureJson; - - if (zipFileManifest != null) { - // create data structure from manifest - filesDataStructure = createFileDataStructureFromManifest(zipFileManifest); - Set<String> zipFileList = zipContentMap.getFileList(); - balanceManifestFilesWithZipFiles(filesDataStructure, - zipContentMap, analyzedZipHeatFiles); - Set<String> filesDataStructureFiles = getFlatFileNames(filesDataStructure); - filesDataStructure.getUnassigned().addAll(zipFileList.stream() - .filter(fileName -> (!filesDataStructureFiles.contains(fileName) - && !filesDataStructure.getNested().contains(fileName) - && !fileName.equals(SdcCommon.MANIFEST_NAME))) - .collect(Collectors.toList())); - dataStructureJson = JsonUtil.object2Json(filesDataStructure); - } else { - // create data structure from based on naming convention - dataStructureJson = - heatStructureTreeToFileDataStructure(candidateDataEntityTo.getTree(), zipContentMap, - candidateDataEntityTo.getErrors(), analyzedZipHeatFiles); - } - - OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); - candidateData.setContentData(ByteBuffer.wrap(candidateDataEntityTo.getUploadedFileData())); - candidateData.setFilesDataStructure(dataStructureJson); - return candidateData; - } - - private void balanceManifestFilesWithZipFiles( - FilesDataStructure filesDataStructure, - FileContentHandler fileContentHandler, AnalyzedZipHeatFiles analyzedZipHeatFiles) { - Set<String> zipFileList = fileContentHandler.getFileList(); - filesDataStructure.getNested().addAll(analyzedZipHeatFiles.getNestedFiles()); - List<Module> modules = filesDataStructure.getModules(); - if (CollectionUtils.isEmpty(modules)) { - return; - } - - for (int i = 0; i < modules.size(); i++) { - Module module = modules.get(i); - if (!isFileExistInZipContains(zipFileList, module.getYaml())) { - addFileToUnassigned(filesDataStructure, zipFileList, module.getEnv()); - addFileToUnassigned(filesDataStructure, zipFileList, module.getVol()); - addFileToUnassigned(filesDataStructure, zipFileList, module.getVolEnv()); - modules.remove(i--); - } else if (Objects.nonNull(module.getVol()) && !zipFileList.contains(module.getVol())) { - module.setVol(null); - CollectionUtils - .addIgnoreNull(filesDataStructure.getUnassigned(), module.getVolEnv()); - } else { - if (filesDataStructure.getNested().contains(module.getYaml())) { - moveModuleFileToNested(filesDataStructure, i--, module); + return Optional.empty(); + } + + @Override + public Optional<ErrorMessage> validateRawZipData(String fileSuffix, byte[] uploadedFileData) { + if (Objects.isNull(uploadedFileData)) { + return Optional.of(new ErrorMessage(ErrorLevel.ERROR, + getErrorWithParameters(Messages.NO_FILE_WAS_UPLOADED_OR_FILE_NOT_EXIST.getErrorMessage(), fileSuffix))); } - } - } - } - - private void addFileToUnassigned(FilesDataStructure filesDataStructure, Set<String> zipFileList, - String fileName) { - if (isFileExistInZipContains(zipFileList, fileName)) { - filesDataStructure.getUnassigned().add(fileName); - } - } - - private boolean isFileExistInZipContains(Set<String> zipFileList, String fileName) { - return Objects.nonNull(fileName) && zipFileList.contains(fileName); - } - - private void moveModuleFileToNested(FilesDataStructure filesDataStructure, int i, - Module module) { - if (!filesDataStructure.getNested().contains(module.getYaml())) { - filesDataStructure.getNested().add(module.getYaml()); - } - if (Objects.nonNull(module.getEnv())) { - filesDataStructure.getNested().add(module.getEnv()); - } - if (Objects.nonNull(module.getVol())) { - filesDataStructure.getNested().add(module.getVol()); - } - if (Objects.nonNull(module.getVolEnv())) { - filesDataStructure.getNested().add(module.getVolEnv()); - } - filesDataStructure.getModules().remove(i); - } - - private Set<String> getFlatFileNames(FilesDataStructure filesDataStructure) { - Set<String> fileNames = new HashSet<>(); - if (!CollectionUtils.isEmpty(filesDataStructure.getModules())) { - for (Module module : filesDataStructure.getModules()) { - CollectionUtils.addIgnoreNull(fileNames, module.getEnv()); - CollectionUtils.addIgnoreNull(fileNames, module.getVol()); - CollectionUtils.addIgnoreNull(fileNames, module.getVolEnv()); - CollectionUtils.addIgnoreNull(fileNames, module.getYaml()); - } - } - fileNames.addAll(filesDataStructure.getArtifacts()); - fileNames.addAll(filesDataStructure.getNested()); - fileNames.addAll(filesDataStructure.getUnassigned()); - - return fileNames; - } - - private FilesDataStructure createFileDataStructureFromManifest(InputStream isManifestContent) { - ManifestContent manifestContent = - JsonUtil.json2Object(isManifestContent, ManifestContent.class); - FilesDataStructure structure = new FilesDataStructure(); - for (FileData fileData : manifestContent.getData()) { - if (Objects.nonNull(fileData.getType()) && - fileData.getType().equals(FileData.Type.HEAT)) { - Module module = new Module(); - module.setType(FileData.Type.HEAT); - module.setYaml(fileData.getFile()); - module.setIsBase(fileData.getBase()); - addHeatDependenciesToModule(module, fileData.getData()); - structure.getModules().add(module); - }else if (Objects.nonNull(fileData.getType()) && - fileData.getType().equals(FileData.Type.HELM)) { - Module module = new Module(); - module.setType(FileData.Type.HELM); - module.setYaml(fileData.getFile()); - module.setIsBase(fileData.getBase()); - structure.getModules().add(module); - } - else if (HeatFileAnalyzer.isYamlOrEnvFile(fileData.getFile()) && - !FileData.Type.isArtifact(fileData.getType())) { - structure.getUnassigned().add(fileData.getFile()); - } else { - structure.getArtifacts().add(fileData.getFile()); - } - } - return structure; - } - - private void addHeatDependenciesToModule(Module module, List<FileData> data) { - if (CollectionUtils.isEmpty(data)) { - return; - } - - for (FileData fileData : data) { - if (fileData.getType().equals(FileData.Type.HEAT_ENV)) { - module.setEnv(fileData.getFile()); - } else if (fileData.getType().equals(FileData.Type.HEAT_VOL)) { // must be volume - module.setVol(fileData.getFile()); - if (!CollectionUtils.isEmpty(fileData.getData())) { - FileData volEnv = fileData.getData().get(0); - if (volEnv.getType().equals(FileData.Type.HEAT_ENV)) { - module.setVolEnv(volEnv.getFile()); - } else { - throw new CoreException((new ErrorCode.ErrorCodeBuilder()) - .withMessage(Messages.ILLEGAL_MANIFEST.getErrorMessage()) - .withId(Messages.ILLEGAL_MANIFEST.getErrorMessage()) - .withCategory(ErrorCategory.APPLICATION).build()); - } + return Optional.empty(); + } + + private String heatStructureTreeToFileDataStructure(HeatStructureTree tree, FileContentHandler zipContentMap, + Map<String, List<ErrorMessage>> uploadErrors, AnalyzedZipHeatFiles analyzedZipHeatFiles) { + FilesDataStructure structure = new FilesDataStructure(); + Set<String> usedEnvFiles = new HashSet<>(); + addHeatsToFileDataStructure(tree, usedEnvFiles, structure, uploadErrors, analyzedZipHeatFiles); + handleOtherResources(tree, usedEnvFiles, structure); + FilesDataStructure fileDataStructureFromManifest = createFileDataStructureFromManifest( + zipContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)); + List<String> structureArtifacts = structure.getArtifacts(); + structureArtifacts.addAll( + fileDataStructureFromManifest.getArtifacts().stream().filter(artifact -> isNotStrctureArtifact(structureArtifacts, artifact)) + .collect(Collectors.toList())); + handleArtifactsFromTree(tree, structure); + return JsonUtil.object2Json(structure); + } + + private boolean isNotStrctureArtifact(List<String> structureArtifacts, String artifact) { + return !structureArtifacts.contains(artifact); + } + + @Override + public OrchestrationTemplateCandidateData createCandidateDataEntity(CandidateDataEntityTo candidateDataEntityTo, InputStream zipFileManifest, + AnalyzedZipHeatFiles analyzedZipHeatFiles) { + FileContentHandler zipContentMap = candidateDataEntityTo.getContentMap(); + FilesDataStructure filesDataStructure; + String dataStructureJson; + if (zipFileManifest != null) { + // create data structure from manifest + filesDataStructure = createFileDataStructureFromManifest(zipFileManifest); + Set<String> zipFileList = zipContentMap.getFileList(); + balanceManifestFilesWithZipFiles(filesDataStructure, zipContentMap, analyzedZipHeatFiles); + Set<String> filesDataStructureFiles = getFlatFileNames(filesDataStructure); + filesDataStructure.getUnassigned().addAll(zipFileList.stream().filter( + fileName -> (!filesDataStructureFiles.contains(fileName) && !filesDataStructure.getNested().contains(fileName) && !fileName + .equals(SdcCommon.MANIFEST_NAME))).collect(Collectors.toList())); + dataStructureJson = JsonUtil.object2Json(filesDataStructure); + } else { + // create data structure from based on naming convention + dataStructureJson = heatStructureTreeToFileDataStructure(candidateDataEntityTo.getTree(), zipContentMap, + candidateDataEntityTo.getErrors(), analyzedZipHeatFiles); } - } else { - throw new CoreException((new ErrorCode.ErrorCodeBuilder()) - .withMessage(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage()) - .withId(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage()) - .withCategory(ErrorCategory.APPLICATION).build()); - } - } - } - - @Override - public void updateCandidateUploadData(final String vspId, final Version version, - final OrchestrationTemplateCandidateData uploadData) { - orchestrationTemplateCandidateDao.update(vspId, version, uploadData); - } - - @Override - public Optional<FilesDataStructure> getOrchestrationTemplateCandidateFileDataStructure( - String vspId, Version version) { - Optional<String> jsonFileDataStructure = - orchestrationTemplateCandidateDao.getStructure(vspId, version); - - if (jsonFileDataStructure.isPresent() && JsonUtil.isValidJson(jsonFileDataStructure.get())) { - return Optional - .of(JsonUtil.json2Object(jsonFileDataStructure.get(), FilesDataStructure.class)); - } else { - return Optional.empty(); - } - } - - @Override - public void updateOrchestrationTemplateCandidateFileDataStructure(String vspId, Version version, - FilesDataStructure fileDataStructure) { - OrchestrationTemplateCandidateDaoFactory.getInstance().createInterface() - .updateStructure(vspId, version, fileDataStructure); - } - - @Override - public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidate(String vspId, - Version version) { - return orchestrationTemplateCandidateDao.get(vspId, version); - } - - @Override - public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidateInfo( - String vspId, - Version version) { - return orchestrationTemplateCandidateDao.getInfo(vspId, version); - } - - @Override - public String createManifest(VspDetails vspDetails, FilesDataStructure structure) { - return JsonUtil.object2Json(manifestCreator.createManifest(vspDetails, structure) - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()).build()))); - } - - @Override - public String createManifestFromExisting(VspDetails vspDetails, FilesDataStructure structure, ManifestContent existingManifest) { - return JsonUtil.object2Json(manifestCreator.createManifestFromExisting(vspDetails, structure, existingManifest) - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()).build()))); - } - - @Override - public Optional<ManifestContent> createManifest(VspDetails vspDetails, - FileContentHandler fileContentHandler, + OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); + candidateData.setContentData(ByteBuffer.wrap(candidateDataEntityTo.getUploadedFileData())); + candidateData.setFilesDataStructure(dataStructureJson); + return candidateData; + } + + private void balanceManifestFilesWithZipFiles(FilesDataStructure filesDataStructure, FileContentHandler fileContentHandler, AnalyzedZipHeatFiles analyzedZipHeatFiles) { - return manifestCreator.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); - } - - @Override - public Optional<ByteArrayInputStream> fetchZipFileByteArrayInputStream(String vspId, - OrchestrationTemplateCandidateData candidateDataEntity, - String manifest, - OnboardingTypesEnum type, - Map<String, List<ErrorMessage>> uploadErrors) { - byte[] file; - ByteArrayInputStream byteArrayInputStream = null; - try { - file = replaceManifestInZip(candidateDataEntity.getContentData(), manifest, type); - byteArrayInputStream = new ByteArrayInputStream( - Objects.isNull(file) ? candidateDataEntity.getContentData().array() - : file); - } catch (IOException e) { - ErrorMessage errorMessage = - new ErrorMessage(ErrorLevel.ERROR, - Messages.CANDIDATE_PROCESS_FAILED.getErrorMessage()); - logger.error(errorMessage.getMessage(), e); - ErrorsUtil - .addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE, errorMessage, uploadErrors); - } - return Optional.ofNullable(byteArrayInputStream); - } - - @Override - public byte[] replaceManifestInZip(ByteBuffer contentData, String manifest, - OnboardingTypesEnum type) - throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - try (final ZipOutputStream zos = new ZipOutputStream(baos); - ZipInputStream zipStream = new ZipInputStream( - new ByteArrayInputStream(contentData.array()))) { - ZipEntry zipEntry; - boolean manifestWritten = false; - while ((zipEntry = zipStream.getNextEntry()) != null) { - if (!zipEntry.getName().equalsIgnoreCase(SdcCommon.MANIFEST_NAME)) { - ZipEntry loc_ze = new ZipEntry(zipEntry.getName()); - zos.putNextEntry(loc_ze); - byte[] buf = new byte[1024]; - int len; - while ((len = zipStream.read(buf)) > 0) { - zos.write(buf, 0, (len < buf.length) ? len : buf.length); - } + Set<String> zipFileList = fileContentHandler.getFileList(); + filesDataStructure.getNested().addAll(analyzedZipHeatFiles.getNestedFiles()); + List<Module> modules = filesDataStructure.getModules(); + if (CollectionUtils.isEmpty(modules)) { + return; + } + for (int i = 0; i < modules.size(); i++) { + Module module = modules.get(i); + if (!isFileExistInZipContains(zipFileList, module.getYaml())) { + addFileToUnassigned(filesDataStructure, zipFileList, module.getEnv()); + addFileToUnassigned(filesDataStructure, zipFileList, module.getVol()); + addFileToUnassigned(filesDataStructure, zipFileList, module.getVolEnv()); + modules.remove(i--); + } else if (Objects.nonNull(module.getVol()) && !zipFileList.contains(module.getVol())) { + module.setVol(null); + CollectionUtils.addIgnoreNull(filesDataStructure.getUnassigned(), module.getVolEnv()); + } else { + if (filesDataStructure.getNested().contains(module.getYaml())) { + moveModuleFileToNested(filesDataStructure, i--, module); + } + } + } + } + + private void addFileToUnassigned(FilesDataStructure filesDataStructure, Set<String> zipFileList, String fileName) { + if (isFileExistInZipContains(zipFileList, fileName)) { + filesDataStructure.getUnassigned().add(fileName); + } + } + + private boolean isFileExistInZipContains(Set<String> zipFileList, String fileName) { + return Objects.nonNull(fileName) && zipFileList.contains(fileName); + } + + private void moveModuleFileToNested(FilesDataStructure filesDataStructure, int i, Module module) { + if (!filesDataStructure.getNested().contains(module.getYaml())) { + filesDataStructure.getNested().add(module.getYaml()); + } + if (Objects.nonNull(module.getEnv())) { + filesDataStructure.getNested().add(module.getEnv()); + } + if (Objects.nonNull(module.getVol())) { + filesDataStructure.getNested().add(module.getVol()); + } + if (Objects.nonNull(module.getVolEnv())) { + filesDataStructure.getNested().add(module.getVolEnv()); + } + filesDataStructure.getModules().remove(i); + } + + private Set<String> getFlatFileNames(FilesDataStructure filesDataStructure) { + Set<String> fileNames = new HashSet<>(); + if (!CollectionUtils.isEmpty(filesDataStructure.getModules())) { + for (Module module : filesDataStructure.getModules()) { + CollectionUtils.addIgnoreNull(fileNames, module.getEnv()); + CollectionUtils.addIgnoreNull(fileNames, module.getVol()); + CollectionUtils.addIgnoreNull(fileNames, module.getVolEnv()); + CollectionUtils.addIgnoreNull(fileNames, module.getYaml()); + } + } + fileNames.addAll(filesDataStructure.getArtifacts()); + fileNames.addAll(filesDataStructure.getNested()); + fileNames.addAll(filesDataStructure.getUnassigned()); + return fileNames; + } + + private FilesDataStructure createFileDataStructureFromManifest(InputStream isManifestContent) { + ManifestContent manifestContent = JsonUtil.json2Object(isManifestContent, ManifestContent.class); + FilesDataStructure structure = new FilesDataStructure(); + for (FileData fileData : manifestContent.getData()) { + if (Objects.nonNull(fileData.getType()) && fileData.getType().equals(FileData.Type.HEAT)) { + Module module = new Module(); + module.setType(FileData.Type.HEAT); + module.setYaml(fileData.getFile()); + module.setIsBase(fileData.getBase()); + addHeatDependenciesToModule(module, fileData.getData()); + structure.getModules().add(module); + } else if (Objects.nonNull(fileData.getType()) && fileData.getType().equals(FileData.Type.HELM)) { + Module module = new Module(); + module.setType(FileData.Type.HELM); + module.setYaml(fileData.getFile()); + module.setIsBase(fileData.getBase()); + structure.getModules().add(module); + } else if (HeatFileAnalyzer.isYamlOrEnvFile(fileData.getFile()) && !FileData.Type.isArtifact(fileData.getType())) { + structure.getUnassigned().add(fileData.getFile()); + } else { + structure.getArtifacts().add(fileData.getFile()); + } + } + return structure; + } + + private void addHeatDependenciesToModule(Module module, List<FileData> data) { + if (CollectionUtils.isEmpty(data)) { + return; + } + for (FileData fileData : data) { + if (fileData.getType().equals(FileData.Type.HEAT_ENV)) { + module.setEnv(fileData.getFile()); + } else if (fileData.getType().equals(FileData.Type.HEAT_VOL)) { // must be volume + module.setVol(fileData.getFile()); + if (!CollectionUtils.isEmpty(fileData.getData())) { + FileData volEnv = fileData.getData().get(0); + if (volEnv.getType().equals(FileData.Type.HEAT_ENV)) { + module.setVolEnv(volEnv.getFile()); + } else { + throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(Messages.ILLEGAL_MANIFEST.getErrorMessage()) + .withId(Messages.ILLEGAL_MANIFEST.getErrorMessage()).withCategory(ErrorCategory.APPLICATION).build()); + } + } + } else { + throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage()) + .withId(Messages.FILE_TYPE_NOT_LEGAL.getErrorMessage()).withCategory(ErrorCategory.APPLICATION).build()); + } + } + } + + @Override + public void updateCandidateUploadData(final String vspId, final Version version, final OrchestrationTemplateCandidateData uploadData) { + orchestrationTemplateCandidateDao.update(vspId, version, uploadData); + } + + @Override + public Optional<FilesDataStructure> getOrchestrationTemplateCandidateFileDataStructure(String vspId, Version version) { + Optional<String> jsonFileDataStructure = orchestrationTemplateCandidateDao.getStructure(vspId, version); + if (jsonFileDataStructure.isPresent() && JsonUtil.isValidJson(jsonFileDataStructure.get())) { + return Optional.of(JsonUtil.json2Object(jsonFileDataStructure.get(), FilesDataStructure.class)); } else { - manifestWritten = true; - writeManifest(manifest, type, zos); + return Optional.empty(); } - zos.closeEntry(); - } - if (!manifestWritten) { - writeManifest(manifest, type, zos); - zos.closeEntry(); - } - } - return baos.toByteArray(); - } - - @Override - public byte[] getZipData(ByteBuffer contentData) - throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - try (final ZipOutputStream zos = new ZipOutputStream(baos); - ZipInputStream zipStream = new ZipInputStream( - new ByteArrayInputStream(contentData.array()))) { - ZipEntry zipEntry; - while ((zipEntry = zipStream.getNextEntry()) != null) { + } + + @Override + public void updateOrchestrationTemplateCandidateFileDataStructure(String vspId, Version version, FilesDataStructure fileDataStructure) { + OrchestrationTemplateCandidateDaoFactory.getInstance().createInterface().updateStructure(vspId, version, fileDataStructure); + } + + @Override + public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidate(String vspId, Version version) { + return orchestrationTemplateCandidateDao.get(vspId, version); + } + + @Override + public Optional<OrchestrationTemplateCandidateData> getOrchestrationTemplateCandidateInfo(String vspId, Version version) { + return orchestrationTemplateCandidateDao.getInfo(vspId, version); + } + + @Override + public String createManifest(VspDetails vspDetails, FilesDataStructure structure) { + return JsonUtil.object2Json(manifestCreator.createManifest(vspDetails, structure).orElseThrow( + () -> new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()).build()))); + } + + @Override + public String createManifestFromExisting(VspDetails vspDetails, FilesDataStructure structure, ManifestContent existingManifest) { + return JsonUtil.object2Json(manifestCreator.createManifestFromExisting(vspDetails, structure, existingManifest).orElseThrow( + () -> new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()).build()))); + } + + @Override + public Optional<ManifestContent> createManifest(VspDetails vspDetails, FileContentHandler fileContentHandler, + AnalyzedZipHeatFiles analyzedZipHeatFiles) { + return manifestCreator.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); + } + + @Override + public Optional<ByteArrayInputStream> fetchZipFileByteArrayInputStream(String vspId, OrchestrationTemplateCandidateData candidateDataEntity, + String manifest, OnboardingTypesEnum type, + Map<String, List<ErrorMessage>> uploadErrors) { + byte[] file; + ByteArrayInputStream byteArrayInputStream = null; try { - ZipUtils.checkForZipSlipInRead(zipEntry); - } catch (ZipSlipException e) { - throw new IOException(e); + file = replaceManifestInZip(candidateDataEntity.getContentData(), manifest, type); + byteArrayInputStream = new ByteArrayInputStream(Objects.isNull(file) ? candidateDataEntity.getContentData().array() : file); + } catch (IOException e) { + ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, Messages.CANDIDATE_PROCESS_FAILED.getErrorMessage()); + logger.error(errorMessage.getMessage(), e); + ErrorsUtil.addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE, errorMessage, uploadErrors); } - ZipEntry locZipEntry = new ZipEntry(zipEntry.getName()); - zos.putNextEntry(locZipEntry); - byte[] buf = new byte[1024]; - int len; - while ((len = zipStream.read(buf)) > 0) { - zos.write(buf, 0, (len < buf.length) ? len : buf.length); + return Optional.ofNullable(byteArrayInputStream); + } + + @Override + public byte[] replaceManifestInZip(ByteBuffer contentData, String manifest, OnboardingTypesEnum type) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (final ZipOutputStream zos = new ZipOutputStream(baos); ZipInputStream zipStream = new ZipInputStream( + new ByteArrayInputStream(contentData.array()))) { + ZipEntry zipEntry; + boolean manifestWritten = false; + while ((zipEntry = zipStream.getNextEntry()) != null) { + if (!zipEntry.getName().equalsIgnoreCase(SdcCommon.MANIFEST_NAME)) { + ZipEntry loc_ze = new ZipEntry(zipEntry.getName()); + zos.putNextEntry(loc_ze); + byte[] buf = new byte[1024]; + int len; + while ((len = zipStream.read(buf)) > 0) { + zos.write(buf, 0, (len < buf.length) ? len : buf.length); + } + } else { + manifestWritten = true; + writeManifest(manifest, type, zos); + } + zos.closeEntry(); + } + if (!manifestWritten) { + writeManifest(manifest, type, zos); + zos.closeEntry(); + } } - zos.closeEntry(); - } + return baos.toByteArray(); } - return baos.toByteArray(); - } - @Override - public Optional<List<ErrorMessage>> validateFileDataStructure( - FilesDataStructure filesDataStructure) { - return candidateServiceValidator.validateFileDataStructure(filesDataStructure); - } + @Override + public byte[] getZipData(ByteBuffer contentData) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (final ZipOutputStream zos = new ZipOutputStream(baos); ZipInputStream zipStream = new ZipInputStream( + new ByteArrayInputStream(contentData.array()))) { + ZipEntry zipEntry; + while ((zipEntry = zipStream.getNextEntry()) != null) { + try { + ZipUtils.checkForZipSlipInRead(zipEntry); + } catch (ZipSlipException e) { + throw new IOException(e); + } + ZipEntry locZipEntry = new ZipEntry(zipEntry.getName()); + zos.putNextEntry(locZipEntry); + byte[] buf = new byte[1024]; + int len; + while ((len = zipStream.read(buf)) > 0) { + zos.write(buf, 0, (len < buf.length) ? len : buf.length); + } + zos.closeEntry(); + } + } + return baos.toByteArray(); + } - @Override - public void deleteOrchestrationTemplateCandidate(String vspId, Version versionId) { - orchestrationTemplateCandidateDao.delete(vspId, versionId); - } + @Override + public Optional<List<ErrorMessage>> validateFileDataStructure(FilesDataStructure filesDataStructure) { + return candidateServiceValidator.validateFileDataStructure(filesDataStructure); + } - @Override - public void updateValidationData(String vspId, Version version, ValidationStructureList - validationData) { - orchestrationTemplateCandidateDao.updateValidationData(vspId, version, validationData); - } + @Override + public void deleteOrchestrationTemplateCandidate(String vspId, Version versionId) { + orchestrationTemplateCandidateDao.delete(vspId, versionId); + } - private void writeManifest(String manifest, - OnboardingTypesEnum type, - ZipOutputStream zos) throws IOException { + @Override + public void updateValidationData(String vspId, Version version, ValidationStructureList validationData) { + orchestrationTemplateCandidateDao.updateValidationData(vspId, version, validationData); + } - if (isManifestNeedsToGetWritten(type)) { - return; + private void writeManifest(String manifest, OnboardingTypesEnum type, ZipOutputStream zos) throws IOException { + if (isManifestNeedsToGetWritten(type)) { + return; + } + zos.putNextEntry(new ZipEntry(SdcCommon.MANIFEST_NAME)); + try (InputStream manifestStream = new ByteArrayInputStream(manifest.getBytes(StandardCharsets.UTF_8))) { + byte[] buf = new byte[1024]; + int len; + while ((len = (manifestStream.read(buf))) > 0) { + zos.write(buf, 0, (len < buf.length) ? len : buf.length); + } + } } - zos.putNextEntry(new ZipEntry(SdcCommon.MANIFEST_NAME)); - try (InputStream manifestStream = new ByteArrayInputStream( - manifest.getBytes(StandardCharsets.UTF_8))) { - byte[] buf = new byte[1024]; - int len; - while ((len = (manifestStream.read(buf))) > 0) { - zos.write(buf, 0, (len < buf.length) ? len : buf.length); - } + private boolean isManifestNeedsToGetWritten(OnboardingTypesEnum type) { + return type.equals(OnboardingTypesEnum.CSAR); } - } - private boolean isManifestNeedsToGetWritten(OnboardingTypesEnum type) { - return type.equals(OnboardingTypesEnum.CSAR); - } + private void handleArtifactsFromTree(HeatStructureTree tree, FilesDataStructure structure) { + if (Objects.isNull(tree) || Objects.isNull(tree.getArtifacts())) { + return; + } + if (CollectionUtils.isNotEmpty(tree.getArtifacts())) { + structure.getArtifacts().addAll( + tree.getArtifacts().stream().map(Artifact::getFileName).filter(fileName -> !structure.getArtifacts().contains(fileName)) + .collect(Collectors.toList())); + } + } + + private void handleOtherResources(HeatStructureTree tree, Set<String> usedEnvFiles, FilesDataStructure structure) { + Set<HeatStructureTree> others = tree.getOther(); + if (Objects.isNull(others)) { + return; + } + List<String> artifacts = new ArrayList<>(); + List<String> unassigned = new ArrayList<>(); + for (HeatStructureTree other : others) { + if (HeatFileAnalyzer.isYamlOrEnvFile(other.getFileName())) { + if (isEnvFileUsedByHeatFile(usedEnvFiles, other)) { + continue; + } + unassigned.add(other.getFileName()); + } else { + artifacts.add(other.getFileName()); + } + handleArtifactsFromTree(other, structure); + } + structure.getArtifacts().addAll(artifacts); + structure.getUnassigned().addAll(unassigned); + } - private void handleArtifactsFromTree(HeatStructureTree tree, FilesDataStructure structure) { + private boolean isEnvFileUsedByHeatFile(Set<String> usedEnvFiles, HeatStructureTree other) { + return HeatFileAnalyzer.isEnvFile(other.getFileName()) && usedEnvFiles.contains(other.getFileName()); + } - if (Objects.isNull(tree) || Objects.isNull(tree.getArtifacts())) { - return; + private void addHeatsToFileDataStructure(HeatStructureTree tree, Set<String> usedEnvFiles, FilesDataStructure structure, + Map<String, List<ErrorMessage>> uploadErrors, AnalyzedZipHeatFiles analyzedZipHeatFiles) { + List<Module> modules = new ArrayList<>(); + Set<HeatStructureTree> heatsSet = tree.getHeat(); + if (Objects.isNull(heatsSet)) { + return; + } + for (HeatStructureTree heat : heatsSet) { + if (isFileBaseFile(heat.getFileName())) { + handleSingleHeat(structure, modules, heat, uploadErrors); + } else if (isFileModuleFile(heat.getFileName(), analyzedZipHeatFiles.getModuleFiles())) { + handleSingleHeat(structure, modules, heat, uploadErrors); + } else { + structure.getUnassigned().add(heat.getFileName()); + addNestedToFileDataStructure(heat, structure); + } + if (!Objects.isNull(heat.getEnv())) { + usedEnvFiles.add(heat.getEnv() == null ? null : heat.getEnv().getFileName()); + } + } + structure.setModules(modules); } - if (CollectionUtils.isNotEmpty(tree.getArtifacts())) { - structure.getArtifacts().addAll( - tree.getArtifacts() - .stream() - .map(Artifact::getFileName) - .filter(fileName -> !structure.getArtifacts().contains(fileName)) - .collect(Collectors.toList())); + private boolean isFileModuleFile(String fileName, Set<String> modulesFileNames) { + return modulesFileNames.contains(fileName); } - } - private void handleOtherResources(HeatStructureTree tree, Set<String> usedEnvFiles, - FilesDataStructure structure) { - Set<HeatStructureTree> others = tree.getOther(); - if (Objects.isNull(others)) { - return; + private boolean isFileBaseFile(String fileName) { + return manifestCreator.isFileBaseFile(fileName); } - List<String> artifacts = new ArrayList<>(); - List<String> unassigned = new ArrayList<>(); - for (HeatStructureTree other : others) { - if (HeatFileAnalyzer.isYamlOrEnvFile(other.getFileName())) { - if (isEnvFileUsedByHeatFile(usedEnvFiles, other)) { - continue; - } - unassigned.add(other.getFileName()); - } else { - artifacts.add(other.getFileName()); - } - handleArtifactsFromTree(other, structure); - } - structure.getArtifacts().addAll(artifacts); - structure.getUnassigned().addAll(unassigned); - } - - private boolean isEnvFileUsedByHeatFile(Set<String> usedEnvFiles, HeatStructureTree other) { - return HeatFileAnalyzer.isEnvFile(other.getFileName()) && - usedEnvFiles.contains(other.getFileName()); - } - - private void addHeatsToFileDataStructure(HeatStructureTree tree, Set<String> usedEnvFiles, - FilesDataStructure structure, - Map<String, List<ErrorMessage>> uploadErrors, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { - List<Module> modules = new ArrayList<>(); - Set<HeatStructureTree> heatsSet = tree.getHeat(); - if (Objects.isNull(heatsSet)) { - return; - } - for (HeatStructureTree heat : heatsSet) { - if (isFileBaseFile(heat.getFileName())) { - handleSingleHeat(structure, modules, heat, uploadErrors); - } else if (isFileModuleFile(heat.getFileName(), - analyzedZipHeatFiles.getModuleFiles())) { - handleSingleHeat(structure, modules, heat, uploadErrors); - } else { - structure.getUnassigned().add(heat.getFileName()); + private void handleSingleHeat(FilesDataStructure structure, List<Module> modules, HeatStructureTree heat, + Map<String, List<ErrorMessage>> uploadErrors) { + Module module = new Module(); + module.setYaml(heat.getFileName()); + module.setIsBase(heat.getBase()); addNestedToFileDataStructure(heat, structure); - } - if (!Objects.isNull(heat.getEnv())) { - usedEnvFiles.add(heat.getEnv() == null ? null : heat.getEnv().getFileName()); - } - } - structure.setModules(modules); - - } - - private boolean isFileModuleFile(String fileName, Set<String> modulesFileNames) { - return modulesFileNames.contains(fileName); - } - - private boolean isFileBaseFile(String fileName) { - return manifestCreator.isFileBaseFile(fileName); - } - - private void handleSingleHeat(FilesDataStructure structure, List<Module> modules, - HeatStructureTree heat, - Map<String, List<ErrorMessage>> uploadErrors) { - Module module = new Module(); - module.setYaml(heat.getFileName()); - module.setIsBase(heat.getBase()); - addNestedToFileDataStructure(heat, structure); - Set<HeatStructureTree> volumeSet = heat.getVolume(); - int inx = 0; - if (Objects.nonNull(volumeSet)) { - handleVolumes(module, volumeSet, structure, inx, uploadErrors); - } - handleEnv(module, heat, false, structure); - modules.add(module); - } - - private void handleVolumes(Module module, Set<HeatStructureTree> volumeSet, - FilesDataStructure structure, int inx, - Map<String, List<ErrorMessage>> uploadErrors) { - for (HeatStructureTree volume : volumeSet) { - Objects.requireNonNull(volume, "volume cannot be null!"); - if (inx++ > 0) { - ErrorsUtil.addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE, - new ErrorMessage(ErrorLevel.WARNING, - Messages.MORE_THEN_ONE_VOL_FOR_HEAT.getErrorMessage()), uploadErrors); - break; - } - handleArtifactsFromTree(volume, structure); - module.setVol(volume.getFileName()); - handleEnv(module, volume, true, structure); - addNestedToFileDataStructure(volume, structure); - } - } - - private void handleEnv(Module module, HeatStructureTree tree, boolean isVolEnv, - FilesDataStructure structure) { - if (Objects.nonNull(tree.getEnv())) { - if (isVolEnv) { - module.setVolEnv(tree.getEnv().getFileName()); - } else { - module.setEnv(tree.getEnv().getFileName()); - } - handleArtifactsFromTree(tree.getEnv(), structure); - } - } - - private void addNestedToFileDataStructure(HeatStructureTree heat, - FilesDataStructure structure) { - Set<HeatStructureTree> nestedSet = heat.getNested(); - if (Objects.isNull(nestedSet)) { - return; - } - for (HeatStructureTree nested : nestedSet) { - if (structure.getNested().contains(nested.getFileName())) { - continue; - } - structure.getNested().add(nested.getFileName()); - if (CollectionUtils.isNotEmpty(nested.getArtifacts())) { - handleArtifactsFromTree(nested, structure); - } - addNestedToFileDataStructure(nested, structure); - } - } + Set<HeatStructureTree> volumeSet = heat.getVolume(); + int inx = 0; + if (Objects.nonNull(volumeSet)) { + handleVolumes(module, volumeSet, structure, inx, uploadErrors); + } + handleEnv(module, heat, false, structure); + modules.add(module); + } + + private void handleVolumes(Module module, Set<HeatStructureTree> volumeSet, FilesDataStructure structure, int inx, + Map<String, List<ErrorMessage>> uploadErrors) { + for (HeatStructureTree volume : volumeSet) { + Objects.requireNonNull(volume, "volume cannot be null!"); + if (inx++ > 0) { + ErrorsUtil.addStructureErrorToErrorMap(SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.WARNING, Messages.MORE_THEN_ONE_VOL_FOR_HEAT.getErrorMessage()), uploadErrors); + break; + } + handleArtifactsFromTree(volume, structure); + module.setVol(volume.getFileName()); + handleEnv(module, volume, true, structure); + addNestedToFileDataStructure(volume, structure); + } + } + + private void handleEnv(Module module, HeatStructureTree tree, boolean isVolEnv, FilesDataStructure structure) { + if (Objects.nonNull(tree.getEnv())) { + if (isVolEnv) { + module.setVolEnv(tree.getEnv().getFileName()); + } else { + module.setEnv(tree.getEnv().getFileName()); + } + handleArtifactsFromTree(tree.getEnv(), structure); + } + } + + private void addNestedToFileDataStructure(HeatStructureTree heat, FilesDataStructure structure) { + Set<HeatStructureTree> nestedSet = heat.getNested(); + if (Objects.isNull(nestedSet)) { + return; + } + for (HeatStructureTree nested : nestedSet) { + if (structure.getNested().contains(nested.getFileName())) { + continue; + } + structure.getNested().add(nested.getFileName()); + if (CollectionUtils.isNotEmpty(nested.getArtifacts())) { + handleArtifactsFromTree(nested, structure); + } + addNestedToFileDataStructure(nested, structure); + } + } } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/ManifestCreatorNamingConventionImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/ManifestCreatorNamingConventionImpl.java index 54cc1fbf1c..f935318d01 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/ManifestCreatorNamingConventionImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/filedatastructuremodule/ManifestCreatorNamingConventionImpl.java @@ -14,9 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; import org.apache.commons.collections4.CollectionUtils; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.heat.datatypes.manifest.FileData; @@ -31,322 +40,278 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; -import java.util.regex.Pattern; - public class ManifestCreatorNamingConventionImpl implements ManifestCreator { - protected static final Logger logger = - LoggerFactory.getLogger(ManifestCreatorNamingConventionImpl.class); - - private static final String CLOUD_SPECIFIC_FIXED_KEY_WORD = "cloudtech"; - private static final String[][] CLOUD_SPECIFIC_KEY_WORDS = {{"k8s", "azure", "aws"}, /* cloud specific technology */ - {"charts", "day0", "configtemplate"} /*cloud specific sub type*/}; - private static final String CONTROLLER_BLUEPRINT_ARCHIVE_FIXED_KEY_WORD = "CBA"; - private static final String HELM_KEY_WORD = "HELM"; - @Override - public Optional<ManifestContent> createManifest( - VspDetails vspDetails, FilesDataStructure filesDataStructure) { - if (Objects.isNull(filesDataStructure)) { - return Optional.empty(); + protected static final Logger logger = LoggerFactory.getLogger(ManifestCreatorNamingConventionImpl.class); + private static final String CLOUD_SPECIFIC_FIXED_KEY_WORD = "cloudtech"; + private static final String[][] CLOUD_SPECIFIC_KEY_WORDS = {{"k8s", "azure", "aws"}, /* cloud specific technology */ + {"charts", "day0", "configtemplate"} /*cloud specific sub type*/}; + private static final String CONTROLLER_BLUEPRINT_ARCHIVE_FIXED_KEY_WORD = "CBA"; + private static final String HELM_KEY_WORD = "HELM"; + + @Override + public Optional<ManifestContent> createManifest(VspDetails vspDetails, FilesDataStructure filesDataStructure) { + if (Objects.isNull(filesDataStructure)) { + return Optional.empty(); + } + List<FileData> fileDataList = new ArrayList<>(); + addModulesToManifestFileDataList(filesDataStructure, fileDataList); + addNestedToManifest(filesDataStructure, fileDataList); + addArtifactsToManifestFileDataList(filesDataStructure, fileDataList); + ManifestContent manifestContent = createManifest(vspDetails, fileDataList); + return Optional.of(manifestContent); } - List<FileData> fileDataList = new ArrayList<>(); - addModulesToManifestFileDataList(filesDataStructure, fileDataList); - addNestedToManifest(filesDataStructure, fileDataList); - addArtifactsToManifestFileDataList(filesDataStructure, fileDataList); - - ManifestContent manifestContent = createManifest(vspDetails, fileDataList); - return Optional.of(manifestContent); - } - - @Override - public Optional<ManifestContent> createManifestFromExisting(VspDetails vspDetails, FilesDataStructure filesDataStructure, ManifestContent existingManifest) { - if (Objects.isNull(filesDataStructure)) { - return Optional.empty(); + @Override + public Optional<ManifestContent> createManifestFromExisting(VspDetails vspDetails, FilesDataStructure filesDataStructure, + ManifestContent existingManifest) { + if (Objects.isNull(filesDataStructure)) { + return Optional.empty(); + } + List<FileData> fileDataList = new ArrayList<>(); + addModulesToManifestFileDataList(filesDataStructure, fileDataList); + addNestedToManifest(filesDataStructure, fileDataList); + addArtifactsToManifestFileDataList(filesDataStructure, fileDataList, existingManifest); + ManifestContent manifestContent = createManifest(vspDetails, fileDataList); + return Optional.of(manifestContent); } - List<FileData> fileDataList = new ArrayList<>(); - addModulesToManifestFileDataList(filesDataStructure, fileDataList); - addNestedToManifest(filesDataStructure, fileDataList); - addArtifactsToManifestFileDataList(filesDataStructure, fileDataList, existingManifest); - - ManifestContent manifestContent = createManifest(vspDetails, fileDataList); - return Optional.of(manifestContent); - } - - private void addArtifactsToManifestFileDataList(FilesDataStructure filesDataStructure, List<FileData> fileDataList, ManifestContent existingManifest) { - Collection<String> forArtifacts = CollectionUtils - .union(filesDataStructure.getArtifacts(), filesDataStructure.getUnassigned()); - if (CollectionUtils.isNotEmpty(forArtifacts)) { - for (String artifact : forArtifacts) { - if (isCloudSpecificArtifact(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT, artifact)); - } else if (isControllerBlueprintArchive(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.CONTROLLER_BLUEPRINT_ARCHIVE, artifact)); - } else if (isHelm(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.HELM, artifact)); - } else if (isPmDictionary(artifact, existingManifest)) { - fileDataList.add(createBaseFileData(FileData.Type.PM_DICTIONARY, artifact)); + private void addArtifactsToManifestFileDataList(FilesDataStructure filesDataStructure, List<FileData> fileDataList, + ManifestContent existingManifest) { + Collection<String> forArtifacts = CollectionUtils.union(filesDataStructure.getArtifacts(), filesDataStructure.getUnassigned()); + if (CollectionUtils.isNotEmpty(forArtifacts)) { + for (String artifact : forArtifacts) { + if (isCloudSpecificArtifact(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT, artifact)); + } else if (isControllerBlueprintArchive(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.CONTROLLER_BLUEPRINT_ARCHIVE, artifact)); + } else if (isHelm(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.HELM, artifact)); + } else if (isPmDictionary(artifact, existingManifest)) { + fileDataList.add(createBaseFileData(FileData.Type.PM_DICTIONARY, artifact)); + } else { + fileDataList.add(createBaseFileData(FileData.Type.OTHER, artifact)); + } + } } - else { - fileDataList.add(createBaseFileData(FileData.Type.OTHER, artifact)); - } - } } - } - private boolean isPmDictionary(String artifact, ManifestContent existingManifest) { - return existingManifest.getData() - .stream() - .filter(fileData -> fileData.getType() - .equals(FileData.Type.PM_DICTIONARY)) - .map(FileData::getFile) + private boolean isPmDictionary(String artifact, ManifestContent existingManifest) { + return existingManifest.getData().stream().filter(fileData -> fileData.getType().equals(FileData.Type.PM_DICTIONARY)).map(FileData::getFile) .anyMatch(pmDictionaryFile -> pmDictionaryFile.equals(artifact)); - } - - private void addNestedToManifest( - FilesDataStructure filesDataStructure, List<FileData> fileDataList) { - if (CollectionUtils.isNotEmpty(filesDataStructure.getNested())) { - for (String nested : filesDataStructure.getNested()) { - fileDataList.add(createBaseFileData(FileData.Type.HEAT, nested)); - } } - } - @Override - public Optional<ManifestContent> createManifest(VspDetails vspDetails, - FileContentHandler fileContentHandler, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { - logger.info("Trying to generate manifest"); - if (Objects.isNull(fileContentHandler) - || CollectionUtils.isEmpty(fileContentHandler.getFileList())) { - logger.info("fileContentHandler or filesList is empty. ManifestContent will not be created"); - return Optional.empty(); + private void addNestedToManifest(FilesDataStructure filesDataStructure, List<FileData> fileDataList) { + if (CollectionUtils.isNotEmpty(filesDataStructure.getNested())) { + for (String nested : filesDataStructure.getNested()) { + fileDataList.add(createBaseFileData(FileData.Type.HEAT, nested)); + } + } } - Map<String, byte[]> files = fileContentHandler.getFiles(); + @Override + public Optional<ManifestContent> createManifest(VspDetails vspDetails, FileContentHandler fileContentHandler, + AnalyzedZipHeatFiles analyzedZipHeatFiles) { + logger.info("Trying to generate manifest"); + if (Objects.isNull(fileContentHandler) || CollectionUtils.isEmpty(fileContentHandler.getFileList())) { + logger.info("fileContentHandler or filesList is empty. ManifestContent will not be created"); + return Optional.empty(); + } + Map<String, byte[]> files = fileContentHandler.getFiles(); + List<FileData> fileDataList = createFileDataListFromZipFiles(fileContentHandler, files, analyzedZipHeatFiles.getFilesNotEligbleForModules()); + ManifestContent manifestContent = createManifest(vspDetails, fileDataList); + return Optional.of(manifestContent); + } - List<FileData> fileDataList = - createFileDataListFromZipFiles(fileContentHandler, files, - analyzedZipHeatFiles.getFilesNotEligbleForModules()); - ManifestContent manifestContent = createManifest(vspDetails, fileDataList); + private ManifestContent createManifest(VspDetails vspDetails, List<FileData> fileDataList) { + ManifestContent manifestContent = new ManifestContent(); + manifestContent.setName(vspDetails.getName()); + manifestContent.setDescription(vspDetails.getDescription()); + manifestContent.setVersion(vspDetails.getVersion() == null ? null : vspDetails.getVersion().toString()); + // vsp version, need to check in confluence + manifestContent.setData(fileDataList); + return manifestContent; + } - return Optional.of(manifestContent); - } + private List<FileData> createFileDataListFromZipFiles(FileContentHandler fileContentHandler, Map<String, byte[]> files, + Collection<String> filesNotEligibleForModules) { + Set<String> processedFiles = new HashSet<>(); + List<FileData> fileDataList = new ArrayList<>(); + for (String fileName : files.keySet()) { + if (processedFiles.contains(fileName)) { + continue; + } + if (isFileBaseFile(fileName)) { + fileDataList.add(createModuleFileData(fileName, true, processedFiles, fileContentHandler.getFileList(), fileDataList)); + } else if (isFileModuleFile(fileName, filesNotEligibleForModules)) { + fileDataList.add(createModuleFileData(fileName, false, processedFiles, fileContentHandler.getFileList(), fileDataList)); + } else { + if (HeatFileAnalyzer.isYamlFile(fileName)) { + fileDataList.add(createBasicFileData(fileName, FileData.Type.HEAT, null)); + } else if (HeatFileAnalyzer.isEnvFile(fileName)) { + fileDataList.add(createBasicFileData(fileName, FileData.Type.HEAT_ENV, null)); + } else { + fileDataList.add(createBasicFileData(fileName, FileData.Type.OTHER, null)); + } + } + } + return fileDataList; + } - private ManifestContent createManifest(VspDetails vspDetails, List<FileData> fileDataList) { - ManifestContent manifestContent = new ManifestContent(); - manifestContent.setName(vspDetails.getName()); - manifestContent.setDescription(vspDetails.getDescription()); - manifestContent - .setVersion(vspDetails.getVersion() == null ? null : vspDetails.getVersion().toString()); - // vsp version, need to check in confluence - manifestContent.setData(fileDataList); - return manifestContent; - } + private boolean isFileModuleFile(String fileName, Collection<String> filesCannotBeModule) { + return !filesCannotBeModule.contains(fileName); + } - private List<FileData> createFileDataListFromZipFiles(FileContentHandler fileContentHandler, - Map<String, byte[]> files, - Collection<String> filesNotEligibleForModules) { + @Override + public boolean isFileBaseFile(String fileName) { + return Pattern.matches(Constants.BASE_HEAT_REGEX, fileName) && !isVolFile(fileName); + } - Set<String> processedFiles = new HashSet<>(); - List<FileData> fileDataList = new ArrayList<>(); - for (String fileName : files.keySet()) { - if (processedFiles.contains(fileName)) { - continue; - } - if (isFileBaseFile(fileName)) { - fileDataList - .add(createModuleFileData( - fileName, true, processedFiles, fileContentHandler.getFileList(), fileDataList)); - } else if (isFileModuleFile(fileName, filesNotEligibleForModules)) { - fileDataList - .add(createModuleFileData( - fileName, false, processedFiles, fileContentHandler.getFileList(), fileDataList)); - } else { - if (HeatFileAnalyzer.isYamlFile(fileName)) { - fileDataList.add(createBasicFileData(fileName, FileData.Type.HEAT, null)); - } else if (HeatFileAnalyzer.isEnvFile(fileName)) { - fileDataList.add(createBasicFileData(fileName, FileData.Type.HEAT_ENV, null)); + protected boolean isCloudSpecificArtifact(String artifact) { + if (artifact.contains(CLOUD_SPECIFIC_FIXED_KEY_WORD)) { + for (int i = 0; i < CLOUD_SPECIFIC_KEY_WORDS.length; i++) { + if (Arrays.stream(CLOUD_SPECIFIC_KEY_WORDS[i]).noneMatch(str -> artifact.contains(str))) { + return false; + } + } + return true; } else { - fileDataList.add(createBasicFileData(fileName, FileData.Type.OTHER, null)); + return false; } - } } - return fileDataList; - } - - private boolean isFileModuleFile(String fileName, Collection<String> filesCannotBeModule) { - return !filesCannotBeModule.contains(fileName); - } - @Override - public boolean isFileBaseFile(String fileName) { - return Pattern.matches(Constants.BASE_HEAT_REGEX, fileName) && !isVolFile(fileName); - } - - protected boolean isCloudSpecificArtifact(String artifact) { - if (artifact.contains(CLOUD_SPECIFIC_FIXED_KEY_WORD)) { - for (int i = 0; i < CLOUD_SPECIFIC_KEY_WORDS.length; i++) { - if (Arrays.stream(CLOUD_SPECIFIC_KEY_WORDS[i]).noneMatch(str -> artifact.contains(str))) { - return false; - } - } - return true; - } else { - return false; - } - } - - private boolean isControllerBlueprintArchive(String artifact) { - return artifact.toUpperCase().contains(CONTROLLER_BLUEPRINT_ARCHIVE_FIXED_KEY_WORD); - } + private boolean isControllerBlueprintArchive(String artifact) { + return artifact.toUpperCase().contains(CONTROLLER_BLUEPRINT_ARCHIVE_FIXED_KEY_WORD); + } - private boolean isHelm(String artifact) { - return artifact.toUpperCase().contains(HELM_KEY_WORD); - } + private boolean isHelm(String artifact) { + return artifact.toUpperCase().contains(HELM_KEY_WORD); + } - private void addArtifactsToManifestFileDataList( - FilesDataStructure filesDataStructure, List<FileData> fileDataList) { - Collection<String> forArtifacts = CollectionUtils - .union(filesDataStructure.getArtifacts(), filesDataStructure.getUnassigned()); - if (CollectionUtils.isNotEmpty(forArtifacts)) { - for (String artifact : forArtifacts) { - if (isCloudSpecificArtifact(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT, artifact)); - } else if (isControllerBlueprintArchive(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.CONTROLLER_BLUEPRINT_ARCHIVE, artifact)); - } else if (isHelm(artifact)) { - fileDataList.add(createBaseFileData(FileData.Type.HELM, artifact)); - } else { - fileDataList.add(createBaseFileData(FileData.Type.OTHER, artifact)); + private void addArtifactsToManifestFileDataList(FilesDataStructure filesDataStructure, List<FileData> fileDataList) { + Collection<String> forArtifacts = CollectionUtils.union(filesDataStructure.getArtifacts(), filesDataStructure.getUnassigned()); + if (CollectionUtils.isNotEmpty(forArtifacts)) { + for (String artifact : forArtifacts) { + if (isCloudSpecificArtifact(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT, artifact)); + } else if (isControllerBlueprintArchive(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.CONTROLLER_BLUEPRINT_ARCHIVE, artifact)); + } else if (isHelm(artifact)) { + fileDataList.add(createBaseFileData(FileData.Type.HELM, artifact)); + } else { + fileDataList.add(createBaseFileData(FileData.Type.OTHER, artifact)); + } + } } - } } - } - private void addModulesToManifestFileDataList( - FilesDataStructure filesDataStructure, List<FileData> fileDataList) { - if (CollectionUtils.isNotEmpty(filesDataStructure.getModules())) { - for (Module module : filesDataStructure.getModules()) { - FileData.Type type = module.getType(); - if (type == null) { - type = FileData.Type.HEAT; + private void addModulesToManifestFileDataList(FilesDataStructure filesDataStructure, List<FileData> fileDataList) { + if (CollectionUtils.isNotEmpty(filesDataStructure.getModules())) { + for (Module module : filesDataStructure.getModules()) { + FileData.Type type = module.getType(); + if (type == null) { + type = FileData.Type.HEAT; + } + FileData fileData = createBaseFileData(type, module.getYaml()); + fileData.setBase(module.getIsBase()); + addEnv(module, fileData); + addVolume(module, fileData); + fileDataList.add(fileData); + } } - FileData fileData = createBaseFileData(type, module.getYaml()); - fileData.setBase(module.getIsBase()); - addEnv(module, fileData); - addVolume(module, fileData); - fileDataList.add(fileData); - } } - } - private void addEnv(Module module, FileData fileData) { - if (Objects.nonNull(module.getEnv())) { - FileData env = createBaseFileData(FileData.Type.HEAT_ENV, module.getEnv()); - fileData.addFileData(env); + private void addEnv(Module module, FileData fileData) { + if (Objects.nonNull(module.getEnv())) { + FileData env = createBaseFileData(FileData.Type.HEAT_ENV, module.getEnv()); + fileData.addFileData(env); + } } - } - private void addVolume(Module module, FileData fileData) { - String volModule = module.getVol(); - if (Objects.nonNull(volModule)) { - FileData vol = createBaseFileData(FileData.Type.HEAT_VOL, volModule); - if (Objects.nonNull(module.getVolEnv())) { - vol.addFileData(createBaseFileData(FileData.Type.HEAT_ENV, module.getVolEnv())); - } - fileData.addFileData(vol); + private void addVolume(Module module, FileData fileData) { + String volModule = module.getVol(); + if (Objects.nonNull(volModule)) { + FileData vol = createBaseFileData(FileData.Type.HEAT_VOL, volModule); + if (Objects.nonNull(module.getVolEnv())) { + vol.addFileData(createBaseFileData(FileData.Type.HEAT_ENV, module.getVolEnv())); + } + fileData.addFileData(vol); + } } - } - - private FileData createBaseFileData(FileData.Type heat, String yaml) { - FileData fileData = new FileData(); - fileData.setType(heat); - fileData.setFile(yaml); - return fileData; - } - - private FileData createModuleFileData( - String moduleFileName, boolean isBase, Set<String> processedFiles, - Set<String> fileNames, List<FileData> fileDataList) { - FileData moduleFileData = createBasicFileData(moduleFileName, FileData.Type.HEAT, isBase); - Optional<String> volFile = fetchRelatedVolume(moduleFileName, fileNames); - volFile.ifPresent(vol -> { - markFileAsProcessed(vol, processedFiles); - removeFromFileDataListIfAlreadyProcessed(fileDataList, vol); - FileData volFileData = createBasicFileData(vol, FileData.Type.HEAT_VOL, null); - Optional<String> envFile = fetchRelatedEnv(vol, fileNames); - envFile.ifPresent(env -> { - markFileAsProcessed(env, processedFiles); - removeFromFileDataListIfAlreadyProcessed(fileDataList, env); - FileData envFileData = createBasicFileData(env, FileData.Type.HEAT_ENV, null); - volFileData.addFileData(envFileData); - }); - moduleFileData.addFileData(volFileData); - }); - Optional<String> envFile = fetchRelatedEnv(moduleFileName, fileNames); - envFile.ifPresent(env -> { - markFileAsProcessed(env, processedFiles); - FileData envFileData = createBasicFileData(env, FileData.Type.HEAT_ENV, null); - moduleFileData.addFileData(envFileData); - }); - return moduleFileData; - } - private void removeFromFileDataListIfAlreadyProcessed(List<FileData> fileDataList, String vol) { - fileDataList.removeIf(fileData -> fileData.getFile().equals(vol)); - } - - private FileData createBasicFileData(String fileName, FileData.Type type, Boolean isBase) { - FileData fileData = new FileData(); - if (isBase != null) { - fileData.setBase(isBase); + private FileData createBaseFileData(FileData.Type heat, String yaml) { + FileData fileData = new FileData(); + fileData.setType(heat); + fileData.setFile(yaml); + return fileData; } - fileData.setType(type); - fileData.setFile(fileName); - return fileData; - } - - private Optional<String> fetchRelatedEnv(String fileName, Set<String> fileNames) { - String envFileName - = fileName.substring(0, fileName.lastIndexOf(".")) + Constants.ENV_FILE_EXTENSION; - return fileNames.contains(envFileName) ? Optional.of(envFileName) : Optional.empty(); - } - private Optional<String> fetchRelatedVolume(String fileName, Set<String> fileNames) { - - String volFile1stExt = - extractVolFileName(fileName, ".yaml"); - String volFile2ndExt = - extractVolFileName(fileName, ".yml"); + private FileData createModuleFileData(String moduleFileName, boolean isBase, Set<String> processedFiles, Set<String> fileNames, + List<FileData> fileDataList) { + FileData moduleFileData = createBasicFileData(moduleFileName, FileData.Type.HEAT, isBase); + Optional<String> volFile = fetchRelatedVolume(moduleFileName, fileNames); + volFile.ifPresent(vol -> { + markFileAsProcessed(vol, processedFiles); + removeFromFileDataListIfAlreadyProcessed(fileDataList, vol); + FileData volFileData = createBasicFileData(vol, FileData.Type.HEAT_VOL, null); + Optional<String> envFile = fetchRelatedEnv(vol, fileNames); + envFile.ifPresent(env -> { + markFileAsProcessed(env, processedFiles); + removeFromFileDataListIfAlreadyProcessed(fileDataList, env); + FileData envFileData = createBasicFileData(env, FileData.Type.HEAT_ENV, null); + volFileData.addFileData(envFileData); + }); + moduleFileData.addFileData(volFileData); + }); + Optional<String> envFile = fetchRelatedEnv(moduleFileName, fileNames); + envFile.ifPresent(env -> { + markFileAsProcessed(env, processedFiles); + FileData envFileData = createBasicFileData(env, FileData.Type.HEAT_ENV, null); + moduleFileData.addFileData(envFileData); + }); + return moduleFileData; + } - if (fileNames.contains(volFile1stExt)) { - return Optional.of(volFile1stExt); + private void removeFromFileDataListIfAlreadyProcessed(List<FileData> fileDataList, String vol) { + fileDataList.removeIf(fileData -> fileData.getFile().equals(vol)); } - if (fileNames.contains(volFile2ndExt)) { - return Optional.of(volFile2ndExt); + + private FileData createBasicFileData(String fileName, FileData.Type type, Boolean isBase) { + FileData fileData = new FileData(); + if (isBase != null) { + fileData.setBase(isBase); + } + fileData.setType(type); + fileData.setFile(fileName); + return fileData; } - return Optional.empty(); - } - private String extractVolFileName(String fileName, String fileExt) { - return fileName.substring( - 0, fileName.lastIndexOf(".")) - + Constants.VOL_FILE_NAME_SUFFIX + fileExt; - } + private Optional<String> fetchRelatedEnv(String fileName, Set<String> fileNames) { + String envFileName = fileName.substring(0, fileName.lastIndexOf(".")) + Constants.ENV_FILE_EXTENSION; + return fileNames.contains(envFileName) ? Optional.of(envFileName) : Optional.empty(); + } + private Optional<String> fetchRelatedVolume(String fileName, Set<String> fileNames) { + String volFile1stExt = extractVolFileName(fileName, ".yaml"); + String volFile2ndExt = extractVolFileName(fileName, ".yml"); + if (fileNames.contains(volFile1stExt)) { + return Optional.of(volFile1stExt); + } + if (fileNames.contains(volFile2ndExt)) { + return Optional.of(volFile2ndExt); + } + return Optional.empty(); + } - private boolean isVolFile(String fileName) { - return fileName - .endsWith( - Constants.VOL_FILE_NAME_SUFFIX + ".yaml") - || fileName.endsWith(Constants.VOL_FILE_NAME_SUFFIX + ".yml"); - } + private String extractVolFileName(String fileName, String fileExt) { + return fileName.substring(0, fileName.lastIndexOf(".")) + Constants.VOL_FILE_NAME_SUFFIX + fileExt; + } + private boolean isVolFile(String fileName) { + return fileName.endsWith(Constants.VOL_FILE_NAME_SUFFIX + ".yaml") || fileName.endsWith(Constants.VOL_FILE_NAME_SUFFIX + ".yml"); + } - private void markFileAsProcessed(String fileName, Set<String> processedFiles) { - processedFiles.add(fileName); - } + private void markFileAsProcessed(String fileName, Set<String> processedFiles) { + processedFiles.add(fileName); + } } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateServiceValidator.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateServiceValidator.java index c366b7f4b9..a3d2e65889 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateServiceValidator.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateServiceValidator.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,9 +17,13 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.vendorsoftwareproduct.services.utils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; @@ -29,48 +33,41 @@ import org.openecomp.sdc.datatypes.error.ErrorMessage; import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure; import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module; -import java.util.*; - /** * Created by Talio on 12/6/2016. */ public class CandidateServiceValidator { - public Optional<List<ErrorMessage>> validateFileDataStructure( - FilesDataStructure filesDataStructure) { - if (Objects.isNull(filesDataStructure)) { - return Optional.empty(); - } - if (validateAtLeaseOneModuleExist(filesDataStructure)) { - return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages - .NO_MODULES_IN_MANIFEST.getErrorMessage()))); - } - List<ErrorMessage> errors = new ArrayList<>(); - for (Module module : filesDataStructure.getModules()) { - validateModuleHaveYaml(errors, module); - validateNoVolEnvWithoutVol(errors, module); + public Optional<List<ErrorMessage>> validateFileDataStructure(FilesDataStructure filesDataStructure) { + if (Objects.isNull(filesDataStructure)) { + return Optional.empty(); + } + if (validateAtLeaseOneModuleExist(filesDataStructure)) { + return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages.NO_MODULES_IN_MANIFEST.getErrorMessage()))); + } + List<ErrorMessage> errors = new ArrayList<>(); + for (Module module : filesDataStructure.getModules()) { + validateModuleHaveYaml(errors, module); + validateNoVolEnvWithoutVol(errors, module); + } + return Optional.of(errors); } - return Optional.of(errors); - } - - private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) { - return CollectionUtils.isEmpty(filesDataStructure.getModules()); - } + private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) { + return CollectionUtils.isEmpty(filesDataStructure.getModules()); + } - private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) { - if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) { - errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder - .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(), - module.getName()))); + private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) { + if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) { + errors.add(new ErrorMessage(ErrorLevel.ERROR, + ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(), module.getName()))); + } } - } - private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) { - if (StringUtils.isEmpty(module.getYaml())) { - errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder - .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(), - module.getName()))); + private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) { + if (StringUtils.isEmpty(module.getYaml())) { + errors.add(new ErrorMessage(ErrorLevel.ERROR, + ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(), module.getName()))); + } } - } } |