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.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;
77 import java.io.ByteArrayInputStream;
78 import java.io.IOException;
79 import java.io.InputStream;
80 import java.text.ParseException;
83 //import org.codehaus.jackson.JsonNode;
85 public class ResponseParser {
87 // comment by Andrey, for test only
88 // public static void main(String[] args) {
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
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
101 // operation.\",\"definition\":false},\"start\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.start\",\"description\":\"Standard
103 // operation.\",\"definition\":false},\"delete\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.delete\",\"description\":\"Standard
105 // operation.\",\"definition\":false},\"create\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.create\",\"description\":\"Standard
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);
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";
122 static Logger logger = LogManager.getLogger(ResponseParser.class);
124 public static String getValueFromJsonResponse(String response, String fieldName) {
126 // JSONObject jsonResp = (JSONObject) JSONValue.parse(response);
127 String[] split = fieldName.split(":");
128 String fieldValue = response;
130 for(int i=0; i<split.length; i++) {
131 fieldValue = parser(fieldValue, split[i]);
134 } catch (Exception e) {
140 private static String parser(String response, String field){
141 JSONObject fieldValue = (JSONObject) JSONValue.parse(response);
142 return fieldValue.get(field).toString();
145 public static String getUniqueIdFromResponse(RestResponse response) {
146 return getValueFromJsonResponse(response.getResponse(), UNIQUE_ID);
149 public static String getInvariantUuid(RestResponse response) {
150 return getValueFromJsonResponse(response.getResponse(), INVARIANT_UUID);
153 public static String getUuidFromResponse(RestResponse response) {
154 return getValueFromJsonResponse(response.getResponse(), UUID);
157 public static String getNameFromResponse(RestResponse response) {
158 return getValueFromJsonResponse(response.getResponse(), NAME);
161 public static String getVersionFromResponse(RestResponse response) {
162 return ResponseParser.getValueFromJsonResponse(response.getResponse(), VERSION);
165 public static String getComponentTypeFromResponse(RestResponse response) {
166 return ResponseParser.getValueFromJsonResponse(response.getResponse(), ORIGIN_TYPE);
169 public static String getToscaResourceNameFromResponse(RestResponse response) {
170 return getValueFromJsonResponse(response.getResponse(), TOSCA_RESOURCE_NAME);
173 @SuppressWarnings("unchecked")
174 public static ResourceRespJavaObject parseJsonListReturnResourceDetailsObj(RestResponse restResponse,
175 String resourceType, String searchPattern, String expectedResult) throws Exception {
177 // Gson gson = new Gson;
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();
186 for (int counter = 0; counter < arrayOfObjects.size(); counter++) {
187 JsonObject jHitObject = (JsonObject) arrayOfObjects.get(counter);
189 map = (Map<String, Object>) gson.fromJson(jHitObject.toString(), map.getClass());
190 if (map.get(searchPattern).toString().contains(expectedResult)) {
192 jsonToJavaObject = gson.fromJson(jObject, ResourceRespJavaObject.class);
196 return jsonToJavaObject;
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);
208 public static Resource convertResourceResponseToJavaObject(String response) {
209 ObjectMapper mapper = newObjectMapper();
210 Resource resource = null;
212 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
213 resource = mapper.readValue(response, Resource.class);
215 logger.debug(resource.toString());
216 } catch (IOException e) {
218 List<Resource> resources = Arrays.asList(mapper.readValue(response.toString(), Resource[].class));
219 resource = resources.get(0);
220 } catch (Exception e1) {
221 e1.printStackTrace();
228 public static ComponentInstanceProperty convertPropertyResponseToJavaObject(String response) {
230 ObjectMapper mapper = newObjectMapper();
231 ComponentInstanceProperty propertyDefinition = null;
233 propertyDefinition = mapper.readValue(response, ComponentInstanceProperty.class);
234 logger.debug(propertyDefinition.toString());
235 } catch (IOException e) {
238 return propertyDefinition;
241 public static GroupDefinition convertPropertyResponseToObject(String response) {
243 ObjectMapper mapper = newObjectMapper();
244 GroupDefinition groupDefinition = null;
246 groupDefinition = mapper.readValue(response, GroupDefinition.class);
247 logger.debug(groupDefinition.toString());
248 } catch (IOException e) {
251 return groupDefinition;
254 public static String toJson(Object object) {
255 Gson gson = new Gson();
256 return gson.toJson(object);
259 public static ArtifactDefinition convertArtifactDefinitionResponseToJavaObject(String response) {
260 ObjectMapper mapper = new ObjectMapper();
261 ArtifactDefinition artifactDefinition = null;
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) {
270 return artifactDefinition;
274 public static ArtifactReqDetails convertArtifactReqDetailsToJavaObject(String response) {
276 ArtifactReqDetails artifactReqDetails = null;
277 Gson gson = new Gson();
278 artifactReqDetails = gson.fromJson(response, ArtifactReqDetails.class);
279 return artifactReqDetails;
282 public static <T> T parseToObject(String json, Class<T> clazz) {
283 Gson gson = new Gson();
286 object = gson.fromJson(json, clazz);
287 } catch (Exception e) {
288 object = parseToObjectUsingMapper(json, clazz);
293 public static <T> T parseToObjectUsingMapper(String json, Class<T> clazz) {
295 ObjectMapper mapper = newObjectMapper();
298 object = mapper.readValue(json, clazz);
299 } catch (IOException e) {
306 public static ArtifactReqDetails convertArtifactDefinitionToArtifactReqDetailsObject(
307 ArtifactDefinition artifactDefinition) {
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;
316 /*public static Service convertServiceResponseToJavaObject(String response) {
318 ObjectMapper mapper = newObjectMapper();
319 Service service = null;
321 service = mapper.readValue(response, Service.class);
322 logger.debug(service.toString());
323 } catch (IOException e) {
329 public static Service convertServiceResponseToJavaObject(String response) {
331 ObjectMapper mapper = newObjectMapper();
332 Service service = null;
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);
341 } catch(IOException e){
349 public static Product convertProductResponseToJavaObject(String response) {
351 ObjectMapper mapper = newObjectMapper();
352 Product product = null;
354 product = mapper.readValue(response, Product.class);
355 logger.debug(product.toString());
356 } catch (IOException e) {
363 public static ComponentInstance convertComponentInstanceResponseToJavaObject(String response) {
365 ObjectMapper mapper = newObjectMapper();
366 ComponentInstance componentInstance = null;
368 componentInstance = mapper.readValue(response, ComponentInstance.class);
369 logger.debug(componentInstance.toString());
370 } catch (IOException e) {
371 // TODO Auto-generated catch block
375 return componentInstance;
378 public static List<String> getValuesFromJsonArray(RestResponse message) throws Exception {
379 List<String> artifactTypesArrayFromApi = new ArrayList<>();
381 org.json.JSONObject responseObject = new org.json.JSONObject(message.getResponse());
382 JSONArray jArr = responseObject.getJSONArray("artifactTypes");
384 for (int i = 0; i < jArr.length(); i++) {
385 org.json.JSONObject jObj = jArr.getJSONObject(i);
386 String value = jObj.get("name").toString();
388 artifactTypesArrayFromApi.add(value);
390 return artifactTypesArrayFromApi;
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);
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;
410 public static List<Map<String, Object>> getAuditFromMessage(Map<String, Object> auditingMessage) {
411 List<Map<String, Object>> auditList = new ArrayList<>();
412 auditList.add(auditingMessage);
416 public static List<CategoryDefinition> parseCategories(RestResponse getAllCategoriesRest) {
418 List<CategoryDefinition> categories = new ArrayList<>();
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);
429 } catch (Exception e) {
436 public static JSONArray getListFromJson(RestResponse res, String field) throws JSONException {
437 String valueFromJsonResponse = getValueFromJsonResponse(res.getResponse(), field);
438 JSONArray jArr = new JSONArray(valueFromJsonResponse);
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));
453 public static Map<String, Object> convertStringToMap(String obj) {
454 Map<String, Object> object = (Map<String, Object>) JSONValue.parse(obj);
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);
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);
475 public static String getJsonObjectValueByKey(String metadata, String key) {
476 JsonElement jelement = new JsonParser().parse(metadata);
478 JsonObject jobject = jelement.getAsJsonObject();
479 Object obj = jobject.get(key);
483 return obj.toString();
487 // /*public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
489 // // Map<String, ArrayList<Component>> map = new HashMap<String,
490 // // ArrayList<Component>>();
491 // Map<String, List<Component>> map = new HashMap<>();
493 // JsonElement jElement = new JsonParser().parse(response);
494 // JsonObject jObject = jElement.getAsJsonObject();
495 // JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
496 // JsonArray jArrServices = jObject.getAsJsonArray("services");
498 //// JsonArray jArrProducts = jObject.getAsJsonArray("products");
500 // if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
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);
512 // map.put("resources", restResponseArray);
514 // ///////// SERVICE/////////////////////////////
516 // restResponseArray = new ArrayList<>();
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);
524 // map.put("services", restResponseArray);
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);
535 //// map.put("products", restResponseArray);
539 // map.put("resources", new ArrayList<>());
540 // map.put("services", new ArrayList<>());
542 //// map.put("products", new ArrayList<>());
549 public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
551 // Map<String, ArrayList<Component>> map = new HashMap<String,
552 // ArrayList<Component>>();
553 Map<String, List<Component>> map = new HashMap<>();
555 JsonElement jElement = new JsonParser().parse(response);
556 JsonObject jObject = jElement.getAsJsonObject();
557 JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
558 JsonArray jArrServices = jObject.getAsJsonArray("services");
560 // JsonArray jArrProducts = jObject.getAsJsonArray("products");
563 if (jArrReousrces != null && jArrServices != null /*&& jArrProducts != null*/){
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);
575 map.put("resources", restResponseArray);
577 ///////// SERVICE/////////////////////////////
579 restResponseArray = new ArrayList<>();
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);
587 map.put("services", restResponseArray);
590 ///////// PRODUCT/////////////////////////////
591 // restResponseArray = new ArrayList<>();
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);
601 // map.put("products", restResponseArray);
606 map.put("resources", new ArrayList<>());
607 map.put("services", new ArrayList<>());
609 // map.put("products", new ArrayList<>());
616 public static Map<Long, ServiceDistributionStatus> convertServiceDistributionStatusToObject(String response) throws ParseException {
618 Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap = new HashMap<>();
619 ServiceDistributionStatus serviceDistributionStatusObject = null;
621 JsonElement jElement = new JsonParser().parse(response);
622 JsonObject jObject = jElement.getAsJsonObject();
623 JsonArray jDistrStatusArray = jObject.getAsJsonArray("distributionStatusOfServiceList");
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);
632 return serviceDistributionStatusMap;
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);
647 return propertiesMap;
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);
657 if(resourceName.contains(parsedResponse.getName()) && parsedResponse.getName().contains(resourceName)) {
658 return parsedResponse;
665 public static Map<String, VfModuleDefinition> convertVfModuleJsonResponseToJavaObject(String response) {
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();
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);
684 public static InterfaceDefinition convertInterfaceDefinitionResponseToJavaObject(String response) {
685 ObjectMapper mapper = new ObjectMapper();
686 InterfaceDefinition interfaceDefinition = null;
688 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
689 interfaceDefinition = mapper.readValue(response, InterfaceDefinition.class);
690 logger.debug(interfaceDefinition.toString());
692 catch (IOException e) {
695 return interfaceDefinition;