9ab767876d6f446bd0806b1744c3b208287b12d8
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / OnboardedCsarInfo.java
1 /*
2  * -
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.components.csar;
23
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;
27
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;
34 import java.util.Map;
35 import java.util.Set;
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;
48
49 /**
50  * Provides access to the contents of a CSAR which has been created through the SDC onboarding process
51  */
52 public class OnboardedCsarInfo extends CsarInfo {
53
54     private List<Map.Entry<String, byte[]>> globalSubstitutes;
55
56     OnboardedCsarInfo(NonManoConfiguration nonManoConfiguration) {
57         super(nonManoConfiguration);
58     }
59
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);
64     }
65
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);
70     }
71
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);
77             }
78         }
79         return globalSubstitutesInCsar;
80     }
81
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);
87         }
88         if (CollectionUtils.isNotEmpty(globalSubstitutes)) {
89             setDerivedFrom(nodeTypesInfo);
90             addGlobalSubstitutionsToNodeTypes(nodeTypesUsedInNodeTemplates, nodeTypesInfo);
91         }
92
93         markNestedVfc(getMappedToscaMainTemplate(), nodeTypesInfo);
94         return nodeTypesInfo;
95     }
96
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));
109             }
110         }
111     }
112
113     private boolean isAServiceTemplate(final String filePath) {
114         return Pattern.compile(CsarUtils.SERVICE_TEMPLATE_PATH_PATTERN).matcher(filePath).matches();
115     }
116
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);
120     }
121
122
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);
135         }
136         return ResultStatusEnum.OK;
137     }
138
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();
146         }
147         return Collections.emptySet();
148     }
149
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);
161                 }
162             }
163         }
164     }
165
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);
174         }
175     }
176
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));
189                     }
190                 }
191             }
192         }
193     }
194
195     @Override
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));
203             }
204             artifacttypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.ARTIFACT_TYPES));
205         }
206         return artifacttypeDefinitions;
207     }
208
209     @Override
210     public Map<String, Object> getDataTypes() {
211         return getTypes(DATA_TYPES);
212     }
213
214     @Override
215     public Map<String, Object> getGroupTypes() {
216         return getTypes(GROUP_TYPES);
217     }
218
219     @Override
220     public Map<String, Object> getCapabilityTypes() {
221         return getTypes(ToscaTagNamesEnum.CAPABILITY_TYPES);
222     }
223
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));
229         }
230         types.putAll(getTypesFromTemplate(mappedToscaMainTemplate, toscaTag));
231         return types;
232     }
233
234 }