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;
38 import org.apache.commons.collections.CollectionUtils;
39 import org.apache.commons.collections.MapUtils;
40 import org.openecomp.sdc.be.components.impl.ImportUtils;
41 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
42 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
44 import org.openecomp.sdc.be.model.InterfaceDefinition;
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.utils.TypeUtils;
53 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
54 import org.openecomp.sdc.common.log.wrappers.Logger;
55 import org.yaml.snakeyaml.Yaml;
58 * Provides access to the contents of a Service CSAR
60 public class ServiceCsarInfo extends CsarInfo {
62 private static final Logger log = Logger.getLogger(ServiceCsarInfo.class);
63 private final Map<String, Map<String, Object>> mainTemplateImports;
64 private List<NodeTypeDefinition> nodeTypeDefinitions;
65 private final String model;
67 public ServiceCsarInfo(final User modifier, final String csarUUID, final Map<String, byte[]> csar,
68 final String vfResourceName, final String model,
69 final String mainTemplateName, final String mainTemplateContent, final boolean isUpdate) {
70 super(modifier, csarUUID, csar, vfResourceName, mainTemplateName, mainTemplateContent, isUpdate);
72 final Path mainTemplateDir = Paths.get(getMainTemplateName().substring(0, getMainTemplateName().lastIndexOf('/') + 1));
73 final Collection<Path> filesHandled = new HashSet<>();
74 filesHandled.add(Paths.get(mainTemplateName));
75 this.mainTemplateImports = getTemplateImports(csar, new Yaml().load(mainTemplateContent), mainTemplateDir, filesHandled);
78 private Map<String, Map<String, Object>> getTemplateImports(final Map<String, byte[]> csar, Map<String, Object> mappedToscaMainTemplate,
79 final Path fileParentDir, final Collection<Path> filesHandled) {
80 final Map<String, Map<String, Object>> templateImports = new HashMap<>();
82 final List<Path> importFilePaths = getTemplateImportFilePaths(mappedToscaMainTemplate, fileParentDir);
84 importFilePaths.stream().filter(path -> !filesHandled.contains(path)).forEach(
86 byte[] importFile = csar.get(importFilePath.toString());
87 if (importFile != null) {
88 filesHandled.add(importFilePath);
89 Map<String, Object> mappedImportFile = new Yaml().load(new String(csar.get(importFilePath.toString())));
90 templateImports.put(importFilePath.toString(), mappedImportFile);
91 templateImports.putAll(getTemplateImports(csar, mappedImportFile, importFilePath.getParent(), filesHandled));
93 log.info("Import {} cannot be found in CSAR", importFilePath.toString());
97 return templateImports;
100 @SuppressWarnings({"unchecked", "rawtypes"})
101 private List<Path> getTemplateImportFilePaths(final Map<String, Object> mappedToscaTemplate, final Path fileParentDir) {
102 final Either<Object, ResultStatusEnum> importsEither =
103 findToscaElement(mappedToscaTemplate, ToscaTagNamesEnum.IMPORTS, ToscaElementTypeEnum.ALL);
105 if (importsEither.isLeft()) {
106 final List importsList = (List) importsEither.left().value();
107 if (CollectionUtils.isNotEmpty(importsList)) {
108 if (importsList.get(0) instanceof String) {
109 List<Path> importPaths = new ArrayList<>();
112 final Path path = fileParentDir == null ?
113 Paths.get((String) importPath) : fileParentDir.resolve(Paths.get((String) importPath)).normalize();
114 importPaths.add(path);
117 } else if (importsList.get(0) instanceof Map) {
118 return getTemplateImportFilePathsMultiLineGrammar(importsList, fileParentDir);
123 return Collections.emptyList();
126 @SuppressWarnings("unchecked")
127 private List<Path> getTemplateImportFilePathsMultiLineGrammar(final List<Map<String, Object>> importsList, final Path fileParentDir) {
128 final List<Path> importFiles = new ArrayList<>();
130 for (Map<String, Object> importFileMultiLineGrammar : importsList) {
131 if (MapUtils.isNotEmpty(importFileMultiLineGrammar)) {
132 if (importFileMultiLineGrammar.values().iterator().next() instanceof String) {
133 Path relativePath = Paths.get((String) importFileMultiLineGrammar.get("file"));
134 Path absolutePath = fileParentDir == null ? relativePath : fileParentDir.resolve(relativePath).normalize();
135 importFiles.add(absolutePath);
136 } else if (importFileMultiLineGrammar.values().iterator().next() instanceof Map) {
137 importFileMultiLineGrammar.values().forEach(value -> {
138 Path relativePath = Paths.get((String) ((Map<String, Object>) value).get("file"));
139 Path absolutePath = fileParentDir == null ? relativePath : fileParentDir.resolve(relativePath).normalize();
140 importFiles.add(absolutePath);
149 public Map<String, NodeTypeInfo> extractTypesInfo() {
150 return Collections.emptyMap();
154 public Map<String, Object> getDataTypes() {
155 return getTypes(ToscaTagNamesEnum.DATA_TYPES);
159 public Map<String, Object> getGroupTypes() {
160 return getTypes(ToscaTagNamesEnum.GROUP_TYPES);
164 public Map<String, Object> getCapabilityTypes() {
165 return getTypes(ToscaTagNamesEnum.CAPABILITY_TYPES);
168 private Map<String, Object> getTypes(ToscaTagNamesEnum toscaTag) {
169 final Map<String, Object> types = new HashMap<>();
170 mainTemplateImports.entrySet().forEach(entry -> types.putAll(getTypesFromTemplate(entry.getValue(), toscaTag)));
171 types.putAll(getTypesFromTemplate(getMappedToscaMainTemplate(), toscaTag));
175 public Map<String, Object> getArtifactTypes() {
176 final Map<String, Object> artifactsTypes = new HashMap<>();
177 mainTemplateImports.entrySet()
178 .forEach(entry -> artifactsTypes.putAll(getTypesFromTemplate(entry.getValue(), TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES)));
179 artifactsTypes.putAll(getTypesFromTemplate(getMappedToscaMainTemplate(), TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES));
180 return artifactsTypes;
184 public Map<String, Object> getInterfaceTypes() {
185 return getTypes(ToscaTagNamesEnum.INTERFACE_TYPES);
188 public List<NodeTypeDefinition> getNodeTypesUsed() {
189 if (nodeTypeDefinitions == null) {
190 nodeTypeDefinitions = new ArrayList<>();
191 final Set<String> nodeTypesUsed = getNodeTypesUsedInToscaTemplate(getMappedToscaMainTemplate());
192 nodeTypeDefinitions.addAll(getNodeTypeDefinitions(nodeTypesUsed));
194 nodeTypeDefinitions = sortNodeTypesByDependencyOrder(nodeTypeDefinitions);
195 return nodeTypeDefinitions;
198 private List<NodeTypeDefinition> sortNodeTypesByDependencyOrder(final List<NodeTypeDefinition> nodeTypes) {
199 final List<NodeTypeDefinition> sortedNodeTypeDefinitions = new ArrayList<>();
200 final Map<String, NodeTypeDefinition> nodeTypeDefinitionsMap = new HashMap<>();
202 nodeTypes.forEach(nodeType -> {
203 int highestDependencyIndex = -1;
204 for (final String dependencyName : getDependencyTypes(nodeType, nodeTypes)) {
205 final NodeTypeDefinition dependency = nodeTypeDefinitionsMap.get(dependencyName);
206 final int indexOfDependency = sortedNodeTypeDefinitions.lastIndexOf(dependency);
207 highestDependencyIndex = Math.max(indexOfDependency, highestDependencyIndex);
209 sortedNodeTypeDefinitions.add(highestDependencyIndex + 1, nodeType);
210 nodeTypeDefinitionsMap.put(nodeType.getMappedNodeType().getKey(), nodeType);
212 return sortedNodeTypeDefinitions;
215 private Collection<String> getDependencyTypes(final NodeTypeDefinition nodeType, final List<NodeTypeDefinition> nodeTypes) {
216 final Set<String> dependencies = new HashSet<>();
217 Either<Object, ResultStatusEnum> derivedFromTypeEither = findToscaElement((Map<String, Object>) nodeType.getMappedNodeType().getValue(),
218 TypeUtils.ToscaTagNamesEnum.DERIVED_FROM, ToscaElementTypeEnum.STRING);
219 if (derivedFromTypeEither.isLeft() && derivedFromTypeEither.left().value() != null) {
220 final String derivedFrom = (String) derivedFromTypeEither.left().value();
221 dependencies.add(derivedFrom);
222 nodeTypes.stream().filter(derivedFromCandidate -> derivedFrom.contentEquals(derivedFromCandidate.getMappedNodeType().getKey()))
223 .forEach(derivedFromNodeType -> dependencies.addAll(getDependencyTypes(derivedFromNodeType, nodeTypes)));
228 private Set<NodeTypeDefinition> getNodeTypeDefinitions(final Set<String> nodeTypesToGet) {
229 final Set<NodeTypeDefinition> foundNodeTypes = getTypes(nodeTypesToGet);
230 final Set<NodeTypeDefinition> nodeTypesToReturn = new HashSet<>(foundNodeTypes);
231 final Set<String> recursiveNodeTypesToGet = new HashSet<>();
232 foundNodeTypes.forEach(nodeTypeDef -> {
233 Either<Object, ResultStatusEnum> derivedFromTypeEither =
234 findToscaElement((Map<String, Object>) nodeTypeDef.getMappedNodeType().getValue(), TypeUtils.ToscaTagNamesEnum.DERIVED_FROM,
235 ToscaElementTypeEnum.STRING);
236 if (derivedFromTypeEither.isLeft()) {
237 recursiveNodeTypesToGet.add((String) derivedFromTypeEither.left().value());
240 recursiveNodeTypesToGet.removeAll(nodeTypesToGet);
241 if (CollectionUtils.isNotEmpty(recursiveNodeTypesToGet)) {
242 nodeTypesToReturn.addAll(getNodeTypeDefinitions(recursiveNodeTypesToGet));
244 return nodeTypesToReturn;
248 private Set<NodeTypeDefinition> getTypes(final Set<String> nodeTypes) {
249 Set<NodeTypeDefinition> nodeTypeDefinitionsLocal = new HashSet<>();
250 mainTemplateImports.values().forEach(template -> {
251 final Map<String, Object> types = getTypesFromTemplate(template, ToscaTagNamesEnum.NODE_TYPES, nodeTypes);
252 if (MapUtils.isNotEmpty(types)) {
253 types.entrySet().forEach(typesEntry -> {
254 final NodeTypeMetadata metadata =
255 getMetaDataFromTemplate(template, typesEntry.getKey());
256 nodeTypeDefinitionsLocal.add(new NodeTypeDefinition(typesEntry, metadata));
260 return nodeTypeDefinitionsLocal;
263 @SuppressWarnings("unchecked")
264 private Set<String> getNodeTypesUsedInToscaTemplate(Map<String, Object> mappedToscaTemplate) {
265 final Either<Object, ResultStatusEnum> nodeTemplatesEither = findToscaElement(mappedToscaTemplate,
266 TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP);
267 final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>();
268 if (nodeTemplatesEither.isLeft()) {
269 final Map<String, Map<String, Object>> nodeTemplates =
270 (Map<String, Map<String, Object>>) nodeTemplatesEither.left().value();
271 nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates));
273 return nodeTypesUsedInNodeTemplates;
276 private NodeTypeMetadata getMetaDataFromTemplate(Map<String, Object> mappedResourceTemplate, String nodeTemplateType) {
277 NodeTypeMetadata nodeTypeMetadata = new NodeTypeMetadata();
278 Either<Map<String, Object>, ImportUtils.ResultStatusEnum> metadataEither = ImportUtils.findFirstToscaMapElement(mappedResourceTemplate,
279 TypeUtils.ToscaTagNamesEnum.METADATA);
280 if (metadataEither.isLeft() && metadataEither.left().value().get("type").equals(ResourceTypeEnum.VFC.getValue())) {
281 Map<String, Object> metadata = metadataEither.left().value();
282 createMetadataFromTemplate(nodeTypeMetadata, metadata, nodeTemplateType);
284 nodeTypeMetadata = createDefaultMetadata(nodeTemplateType);
286 return nodeTypeMetadata;
289 private void createMetadataFromTemplate(NodeTypeMetadata nodeTypeMetadata, Map<String, Object> metadata, String nodeTemplateType) {
290 nodeTypeMetadata.setToscaName(nodeTemplateType);
291 nodeTypeMetadata.setContactId(getModifier().getUserId());
292 nodeTypeMetadata.setDescription((String) metadata.get("description"));
293 List<String> tags = new ArrayList<>();
294 tags.add((String) metadata.get("name"));
295 nodeTypeMetadata.setTags(tags);
296 SubCategoryDefinition subCategory = new SubCategoryDefinition();
297 subCategory.setName((String) metadata.get("subcategory"));
298 CategoryDefinition category = new CategoryDefinition();
299 category.setName((String) metadata.get("category"));
300 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
301 category.setIcons(List.of(DEFAULT_ICON));
302 category.setNormalizedName(((String) metadata.get("category")).toLowerCase());
303 category.addSubCategory(subCategory);
304 List<CategoryDefinition> categories = new ArrayList<>();
305 categories.add(category);
306 nodeTypeMetadata.setCategories(categories);
307 nodeTypeMetadata.setName((String) metadata.get("name"));
308 nodeTypeMetadata.setIcon("defaulticon");
309 nodeTypeMetadata.setResourceVendorModelNumber((String) metadata.get("resourceVendorModelNumber"));
310 nodeTypeMetadata.setResourceType((String) metadata.get("type"));
311 nodeTypeMetadata.setVendorName((String) metadata.get("resourceVendor"));
312 nodeTypeMetadata.setVendorRelease(String.valueOf(metadata.get("resourceVendorRelease")));
313 nodeTypeMetadata.setModel(model);
314 nodeTypeMetadata.setNormative(false);
317 private NullNodeTypeMetadata createDefaultMetadata(String nodeTemplateType) {
318 NullNodeTypeMetadata nodeTypeMetadata = new NullNodeTypeMetadata();
319 nodeTypeMetadata.setToscaName(nodeTemplateType);
320 nodeTypeMetadata.setModel(model);
321 return nodeTypeMetadata;