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.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29 import java.lang.reflect.Type;
30 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * GSON type adapter for TOSCA policy types.
44 * @author Chenfei Gao (cgao@research.att.com)
46 public class ToscaPolicyTypeJsonAdapter
47 implements JsonSerializer<JpaToscaPolicyType>, JsonDeserializer<JpaToscaPolicyType> {
49 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeJsonAdapter.class);
51 private static final String DERIVED_FROM = "derived_from";
52 private static final String DESCRIPTION = "description";
53 private static final String VERSION = "version";
54 private static final String PROPERTIES = "properties";
55 private static final String DEFAULT_VERSION = "1.0.0";
58 public JpaToscaPolicyType deserialize(@NonNull final JsonElement policyTypeElement, @NonNull final Type type,
59 @NonNull final JsonDeserializationContext context) {
62 final JsonObject policyTypeJsonMapObject = policyTypeElement.getAsJsonObject();
64 // We should only have a single entry for the policy type
65 if (policyTypeJsonMapObject.entrySet().size() != 1) {
66 String errorMessage = "a policy type list entry may only contain one and only one policy type";
67 LOGGER.debug(errorMessage);
68 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
71 final String policyTypeName = policyTypeJsonMapObject.entrySet().iterator().next().getKey();
72 final JsonObject policyTypeJsonObject =
73 policyTypeJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject();
76 PfConceptKey policyTypeKey;
77 if (policyTypeJsonObject.get(VERSION) == null) {
78 policyTypeKey = new PfConceptKey(policyTypeName, DEFAULT_VERSION);
80 policyTypeKey = new PfConceptKey(policyTypeName, policyTypeJsonObject.get(VERSION).getAsString());
82 JpaToscaPolicyType policyType = new JpaToscaPolicyType(policyTypeKey);
85 policyType.setDerivedFrom(
86 new PfConceptKey(policyTypeJsonObject.get(DERIVED_FROM).getAsString(), DEFAULT_VERSION));
89 if (policyTypeJsonObject.has(DESCRIPTION)) {
90 final String policyTypeDescription = policyTypeJsonObject.get(DESCRIPTION).getAsString();
91 policyType.setDescription(policyTypeDescription);
95 if (policyTypeJsonObject.has(PROPERTIES)) {
96 policyType.setProperties(
97 new ToscaPropertiesJsonAdapter().deserializeProperties(policyTypeJsonObject.get(PROPERTIES)));
98 for (JpaToscaProperty property : policyType.getProperties()) {
99 property.getKey().setParentConceptKey(policyTypeKey);
100 property.getType().setVersion(policyType.getKey().getVersion());
108 public JsonElement serialize(@NonNull final JpaToscaPolicyType policyType, @NonNull final Type type,
109 @NonNull final JsonSerializationContext context) {
111 JsonObject policyTypeValJsonObject = new JsonObject();
114 if (policyType.getDerivedFrom() != null) {
115 policyTypeValJsonObject.addProperty(DERIVED_FROM, policyType.getDerivedFrom().getName());
119 if (policyType.getDescription() != null) {
120 policyTypeValJsonObject.addProperty(DESCRIPTION, policyType.getDescription());
124 if (policyType.getKey().getVersion() != null) {
125 policyTypeValJsonObject.addProperty(VERSION, policyType.getKey().getVersion());
129 if (policyType.getProperties() != null) {
130 JsonElement propertiesJsonElement =
131 new ToscaPropertiesJsonAdapter().serializeProperties(policyType.getProperties());
132 policyTypeValJsonObject.add(PROPERTIES, propertiesJsonElement);
135 JsonObject policyTypeJsonObject = new JsonObject();
136 policyTypeJsonObject.add(policyType.getKey().getName(), policyTypeValJsonObject);
137 return policyTypeJsonObject;