2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.serialization;
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
34 import java.util.Map.Entry;
35 import javax.ws.rs.core.Response;
37 import lombok.NonNull;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * GSON type adapter for TOSCA policies.
48 * @author Liam Fallon (liam.fallon@est.tech)
49 * @author Chenfei Gao (cgao@research.att.com)
51 public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, JsonDeserializer<ToscaPolicy> {
52 // Logger for this class
53 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class);
55 private static final String TYPE = "type";
56 private static final String DESCRIPTION = "description";
57 private static final String VERSION = "version";
58 private static final String METADATA = "metadata";
59 private static final String PROPERTIES = "properties";
62 public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
63 @NonNull final JsonDeserializationContext context) {
66 final JsonObject policyJsonMapObject = policyElement.getAsJsonObject();
68 // We should only have a single entry for the policy
69 if (policyJsonMapObject.entrySet().size() != 1) {
70 String errorMessage = "a policy list entry may only contain one and only one policy";
71 LOGGER.debug(errorMessage);
72 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
75 final String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
76 final JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next()
77 .getValue().getAsJsonObject();
80 PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get(VERSION).getAsString());
81 PfConceptKey policyTypeKey = new PfConceptKey(
82 policyJsonObject.get(TYPE).getAsString(),
83 policyJsonObject.get(VERSION).getAsString());
84 ToscaPolicy policy = new ToscaPolicy(policyKey, policyTypeKey);
87 if (policyJsonObject.has(DESCRIPTION)) {
88 final String policyDescription = policyJsonObject.get(DESCRIPTION).getAsString();
89 policy.setDescription(policyDescription);
93 if (policyJsonObject.has(METADATA)) {
94 final JsonObject policyMetadataMapObject = policyJsonObject.get(METADATA).getAsJsonObject();
95 Map<String, String> policyMetadataMap = new HashMap<>();
96 for (Entry<String, JsonElement> entry : policyMetadataMapObject.entrySet()) {
97 final String policyMetadataEntryKey = entry.getKey();
98 final String policyMetadataEntryValue = entry.getValue().getAsString();
99 policyMetadataMap.put(policyMetadataEntryKey, policyMetadataEntryValue);
101 policy.setMetadata(policyMetadataMap);
105 if (policyJsonObject.has(PROPERTIES)) {
106 final JsonObject policyPropertiesMapObject = policyJsonObject.get(PROPERTIES).getAsJsonObject();
107 Map<String, Object> propertiesMap = new HashMap<>();
108 for (Entry<String, JsonElement> entry : policyPropertiesMapObject.entrySet()) {
109 final String policyPropertiesEntryKey = entry.getKey();
110 final JsonElement policyPropertiesEntryValue = entry.getValue();
111 propertiesMap.put(policyPropertiesEntryKey, policyPropertiesEntryValue);
113 policy.setProperties(propertiesMap);
119 public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type,
120 @NonNull final JsonSerializationContext context) {
122 JsonObject policyValJsonObject = new JsonObject();
125 policyValJsonObject.addProperty(TYPE, policy.getType().getName());
128 policyValJsonObject.addProperty(VERSION, policy.getType().getVersion());
131 if (policy.getDescription() != null) {
132 policyValJsonObject.addProperty(DESCRIPTION, policy.getDescription());
136 if (policy.getMetadata() != null) {
137 JsonObject metadataMapObject = new JsonObject();
138 for (Entry<String, String> entry : policy.getMetadata().entrySet()) {
139 final String entryKey = entry.getKey();
140 final String entryVal = entry.getValue();
141 metadataMapObject.addProperty(entryKey, entryVal);
143 policyValJsonObject.add(METADATA, metadataMapObject);
147 if (policy.getProperties() != null) {
148 JsonObject propertiesMapObject = new JsonObject();
149 for (Entry<String, Object> entry : policy.getProperties().entrySet()) {
150 final String entryKey = entry.getKey();
151 JsonElement entryVal = null;
152 if (entry.getValue() instanceof JsonElement) {
153 entryVal = (JsonElement) entry.getValue();
155 propertiesMapObject.add(entryKey, entryVal);
157 policyValJsonObject.add(PROPERTIES, propertiesMapObject);
160 JsonObject policyJsonObject = new JsonObject();
161 policyJsonObject.add(policy.getKey().getName(), policyValJsonObject);
162 return policyJsonObject;