2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.utils;
23 import static org.testng.AssertJUnit.assertNotNull;
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Map.Entry;
33 import org.apache.commons.codec.binary.Base64;
34 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
35 import org.openecomp.sdc.be.model.ArtifactUiDownloadData;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.ToscaKeysEnum;
39 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
40 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaDefinition;
41 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTemplatesTopologyTemplateDefinition;
42 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaNodeTypesDefinition;
43 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaPropertiesNodeTemplatesDefinition;
44 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaRequirementsNodeTemplatesDefinition;
45 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaTopologyTemplateDefinition;
46 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
47 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
48 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
49 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.yaml.snakeyaml.Yaml;
54 public class ToscaParserUtils {
56 private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
58 public static Map<?, ?> getToscaYamlMap(String csarUUID, String fileLocation) throws Exception {
59 String csarPayload = CsarValidationUtils.getCsarPayload(csarUUID, fileLocation);
60 if (csarPayload != null) {
61 Yaml yaml = new Yaml();
62 Map<?, ?> map = (Map<?, ?>) yaml.load(csarPayload);
68 public static Map<String, Object> downloadAndParseToscaTemplate(User sdncModifierDetails, Component createdComponent) throws Exception {
69 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscatemplate").getUniqueId();
70 RestResponse toscaTemplate;
72 if (createdComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
73 toscaTemplate = ArtifactRestUtils.downloadResourceArtifactInternalApi(createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
76 toscaTemplate = ArtifactRestUtils.downloadServiceArtifactInternalApi(createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
78 BaseRestUtils.checkSuccess(toscaTemplate);
80 ArtifactUiDownloadData artifactUiDownloadData = ResponseParser.parseToObject(toscaTemplate.getResponse(), ArtifactUiDownloadData.class);
81 byte[] fromUiDownload = artifactUiDownloadData.getBase64Contents().getBytes();
82 byte[] decodeBase64 = Base64.decodeBase64(fromUiDownload);
83 Yaml yaml = new Yaml();
85 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
87 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
91 public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
93 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
94 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
95 assertNotNull("Tosca Entry-Definitions is null", map);
97 String definitionYamlLocation = (String) map.get("Entry-Definitions");
98 Map<?, ?> toscaMap = getToscaYamlMap(csarUUID, definitionYamlLocation);
99 assertNotNull("Tosca definition is null", toscaMap);
100 if (toscaMap != null) {
101 ToscaDefinition toscaDefinition = new ToscaDefinition();
102 Set<?> keySet = toscaMap.keySet();
103 for (Object key : keySet) {
104 ToscaKeysEnum toscaKey = ToscaKeysEnum.findToscaKey((String) key);
106 case TOSCA_DEFINITION_VERSION:
107 enrichToscaDefinitionWithToscaVersion(toscaMap, toscaDefinition);
110 getToscaNodeTypes(toscaMap, toscaDefinition);
112 case TOPOLOGY_TEMPLATE:
113 getToscaTopologyTemplate(toscaMap, toscaDefinition);
116 // toscaMap.get("imports");
122 return toscaDefinition;
129 public static void enrichToscaDefinitionWithToscaVersion(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
130 if (toscaMap.get("tosca_definitions_version") != null) {
131 toscaDefinition.setToscaDefinitionVersion(getToscaVersion(toscaMap));
135 public static String getToscaVersion(Map<?, ?> toscaMap) {
136 return (String) toscaMap.get("tosca_definitions_version");
140 public static void getToscaNodeTypes(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
141 @SuppressWarnings("unchecked")
142 Map<String, Map<String, String>> nodeTypes = (Map<String, Map<String, String>>) toscaMap.get("node_types");
143 List<ToscaNodeTypesDefinition> listToscaNodeTypes = new ArrayList<>();
144 if (nodeTypes != null) {
145 for (Entry<String, Map<String, String>> entry : nodeTypes.entrySet()) {
146 ToscaNodeTypesDefinition toscaNodeTypes = new ToscaNodeTypesDefinition();
147 String toscaNodeName = entry.getKey();
148 toscaNodeTypes.setName(toscaNodeName);
150 Map<String, String> toscaNodeType = entry.getValue();
151 if (toscaNodeType != null) {
152 Set<Entry<String, String>> entrySet = toscaNodeType.entrySet();
153 if (entrySet != null) {
154 // boolean found = false;
155 for (Entry<String, String> toscaNodeTypeMap : entrySet) {
156 String key = toscaNodeTypeMap.getKey();
157 if (key.equals("derived_from")) {
158 String derivedFrom = toscaNodeTypeMap.getValue();
159 toscaNodeTypes.setDerivedFrom(derivedFrom);
167 // if (found == false) {
168 // System.out.println("Tosca file not valid,
169 // derived_from not found");
174 listToscaNodeTypes.add(toscaNodeTypes);
176 toscaDefinition.setToscaNodeTypes(listToscaNodeTypes);
180 public static void getToscaTopologyTemplate(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
181 ToscaTopologyTemplateDefinition toscaTopologyTemplate = new ToscaTopologyTemplateDefinition();
182 @SuppressWarnings("unchecked")
183 Map<String, Map<String, Object>> topologyTemplateMap = (Map<String, Map<String, Object>>) toscaMap
184 .get("topology_template");
185 List<ToscaNodeTemplatesTopologyTemplateDefinition> listToscaNodeTemplates = new ArrayList<>();
187 if (topologyTemplateMap != null) {
188 getToscaNodeTemplates(topologyTemplateMap, listToscaNodeTemplates);
190 toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
191 toscaDefinition.setToscaTopologyTemplate(toscaTopologyTemplate);
194 public static void getToscaNodeTemplates(Map<String, Map<String, Object>> topologyTemplateMap,
195 List<ToscaNodeTemplatesTopologyTemplateDefinition> listToscaNodeTemplates) {
196 Map<String, Object> nodeTemplatesMap = topologyTemplateMap.get("node_templates");
197 if (nodeTemplatesMap != null) {
199 for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
200 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
201 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
203 @SuppressWarnings("unchecked")
204 Map<String, Object> node = (Map<String, Object>) nodeTemplates.getValue();
205 getNodeTemplatesType(toscaNodeTemplates, node);
206 getToscaNodeTemplateProperties(toscaNodeTemplates, node);
207 getToscaNodeTemplateRequirements(toscaNodeTemplates, node);
208 listToscaNodeTemplates.add(toscaNodeTemplates);
213 public static void getToscaNodeTemplateRequirements(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
214 Map<String, Object> node) {
215 List<ToscaRequirementsNodeTemplatesDefinition> toscaRequirements = new ArrayList<>();
216 if (node.get("requirements") != null) {
217 @SuppressWarnings("unchecked")
218 List<Map<String, Object>> requirementList = (List<Map<String, Object>>) node.get("requirements");
219 for (int i = 0; i < requirementList.size(); i++) {
220 for (Entry<String, Object> requirement : requirementList.get(i).entrySet()) {
221 ToscaRequirementsNodeTemplatesDefinition toscaRequirement = new ToscaRequirementsNodeTemplatesDefinition();
222 if (requirement.getKey() != null) {
223 String requirementName = requirement.getKey();
224 toscaRequirement.setName(requirementName);
226 log.debug("Tosca file not valid, requirements should contain name");
229 @SuppressWarnings("unchecked")
230 Map<String, String> requirementMap = (Map<String, String>) requirement.getValue();
231 Set<Entry<String, String>> entrySet = requirementMap.entrySet();
232 if (entrySet != null) {
233 for (Entry<String, String> requirementField : entrySet) {
234 String key = requirementField.getKey();
237 if (requirementMap.get(key) != null) {
238 String capability = (String) requirementMap.get(key);
239 toscaRequirement.setCapability(capability);
245 if (requirementMap.get(key) != null) {
246 String requirementNode = (String) requirementMap.get(key);
247 toscaRequirement.setNode(requirementNode);
253 if (requirementMap.get(key) != null) {
254 String relationship = (String) requirementMap.get(key);
255 toscaRequirement.setRelationship(relationship);
265 toscaRequirements.add(toscaRequirement);
269 toscaNodeTemplates.setRequirements(toscaRequirements);
272 public static void getToscaNodeTemplateProperties(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
273 Map<String, Object> node) {
274 List<ToscaPropertiesNodeTemplatesDefinition> listToscaProperties = new ArrayList<>();
275 if (node.get("properties") != null) {
276 @SuppressWarnings("unchecked")
277 Map<String, Object> properties = (Map<String, Object>) node.get("properties");
278 for (Entry<String, Object> property : properties.entrySet()) {
279 ToscaPropertiesNodeTemplatesDefinition toscaProperty = new ToscaPropertiesNodeTemplatesDefinition();
280 String propertyName = property.getKey();
281 Object propertyValue = property.getValue();
282 toscaProperty.setName(propertyName);
283 toscaProperty.setValue(propertyValue);
284 listToscaProperties.add(toscaProperty);
287 toscaNodeTemplates.setProperties(listToscaProperties);
290 protected static void getNodeTemplatesType(ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates,
291 Map<String, Object> node) {
292 if (node.get("type") != null) {
293 String type = (String) node.get("type");
294 toscaNodeTemplates.setType(type);
296 log.debug("Tosca file not valid, nodeTemplate should contain type");
300 protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
301 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
302 String name = nodeTemplates.getKey();
303 toscaNodeTemplates.setName(name);