re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ResponseParser.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.rest;
22
23 //import com.fasterxml.jackson.databind.DeserializationFeature;
24 //import com.fasterxml.jackson.databind.ObjectMapper;
25 //import com.fasterxml.jackson.databind.module.SimpleModule;
26 //import com.google.gson.*;
27 //import org.apache.commons.codec.binary.Base64;
28 //import org.apache.log4j.Logger;
29 //import org.codehaus.jackson.JsonNode;
30 //
31 //import org.json.JSONArray;
32 //import org.json.JSONException;
33 //import org.json.simple.JSONObject;
34 //import org.json.simple.JSONValue;
35 //import org.openecomp.sdc.be.model.*;
36 //import org.openecomp.sdc.be.model.category.CategoryDefinition;
37 //import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintJacksonDeserializer;;
38 //import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
39 //import org.openecomp.sdc.ci.tests.datatypes.ResourceAssetStructure;
40 //import org.openecomp.sdc.ci.tests.datatypes.ResourceRespJavaObject;
41 //import org.openecomp.sdc.ci.tests.datatypes.ServiceDistributionStatus;
42 //import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
43 //import org.openecomp.sdc.ci.tests.tosca.datatypes.VfModuleDefinition;
44 //import org.openecomp.sdc.ci.tests.utils.Utils;
45 //import org.yaml.snakeyaml.Yaml;
46 //
47 //import java.io.ByteArrayInputStream;
48 //import java.io.IOException;
49 //import java.io.InputStream;
50 //import java.text.ParseException;
51 //import java.util.*;
52
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.fasterxml.jackson.databind.exc.InvalidFormatException;
56 import com.fasterxml.jackson.databind.module.SimpleModule;
57 import com.google.gson.*;
58 import org.apache.commons.codec.binary.Base64;
59 import org.apache.log4j.Logger;
60 import org.json.JSONArray;
61 import org.json.JSONException;
62 import org.json.simple.JSONObject;
63 import org.json.simple.JSONValue;
64 import org.openecomp.sdc.be.model.*;
65 import org.openecomp.sdc.be.model.category.CategoryDefinition;
66 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
67 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
68 import org.openecomp.sdc.ci.tests.datatypes.ResourceAssetStructure;
69 import org.openecomp.sdc.ci.tests.datatypes.ResourceRespJavaObject;
70 import org.openecomp.sdc.ci.tests.datatypes.ServiceDistributionStatus;
71 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
72 import org.openecomp.sdc.ci.tests.tosca.datatypes.VfModuleDefinition;
73 import org.openecomp.sdc.ci.tests.utils.Utils;
74 import org.yaml.snakeyaml.Yaml;
75
76 import java.io.ByteArrayInputStream;
77 import java.io.IOException;
78 import java.io.InputStream;
79 import java.text.ParseException;
80 import java.util.*;
81
82 //import org.codehaus.jackson.JsonNode;
83
84 public class ResponseParser {
85
86         // comment by Andrey, for test only
87         // public static void main(String[] args) {
88         // String response =
89         // "{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"resourceName\":\"importResource4test\",\"resourceVersion\":\"0.1\",\"creatorUserId\":\"jh0003\",\"creatorFullName\":\"Jimmy
90         // Hendrix\",\"lastUpdaterUserId\":\"jh0003\",\"lastUpdaterFullName\":\"Jimmy
91         // Hendrix\",\"creationDate\":1446742241514,\"lastUpdateDate\":1446742241514,\"description\":\"Represents
92         // a generic software component that can be managed and run by a Compute
93         // Node
94         // Type.\",\"icon\":\"defaulticon\",\"tags\":[\"importResource4test\"],\"category\":\"Generic/Infrastructure\",\"lifecycleState\":\"NOT_CERTIFIED_CHECKOUT\",\"derivedFrom\":[\"tosca.nodes.Root\"],\"artifacts\":{},\"deploymentArtifacts\":{},\"properties\":[{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.port\",\"type\":\"integer\",\"required\":false,\"description\":\"the
95         // port the DBMS service will listen to for data and
96         // requests\",\"password\":false,\"name\":\"port\",\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"definition\":true},{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.root_password\",\"type\":\"string\",\"required\":false,\"description\":\"the
97         // optional root password for the DBMS
98         // service\",\"password\":false,\"name\":\"root_password\",\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"definition\":true}],\"interfaces\":{\"standard\":{\"type\":\"tosca.interfaces.node.lifecycle.Standard\",\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard\",\"operations\":{\"stop\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.stop\",\"description\":\"Standard
99         // lifecycle stop
100         // operation.\",\"definition\":false},\"start\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.start\",\"description\":\"Standard
101         // lifecycle start
102         // operation.\",\"definition\":false},\"delete\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.delete\",\"description\":\"Standard
103         // lifecycle delete
104         // operation.\",\"definition\":false},\"create\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.create\",\"description\":\"Standard
105         // lifecycle create
106         // operation.\",\"definition\":false},\"configure\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.configure\",\"description\":\"Standard
107         // lifecycle configure
108         // operation.\",\"definition\":false}},\"definition\":false}},\"capabilities\":{\"feature\":{\"uniqueId\":\"capability.8313348e-3623-4f4a-9b8f-d2fbadaf9a31.feature\",\"type\":\"tosca.capabilities.Node\"},\"feature2\":{\"uniqueId\":\"capability.52eb0139-a855-47b9-a0e6-c90f0a90b1d2.feature2\",\"type\":\"tosca.capabilities.Node\"}},\"requirements\":{\"dependency\":{\"uniqueId\":\"8313348e-3623-4f4a-9b8f-d2fbadaf9a31.dependency\",\"capability\":\"tosca.capabilities.Node\",\"node\":\"tosca.nodes.Root\",\"relationship\":\"tosca.relationships.DependsOn\"},\"dependency2\":{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.dependency2\",\"capability\":\"tosca.capabilities.Node\",\"node\":\"tosca.nodes.importResource4test\",\"relationship\":\"tosca.relationships.DependsOn\"}},\"vendorName\":\"ATT
109         // (Tosca)\",\"vendorRelease\":\"1.0.0.wd03\",\"contactId\":\"jh0003\",\"systemName\":\"Importresource4test\",\"additionalInformation\":[{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.additionalinformation\",\"lastCreatedCounter\":0,\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"parameters\":[]}],\"allVersions\":{\"0.1\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\"},\"abstract\":false,\"highestVersion\":true,\"uuid\":\"2e91a2df-b066-49bb-abde-4c1c01e409db\"}";
110         // convertResourceResponseToJavaObject(response);
111         // }
112
113         private static final String INVARIANT_UUID = "invariantUUID";
114         public static final String UNIQUE_ID = "uniqueId";
115         public static final String VERSION = "version";
116         public static final String UUID = "uuid";
117         public static final String NAME = "name";
118         public static final String ORIGIN_TYPE = "originType";
119         public static final String TOSCA_RESOURCE_NAME = "toscaResourceName";
120
121         static Logger logger = Logger.getLogger(ResponseParser.class.getName());
122
123         public static String getValueFromJsonResponse(String response, String fieldName) {
124                 try {
125 //                      JSONObject jsonResp = (JSONObject) JSONValue.parse(response);
126                         String[] split = fieldName.split(":");
127                         String fieldValue = response;
128
129                         for(int i=0; i<split.length; i++) {
130                                 fieldValue = parser(fieldValue, split[i]);
131                         }
132                         return fieldValue;
133                 } catch (Exception e) {
134                         return null;
135                 }
136
137         }
138
139         private static String parser(String response, String field){
140                 JSONObject fieldValue = (JSONObject) JSONValue.parse(response);
141                 return fieldValue.get(field).toString();
142         }
143
144         public static String getUniqueIdFromResponse(RestResponse response) {
145                 return getValueFromJsonResponse(response.getResponse(), UNIQUE_ID);
146         }
147
148         public static String getInvariantUuid(RestResponse response) {
149                 return getValueFromJsonResponse(response.getResponse(), INVARIANT_UUID);
150         }
151
152         public static String getUuidFromResponse(RestResponse response) {
153                 return getValueFromJsonResponse(response.getResponse(), UUID);
154         }
155
156         public static String getNameFromResponse(RestResponse response) {
157                 return getValueFromJsonResponse(response.getResponse(), NAME);
158         }
159
160         public static String getVersionFromResponse(RestResponse response) {
161                 return ResponseParser.getValueFromJsonResponse(response.getResponse(), VERSION);
162         }
163
164         public static String getComponentTypeFromResponse(RestResponse response) {
165                 return ResponseParser.getValueFromJsonResponse(response.getResponse(), ORIGIN_TYPE);
166         }
167
168         public static String getToscaResourceNameFromResponse(RestResponse response) {
169                 return getValueFromJsonResponse(response.getResponse(), TOSCA_RESOURCE_NAME);
170         }
171
172         @SuppressWarnings("unchecked")
173         public static ResourceRespJavaObject parseJsonListReturnResourceDetailsObj(RestResponse restResponse,
174                         String resourceType, String searchPattern, String expectedResult) throws Exception {
175
176                 // Gson gson = new Gson;
177
178                 JsonElement jElement = new JsonParser().parse(restResponse.getResponse());
179                 JsonObject jObject = jElement.getAsJsonObject();
180                 JsonArray arrayOfObjects = (JsonArray) jObject.get(resourceType);
181                 Gson gson = new Gson();
182                 Map<String, Object> map = new HashMap<>();
183                 ResourceRespJavaObject jsonToJavaObject = new ResourceRespJavaObject();
184
185                 for (int counter = 0; counter < arrayOfObjects.size(); counter++) {
186                         JsonObject jHitObject = (JsonObject) arrayOfObjects.get(counter);
187
188                         map = (Map<String, Object>) gson.fromJson(jHitObject.toString(), map.getClass());
189                         if (map.get(searchPattern).toString().contains(expectedResult)) {
190
191                                 jsonToJavaObject = gson.fromJson(jObject, ResourceRespJavaObject.class);
192                                 break;
193                         }
194                 }
195                 return jsonToJavaObject;
196
197         }
198
199         private static ObjectMapper newObjectMapper() {
200                 SimpleModule module = new SimpleModule("customDeserializationModule");
201                 module.addDeserializer(PropertyConstraint.class, new PropertyOperation.PropertyConstraintJacksonDeserializer());
202                 return new ObjectMapper()
203                         .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
204                         .registerModule(module);
205         }
206
207         public static Resource convertResourceResponseToJavaObject(String response) {
208                 ObjectMapper mapper = newObjectMapper();
209                 Resource resource = null;
210                 try {
211                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212                         resource = mapper.readValue(response, Resource.class);
213                         
214                         logger.debug(resource.toString());
215                 } catch (IOException e) {
216                         try {
217                                 List<Resource> resources = Arrays.asList(mapper.readValue(response.toString(), Resource[].class));
218                                 resource = resources.get(0);
219                         } catch (Exception e1) {
220                                 e1.printStackTrace();
221                         }
222                 }
223
224                 return resource;
225         }
226
227         public static ComponentInstanceProperty convertPropertyResponseToJavaObject(String response) {
228
229                 ObjectMapper mapper = newObjectMapper();
230                 ComponentInstanceProperty propertyDefinition = null;
231                 try {
232                         propertyDefinition = mapper.readValue(response, ComponentInstanceProperty.class);
233                         logger.debug(propertyDefinition.toString());
234                 } catch (IOException e) {
235                         e.printStackTrace();
236                 }
237                 return propertyDefinition;
238         }
239
240         public static GroupDefinition convertPropertyResponseToObject(String response) {
241
242                 ObjectMapper mapper = newObjectMapper();
243                 GroupDefinition groupDefinition = null;
244                 try {
245                         groupDefinition = mapper.readValue(response, GroupDefinition.class);
246                         logger.debug(groupDefinition.toString());
247                 } catch (IOException e) {
248                         e.printStackTrace();
249                 }
250                 return groupDefinition;
251         }
252
253         public static String toJson(Object object) {
254                 Gson gson = new Gson();
255                 return gson.toJson(object);
256         }
257
258         public static ArtifactDefinition convertArtifactDefinitionResponseToJavaObject(String response) {
259                 ObjectMapper mapper = new ObjectMapper();
260                 ArtifactDefinition artifactDefinition = null;
261                 try {
262                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
263                         artifactDefinition = mapper.readValue(response, ArtifactDefinition.class);
264                         logger.debug(artifactDefinition.toString());
265                 } catch (IOException e) {
266                         e.printStackTrace();
267                 }
268
269                 return artifactDefinition;
270
271         }
272
273         public static ArtifactReqDetails convertArtifactReqDetailsToJavaObject(String response) {
274
275                 ArtifactReqDetails artifactReqDetails = null;
276                 Gson gson = new Gson();
277                 artifactReqDetails = gson.fromJson(response, ArtifactReqDetails.class);
278                 return artifactReqDetails;
279         }
280
281         public static <T> T parseToObject(String json, Class<T> clazz) {
282                 Gson gson = new Gson();
283                 T object;
284                 try {
285                         object = gson.fromJson(json, clazz);
286                 } catch (Exception e) {
287                         object = parseToObjectUsingMapper(json, clazz);
288                 }
289                 return object;
290         }
291
292         public static <T> T parseToObjectUsingMapper(String json, Class<T> clazz) {
293                 // Generic convert
294                 ObjectMapper mapper = newObjectMapper();
295                 T object = null;
296                 try {
297                         object = mapper.readValue(json, clazz);
298                 } catch (IOException e) {
299                         e.printStackTrace();
300                 }
301
302                 return object;
303         }
304
305         public static ArtifactReqDetails convertArtifactDefinitionToArtifactReqDetailsObject(
306                         ArtifactDefinition artifactDefinition) {
307
308                 ArtifactReqDetails artifactReqDetails = null;
309                 Gson gson = new Gson();
310                 String artDef = gson.toJson(artifactDefinition);
311                 artifactReqDetails = gson.fromJson(artDef, ArtifactReqDetails.class);
312                 return artifactReqDetails;
313         }
314
315         /*public static Service convertServiceResponseToJavaObject(String response) {
316
317                 ObjectMapper mapper = newObjectMapper();
318                 Service service = null;
319                 try {
320                         service = mapper.readValue(response, Service.class);
321                         logger.debug(service.toString());
322                 } catch (IOException e) {
323                         e.printStackTrace();
324                 }
325
326                 return service;
327         }*/
328         public static Service convertServiceResponseToJavaObject(String response) {
329
330                 ObjectMapper mapper = newObjectMapper();
331                 Service service = null;
332                 try {
333                         service = mapper.readValue(response, Service.class);
334                         logger.debug(service.toString());
335                         //Temporary catch until bug with distribution status fixed
336                 } catch (InvalidFormatException e) {
337                         System.out.println("broken service with invalid distribution status : " + response);
338                         logger.debug("broken service with invalid distribution status : " + response);
339                         return service;
340         } catch(IOException e){
341
342                 e.printStackTrace();
343         }
344
345                 return service;
346 }
347
348         public static Product convertProductResponseToJavaObject(String response) {
349
350                 ObjectMapper mapper = newObjectMapper();
351                 Product product = null;
352                 try {
353                         product = mapper.readValue(response, Product.class);
354                         logger.debug(product.toString());
355                 } catch (IOException e) {
356                         e.printStackTrace();
357                 }
358
359                 return product;
360         }
361
362         public static ComponentInstance convertComponentInstanceResponseToJavaObject(String response) {
363
364                 ObjectMapper mapper = newObjectMapper();
365                 ComponentInstance componentInstance = null;
366                 try {
367                         componentInstance = mapper.readValue(response, ComponentInstance.class);
368                         logger.debug(componentInstance.toString());
369                 } catch (IOException e) {
370                         // TODO Auto-generated catch block
371                         e.printStackTrace();
372                 }
373
374                 return componentInstance;
375         }
376
377         public static List<String> getValuesFromJsonArray(RestResponse message) throws Exception {
378                 List<String> artifactTypesArrayFromApi = new ArrayList<>();
379
380                 org.json.JSONObject responseObject = new org.json.JSONObject(message.getResponse());
381                 JSONArray jArr = responseObject.getJSONArray("artifactTypes");
382
383                 for (int i = 0; i < jArr.length(); i++) {
384                         org.json.JSONObject jObj = jArr.getJSONObject(i);
385                         String value = jObj.get("name").toString();
386
387                         artifactTypesArrayFromApi.add(value);
388                 }
389                 return artifactTypesArrayFromApi;
390         }
391
392         public static String calculateMD5Header(ArtifactReqDetails artifactDetails) {
393                 Gson gson = new Gson();
394                 String jsonBody = gson.toJson(artifactDetails);
395                 // calculate MD5 for json body
396                 return calculateMD5(jsonBody);
397
398         }
399
400         public static String calculateMD5(String data) {
401                 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
402                 // encode base-64 result
403                 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
404                 String encodeBase64Str = new String(encodeBase64);
405                 return encodeBase64Str;
406
407         }
408
409         public static List<Map<String, Object>> getAuditFromMessage(Map<String, Object> auditingMessage) {
410                 List<Map<String, Object>> auditList = new ArrayList<>();
411                 auditList.add(auditingMessage);
412                 return auditList;
413         }
414
415         public static List<CategoryDefinition> parseCategories(RestResponse getAllCategoriesRest) {
416
417                 List<CategoryDefinition> categories = new ArrayList<>();
418                 try {
419                         JsonElement jElement = new JsonParser().parse(getAllCategoriesRest.getResponse());
420                         JsonArray cagegories = jElement.getAsJsonArray();
421                         Iterator<JsonElement> iter = cagegories.iterator();
422                         while (iter.hasNext()) {
423                                 JsonElement next = iter.next();
424                                 CategoryDefinition category = ResponseParser.parseToObject(next.toString(), CategoryDefinition.class);
425                                 categories.add(category);
426                         }
427
428                 } catch (Exception e) {
429                         e.printStackTrace();
430                 }
431
432                 return categories;
433         }
434
435         public static JSONArray getListFromJson(RestResponse res, String field) throws JSONException {
436                 String valueFromJsonResponse = getValueFromJsonResponse(res.getResponse(), field);
437                 JSONArray jArr = new JSONArray(valueFromJsonResponse);
438
439                 return jArr;
440         }
441
442         public static List<String> getDerivedListFromJson(RestResponse res) throws JSONException {
443                 JSONArray listFromJson = getListFromJson(res, "derivedFrom");
444                 List<String> lst = new ArrayList<>();
445                 for (int i = 0; i < listFromJson.length(); i++) {
446                         lst.add(listFromJson.getString(i));
447                 }
448
449                 return lst;
450         }
451
452         public static Map<String, Object> convertStringToMap(String obj) {
453                 Map<String, Object> object = (Map<String, Object>) JSONValue.parse(obj);
454                 return object;
455         }
456
457         public static List<Map<String, Object>> getListOfMapsFromJson(RestResponse res, String field) throws Exception {
458                 List<Map<String, Object>> list = new ArrayList<>();
459                 JSONArray listFromJson = getListFromJson(res, field);
460                 for (int i = 0; i < listFromJson.length(); i++) {
461                         Map<String, Object> convertStringToMap = convertStringToMap(listFromJson.getString(i));
462                         list.add(convertStringToMap);
463                 }
464                 return list;
465
466         }
467
468         public static Map<String, Object> getJsonValueAsMap(RestResponse response, String key) {
469                 String valueField = getValueFromJsonResponse(response.getResponse(), key);
470                 Map<String, Object> convertToMap = convertStringToMap(valueField);
471                 return convertToMap;
472         }
473
474         public static String getJsonObjectValueByKey(String metadata, String key) {
475                 JsonElement jelement = new JsonParser().parse(metadata);
476
477                 JsonObject jobject = jelement.getAsJsonObject();
478                 Object obj = jobject.get(key);
479                 if (obj == null) {
480                         return null;
481                 } else {
482                         return obj.toString();
483                 }
484         }
485
486 //      /*public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
487 //
488 //              // Map<String, ArrayList<Component>> map = new HashMap<String,
489 //              // ArrayList<Component>>();
490 //              Map<String, List<Component>> map = new HashMap<>();
491 //
492 //              JsonElement jElement = new JsonParser().parse(response);
493 //              JsonObject jObject = jElement.getAsJsonObject();
494 //              JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
495 //              JsonArray jArrServices = jObject.getAsJsonArray("services");
496 //              //Product removed
497 ////            JsonArray jArrProducts = jObject.getAsJsonArray("products");
498 //              //Product removed
499 //              if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
500 //
501 //
502 //              //////// RESOURCE/////////////////////////////
503 //              ArrayList<Component> restResponseArray = new ArrayList<>();
504 //              Component component = null;
505 //              for (int i = 0; i < jArrReousrces.size(); i++) {
506 //                      String resourceString = (String) jArrReousrces.get(i).toString();
507 //                      component = ResponseParser.convertResourceResponseToJavaObject(resourceString);
508 //                      restResponseArray.add(component);
509 //              }
510 //
511 //              map.put("resources", restResponseArray);
512 //
513 //              ///////// SERVICE/////////////////////////////
514 //
515 //              restResponseArray = new ArrayList<>();
516 //              component = null;
517 //              for (int i = 0; i < jArrServices.size(); i++) {
518 //                      String resourceString = (String) jArrServices.get(i).toString();
519 //                      component = ResponseParser.convertServiceResponseToJavaObject(resourceString);
520 //                      restResponseArray.add(component);
521 //              }
522 //
523 //              map.put("services", restResponseArray);
524 ////Product removed
525 //              ///////// PRODUCT/////////////////////////////
526 ////            restResponseArray = new ArrayList<>();
527 ////            component = null;
528 ////            for (int i = 0; i < jArrProducts.size(); i++) {
529 ////                    String resourceString = (String) jArrProducts.get(i).toString();
530 ////                    component = ResponseParser.convertProductResponseToJavaObject(resourceString);
531 ////                    restResponseArray.add(component);
532 ////            }
533 ////
534 ////            map.put("products", restResponseArray);
535 ////
536 //      }
537 //              else {
538 //                      map.put("resources", new ArrayList<>());
539 //                      map.put("services", new ArrayList<>());
540 //                      //Product removed
541 ////                    map.put("products", new ArrayList<>());
542 //              }
543 //
544 //              return map;
545 //
546 //      }*/
547
548         public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
549
550                 // Map<String, ArrayList<Component>> map = new HashMap<String,
551                 // ArrayList<Component>>();
552                 Map<String, List<Component>> map = new HashMap<>();
553
554                 JsonElement jElement = new JsonParser().parse(response);
555                 JsonObject jObject = jElement.getAsJsonObject();
556                 JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
557                 JsonArray jArrServices = jObject.getAsJsonArray("services");
558                 //Product removed
559 //              JsonArray jArrProducts = jObject.getAsJsonArray("products");
560                 //Product removed
561
562                 if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
563
564
565                         //////// RESOURCE/////////////////////////////
566                         ArrayList<Component> restResponseArray = new ArrayList<>();
567                         Component component = null;
568                         for (int i = 0; i < jArrReousrces.size(); i++) {
569                                 String resourceString = (String) jArrReousrces.get(i).toString();
570                                 component = ResponseParser.convertResourceResponseToJavaObject(resourceString);
571                                 restResponseArray.add(component);
572                         }
573
574                         map.put("resources", restResponseArray);
575
576                         ///////// SERVICE/////////////////////////////
577
578                         restResponseArray = new ArrayList<>();
579                         component = null;
580                         for (int i = 0; i < jArrServices.size(); i++) {
581                                 String resourceString = (String) jArrServices.get(i).toString();
582                                 component = ResponseParser.convertServiceResponseToJavaObject(resourceString);
583                                 restResponseArray.add(component);
584                         }
585
586                         map.put("services", restResponseArray);
587 //Product removed
588
589                         ///////// PRODUCT/////////////////////////////
590 //              restResponseArray = new ArrayList<>();
591 //              component = null;
592 //              for (int i = 0; i < jArrProducts.size(); i++) {
593 //                      String resourceString = (String) jArrProducts.get(i).toString();
594 //                      component = ResponseParser.convertProductResponseToJavaObject(resourceString);
595 //                      restResponseArray.add(component);
596 //              }
597 //
598
599
600 //              map.put("products", restResponseArray);
601 //
602
603                 }
604                 else {
605                         map.put("resources", new ArrayList<>());
606                         map.put("services", new ArrayList<>());
607                         //Product removed
608 //                      map.put("products", new ArrayList<>());
609                 }
610
611                 return map;
612
613         }
614         
615         public static Map<Long, ServiceDistributionStatus> convertServiceDistributionStatusToObject(String response) throws ParseException {
616
617                 Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap = new HashMap<>();
618                 ServiceDistributionStatus serviceDistributionStatusObject = null;
619                 
620                 JsonElement jElement = new JsonParser().parse(response);
621                 JsonObject jObject = jElement.getAsJsonObject();
622                 JsonArray jDistrStatusArray = jObject.getAsJsonArray("distributionStatusOfServiceList");
623                 
624                 for (int i = 0; i < jDistrStatusArray.size(); i++){
625                         Gson gson = new Gson();
626                         String servDistrStatus = gson.toJson(jDistrStatusArray.get(i));
627                         serviceDistributionStatusObject = gson.fromJson(servDistrStatus, ServiceDistributionStatus.class);
628                         serviceDistributionStatusMap.put(Utils.getEpochTimeFromUTC(serviceDistributionStatusObject.getTimestamp()), serviceDistributionStatusObject);
629                 }
630
631                 return serviceDistributionStatusMap;
632                 
633         }
634         
635         public static Map<String, String> getPropertiesNameType(RestResponse restResponse)
636                         throws JSONException {
637                 Map<String, String> propertiesMap = new HashMap<>();
638                 JSONArray propertiesList = getListFromJson(restResponse, "properties");
639                 for (int i = 0; i < propertiesList.length() ; i ++){
640                         JSONObject  prop = (JSONObject) JSONValue.parse(propertiesList.get(i).toString());
641                         String propName = prop.get("name").toString();
642                         String propType = prop.get("type").toString();
643                         propertiesMap.put(propName, propType);
644                 }
645                 
646                 return propertiesMap;
647         }
648         
649         public static ResourceAssetStructure getDataOutOfSearchExternalAPIResponseForResourceName(String response, String resourceName) {
650                 Gson gson = new Gson();
651                 JsonElement jsonElement = new JsonParser().parse(response);
652                 JsonArray jsonArray = jsonElement.getAsJsonArray();
653                 for(JsonElement jElement: jsonArray) {
654                         ResourceAssetStructure parsedResponse = gson.fromJson(jElement, ResourceAssetStructure.class);
655                         
656                         if(resourceName.contains(parsedResponse.getName()) && parsedResponse.getName().contains(resourceName)) {
657                                 return parsedResponse;
658                         }
659                 }
660                 
661                 return null;
662         }
663
664         public static Map<String, VfModuleDefinition> convertVfModuleJsonResponseToJavaObject(String response) {
665
666                 Yaml yaml = new Yaml();
667                 InputStream inputStream = null;
668                 inputStream = new ByteArrayInputStream(response.getBytes());
669                 List<?> list = (List<?> )yaml.load(inputStream);
670                 ObjectMapper mapper = new ObjectMapper();
671
672                 VfModuleDefinition moduleDefinition;
673                 Map<String, VfModuleDefinition> vfModulesMap = new HashMap<>();
674                 for (Object obj : list) {
675 //                      TODO Andrey L. uncomment line below in case to ignore on unknown properties, not recommended
676                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
677                         moduleDefinition = mapper.convertValue(obj, VfModuleDefinition.class);
678                         vfModulesMap.put(moduleDefinition.vfModuleModelName, moduleDefinition);
679                 }
680                 return  vfModulesMap;
681         }
682
683 }