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.findToscaElement;
25 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES;
26 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GROUP_TYPES;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
36 import java.util.regex.Pattern;
37 import org.apache.commons.collections.CollectionUtils;
38 import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
39 import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
40 import org.openecomp.sdc.be.config.NonManoConfiguration;
41 import org.openecomp.sdc.be.model.NodeTypeInfo;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.tosca.CsarUtils;
44 import org.openecomp.sdc.be.utils.TypeUtils;
45 import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.yaml.snakeyaml.Yaml;
50 * Provides access to the contents of a CSAR which has been created through the SDC onboarding process
52 public class OnboardedCsarInfo extends CsarInfo {
54 private List<Map.Entry<String, byte[]>> globalSubstitutes;
56 OnboardedCsarInfo(NonManoConfiguration nonManoConfiguration) {
57 super(nonManoConfiguration);
60 public OnboardedCsarInfo(final User modifier, final String csarUUID, final Map<String, byte[]> csar, final String vfResourceName,
61 final String mainTemplateName, final String mainTemplateContent, final boolean isUpdate) {
62 super(modifier, csarUUID, csar, vfResourceName, mainTemplateName, mainTemplateContent, isUpdate);
63 this.globalSubstitutes = getGlobalSubstitutes(csar);
66 public OnboardedCsarInfo(final User modifier, final String csarUUID, final String csarVersionId, final Map<String, byte[]> csarContent,
67 final String vfResourceName, final String mainTemplateName, final String mainTemplateContent, final boolean isUpdate) {
68 super(modifier, csarUUID, csarVersionId, csarContent, vfResourceName, mainTemplateName, mainTemplateContent, isUpdate);
69 this.globalSubstitutes = getGlobalSubstitutes(csar);
72 private List<Map.Entry<String, byte[]>> getGlobalSubstitutes(final Map<String, byte[]> csar) {
73 final List<Map.Entry<String, byte[]>> globalSubstitutesInCsar = new ArrayList<>();
74 for (Map.Entry<String, byte[]> entry : csar.entrySet()) {
75 if (isAServiceTemplate(entry.getKey()) && isGlobalSubstitute(entry.getKey())) {
76 globalSubstitutesInCsar.add(entry);
79 return globalSubstitutesInCsar;
82 public Map<String, NodeTypeInfo> extractTypesInfo() {
83 final Map<String, NodeTypeInfo> nodeTypesInfo = new HashMap<>();
84 final Set<String> nodeTypesUsedInNodeTemplates = new HashSet<>();
85 for (Map.Entry<String, byte[]> entry : getCsar().entrySet()) {
86 extractNodeTypeInfo(nodeTypesInfo, nodeTypesUsedInNodeTemplates, entry);
88 if (CollectionUtils.isNotEmpty(globalSubstitutes)) {
89 setDerivedFrom(nodeTypesInfo);
90 addGlobalSubstitutionsToNodeTypes(nodeTypesUsedInNodeTemplates, nodeTypesInfo);
93 markNestedVfc(getMappedToscaMainTemplate(), nodeTypesInfo);
97 @SuppressWarnings("unchecked")
98 private void extractNodeTypeInfo(final Map<String, NodeTypeInfo> nodeTypesInfo, final Set<String> nodeTypesUsedInNodeTemplates,
99 final Map.Entry<String, byte[]> entry) {
100 if (isAServiceTemplate(entry.getKey()) && !isGlobalSubstitute(entry.getKey())) {
101 final Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(new String(entry.getValue()));
102 findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP).right()
103 .on(sub -> handleSubstitutionMappings(nodeTypesInfo, entry, mappedToscaTemplate, (Map<String, Object>) sub));
104 final Either<Object, ResultStatusEnum> nodeTypesEither =
105 findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP);
106 if (nodeTypesEither.isLeft()) {
107 final Map<String, Map<String, Object>> nodeTemplates = (Map<String, Map<String, Object>>) nodeTypesEither.left().value();
108 nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates));
113 private boolean isAServiceTemplate(final String filePath) {
114 return Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(filePath).matches();
117 private boolean isGlobalSubstitute(final String fileName) {
118 return fileName.equalsIgnoreCase(Constants.GLOBAL_SUBSTITUTION_TYPES_SERVICE_TEMPLATE)
119 || fileName.equalsIgnoreCase(Constants.ABSTRACT_SUBSTITUTE_GLOBAL_TYPES_SERVICE_TEMPLATE);
123 private ResultStatusEnum handleSubstitutionMappings(final Map<String, NodeTypeInfo> nodeTypesInfo, final Map.Entry<String, byte[]> entry,
124 final Map<String, Object> mappedToscaTemplate,
125 final Map<String, Object> substitutionMappings) {
126 final Set<String> nodeTypesDefinedInTemplate = findNodeTypesDefinedInTemplate(mappedToscaTemplate);
127 if (substitutionMappings.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName())
128 && !nodeTypesDefinedInTemplate.contains(substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()))) {
129 NodeTypeInfo nodeTypeInfo = new NodeTypeInfo();
130 nodeTypeInfo.setSubstitutionMapping(true);
131 nodeTypeInfo.setType((String) substitutionMappings.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPE.getElementName()));
132 nodeTypeInfo.setTemplateFileName(entry.getKey());
133 nodeTypeInfo.setMappedToscaTemplate(mappedToscaTemplate);
134 nodeTypesInfo.put(nodeTypeInfo.getType(), nodeTypeInfo);
136 return ResultStatusEnum.OK;
139 @SuppressWarnings("unchecked")
140 private Set<String> findNodeTypesDefinedInTemplate(final Map<String, Object> mappedToscaTemplate) {
141 final Either<Object, ResultStatusEnum> nodeTypesEither =
142 findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP);
143 if (nodeTypesEither.isLeft()) {
144 final Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value();
145 return nodeTypes.keySet();
147 return Collections.emptySet();
150 @SuppressWarnings("unchecked")
151 private void setDerivedFrom(final Map<String, NodeTypeInfo> nodeTypesInfo) {
152 for (Map.Entry<String, byte[]> entry : globalSubstitutes) {
153 final String yamlFileContents = new String(entry.getValue());
154 final Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(yamlFileContents);
155 Either<Object, ResultStatusEnum> nodeTypesEither =
156 findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP);
157 if (nodeTypesEither.isLeft()) {
158 Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value();
159 for (Map.Entry<String, Object> nodeType : nodeTypes.entrySet()) {
160 processNodeType(nodeTypesInfo, nodeType);
166 @SuppressWarnings("unchecked")
167 private void processNodeType(final Map<String, NodeTypeInfo> nodeTypesInfo, final Map.Entry<String, Object> nodeType) {
168 final Map<String, Object> nodeTypeMap = (Map<String, Object>) nodeType.getValue();
169 if (nodeTypeMap.containsKey(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()) && nodeTypesInfo.containsKey(nodeType.getKey())) {
170 final NodeTypeInfo nodeTypeInfo = nodeTypesInfo.get(nodeType.getKey());
171 final List<String> derivedFrom = new ArrayList<>();
172 derivedFrom.add((String) nodeTypeMap.get(TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName()));
173 nodeTypeInfo.setDerivedFrom(derivedFrom);
177 @SuppressWarnings("unchecked")
178 private void addGlobalSubstitutionsToNodeTypes(final Set<String> nodeTypesUsedInNodeTemplates, final Map<String, NodeTypeInfo> nodeTypesInfo) {
179 for (Map.Entry<String, byte[]> entry : globalSubstitutes) {
180 final String yamlFileContents = new String(entry.getValue());
181 final Map<String, Object> mappedToscaTemplate = new Yaml().load(yamlFileContents);
182 final Either<Object, ResultStatusEnum> nodeTypesEither =
183 findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES, ToscaElementTypeEnum.MAP);
184 if (nodeTypesEither.isLeft()) {
185 final Map<String, Object> nodeTypes = (Map<String, Object>) nodeTypesEither.left().value();
186 for (final Map.Entry<String, Object> nodeType : nodeTypes.entrySet()) {
187 if (!nodeTypesInfo.containsKey(nodeType.getKey()) && nodeTypesUsedInNodeTemplates.contains(nodeType.getKey())) {
188 nodeTypesInfo.put(nodeType.getKey(), buildNodeTypeInfo(nodeType, entry.getKey(), mappedToscaTemplate));
196 public Map<String, Object> getArtifactTypes() {
197 if (artifacttypeDefinitions == null) {
198 artifacttypeDefinitions = new HashMap<>();
199 for (Map.Entry<String, byte[]> entry : globalSubstitutes) {
200 final String yamlFileContents = new String(entry.getValue());
201 final Map<String, Object> mappedToscaTemplate = new Yaml().load(yamlFileContents);
202 artifacttypeDefinitions.putAll(getTypesFromTemplate(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES));
204 artifacttypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES));
206 return artifacttypeDefinitions;
210 public Map<String, Object> getDataTypes() {
211 return getTypes(DATA_TYPES);
215 public Map<String, Object> getGroupTypes() {
216 return getTypes(GROUP_TYPES);
220 public Map<String, Object> getCapabilityTypes() {
221 return getTypes(ToscaTagNamesEnum.CAPABILITY_TYPES);
224 private Map<String, Object> getTypes(ToscaTagNamesEnum toscaTag) {
225 final Map<String, Object> types = new HashMap<>();
226 for (Map.Entry<String, byte[]> entry : globalSubstitutes) {
227 final Map<String, Object> mappedToscaTemplate = new Yaml().load(new String(entry.getValue()));
228 types.putAll(getTypesFromTemplate(mappedToscaTemplate, toscaTag));
230 types.putAll(getTypesFromTemplate(mappedToscaMainTemplate, toscaTag));