Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / ToscaParserUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils;
22
23 import static org.testng.AssertJUnit.assertNotNull;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
32
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;
53
54 public class ToscaParserUtils {
55
56         private static Logger log = LoggerFactory.getLogger(ToscaParserUtils.class.getName());
57
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);
63                         return map;
64                 }
65                 return null;
66         }
67
68         public static Map<String, Object> downloadAndParseToscaTemplate(User sdncModifierDetails, Component createdComponent) throws Exception {
69                 String artifactUniqeId = createdComponent.getToscaArtifacts().get("assettoscatemplate").getUniqueId();
70                 RestResponse toscaTemplate;
71
72                 if (createdComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
73                         toscaTemplate = ArtifactRestUtils.downloadResourceArtifactInternalApi(createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
74
75                 } else {
76                         toscaTemplate = ArtifactRestUtils.downloadServiceArtifactInternalApi(createdComponent.getUniqueId(), sdncModifierDetails, artifactUniqeId);
77                 }
78                 BaseRestUtils.checkSuccess(toscaTemplate);
79
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();
84
85                 InputStream inputStream = new ByteArrayInputStream(decodeBase64);
86
87                 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
88                 return load;
89         }
90
91         public static ToscaDefinition getToscaDefinitionObjectByCsarUuid(String csarUUID) throws Exception {
92
93                 String TOSCAMetaLocation = "TOSCA-Metadata/TOSCA.meta";
94                 Map<?, ?> map = getToscaYamlMap(csarUUID, TOSCAMetaLocation);
95                 assertNotNull("Tosca Entry-Definitions is null", map);
96                 if (map != null) {
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);
105                                         switch (toscaKey) {
106                                         case TOSCA_DEFINITION_VERSION:
107                                                 enrichToscaDefinitionWithToscaVersion(toscaMap, toscaDefinition);
108                                                 break;
109                                         case NODE_TYPES:
110                                                 getToscaNodeTypes(toscaMap, toscaDefinition);
111                                                 break;
112                                         case TOPOLOGY_TEMPLATE:
113                                                 getToscaTopologyTemplate(toscaMap, toscaDefinition);
114                                                 break;
115                                         case IMPORTS:
116                                                 // toscaMap.get("imports");
117                                                 break;
118                                         default:
119                                                 break;
120                                         }
121                                 }
122                                 return toscaDefinition;
123                         }
124                 }
125                 return null;
126
127         }
128
129         public static void enrichToscaDefinitionWithToscaVersion(Map<?, ?> toscaMap, ToscaDefinition toscaDefinition) {
130                 if (toscaMap.get("tosca_definitions_version") != null) {
131                         toscaDefinition.setToscaDefinitionVersion(getToscaVersion(toscaMap));
132                 }
133         }
134
135         public static String getToscaVersion(Map<?, ?> toscaMap) {
136                 return (String) toscaMap.get("tosca_definitions_version");
137         }
138
139         // spec 90 page
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);
149
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);
160                                                                 // found = true;
161                                                                 break;
162                                                         } else {
163                                                                 continue;
164                                                         }
165
166                                                 }
167                                                 // if (found == false) {
168                                                 // System.out.println("Tosca file not valid,
169                                                 // derived_from not found");
170                                                 // }
171                                         }
172
173                                 }
174                                 listToscaNodeTypes.add(toscaNodeTypes);
175                         }
176                         toscaDefinition.setToscaNodeTypes(listToscaNodeTypes);
177                 }
178         }
179
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<>();
186
187                 if (topologyTemplateMap != null) {
188                         getToscaNodeTemplates(topologyTemplateMap, listToscaNodeTemplates);
189                 }
190                 toscaTopologyTemplate.setToscaNodeTemplatesTopologyTemplateDefinition(listToscaNodeTemplates);
191                 toscaDefinition.setToscaTopologyTemplate(toscaTopologyTemplate);
192         }
193
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) {
198
199                         for (Entry<String, Object> nodeTemplates : nodeTemplatesMap.entrySet()) {
200                                 ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates = new ToscaNodeTemplatesTopologyTemplateDefinition();
201                                 getToscaNodeTemplatesName(nodeTemplates, toscaNodeTemplates);
202
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);
209                         }
210                 }
211         }
212
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);
225                                         } else {
226                                                 log.debug("Tosca file not valid, requirements should contain name");
227                                         }
228
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();
235                                                         switch (key) {
236                                                         case "capability":
237                                                                 if (requirementMap.get(key) != null) {
238                                                                         String capability = (String) requirementMap.get(key);
239                                                                         toscaRequirement.setCapability(capability);
240                                                                         break;
241                                                                 } else {
242                                                                         continue;
243                                                                 }
244                                                         case "node":
245                                                                 if (requirementMap.get(key) != null) {
246                                                                         String requirementNode = (String) requirementMap.get(key);
247                                                                         toscaRequirement.setNode(requirementNode);
248                                                                         break;
249                                                                 } else {
250                                                                         continue;
251                                                                 }
252                                                         case "relationship":
253                                                                 if (requirementMap.get(key) != null) {
254                                                                         String relationship = (String) requirementMap.get(key);
255                                                                         toscaRequirement.setRelationship(relationship);
256                                                                         break;
257                                                                 } else {
258                                                                         continue;
259                                                                 }
260                                                         default:
261                                                                 break;
262                                                         }
263                                                 }
264                                         }
265                                         toscaRequirements.add(toscaRequirement);
266                                 }
267                         }
268                 }
269                 toscaNodeTemplates.setRequirements(toscaRequirements);
270         }
271
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);
285                         }
286                 }
287                 toscaNodeTemplates.setProperties(listToscaProperties);
288         }
289
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);
295                 } else {
296                         log.debug("Tosca file not valid, nodeTemplate should contain type");
297                 }
298         }
299
300         protected static void getToscaNodeTemplatesName(Entry<String, Object> nodeTemplates,
301                         ToscaNodeTemplatesTopologyTemplateDefinition toscaNodeTemplates) {
302                 String name = nodeTemplates.getKey();
303                 toscaNodeTemplates.setName(name);
304         }
305 }