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 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;
33 public class RequirementDefinitionDeserializer implements JsonDeserializer<RequirementDefinition> {
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";
41 public RequirementDefinition deserialize(JsonElement jsonElement, Type type,
42 JsonDeserializationContext jsonDeserializationContext) {
44 JsonObject jsonObject = jsonElement.getAsJsonObject();
46 RequirementDefinition requirementDefinition = new RequirementDefinition();
47 setRequirementValues(jsonObject, requirementDefinition);
49 Optional<Object[]> occurrences = handleOccurrences(jsonObject);
50 occurrences.ifPresent(requirementDefinition::setOccurrences);
52 return requirementDefinition;
56 private void setRequirementValues(JsonObject jsonObject,
57 RequirementDefinition requirementDefinition) {
59 JsonElement capabilityElement = jsonObject.get(CAPABILITY);
60 if (!Objects.isNull(capabilityElement)) {
61 requirementDefinition.setCapability(capabilityElement.getAsString());
64 JsonElement nodeElement = jsonObject.get(NODE);
65 if (!Objects.isNull(nodeElement)) {
66 requirementDefinition.setNode(nodeElement.getAsString());
69 JsonElement relationshipElement = jsonObject.get(RELATIONSHIP);
70 if (!Objects.isNull(relationshipElement)) {
71 requirementDefinition.setRelationship(relationshipElement.getAsString());
75 private Optional<Object[]> handleOccurrences(JsonObject jsonObject) {
77 JsonElement occurrencesElement = jsonObject.get(OCCURRENCES);
79 if(Objects.isNull(occurrencesElement)){
80 return Optional.empty();
83 JsonArray occurrences = occurrencesElement.getAsJsonArray();
84 Object[] fixedOccurrences = new Object[occurrences.size()];
86 for (int i = 0; i < occurrences.size(); i++) {
87 JsonElement currElement = occurrences.get(i);
89 // values inside occurrences array can be either String or Integer
90 if (currElement.isJsonPrimitive()) {
91 JsonPrimitive jsonPrimitive = currElement.getAsJsonPrimitive();
93 if (jsonPrimitive.isNumber()) {
94 fixedOccurrences[i] = jsonPrimitive.getAsNumber().intValue();
96 fixedOccurrences[i] = jsonPrimitive.getAsString();
101 return Optional.of(fixedOccurrences);