2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.serialization;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
30 import java.lang.reflect.Type;
31 import java.util.Iterator;
32 import javax.ws.rs.core.Response;
33 import lombok.NonNull;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaDataType;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * GSON type adapter for TOSCA data types.
45 * @author Chenfei Gao (cgao@research.att.com)
47 public class ToscaDataTypesJsonAdapter implements JsonSerializer<ToscaDataTypes>, JsonDeserializer<ToscaDataTypes> {
49 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDataTypesJsonAdapter.class);
52 public ToscaDataTypes deserialize(@NonNull final JsonElement dataTypesElement, @NonNull final Type type,
53 @NonNull final JsonDeserializationContext context) {
56 final JsonArray dataTypesJsonArray = dataTypesElement.getAsJsonArray();
58 // The outgoing object
59 final PfConceptKey dataTypesKey = new PfConceptKey("IncomingDataTypes", "0.0.1");
60 final ToscaDataTypes dataTypes = new ToscaDataTypes(dataTypesKey);
63 Iterator<JsonElement> dataTypesIterator = dataTypesJsonArray.iterator();
64 while (dataTypesIterator.hasNext()) {
65 ToscaDataType dataType = new ToscaDataTypeJsonAdapter()
66 .deserialize(dataTypesIterator.next(), ToscaDataType.class, context);
68 dataTypes.getConceptMap().put(dataType.getKey(), dataType);
75 public JsonElement serialize(@NonNull final ToscaDataTypes dataTypes, @NonNull final Type type,
76 @NonNull final JsonSerializationContext context) {
78 JsonArray dataTypesJsonArray = new JsonArray();
80 if (dataTypes.getConceptMap().isEmpty()) {
81 String errorMessage = "data type list is empty";
82 LOGGER.debug(errorMessage);
83 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage);
86 for (ToscaDataType dataType: dataTypes.getConceptMap().values()) {
87 JsonElement dataTypeEntry = new ToscaDataTypeJsonAdapter().serialize(dataType, type, context);
88 dataTypesJsonArray.add(dataTypeEntry);
91 return dataTypesJsonArray;