re base code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / main / java / org / openecomp / core / utilities / deserializers / RequirementDefinitionDeserializer.java
1 package org.openecomp.core.utilities.deserializers;
2
3 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
4
5 import com.google.gson.JsonArray;
6 import com.google.gson.JsonDeserializationContext;
7 import com.google.gson.JsonDeserializer;
8 import com.google.gson.JsonElement;
9 import com.google.gson.JsonObject;
10 import com.google.gson.JsonParseException;
11 import com.google.gson.JsonPrimitive;
12 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
13
14 import java.lang.reflect.Type;
15 import java.util.Objects;
16 import java.util.Optional;
17
18 public class RequirementDefinitionDeserializer implements JsonDeserializer<RequirementDefinition> {
19
20   private static final String CAPABILITY = "capability";
21   private static final String NODE = "node";
22   private static final String RELATIONSHIP = "relationship";
23   private static final String OCCURRENCES = "occurrences";
24
25   @Override
26   public RequirementDefinition deserialize(JsonElement jsonElement, Type type,
27                                            JsonDeserializationContext jsonDeserializationContext)
28       throws JsonParseException {
29
30     JsonObject jsonObject = jsonElement.getAsJsonObject();
31
32     RequirementDefinition requirementDefinition = new RequirementDefinition();
33     setRequirementValues(jsonObject, requirementDefinition);
34
35     Optional<Object[]> occurrences = handleOccurrences(jsonObject);
36     occurrences.ifPresent(requirementDefinition::setOccurrences);
37
38     return requirementDefinition;
39   }
40
41
42   private void setRequirementValues(JsonObject jsonObject,
43                                     RequirementDefinition requirementDefinition) {
44
45     JsonElement capabilityElement = jsonObject.get(CAPABILITY);
46     if (!Objects.isNull(capabilityElement)) {
47       requirementDefinition.setCapability(capabilityElement.getAsString());
48     }
49
50     JsonElement nodeElement = jsonObject.get(NODE);
51     if (!Objects.isNull(nodeElement)) {
52       requirementDefinition.setNode(nodeElement.getAsString());
53     }
54
55     JsonElement relationshipElement = jsonObject.get(RELATIONSHIP);
56     if (!Objects.isNull(relationshipElement)) {
57       requirementDefinition.setRelationship(relationshipElement.getAsString());
58     }
59   }
60
61   private Optional<Object[]> handleOccurrences(JsonObject jsonObject) {
62
63     JsonElement occurrencesElement = jsonObject.get(OCCURRENCES);
64
65     if(Objects.isNull(occurrencesElement)){
66       return Optional.empty();
67     }
68
69     JsonArray occurrences = occurrencesElement.getAsJsonArray();
70     Object[] fixedOccurrences = new Object[occurrences.size()];
71
72     for (int i = 0; i < occurrences.size(); i++) {
73       JsonElement currElement = occurrences.get(i);
74
75       // values inside occurrences array can be either String or Integer
76       if (currElement.isJsonPrimitive()) {
77         JsonPrimitive jsonPrimitive = currElement.getAsJsonPrimitive();
78
79         if (jsonPrimitive.isNumber()) {
80           fixedOccurrences[i] = jsonPrimitive.getAsNumber().intValue();
81         } else {
82           fixedOccurrences[i] = jsonPrimitive.getAsString();
83         }
84       }
85     }
86
87     return Optional.of(fixedOccurrences);
88   }
89 }