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