Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaDataTypeJsonAdapter.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.serialization;
22
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
30 import java.lang.reflect.Type;
31 import javax.ws.rs.core.Response;
32
33 import lombok.NonNull;
34
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.JpaToscaDataType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * GSON type adapter for TOSCA data types.
44  *
45  * @author Chenfei Gao (cgao@research.att.com)
46  */
47 public class ToscaDataTypeJsonAdapter implements JsonSerializer<JpaToscaDataType>, JsonDeserializer<JpaToscaDataType> {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDataTypeJsonAdapter.class);
50
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";
56
57     @Override
58     public JpaToscaDataType deserialize(@NonNull final JsonElement dataTypeElement, @NonNull final Type type,
59             @NonNull final JsonDeserializationContext context) {
60
61         // The incoming JSON
62         final JsonObject dataTypeJsonMapObject = dataTypeElement.getAsJsonObject();
63
64         // We should only have a single entry for the policy type
65         if (dataTypeJsonMapObject.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);
69         }
70
71         final String dataTypeName = dataTypeJsonMapObject.entrySet().iterator().next().getKey();
72         final JsonObject dataTypeJsonObject = dataTypeJsonMapObject.entrySet().iterator().next()
73                                             .getValue().getAsJsonObject();
74
75         // Set keys
76         PfConceptKey dataTypeKey;
77         if (dataTypeJsonObject.get(VERSION) == null) {
78             dataTypeKey = new PfConceptKey(dataTypeName, DEFAULT_VERSION);
79         } else {
80             dataTypeKey = new PfConceptKey(dataTypeName, dataTypeJsonObject.get(VERSION).getAsString());
81         }
82         JpaToscaDataType dataType = new JpaToscaDataType(dataTypeKey);
83
84         // Set derived_from
85         dataType.setDerivedFrom(new PfConceptKey(dataTypeJsonObject.get(DERIVED_FROM).getAsString(),
86                 DEFAULT_VERSION));
87
88         // Set description
89         if (dataTypeJsonObject.has(DESCRIPTION)) {
90             final String dataTypeDescription = dataTypeJsonObject.get(DESCRIPTION).getAsString();
91             dataType.setDescription(dataTypeDescription);
92         }
93
94         // Set properties
95         if (dataTypeJsonObject.has(PROPERTIES)) {
96             dataType.setProperties(
97                     new ToscaPropertiesJsonAdapter().deserializeProperties(dataTypeJsonObject.get(PROPERTIES)));
98             for (JpaToscaProperty property : dataType.getProperties()) {
99                 property.getKey().setParentConceptKey(dataTypeKey);
100                 property.getType().setVersion(dataType.getKey().getVersion());
101             }
102         }
103
104         return dataType;
105     }
106
107     @Override
108     public JsonElement serialize(@NonNull final JpaToscaDataType dataType, @NonNull final Type type,
109             @NonNull final JsonSerializationContext context) {
110
111         JsonObject dataTypeValJsonObject = new JsonObject();
112
113         // Add derived_from
114         if (dataType.getDerivedFrom() != null) {
115             dataTypeValJsonObject.addProperty(DERIVED_FROM, dataType.getDerivedFrom().getName());
116         }
117
118         // Add description
119         if (dataType.getDescription() != null) {
120             dataTypeValJsonObject.addProperty(DESCRIPTION, dataType.getDescription());
121         }
122
123         // Add version
124         if (dataType.getKey().getVersion() != null) {
125             dataTypeValJsonObject.addProperty(VERSION, dataType.getKey().getVersion());
126         }
127
128         // Add properties
129         if (dataType.getProperties() != null) {
130             JsonElement propertiesJsonElement = new ToscaPropertiesJsonAdapter()
131                     .serializeProperties(dataType.getProperties());
132             dataTypeValJsonObject.add(PROPERTIES, propertiesJsonElement);
133         }
134
135         JsonObject dataTypeJsonObject = new JsonObject();
136         dataTypeJsonObject.add(dataType.getKey().getName(), dataTypeValJsonObject);
137         return dataTypeJsonObject;
138     }
139 }