diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services')
9 files changed, 745 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/HeatFileAnalyzer.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/HeatFileAnalyzer.java new file mode 100644 index 0000000000..533f041621 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/HeatFileAnalyzer.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services; + +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; + +import java.io.IOException; +import java.util.Map; + +/** + * @author Avrahamg + * @since December 21, 2016 + */ +public interface HeatFileAnalyzer { + + static boolean isEnvFile(String fileName) { + return fileName.endsWith(".env"); + } + + static boolean isYamlFile(String fileName) { + return fileName.endsWith(".yml") || fileName.endsWith(".yaml"); + } + + static boolean isYamlOrEnvFile(String fileName) { + return isYamlFile(fileName) || isEnvFile(fileName); + } + + AnalyzedZipHeatFiles analyzeFilesNotEligibleForModulesFromFileAnalyzer(Map<String, byte[]> files) throws IOException; +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionDataExtractor.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionDataExtractor.java new file mode 100644 index 0000000000..0b82638277 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionDataExtractor.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.composition; + +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData; + +public interface CompositionDataExtractor { + + CompositionData extractServiceCompositionData(ToscaServiceModel toscaServiceModel); + + String getComponentDisplayName(String componentName); +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionEntityDataManager.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionEntityDataManager.java new file mode 100644 index 0000000000..5bde801188 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/composition/CompositionEntityDataManager.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.composition; + +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData; +import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext; +import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +public interface CompositionEntityDataManager { + + Map<CompositionEntityId, Collection<String>> validateEntitiesQuestionnaire(); + + void addEntity(CompositionEntity entity, SchemaTemplateInput schemaTemplateInput); + + CompositionEntityValidationData validateEntity(CompositionEntity entity, + SchemaTemplateContext schemaTemplateContext, + SchemaTemplateInput schemaTemplateInput); + + void buildTrees(); + + void addErrorsToTrees(Map<CompositionEntityId, Collection<String>> errors); + + Set<CompositionEntityValidationData> getEntityListWithErrors(); + + Collection<CompositionEntityValidationData> getTrees(); + + void saveCompositionData(String vspId, Version version, CompositionData compositionData); + + Set<CompositionEntityValidationData> getAllErrorsByVsp(String vspId); + + void saveComponents(String vspId, Version version, CompositionData compositionData, + Map<String, String> networkIdByName); + + void saveNicsByComponent(String vspId, Version version, + Map<String, String> networkIdByName, Component component, + String componentId); + + Map<String, String> saveNetworks(String vspId, Version version, + CompositionData compositionData); + + NetworkEntity createNetwork(NetworkEntity network); + + ComponentEntity createComponent(ComponentEntity component); + + NicEntity createNic(NicEntity nic); + +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/CandidateService.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/CandidateService.java new file mode 100644 index 0000000000..4ca623e6a3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/CandidateService.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public interface CandidateService { + Optional<ErrorMessage> validateNonEmptyFileToUpload(InputStream heatFileToUpload); + + Optional<ErrorMessage> validateRawZipData(byte[] uploadedFileData); + + OrchestrationTemplateCandidateData createCandidateDataEntity( + CandidateDataEntityTo candidateDataEntityTo, InputStream zipFileManifest, AnalyzedZipHeatFiles analyzedZipHeatFiles) throws Exception; + + void updateCandidateUploadData(OrchestrationTemplateCandidateData uploadData, String + itemId); + + Optional<FilesDataStructure> getOrchestrationTemplateCandidateFileDataStructure(String vspId, + Version activeVersion); + + void updateOrchestrationTemplateCandidateFileDataStructure(String vspId, Version activeVersion, + FilesDataStructure fileDataStructure); + + OrchestrationTemplateCandidateData getOrchestrationTemplateCandidate(String vspId, + Version activeVersion); + + Optional<ByteArrayInputStream> fetchZipFileByteArrayInputStream(String vspId, + OrchestrationTemplateCandidateData candidateDataEntity, + String manifest, Map<String, List<ErrorMessage>> uploadErrors); + + byte[] replaceManifestInZip(ByteBuffer contentData, String manifest, String vspId) + throws IOException; + + Optional<ManifestContent> createManifest(VspDetails vspDetails, + FileContentHandler fileContentHandler, AnalyzedZipHeatFiles analyzedZipHeatFiles); + + String createManifest(VspDetails vspDetails, FilesDataStructure structure); + + Optional<List<ErrorMessage>> validateFileDataStructure(FilesDataStructure filesDataStructure); +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/ManifestCreator.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/ManifestCreator.java new file mode 100644 index 0000000000..fd70d20c22 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/filedatastructuremodule/ManifestCreator.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure; + +import java.util.Optional; + +public interface ManifestCreator { + Optional<ManifestContent> createManifest(VspDetails vspDetails, FilesDataStructure filesDataStructure); + + Optional<ManifestContent> createManifest(VspDetails vspDetails, FileContentHandler + fileContentHandler, AnalyzedZipHeatFiles analyzedZipHeatFiles); + + boolean isFileBaseFile(String fileName); +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java new file mode 100644 index 0000000000..d26942219f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java @@ -0,0 +1,152 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.impl; + +import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.*; +import java.util.regex.Pattern; + +public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer { + private static final String HEAT_IDENTIFIER_REGEX = "^heat_template_version:.*"; + private static final String HEAT_RESOURCES_REGEX = "^resources:\\s*$"; + private static final String HEAT_PARAMETERS_REGEX = "^parameters:\\s*$"; + private static final String HEAT_CONDITIONS_REGEX = "^conditions:\\s*$"; + private static final String HEAT_OUTPUTS_REGEX = "^outputs:\\s*$"; + private static final String HEAT_PARAMETER_GROUP_REGEX = "^parameter_groups:\\s*$"; + private static final String HEAT_DESCRIPTION_REGEX = "^description:\\s*$"; + //allowing spaces at start followed by 'type:' + spaces + any characters + ('.yml' or '.yaml')+ + // spaces + private static final String HEAT_NESTED_RESOURCE_REGEX = "\\s*type:\\s*\\S*.(yml|yaml)\\s*$"; + + + private static final String IDENTIFIER = "IDENTIFIER"; + private static final String RESOURCES = "RESOURCES"; + private static final String PARAMETERS = "PARAMETERS"; + private static final String CONDITIONS = "CONDITIONS"; + private static final String OUTPUTS = "OUTPUTS"; + private static final String PARAMETER_GROUP = "PARAMETER_GROUP"; + private static final String DESCRIPTION = "DESCRIPTION"; + private static final String NESTED_PATTERN = "NESTED_PATTERN"; + + private Map<String, Pattern> patterns; + + public HeatFileAnalyzerRowDataImpl() { + patterns = new HashMap<>(); + patterns.put(IDENTIFIER, Pattern.compile(HEAT_IDENTIFIER_REGEX)); + patterns.put(RESOURCES, Pattern.compile(HEAT_RESOURCES_REGEX)); + patterns.put(PARAMETERS, Pattern.compile(HEAT_PARAMETERS_REGEX)); + patterns.put(CONDITIONS, Pattern.compile(HEAT_CONDITIONS_REGEX)); + patterns.put(OUTPUTS, Pattern.compile(HEAT_OUTPUTS_REGEX)); + patterns.put(PARAMETER_GROUP, Pattern.compile(HEAT_PARAMETER_GROUP_REGEX)); + patterns.put(DESCRIPTION, Pattern.compile(HEAT_DESCRIPTION_REGEX)); + patterns.put(NESTED_PATTERN, Pattern.compile(HEAT_NESTED_RESOURCE_REGEX)); + } + + @Override + public AnalyzedZipHeatFiles analyzeFilesNotEligibleForModulesFromFileAnalyzer(Map<String, byte[]> files) + throws IOException { + AnalyzedZipHeatFiles analyzedZipHeatFiles = new AnalyzedZipHeatFiles(); + + BufferedReader bfReader; + for (Map.Entry<String, byte[]> fileData : files.entrySet()) { + String fileName = fileData.getKey(); + if (!HeatFileAnalyzer.isYamlFile(fileName)) { + analyzedZipHeatFiles.addOtherNonModuleFile(fileName); + continue; + } + boolean foundHeatIdentifier = false; + try (InputStream is = new ByteArrayInputStream(fileData.getValue())) { + bfReader = new BufferedReader(new InputStreamReader(is)); + String line; + boolean isResourcesSection = false; + Set<String> nestedFilesNames = new HashSet<>(); + while ((line = bfReader.readLine()) != null) { + if (!foundHeatIdentifier && isMatch(patterns.get(IDENTIFIER), line)) { + foundHeatIdentifier = true; + analyzedZipHeatFiles.addModuleFile(fileName); + if (isResourcesSection) // it means the identifier is located after the resources + // section + { + break; + } + } else if (isMatch(patterns.get(RESOURCES), line)) { + isResourcesSection = true; + } else if (isResourceSectionEnd(line, isResourcesSection)) { + if (foundHeatIdentifier) { + break; + } + } else if (isResourcesSection) { + Optional<String> optionalNestedFileName = fetchNestedFileName(line); + optionalNestedFileName + .ifPresent(nestedFilesNames::add); + } + } + analyzedZipHeatFiles.addNestedFiles(fetchFileNamesToReturn(nestedFilesNames, + foundHeatIdentifier)); + } + } + return analyzedZipHeatFiles; + } + + private Optional<String> fetchNestedFileName(String line) { + if (isMatch(patterns.get(NESTED_PATTERN), line)) { + String trimmedLine = line.trim(); + String nestedFileName = trimmedLine + .substring(trimmedLine.indexOf("type:") + "type:".length(), trimmedLine.length()) + .trim(); + return Optional.of(nestedFileName); + } + return Optional.empty(); + } + + private Set<String> fetchFileNamesToReturn(Set<String> filesNamesToReturn, + boolean foundHeatIdentifier) { + if (!foundHeatIdentifier) { + return new HashSet<>(); + } else { + return filesNamesToReturn; + } + } + + private boolean isResourceSectionEnd(String line, boolean isResourcesSection) { + return isResourcesSection && isStartOfNonResourcesHeatSection(line); + } + + private boolean isStartOfNonResourcesHeatSection(String line) { + return isMatch(patterns.get(PARAMETERS), line) || + isMatch(patterns.get(CONDITIONS), line) || + isMatch(patterns.get(OUTPUTS), line) || + isMatch(patterns.get(PARAMETER_GROUP), line) || + isMatch(patterns.get(DESCRIPTION), line); + } + + private boolean isMatch(Pattern pattern, String line) { + return pattern.matcher(line).matches(); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGenerator.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGenerator.java new file mode 100644 index 0000000000..6b1837fa10 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGenerator.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator; + +import freemarker.template.Template; +import freemarker.template.TemplateException; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType; +import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext; +import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; + +public class SchemaGenerator { + public static final String SCHEMA_GENERATION_ERROR = "SCHEMA_GENERATION_ERROR"; + + /** + * Generate string. + * + * @param schemaTemplateContext the schema template context + * @param entityType the entity type + * @param input the input + * @return the string + */ + public static String generate(SchemaTemplateContext schemaTemplateContext, + CompositionEntityType entityType, SchemaTemplateInput input) { + Template schemaTemplate = + SchemaGeneratorConfig.getSchemaTemplate(schemaTemplateContext, entityType); + return processTemplate(input, schemaTemplate); + } + + private static String processTemplate(SchemaTemplateInput input, Template schemaTemplate) { + try (Writer writer = new StringWriter(1024)) { + schemaTemplate.process(input, writer); + return writer.toString(); + } catch (IOException | TemplateException exception) { + throw new CoreException( + new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION) + .withId(SCHEMA_GENERATION_ERROR).withMessage(exception.getMessage()).build()); + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGeneratorConfig.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGeneratorConfig.java new file mode 100644 index 0000000000..3937ca2f2e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/schemagenerator/SchemaGeneratorConfig.java @@ -0,0 +1,154 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator; + +import freemarker.cache.StringTemplateLoader; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateExceptionHandler; +import org.openecomp.core.utilities.applicationconfig.ApplicationConfig; +import org.openecomp.core.utilities.applicationconfig.ApplicationConfigFactory; +import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType; +import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class SchemaGeneratorConfig { + public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR = + "SCHEMA_GENERATOR_INITIALIZATION_ERROR"; + public static final String SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG = + "Error occurred while loading questionnaire schema schemaTemplates"; + private static final String CONFIGURATION_NAMESPACE = "vsp.schemaTemplates"; + private static Map<SchemaTemplateId, SchemaTemplate> schemaTemplates = new HashMap<>(); + private static ApplicationConfig applicationConfig = + ApplicationConfigFactory.getInstance().createInterface(); + + private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23); + private static StringTemplateLoader stringLoader = new StringTemplateLoader(); + + static { + configuration.setClassLoaderForTemplateLoading(SchemaGenerator.class.getClassLoader(), + File.pathSeparator); + configuration.setDefaultEncoding("UTF-8"); + configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); + configuration.setLogTemplateExceptions(true); + configuration.setTemplateLoader(stringLoader); + } + + public static void insertSchemaTemplate(SchemaTemplateContext schemaTemplateContext, + CompositionEntityType entityType, + String schemaTemplateString) { + applicationConfig.insertValue(CONFIGURATION_NAMESPACE, + new SchemaTemplateId(schemaTemplateContext, entityType).toString(), schemaTemplateString); + } + + /** + * Gets schema template. + * + * @param schemaTemplateContext the schema template context + * @param entityType the entity type + * @return the schema template + */ + public static Template getSchemaTemplate(SchemaTemplateContext schemaTemplateContext, + CompositionEntityType entityType) { + SchemaTemplateId id = new SchemaTemplateId(schemaTemplateContext, entityType); + ConfigurationData configurationData = + applicationConfig.getConfigurationData(CONFIGURATION_NAMESPACE, id.toString()); + + SchemaTemplate schemaTemplate = schemaTemplates.get(id); + if (schemaTemplate == null || schemaTemplate.timestamp != configurationData.getTimeStamp()) { + stringLoader.putTemplate(id.toString(), configurationData.getValue()); + Template template; + try { + template = configuration.getTemplate(id.toString()); + } catch (IOException exception) { + throw new CoreException( + new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION) + .withId(SCHEMA_GENERATOR_INITIALIZATION_ERROR) + .withMessage(SCHEMA_GENERATOR_INITIALIZATION_ERROR_MSG).build(), exception); + } + schemaTemplate = new SchemaTemplate(template, configurationData.getTimeStamp()); + schemaTemplates.put(id, schemaTemplate); + } + return schemaTemplate.template; + } + + private static class SchemaTemplateId { + private SchemaTemplateContext context; + private CompositionEntityType entityType; + + public SchemaTemplateId(SchemaTemplateContext context, CompositionEntityType entityType) { + this.context = context; + this.entityType = entityType; + } + + @Override + public String toString() { + return context + "." + entityType; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + SchemaTemplateId that = (SchemaTemplateId) obj; + + if (entityType != that.entityType) { + return false; + } + if (context != that.context) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = entityType != null ? entityType.hashCode() : 0; + result = 31 * result + (context != null ? context.hashCode() : 0); + return result; + } + } + + private static class SchemaTemplate { + private Template template; + private long timestamp; + + public SchemaTemplate(Template template, long timestamp) { + this.template = template; + this.timestamp = timestamp; + } + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java new file mode 100644 index 0000000000..9540f3d965 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java @@ -0,0 +1,107 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.services.utils; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.sdc.heat.services.tree.HeatTreeManager; +import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil; +import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer; +import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; +import org.openecomp.sdc.vendorsoftwareproduct.services.impl.HeatFileAnalyzerRowDataImpl; +import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo; +import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; + +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class CandidateEntityBuilder { + private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + private CandidateService candidateService; + + public CandidateEntityBuilder(CandidateService candidateService) { + this.candidateService = candidateService; + } + + public OrchestrationTemplateCandidateData buildCandidateEntityFromZip( + VspDetails vspDetails, byte[] uploadedFileData, FileContentHandler contentMap, + Map<String, List<ErrorMessage>> uploadErrors, String user) throws Exception { + //mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId()); + + InputStream zipFileManifest = contentMap.getFileContent(SdcCommon.MANIFEST_NAME); + HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl(); + AnalyzedZipHeatFiles analyzedZipHeatFiles = + heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles()); + HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles); + + CandidateDataEntityTo candidateDataEntityTo = + new CandidateDataEntityTo(vspDetails.getId(), user, uploadedFileData, tree, contentMap, + vspDetails.getVersion()); + candidateDataEntityTo.setErrors(uploadErrors); + OrchestrationTemplateCandidateData candidateDataEntity = + candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest, + analyzedZipHeatFiles); + + mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId()); + return candidateDataEntity; + } + + private HeatStructureTree getHeatStructureTree(VspDetails vspDetails, + FileContentHandler contentMap, + AnalyzedZipHeatFiles analyzedZipHeatFiles) { + addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles); + HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap); + heatTreeManager.createTree(); + return heatTreeManager.getTree(); + } + + private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails, + FileContentHandler fileContentHandler, + AnalyzedZipHeatFiles analyzedZipHeatFiles) { + mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId()); + + InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME); + if (Objects.isNull(manifest)) { + Optional<ManifestContent> manifestContentOptional = + candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); + if (!manifestContentOptional.isPresent()) { + throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); + } + ManifestContent manifestContent = manifestContentOptional.get(); + fileContentHandler.addFile( + SdcCommon.MANIFEST_NAME, + String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes()); + } + + mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId()); + } +} |