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);
173 private Map<String, Object> getTypes(ToscaTagNamesEnum toscaTag) {
174 final Map<String, Object> types = new HashMap<>();
175 mainTemplateImports.entrySet().forEach(entry -> types.putAll(getTypesFromTemplate(entry.getValue(), toscaTag)));
176 types.putAll(getTypesFromTemplate(getMappedToscaMainTemplate(), toscaTag));
180 public Map<String, Object> getArtifactTypes() {
181 final Map<String, Object> artifactsTypes = new HashMap<>();
182 mainTemplateImports.entrySet()
183 .forEach(entry -> artifactsTypes.putAll(getTypesFromTemplate(entry.getValue(), TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES)));
184 artifactsTypes.putAll(getTypesFromTemplate(getMappedToscaMainTemplate(), TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES));
185 return artifactsTypes;
189 public Map<String, Object> getInterfaceTypes() {
190 return getTypes(ToscaTagNamesEnum.INTERFACE_TYPES);
193 public List<NodeTypeDefinition> getNodeTypesUsed() {
194 if (nodeTypeDefinitions == null) {
195 nodeTypeDefinitions = new ArrayList<>();
196 final Set<String> nodeTypesUsed = getNodeTypesUsedInToscaTemplate(getMappedToscaMainTemplate());
197 nodeTypeDefinitions.addAll(getNodeTypeDefinitions(nodeTypesUsed).values());
199 nodeTypeDefinitions = sortNodeTypesByDependencyOrder(nodeTypeDefinitions);
200 return nodeTypeDefinitions;
203 private List<NodeTypeDefinition> sortNodeTypesByDependencyOrder(final List<NodeTypeDefinition> nodeTypes) {
204 final List<NodeTypeDefinition> sortedNodeTypeDefinitions = new ArrayList<>();
205 final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap = new HashMap<>();
207 nodeTypes.forEach(nodeType -> {
208 int highestDependencyIndex = -1;
209 for (final String dependencyName : getDependencyTypes(nodeType, nodeTypes)) {
210 final NodeTypeDefinition dependency = nodeTypeDefinitionsMap.get(dependencyName);
211 final int indexOfDependency = sortedNodeTypeDefinitions.lastIndexOf(dependency);
212 highestDependencyIndex = Math.max(indexOfDependency, highestDependencyIndex);
214 sortedNodeTypeDefinitions.add(highestDependencyIndex + 1, nodeType);
215 nodeTypeDefinitionsMap.put(nodeType.getMappedNodeType().getKey(), nodeType);
217 return sortedNodeTypeDefinitions;
220 private Collection<String> getDependencyTypes(final NodeTypeDefinition nodeType, final List<NodeTypeDefinition> nodeTypes) {
221 final Set<String> dependencies = new HashSet<>();
222 Either<Object, ResultStatusEnum> derivedFromTypeEither = findToscaElement((Map<String, Object>) nodeType.getMappedNodeType().getValue(),
223 TypeUtils.ToscaTagNamesEnum.DERIVED_FROM, ToscaElementTypeEnum.STRING);
224 if (derivedFromTypeEither.isLeft() && derivedFromTypeEither.left().value() != null) {
225 final String derivedFrom = (String) derivedFromTypeEither.left().value();
226 dependencies.add(derivedFrom);
227 nodeTypes.stream().filter(derivedFromCandidate -> derivedFrom.contentEquals(derivedFromCandidate.getMappedNodeType().getKey()))
228 .forEach(derivedFromNodeType -> dependencies.addAll(getDependencyTypes(derivedFromNodeType, nodeTypes)));
233 private Map<String, NodeTypeDefinition> getNodeTypeDefinitions(final Set<String> nodeTypesToGet) {
234 final Map<String, NodeTypeDefinition> foundNodeTypes = getTypes(nodeTypesToGet);
235 final Map<String, NodeTypeDefinition> nodeTypesToReturn = new HashMap<>(foundNodeTypes);
236 final Set<String> recursiveNodeTypesToGet = new HashSet<>();
237 foundNodeTypes.values().forEach(nodeTypeDef -> {
238 Either<Object, ResultStatusEnum> derivedFromTypeEither =
239 findToscaElement((Map<String, Object>) nodeTypeDef.getMappedNodeType().getValue(), TypeUtils.ToscaTagNamesEnum.DERIVED_FROM,
240 ToscaElementTypeEnum.STRING);
241 if (derivedFromTypeEither.isLeft()) {
242 recursiveNodeTypesToGet.add((String) derivedFromTypeEither.left().value());
245 recursiveNodeTypesToGet.removeAll(nodeTypesToGet);
246 if (CollectionUtils.isNotEmpty(recursiveNodeTypesToGet)) {
247 nodeTypesToReturn.putAll(getNodeTypeDefinitions(recursiveNodeTypesToGet));
249 return nodeTypesToReturn;
253 private Map<String, NodeTypeDefinition> getTypes(final Set<String> nodeTypes) {
254 final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap = new HashMap<>();
255 final Set<String> lowerPrecedenceImports = new HashSet<>();
257 if (model != null && !model.equals(Constants.DEFAULT_MODEL_NAME)) {
258 final Set<String> modelImports = new HashSet<>();
259 modelOperation.findAllModelImports(model, true).forEach(modelImport -> modelImports.add("Definitions/" + modelImport.getFullPath()));
261 lowerPrecedenceImports.add("Definitions/" + ModelOperation.ADDITIONAL_TYPE_DEFINITIONS_PATH);
262 lowerPrecedenceImports.addAll(modelImports);
264 mainTemplateImports.entrySet().stream().filter(entry -> modelImports.contains(entry.getKey()))
265 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
267 mainTemplateImports.entrySet().stream().filter(entry -> entry.getKey().equals(ModelOperation.ADDITIONAL_TYPE_DEFINITIONS_PATH.toString()))
268 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
271 mainTemplateImports.entrySet().stream().filter(entry -> !lowerPrecedenceImports.contains(entry.getKey()))
272 .forEach(template -> addTypesFromTemplate(nodeTypeDefinitionsMap, template.getValue(), nodeTypes));
274 return nodeTypeDefinitionsMap;
278 private void addTypesFromTemplate(final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap, final Map<String, Object> mappedTemplate,
279 final Set<String> nodeTypes) {
280 final Map<String, Object> types = getTypesFromTemplate(mappedTemplate, ToscaTagNamesEnum.NODE_TYPES, nodeTypes);
281 if (MapUtils.isNotEmpty(types)) {
282 types.entrySet().forEach(typesEntry -> {
283 final NodeTypeMetadata metadata = getMetaDataFromTemplate(mappedTemplate, typesEntry.getKey());
284 nodeTypeDefinitionsMap.put(typesEntry.getKey(), new NodeTypeDefinition(typesEntry, metadata));
289 @SuppressWarnings("unchecked")
290 private Set<String> getNodeTypesUsedInToscaTemplate(Map<String, Object> mappedToscaTemplate) {
291 final Either<Object, ResultStatusEnum> nodeTemplatesEither = findToscaElement(mappedToscaTemplate,
292 TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP);
293 final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>();
294 if (nodeTemplatesEither.isLeft()) {
295 final Map<String, Map<String, Object>> nodeTemplates =
296 (Map<String, Map<String, Object>>) nodeTemplatesEither.left().value();
297 nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates));
299 return nodeTypesUsedInNodeTemplates;
302 private NodeTypeMetadata getMetaDataFromTemplate(Map<String, Object> mappedResourceTemplate, String nodeTemplateType) {
303 NodeTypeMetadata nodeTypeMetadata = new NodeTypeMetadata();
304 Either<Map<String, Object>, ImportUtils.ResultStatusEnum> metadataEither = ImportUtils.findFirstToscaMapElement(mappedResourceTemplate,
305 TypeUtils.ToscaTagNamesEnum.METADATA);
306 if (metadataEither.isLeft() && metadataEither.left().value().get("type").equals(ResourceTypeEnum.VFC.getValue())) {
307 Map<String, Object> metadata = metadataEither.left().value();
308 createMetadataFromTemplate(nodeTypeMetadata, metadata, nodeTemplateType);
310 nodeTypeMetadata = createDefaultMetadata(nodeTemplateType);
312 return nodeTypeMetadata;
315 private void createMetadataFromTemplate(NodeTypeMetadata nodeTypeMetadata, Map<String, Object> metadata, String nodeTemplateType) {
316 nodeTypeMetadata.setToscaName(nodeTemplateType);
317 nodeTypeMetadata.setContactId(getModifier().getUserId());
318 nodeTypeMetadata.setDescription((String) metadata.get("description"));
319 List<String> tags = new ArrayList<>();
320 tags.add((String) metadata.get("name"));
321 nodeTypeMetadata.setTags(tags);
322 SubCategoryDefinition subCategory = new SubCategoryDefinition();
323 subCategory.setName((String) metadata.get("subcategory"));
324 CategoryDefinition category = new CategoryDefinition();
325 category.setName((String) metadata.get("category"));
326 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
327 category.setIcons(List.of(DEFAULT_ICON));
328 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
329 category.addSubCategory(subCategory);
330 List<CategoryDefinition> categories = new ArrayList<>();
331 categories.add(category);
332 nodeTypeMetadata.setCategories(categories);
333 nodeTypeMetadata.setName((String) metadata.get("name"));
334 nodeTypeMetadata.setIcon("defaulticon");
335 nodeTypeMetadata.setResourceVendorModelNumber((String) metadata.get("resourceVendorModelNumber"));
336 nodeTypeMetadata.setResourceType((String) metadata.get("type"));
337 nodeTypeMetadata.setVendorName((String) metadata.get("resourceVendor"));
338 nodeTypeMetadata.setVendorRelease(String.valueOf(metadata.get("resourceVendorRelease")));
339 nodeTypeMetadata.setModel(model);
340 nodeTypeMetadata.setNormative(false);
343 private NullNodeTypeMetadata createDefaultMetadata(String nodeTemplateType) {
344 NullNodeTypeMetadata nodeTypeMetadata = new NullNodeTypeMetadata();
345 nodeTypeMetadata.setToscaName(nodeTemplateType);
346 nodeTypeMetadata.setModel(model);
347 return nodeTypeMetadata;