2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.openecomp.core.utilities.deserializers;
22 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
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;
32 import java.lang.reflect.Type;
33 import java.util.Objects;
34 import java.util.Optional;
36 public class RequirementDefinitionDeserializer implements JsonDeserializer<RequirementDefinition> {
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";
44 public RequirementDefinition deserialize(JsonElement jsonElement, Type type,
45 JsonDeserializationContext jsonDeserializationContext) {
47 JsonObject jsonObject = jsonElement.getAsJsonObject();
49 RequirementDefinition requirementDefinition = new RequirementDefinition();
50 setRequirementValues(jsonObject, requirementDefinition);
52 Optional<Object[]> occurrences = handleOccurrences(jsonObject);
53 occurrences.ifPresent(requirementDefinition::setOccurrences);
55 return requirementDefinition;
59 private void setRequirementValues(JsonObject jsonObject,
60 RequirementDefinition requirementDefinition) {
62 JsonElement capabilityElement = jsonObject.get(CAPABILITY);
63 if (!Objects.isNull(capabilityElement)) {
64 requirementDefinition.setCapability(capabilityElement.getAsString());
67 JsonElement nodeElement = jsonObject.get(NODE);
68 if (!Objects.isNull(nodeElement)) {
69 requirementDefinition.setNode(nodeElement.getAsString());
72 JsonElement relationshipElement = jsonObject.get(RELATIONSHIP);
73 if (!Objects.isNull(relationshipElement)) {
74 requirementDefinition.setRelationship(relationshipElement.getAsString());
78 private Optional<Object[]> handleOccurrences(JsonObject jsonObject) {
80 JsonElement occurrencesElement = jsonObject.get(OCCURRENCES);
82 if(Objects.isNull(occurrencesElement)){
83 return Optional.empty();
86 JsonArray occurrences = occurrencesElement.getAsJsonArray();
87 Object[] fixedOccurrences = new Object[occurrences.size()];
89 for (int i = 0; i < occurrences.size(); i++) {
90 JsonElement currElement = occurrences.get(i);
92 // values inside occurrences array can be either String or Integer
93 if (currElement.isJsonPrimitive()) {
94 JsonPrimitive jsonPrimitive = currElement.getAsJsonPrimitive();
96 if (jsonPrimitive.isNumber()) {
97 fixedOccurrences[i] = jsonPrimitive.getAsNumber().intValue();
99 fixedOccurrences[i] = jsonPrimitive.getAsString();
104 return Optional.of(fixedOccurrences);