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