3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.components.csar;
24 import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.DEFAULT_ICON;
25 import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement;
27 import fj.data.Either;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
39 import org.apache.commons.collections.CollectionUtils;
40 import org.apache.commons.collections.MapUtils;
41 import org.openecomp.sdc.be.components.impl.ImportUtils;
42 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
43 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
45 import org.openecomp.sdc.be.model.NodeTypeDefinition;
46 import org.openecomp.sdc.be.model.NodeTypeInfo;
47 import org.openecomp.sdc.be.model.NodeTypeMetadata;
48 import org.openecomp.sdc.be.model.NullNodeTypeMetadata;
49 import org.openecomp.sdc.be.model.User;
50 import org.openecomp.sdc.be.model.category.CategoryDefinition;
51 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
52 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
53 import org.openecomp.sdc.be.utils.TypeUtils;
54 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
55 import org.openecomp.sdc.common.api.Constants;
56 import org.openecomp.sdc.common.log.wrappers.Logger;
57 import org.yaml.snakeyaml.Yaml;
60 * Provides access to the contents of a Service CSAR
62 public class ServiceCsarInfo extends CsarInfo {
64 private static final Logger log = Logger.getLogger(ServiceCsarInfo.class);
65 private final Map<String, Map<String, Object>> mainTemplateImports;
66 private List<NodeTypeDefinition> nodeTypeDefinitions;
67 private final String model;
68 private final ModelOperation modelOperation;
70 public ServiceCsarInfo(final User modifier, final String csarUUID, final Map<String, byte[]> csar,
71 final String vfResourceName, final String model,
72 final String mainTemplateName, final String mainTemplateContent, final boolean isUpdate, final ModelOperation modelOperation) {
73 super(modifier, csarUUID, csar, vfResourceName, mainTemplateName, mainTemplateContent, isUpdate);
75 this.modelOperation = modelOperation;
76 final Path mainTemplateDir = Paths.get(getMainTemplateName().substring(0, getMainTemplateName().lastIndexOf('/') + 1));
77 final Collection<Path> filesHandled = new HashSet<>();
78 filesHandled.add(Paths.get(mainTemplateName));
79 this.mainTemplateImports = getTemplateImports(csar, new Yaml().load(mainTemplateContent), mainTemplateDir, filesHandled);
82 private Map<String, Map<String, Object>> getTemplateImports(final Map<String, byte[]> csar, Map<String, Object> mappedToscaMainTemplate,
83 final Path fileParentDir, final Collection<Path> filesHandled) {
84 final Map<String, Map<String, Object>> templateImports = new HashMap<>();
86 final List<Path> importFilePaths = getTemplateImportFilePaths(mappedToscaMainTemplate, fileParentDir);
88 importFilePaths.stream().filter(path -> !filesHandled.contains(path)).forEach(
90 final String importFilePathString = importFilePath.toString();
91 final byte[] importFile = csar.get(importFilePathString);
92 if (importFile != null) {
93 filesHandled.add(importFilePath);
94 final Map<String, Object> mappedImportFile = new Yaml().load(new String(importFile));
95 templateImports.put(importFilePathString, mappedImportFile);
96 templateImports.putAll(getTemplateImports(csar, mappedImportFile, importFilePath.getParent(), filesHandled));
98 log.warn("Import {} cannot be found in CSAR", importFilePathString);
102 return templateImports;
105 @SuppressWarnings({"unchecked", "rawtypes"})
106 private List<Path> getTemplateImportFilePaths(final Map<String, Object> mappedToscaTemplate, final Path fileParentDir) {
107 final Either<Object, ResultStatusEnum> importsEither =
108 findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.IMPORTS, ToscaElementTypeEnum.ALL);
110 if (importsEither.isLeft()) {
111 final List importsList = (List) importsEither.left().value();
112 if (CollectionUtils.isNotEmpty(importsList)) {
113 if (importsList.get(0) instanceof String) {
114 List<Path> importPaths = new ArrayList<>();
117 final Path path = fileParentDir == null ?
118 Paths.get((String) importPath) : fileParentDir.resolve(Paths.get((String) importPath)).normalize();
119 importPaths.add(path);
122 } else if (importsList.get(0) instanceof Map) {
123 return getTemplateImportFilePathsMultiLineGrammar(importsList, fileParentDir);
128 return Collections.emptyList();
131 @SuppressWarnings("unchecked")
132 private List<Path> getTemplateImportFilePathsMultiLineGrammar(final List<Map<String, Object>> importsList, final Path fileParentDir) {
133 final List<Path> importFiles = new ArrayList<>();
135 for (Map<String, Object> importFileMultiLineGrammar : importsList) {
136 if (MapUtils.isNotEmpty(importFileMultiLineGrammar)) {
137 if (importFileMultiLineGrammar.values().iterator().next() instanceof String) {
138 Path relativePath = Paths.get((String) importFileMultiLineGrammar.get("file"));
139 Path absolutePath = fileParentDir == null ? relativePath : fileParentDir.resolve(relativePath).normalize();
140 importFiles.add(absolutePath);
141 } else if (importFileMultiLineGrammar.values().iterator().next() instanceof Map) {
142 importFileMultiLineGrammar.values().forEach(value -> {
143 Path relativePath = Paths.get((String) ((Map<String, Object>) value).get("file"));
144 Path absolutePath = fileParentDir == null ? relativePath : fileParentDir.resolve(relativePath).normalize();
145 importFiles.add(absolutePath);
154 public Map<String, NodeTypeInfo> extractTypesInfo() {
155 return Collections.emptyMap();
159 public Map<String, Object> getDataTypes() {
160 return getTypes(ToscaTagNamesEnum.DATA_TYPES);
164 public Map<String, Object> getGroupTypes() {
165 return getTypes(ToscaTagNamesEnum.GROUP_TYPES);
169 public Map<String, Object> getCapabilityTypes() {
170 return getTypes(ToscaTagNamesEnum.CAPABILITY_TYPES);
174 public Map<String, Object> getArtifactTypes() {
175 return getTypes(ToscaTagNamesEnum.ARTIFACT_TYPES);
179 public Map<String, Object> getInterfaceTypes() {
180 return getTypes(ToscaTagNamesEnum.INTERFACE_TYPES);
183 private Map<String, Object> getTypes(ToscaTagNamesEnum toscaTag) {
184 final Map<String, Object> types = new HashMap<>();
185 mainTemplateImports.entrySet().forEach(entry -> types.putAll(getTypesFromTemplate(entry.getValue(), toscaTag)));
186 types.putAll(getTypesFromTemplate(getMappedToscaMainTemplate(), toscaTag));
190 public List<NodeTypeDefinition> getNodeTypesUsed() {
191 if (nodeTypeDefinitions == null) {
192 nodeTypeDefinitions = new ArrayList<>();
193 final Set<String> nodeTypesUsed = getNodeTypesUsedInToscaTemplate(getMappedToscaMainTemplate());
194 nodeTypeDefinitions.addAll(getNodeTypeDefinitions(nodeTypesUsed).values());
196 nodeTypeDefinitions = sortNodeTypesByDependencyOrder(nodeTypeDefinitions);
197 return nodeTypeDefinitions;
200 private List<NodeTypeDefinition> sortNodeTypesByDependencyOrder(final List<NodeTypeDefinition> nodeTypes) {
201 final List<NodeTypeDefinition> sortedNodeTypeDefinitions = new ArrayList<>();
202 final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap = new HashMap<>();
204 nodeTypes.forEach(nodeType -> {
205 int highestDependencyIndex = -1;
206 for (final String dependencyName : getDependencyTypes(nodeType, nodeTypes)) {
207 final NodeTypeDefinition dependency = nodeTypeDefinitionsMap.get(dependencyName);
208 final int indexOfDependency = sortedNodeTypeDefinitions.lastIndexOf(dependency);
209 highestDependencyIndex = Math.max(indexOfDependency, highestDependencyIndex);
211 sortedNodeTypeDefinitions.add(highestDependencyIndex + 1, nodeType);
212 nodeTypeDefinitionsMap.put(nodeType.getMappedNodeType().getKey(), nodeType);
214 return sortedNodeTypeDefinitions;
217 private Collection<String> getDependencyTypes(final NodeTypeDefinition nodeType, final List<NodeTypeDefinition> nodeTypes) {
218 final Set<String> dependencies = new HashSet<>();
219 Either<Object, ResultStatusEnum> derivedFromTypeEither = findToscaElement((Map<String, Object>) nodeType.getMappedNodeType().getValue(),
220 TypeUtils.ToscaTagNamesEnum.DERIVED_FROM, ToscaElementTypeEnum.STRING);
221 if (derivedFromTypeEither.isLeft() && derivedFromTypeEither.left().value() != null) {
222 final String derivedFrom = (String) derivedFromTypeEither.left().value();
223 dependencies.add(derivedFrom);
224 nodeTypes.stream().filter(derivedFromCandidate -> derivedFrom.contentEquals(derivedFromCandidate.getMappedNodeType().getKey()))
225 .forEach(derivedFromNodeType -> dependencies.addAll(getDependencyTypes(derivedFromNodeType, nodeTypes)));
230 private Map<String, NodeTypeDefinition> getNodeTypeDefinitions(final Set<String> nodeTypesToGet) {
231 final Map<String, NodeTypeDefinition> foundNodeTypes = getTypes(nodeTypesToGet);
232 final Map<String, NodeTypeDefinition> nodeTypesToReturn = new HashMap<>(foundNodeTypes);
233 final Set<String> recursiveNodeTypesToGet = new HashSet<>();
234 foundNodeTypes.values().forEach(nodeTypeDef -> {
235 Either<Object, ResultStatusEnum> derivedFromTypeEither =
236 findToscaElement((Map<String, Object>) nodeTypeDef.getMappedNodeType().getValue(), TypeUtils.ToscaTagNamesEnum.DERIVED_FROM,
237 ToscaElementTypeEnum.STRING);
238 if (derivedFromTypeEither.isLeft()) {
239 recursiveNodeTypesToGet.add((String) derivedFromTypeEither.left().value());
242 recursiveNodeTypesToGet.removeAll(nodeTypesToGet);
243 if (CollectionUtils.isNotEmpty(recursiveNodeTypesToGet)) {
244 nodeTypesToReturn.putAll(getNodeTypeDefinitions(recursiveNodeTypesToGet));
246 return nodeTypesToReturn;
249 private Map<String, NodeTypeDefinition> getTypes(final Set<String> nodeTypes) {
250 final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap = new HashMap<>();
251 final Set<String> lowerPrecedenceImports = new HashSet<>();
253 if (model != null && !model.equals(Constants.DEFAULT_MODEL_NAME)) {
254 final Set<String> modelImports = new HashSet<>();
255 modelOperation.findAllModelImports(model, true).forEach(modelImport -> modelImports.add("Definitions/" + modelImport.getFullPath()));
257 lowerPrecedenceImports.add("Definitions/" + ModelOperation.ADDITIONAL_TYPE_DEFINITIONS_PATH);
258 lowerPrecedenceImports.addAll(modelImports);
260 mainTemplateImports.entrySet().stream().filter(entry -> modelImports.contains(entry.getKey()))
261 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
263 mainTemplateImports.entrySet().stream().filter(entry -> entry.getKey().equals(ModelOperation.ADDITIONAL_TYPE_DEFINITIONS_PATH.toString()))
264 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
267 mainTemplateImports.entrySet().stream().filter(entry -> !lowerPrecedenceImports.contains(entry.getKey()))
268 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
270 return nodeTypeDefinitionsMap;
273 private void addTypesFromTemplate(final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap, final Map<String, Object> mappedTemplate,
274 final Set<String> nodeTypes) {
275 final Map<String, Object> types = getTypesFromTemplate(mappedTemplate, ToscaTagNamesEnum.NODE_TYPES, nodeTypes);
276 if (MapUtils.isNotEmpty(types)) {
277 types.entrySet().forEach(typesEntry -> {
278 final NodeTypeMetadata metadata = getMetaDataFromTemplate(mappedTemplate, typesEntry.getKey());
279 nodeTypeDefinitionsMap.put(typesEntry.getKey(), new NodeTypeDefinition(typesEntry, metadata));
284 @SuppressWarnings("unchecked")
285 private Set<String> getNodeTypesUsedInToscaTemplate(Map<String, Object> mappedToscaTemplate) {
286 final Either<Object, ResultStatusEnum> nodeTemplatesEither = findToscaElement(mappedToscaTemplate,
287 TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP);
288 final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>();
289 if (nodeTemplatesEither.isLeft()) {
290 final Map<String, Map<String, Object>> nodeTemplates =
291 (Map<String, Map<String, Object>>) nodeTemplatesEither.left().value();
292 nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates));
294 return nodeTypesUsedInNodeTemplates;
297 private NodeTypeMetadata getMetaDataFromTemplate(Map<String, Object> mappedResourceTemplate, String nodeTemplateType) {
298 NodeTypeMetadata nodeTypeMetadata = new NodeTypeMetadata();
299 Either<Map<String, Object>, ImportUtils.ResultStatusEnum> metadataEither = ImportUtils.findFirstToscaMapElement(mappedResourceTemplate,
300 TypeUtils.ToscaTagNamesEnum.METADATA);
301 if (metadataEither.isLeft() && metadataEither.left().value().get("type").equals(ResourceTypeEnum.VFC.getValue())) {
302 Map<String, Object> metadata = metadataEither.left().value();
303 createMetadataFromTemplate(nodeTypeMetadata, metadata, nodeTemplateType);
305 nodeTypeMetadata = createDefaultMetadata(nodeTemplateType);
307 return nodeTypeMetadata;
310 private void createMetadataFromTemplate(NodeTypeMetadata nodeTypeMetadata, Map<String, Object> metadata, String nodeTemplateType) {
311 nodeTypeMetadata.setToscaName(nodeTemplateType);
312 nodeTypeMetadata.setContactId(getModifier().getUserId());
313 nodeTypeMetadata.setDescription((String) metadata.get("description"));
314 List<String> tags = new ArrayList<>();
315 tags.add((String) metadata.get("name"));
316 nodeTypeMetadata.setTags(tags);
317 SubCategoryDefinition subCategory = new SubCategoryDefinition();
318 subCategory.setName((String) metadata.get("subcategory"));
319 CategoryDefinition category = new CategoryDefinition();
320 category.setName((String) metadata.get("category"));
321 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
322 category.setIcons(List.of(DEFAULT_ICON));
323 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
324 category.addSubCategory(subCategory);
325 List<CategoryDefinition> categories = new ArrayList<>();
326 categories.add(category);
327 nodeTypeMetadata.setCategories(categories);
328 nodeTypeMetadata.setName((String) metadata.get("name"));
329 nodeTypeMetadata.setIcon("defaulticon");
330 nodeTypeMetadata.setResourceVendorModelNumber((String) metadata.get("resourceVendorModelNumber"));
331 nodeTypeMetadata.setResourceType((String) metadata.get("type"));
332 nodeTypeMetadata.setVendorName((String) metadata.get("resourceVendor"));
333 nodeTypeMetadata.setVendorRelease(String.valueOf(metadata.get("resourceVendorRelease")));
334 nodeTypeMetadata.setModel(model);
335 nodeTypeMetadata.setNormative(false);
338 private NullNodeTypeMetadata createDefaultMetadata(String nodeTemplateType) {
339 NullNodeTypeMetadata nodeTypeMetadata = new NullNodeTypeMetadata();
340 nodeTypeMetadata.setToscaName(nodeTemplateType);
341 nodeTypeMetadata.setModel(model);
342 return nodeTypeMetadata;