Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / CsarParserUtils.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.assertTrue;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.json.simple.JSONArray;
31 import org.json.simple.JSONObject;
32 import org.json.simple.parser.JSONParser;
33 import org.json.simple.parser.ParseException;
34 import org.openecomp.sdc.ci.tests.datatypes.GroupHeatMetaDefinition;
35 import org.openecomp.sdc.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
36 import org.openecomp.sdc.ci.tests.datatypes.PropertyHeatMetaDefinition;
37 import org.openecomp.sdc.ci.tests.datatypes.TypeHeatMetaDefinition;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
39 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaParameterConstants;
40 import org.openecomp.sdc.ci.tests.utils.validation.CsarValidationUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class CsarParserUtils {
45         private static Logger log = LoggerFactory.getLogger(CsarValidationUtils.class.getName());
46
47         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(File csarFileLocation) throws Exception {
48
49                 String artifactHeatMetaLocation = ToscaParameterConstants.HEAT_META_PATH;
50                 String csarPayload = ToscaParserUtils.getYamlPayloadFromCsar(csarFileLocation, artifactHeatMetaLocation);
51                 return getListTypeHeatMetaDefinitionByPayload(csarPayload);
52         }
53
54         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinition(String csarUUID) throws Exception {
55                 
56                 String artifactHeatMetaLocation = ToscaParameterConstants.HEAT_META_PATH;
57                 String csarPayload = ToscaParserUtils.getCsarPayload(csarUUID, artifactHeatMetaLocation);
58                 return getListTypeHeatMetaDefinitionByPayload(csarPayload);
59         }
60
61         public static List<TypeHeatMetaDefinition> getListTypeHeatMetaDefinitionByPayload(String csarPayload) throws ParseException {
62                 JSONParser parser = new JSONParser();
63                 if (csarPayload != null) {
64                         Object parse = parser.parse(csarPayload);
65                         JSONObject jsonObject = (JSONObject) parse;
66                         JSONObject jsonObjectImportStructure = (JSONObject) jsonObject.get(ToscaParameterConstants.IMPORT_STRUCTURE);
67                         List<TypeHeatMetaDefinition> listHeatMetaDefenition = new ArrayList<>();
68                         listHeatMetaDefenition = getArtifactsByGroup(jsonObjectImportStructure, listHeatMetaDefenition);
69                         return listHeatMetaDefenition;
70                 }
71                 return null;
72         }
73         
74         
75         protected static List<TypeHeatMetaDefinition> getArtifactsByGroup(JSONObject jsonObjectImportStructure, List<TypeHeatMetaDefinition> listHeatMetaDefenition) {
76
77                 @SuppressWarnings("unchecked")
78                 Set<Object> typeSet = jsonObjectImportStructure.keySet();
79                 for (Object type : typeSet) {
80                         TypeHeatMetaDefinition heatMetaDefenition = new TypeHeatMetaDefinition();
81                         log.debug(type.toString());
82                         log.debug("{}", jsonObjectImportStructure.get(type));
83                         JSONArray array = (JSONArray) jsonObjectImportStructure.get(type);
84                         heatMetaDefenition.setTypeName((String) type);
85                         List<GroupHeatMetaDefinition> groupHeatMetaDefinitions = new ArrayList<>();
86                         heatMetaDefenition.setGroupHeatMetaDefinition(fetchArtifactByGroupFromJsonArray(array, groupHeatMetaDefinitions, true, false));
87                         listHeatMetaDefenition.add(heatMetaDefenition);
88                 }
89                 return listHeatMetaDefenition;
90         }
91         
92         protected static List<GroupHeatMetaDefinition> fetchArtifactByGroupFromJsonArray(JSONArray array, List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, Boolean openNewGroup, Boolean isNested) {
93
94                 GroupHeatMetaDefinition groupHeatMetaDefinition;
95
96                 if (array != null) {
97                         for (int i = 0; i < array.size(); i++) {
98                                 if (openNewGroup) {
99                                         groupHeatMetaDefinition = new GroupHeatMetaDefinition();
100                                         int groupNumber = listGroupHeatMetaDefinition.size() + 1;
101                                         log.debug("groupName={}", groupNumber);
102                                         groupHeatMetaDefinition.setGroup(groupNumber);
103                                         listGroupHeatMetaDefinition.add(groupHeatMetaDefinition);
104                                         PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
105                                         propertyHeatMetaDefinition.setName("isBase");
106                                         propertyHeatMetaDefinition.setValue(false);
107                                         groupHeatMetaDefinition.setPropertyHeatMetaDefinition(propertyHeatMetaDefinition);
108                                 }
109                                 groupHeatMetaDefinition = listGroupHeatMetaDefinition.get(listGroupHeatMetaDefinition.size() - 1);
110                                 JSONObject jsonObject = (JSONObject) array.get(i);
111                                 if (openNewGroup) {
112                                         String groupName;
113                                         int lastIndexOfDot = jsonObject.get("fileName").toString().lastIndexOf(".");
114                                         if(lastIndexOfDot == -1){
115                                                 groupName = jsonObject.get("fileName").toString();
116                                         }else {
117                                                 groupName = jsonObject.get("fileName").toString().substring(0, lastIndexOfDot);
118                                         }
119                                         groupHeatMetaDefinition.setGroupName(groupName);
120                                 }
121                                 fetchArtifactByGroupFromJsonObject(listGroupHeatMetaDefinition, groupHeatMetaDefinition, jsonObject, isNested);
122                         }
123                 }
124                 return listGroupHeatMetaDefinition;
125         }
126         
127         
128         public static void fetchArtifactByGroupFromJsonObject(List<GroupHeatMetaDefinition> listGroupHeatMetaDefinition, GroupHeatMetaDefinition groupHeatMetaDefinition, JSONObject jsonObject, Boolean isNested) {
129                 @SuppressWarnings("unchecked")
130                 Set<Object> groupsKey = jsonObject.keySet();
131                 for (Object groupKey : groupsKey) {
132                         String groupKeyStr = (String) groupKey;
133                         if (groupKeyStr.equals("isBase")) {
134                                 PropertyHeatMetaDefinition propertyHeatMetaDefinition = new PropertyHeatMetaDefinition();
135                                 propertyHeatMetaDefinition.setName(groupKeyStr);
136                                 propertyHeatMetaDefinition.setValue((boolean) jsonObject.get(groupKeyStr));
137                                 if (!groupHeatMetaDefinition.getPropertyHeatMetaDefinition().equals(propertyHeatMetaDefinition)) {
138                                         groupHeatMetaDefinition.getPropertyHeatMetaDefinition().setValue((boolean) jsonObject.get(groupKeyStr));
139                                 }
140                         }
141                         if (groupKeyStr.equals("fileName")) {
142                                 String artifactName = (String) jsonObject.get(groupKeyStr);
143                                 String artifactType = ArtifactTypeEnum.HEAT_ARTIFACT.getType();
144                                 if(isNested){
145                                         artifactType = ArtifactTypeEnum.HEAT_NESTED.getType();
146                                 }
147                                 if(jsonObject.get("type") != null && isNested == false){
148                                         artifactType = (String) jsonObject.get("type");
149                                 }
150                                 HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition = new HeatMetaFirstLevelDefinition(artifactName, artifactType, null);
151                                 List<HeatMetaFirstLevelDefinition> listArtifactNames = groupHeatMetaDefinition.getArtifactList();
152                                 listArtifactNames.add(heatMetaFirstLevelDefinition);
153                                 groupHeatMetaDefinition.setArtifactList(listArtifactNames);
154                         } else {
155                                 if((groupKeyStr.equals("env"))){
156                                         if (jsonObject.get(groupKeyStr) instanceof JSONObject){
157                                                 fetchArtifactByGroupFromJsonObject(listGroupHeatMetaDefinition, groupHeatMetaDefinition, (JSONObject) jsonObject.get(groupKeyStr), false);
158                                         }else{
159                                                 assertTrue("Expected object is JSONObject, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONObject"));
160                                         }
161                                 }
162                                 if((groupKeyStr.equals("nested"))){
163                                         if (jsonObject.get(groupKeyStr) instanceof JSONArray){
164                                                 fetchArtifactByGroupFromJsonArray((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition, false, true);
165                                         }else{
166                                                 assertTrue("Expected object is JSONArray, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONArray"));
167                                         }
168                                         
169                                 }else if (!(groupKeyStr.equals("isBase") || groupKeyStr.equals("type") || groupKeyStr.equals("env"))) {
170                                         if (jsonObject.get(groupKeyStr) instanceof JSONArray){
171                                                 fetchArtifactByGroupFromJsonArray((JSONArray) jsonObject.get(groupKeyStr), listGroupHeatMetaDefinition, false, false);
172                                         }else{
173                                                 assertTrue("Expected object is JSONArray, but actual: " + jsonObject.get(groupKeyStr).getClass(), jsonObject.get(groupKeyStr).getClass().equals("JSONArray"));
174                                         }
175                                 }
176                         }
177                 }
178         }
179         
180 }