2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.rest;
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;
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;
47 //import java.io.ByteArrayInputStream;
48 //import java.io.IOException;
49 //import java.io.InputStream;
50 //import java.text.ParseException;
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;
76 import java.io.ByteArrayInputStream;
77 import java.io.IOException;
78 import java.io.InputStream;
79 import java.text.ParseException;
82 //import org.codehaus.jackson.JsonNode;
84 public class ResponseParser {
86 // comment by Andrey, for test only
87 // public static void main(String[] args) {
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
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
100 // operation.\",\"definition\":false},\"start\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.start\",\"description\":\"Standard
102 // operation.\",\"definition\":false},\"delete\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.delete\",\"description\":\"Standard
104 // operation.\",\"definition\":false},\"create\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.create\",\"description\":\"Standard
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);
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";
121 static Logger logger = Logger.getLogger(ResponseParser.class.getName());
123 public static String getValueFromJsonResponse(String response, String fieldName) {
125 // JSONObject jsonResp = (JSONObject) JSONValue.parse(response);
126 String[] split = fieldName.split(":");
127 String fieldValue = response;
129 for(int i=0; i<split.length; i++) {
130 fieldValue = parser(fieldValue, split[i]);
133 } catch (Exception e) {
139 private static String parser(String response, String field){
140 JSONObject fieldValue = (JSONObject) JSONValue.parse(response);
141 return fieldValue.get(field).toString();
144 public static String getUniqueIdFromResponse(RestResponse response) {
145 return getValueFromJsonResponse(response.getResponse(), UNIQUE_ID);
148 public static String getInvariantUuid(RestResponse response) {
149 return getValueFromJsonResponse(response.getResponse(), INVARIANT_UUID);
152 public static String getUuidFromResponse(RestResponse response) {
153 return getValueFromJsonResponse(response.getResponse(), UUID);
156 public static String getNameFromResponse(RestResponse response) {
157 return getValueFromJsonResponse(response.getResponse(), NAME);
160 public static String getVersionFromResponse(RestResponse response) {
161 return ResponseParser.getValueFromJsonResponse(response.getResponse(), VERSION);
164 public static String getComponentTypeFromResponse(RestResponse response) {
165 return ResponseParser.getValueFromJsonResponse(response.getResponse(), ORIGIN_TYPE);
168 public static String getToscaResourceNameFromResponse(RestResponse response) {
169 return getValueFromJsonResponse(response.getResponse(), TOSCA_RESOURCE_NAME);
172 @SuppressWarnings("unchecked")
173 public static ResourceRespJavaObject parseJsonListReturnResourceDetailsObj(RestResponse restResponse,
174 String resourceType, String searchPattern, String expectedResult) throws Exception {
176 // Gson gson = new Gson;
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();
185 for (int counter = 0; counter < arrayOfObjects.size(); counter++) {
186 JsonObject jHitObject = (JsonObject) arrayOfObjects.get(counter);
188 map = (Map<String, Object>) gson.fromJson(jHitObject.toString(), map.getClass());
189 if (map.get(searchPattern).toString().contains(expectedResult)) {
191 jsonToJavaObject = gson.fromJson(jObject, ResourceRespJavaObject.class);
195 return jsonToJavaObject;
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);
207 public static Resource convertResourceResponseToJavaObject(String response) {
208 ObjectMapper mapper = newObjectMapper();
209 Resource resource = null;
211 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212 resource = mapper.readValue(response, Resource.class);
214 logger.debug(resource.toString());
215 } catch (IOException e) {
217 List<Resource> resources = Arrays.asList(mapper.readValue(response.toString(), Resource[].class));
218 resource = resources.get(0);
219 } catch (Exception e1) {
220 e1.printStackTrace();
227 public static ComponentInstanceProperty convertPropertyResponseToJavaObject(String response) {
229 ObjectMapper mapper = newObjectMapper();
230 ComponentInstanceProperty propertyDefinition = null;
232 propertyDefinition = mapper.readValue(response, ComponentInstanceProperty.class);
233 logger.debug(propertyDefinition.toString());
234 } catch (IOException e) {
237 return propertyDefinition;
240 public static GroupDefinition convertPropertyResponseToObject(String response) {
242 ObjectMapper mapper = newObjectMapper();
243 GroupDefinition groupDefinition = null;
245 groupDefinition = mapper.readValue(response, GroupDefinition.class);
246 logger.debug(groupDefinition.toString());
247 } catch (IOException e) {
250 return groupDefinition;
253 public static String toJson(Object object) {
254 Gson gson = new Gson();
255 return gson.toJson(object);
258 public static ArtifactDefinition convertArtifactDefinitionResponseToJavaObject(String response) {
259 ObjectMapper mapper = new ObjectMapper();
260 ArtifactDefinition artifactDefinition = null;
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) {
269 return artifactDefinition;
273 public static ArtifactReqDetails convertArtifactReqDetailsToJavaObject(String response) {
275 ArtifactReqDetails artifactReqDetails = null;
276 Gson gson = new Gson();
277 artifactReqDetails = gson.fromJson(response, ArtifactReqDetails.class);
278 return artifactReqDetails;
281 public static <T> T parseToObject(String json, Class<T> clazz) {
282 Gson gson = new Gson();
285 object = gson.fromJson(json, clazz);
286 } catch (Exception e) {
287 object = parseToObjectUsingMapper(json, clazz);
292 public static <T> T parseToObjectUsingMapper(String json, Class<T> clazz) {
294 ObjectMapper mapper = newObjectMapper();
297 object = mapper.readValue(json, clazz);
298 } catch (IOException e) {
305 public static ArtifactReqDetails convertArtifactDefinitionToArtifactReqDetailsObject(
306 ArtifactDefinition artifactDefinition) {
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;
315 /*public static Service convertServiceResponseToJavaObject(String response) {
317 ObjectMapper mapper = newObjectMapper();
318 Service service = null;
320 service = mapper.readValue(response, Service.class);
321 logger.debug(service.toString());
322 } catch (IOException e) {
328 public static Service convertServiceResponseToJavaObject(String response) {
330 ObjectMapper mapper = newObjectMapper();
331 Service service = null;
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);
340 } catch(IOException e){
348 public static Product convertProductResponseToJavaObject(String response) {
350 ObjectMapper mapper = newObjectMapper();
351 Product product = null;
353 product = mapper.readValue(response, Product.class);
354 logger.debug(product.toString());
355 } catch (IOException e) {
362 public static ComponentInstance convertComponentInstanceResponseToJavaObject(String response) {
364 ObjectMapper mapper = newObjectMapper();
365 ComponentInstance componentInstance = null;
367 componentInstance = mapper.readValue(response, ComponentInstance.class);
368 logger.debug(componentInstance.toString());
369 } catch (IOException e) {
370 // TODO Auto-generated catch block
374 return componentInstance;
377 public static List<String> getValuesFromJsonArray(RestResponse message) throws Exception {
378 List<String> artifactTypesArrayFromApi = new ArrayList<>();
380 org.json.JSONObject responseObject = new org.json.JSONObject(message.getResponse());
381 JSONArray jArr = responseObject.getJSONArray("artifactTypes");
383 for (int i = 0; i < jArr.length(); i++) {
384 org.json.JSONObject jObj = jArr.getJSONObject(i);
385 String value = jObj.get("name").toString();
387 artifactTypesArrayFromApi.add(value);
389 return artifactTypesArrayFromApi;
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);
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;
409 public static List<Map<String, Object>> getAuditFromMessage(Map<String, Object> auditingMessage) {
410 List<Map<String, Object>> auditList = new ArrayList<>();
411 auditList.add(auditingMessage);
415 public static List<CategoryDefinition> parseCategories(RestResponse getAllCategoriesRest) {
417 List<CategoryDefinition> categories = new ArrayList<>();
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);
428 } catch (Exception e) {
435 public static JSONArray getListFromJson(RestResponse res, String field) throws JSONException {
436 String valueFromJsonResponse = getValueFromJsonResponse(res.getResponse(), field);
437 JSONArray jArr = new JSONArray(valueFromJsonResponse);
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));
452 public static Map<String, Object> convertStringToMap(String obj) {
453 Map<String, Object> object = (Map<String, Object>) JSONValue.parse(obj);
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);
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);
474 public static String getJsonObjectValueByKey(String metadata, String key) {
475 JsonElement jelement = new JsonParser().parse(metadata);
477 JsonObject jobject = jelement.getAsJsonObject();
478 Object obj = jobject.get(key);
482 return obj.toString();
486 // /*public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
488 // // Map<String, ArrayList<Component>> map = new HashMap<String,
489 // // ArrayList<Component>>();
490 // Map<String, List<Component>> map = new HashMap<>();
492 // JsonElement jElement = new JsonParser().parse(response);
493 // JsonObject jObject = jElement.getAsJsonObject();
494 // JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
495 // JsonArray jArrServices = jObject.getAsJsonArray("services");
497 //// JsonArray jArrProducts = jObject.getAsJsonArray("products");
499 // if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
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);
511 // map.put("resources", restResponseArray);
513 // ///////// SERVICE/////////////////////////////
515 // restResponseArray = new ArrayList<>();
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);
523 // map.put("services", restResponseArray);
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);
534 //// map.put("products", restResponseArray);
538 // map.put("resources", new ArrayList<>());
539 // map.put("services", new ArrayList<>());
541 //// map.put("products", new ArrayList<>());
548 public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
550 // Map<String, ArrayList<Component>> map = new HashMap<String,
551 // ArrayList<Component>>();
552 Map<String, List<Component>> map = new HashMap<>();
554 JsonElement jElement = new JsonParser().parse(response);
555 JsonObject jObject = jElement.getAsJsonObject();
556 JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
557 JsonArray jArrServices = jObject.getAsJsonArray("services");
559 // JsonArray jArrProducts = jObject.getAsJsonArray("products");
562 if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
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);
574 map.put("resources", restResponseArray);
576 ///////// SERVICE/////////////////////////////
578 restResponseArray = new ArrayList<>();
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);
586 map.put("services", restResponseArray);
589 ///////// PRODUCT/////////////////////////////
590 // restResponseArray = new ArrayList<>();
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);
600 // map.put("products", restResponseArray);
605 map.put("resources", new ArrayList<>());
606 map.put("services", new ArrayList<>());
608 // map.put("products", new ArrayList<>());
615 public static Map<Long, ServiceDistributionStatus> convertServiceDistributionStatusToObject(String response) throws ParseException {
617 Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap = new HashMap<>();
618 ServiceDistributionStatus serviceDistributionStatusObject = null;
620 JsonElement jElement = new JsonParser().parse(response);
621 JsonObject jObject = jElement.getAsJsonObject();
622 JsonArray jDistrStatusArray = jObject.getAsJsonArray("distributionStatusOfServiceList");
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);
631 return serviceDistributionStatusMap;
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);
646 return propertiesMap;
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);
656 if(resourceName.contains(parsedResponse.getName()) && parsedResponse.getName().contains(resourceName)) {
657 return parsedResponse;
664 public static Map<String, VfModuleDefinition> convertVfModuleJsonResponseToJavaObject(String response) {
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();
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);