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