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