aef8547106aa53357cb7a7196233a1ee6556a523
[policy/models.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.serialization;
23
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;
30
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import javax.ws.rs.core.Response;
36
37 import lombok.NonNull;
38
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;
44
45 /**
46  * GSON type adapter for TOSCA policies.
47  *
48  * @author Liam Fallon (liam.fallon@est.tech)
49  * @author Chenfei Gao (cgao@research.att.com)
50  */
51 public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, JsonDeserializer<ToscaPolicy> {
52     // Logger for this class
53     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class);
54
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";
60
61     @Override
62     public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
63             @NonNull final JsonDeserializationContext context) {
64
65         // The incoming JSON
66         final JsonObject policyJsonMapObject = policyElement.getAsJsonObject();
67
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);
73         }
74
75         final String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
76         final JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next()
77                                             .getValue().getAsJsonObject();
78
79         // Set keys
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);
85
86         // Set description
87         if (policyJsonObject.has(DESCRIPTION)) {
88             final String policyDescription = policyJsonObject.get(DESCRIPTION).getAsString();
89             policy.setDescription(policyDescription);
90         }
91
92         // Set metadata
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);
100             }
101             policy.setMetadata(policyMetadataMap);
102         }
103
104         // Set properties
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);
112             }
113             policy.setProperties(propertiesMap);
114         }
115         return policy;
116     }
117
118     @Override
119     public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type,
120             @NonNull final JsonSerializationContext context) {
121
122         JsonObject policyValJsonObject = new JsonObject();
123
124         // Add type
125         policyValJsonObject.addProperty(TYPE, policy.getType().getName());
126
127         // Add version
128         policyValJsonObject.addProperty(VERSION, policy.getType().getVersion());
129
130         // Add description
131         if (policy.getDescription() != null) {
132             policyValJsonObject.addProperty(DESCRIPTION, policy.getDescription());
133         }
134
135         // Add metadata
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);
142             }
143             policyValJsonObject.add(METADATA, metadataMapObject);
144         }
145
146         // Add properties
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();
154                 }
155                 propertiesMapObject.add(entryKey, entryVal);
156             }
157             policyValJsonObject.add(PROPERTIES, propertiesMapObject);
158         }
159
160         JsonObject policyJsonObject = new JsonObject();
161         policyJsonObject.add(policy.getKey().getName(), policyValJsonObject);
162         return policyJsonObject;
163     }
164 }