Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaServiceTemplateJsonAdapter.java
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
33 import lombok.NonNull;
34
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
39
40 /**
41  * GSON type adapter for TOSCA policies.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class ToscaServiceTemplateJsonAdapter
47         implements JsonSerializer<JpaToscaServiceTemplate>, JsonDeserializer<JpaToscaServiceTemplate> {
48
49     private static final String TOPOLOGY_TEMPLATE = "topology_template";
50     private static final String TOSCA_DEFINITIONS_VERSION = "tosca_definitions_version";
51     private static final String POLICY_TYPES = "policy_types";
52     private static final String DATA_TYPES = "data_types";
53
54     @Override
55     public JpaToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement,
56             @NonNull final Type type, @NonNull final JsonDeserializationContext context) {
57
58         // The incoming JSON
59         final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject();
60
61         // The outgoing object
62         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
63         serviceTemplate
64                 .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString());
65
66         // Set topology_template
67         if (serviceTemplateJsonObject.has(TOPOLOGY_TEMPLATE)) {
68             serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize(
69                     serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE), JpaToscaTopologyTemplate.class, context));
70         }
71
72         // Set policy_types
73         if (serviceTemplateJsonObject.has(POLICY_TYPES)) {
74             serviceTemplate.setPolicyTypes(new ToscaPolicyTypesJsonAdapter()
75                     .deserialize(serviceTemplateJsonObject.get(POLICY_TYPES), JpaToscaPolicyTypes.class, context));
76         }
77
78         // Set data_types
79         if (serviceTemplateJsonObject.has(DATA_TYPES)) {
80             serviceTemplate.setDataTypes(new ToscaDataTypesJsonAdapter()
81                     .deserialize(serviceTemplateJsonObject.get(DATA_TYPES), JpaToscaDataTypes.class, context));
82         }
83
84         return serviceTemplate;
85     }
86
87     @Override
88     public JsonElement serialize(@NonNull final JpaToscaServiceTemplate serviceTemplate, @NonNull final Type type,
89             @NonNull final JsonSerializationContext context) {
90
91         JsonObject serviceTemplateJsonObject = new JsonObject();
92
93         // Serialize tosca_definitions_version
94         if (serviceTemplate.getToscaDefinitionsVersion() != null) {
95             serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION,
96                     serviceTemplate.getToscaDefinitionsVersion());
97         }
98
99         // Serialize topoligy_template
100         if (serviceTemplate.getTopologyTemplate() != null) {
101             JsonElement topologyTemplateJsonElement = new ToscaTopologyTemplateJsonAdapter()
102                     .serialize(serviceTemplate.getTopologyTemplate(), type, context);
103             serviceTemplateJsonObject.add(TOPOLOGY_TEMPLATE, topologyTemplateJsonElement);
104         }
105
106         // Serialize policy_types
107         if (serviceTemplate.getPolicyTypes() != null) {
108             JsonElement policyTypesJsonElement =
109                     new ToscaPolicyTypesJsonAdapter().serialize(serviceTemplate.getPolicyTypes(), type, context);
110             serviceTemplateJsonObject.add(POLICY_TYPES, policyTypesJsonElement);
111         }
112
113         // Serialize data_types
114         if (serviceTemplate.getDataTypes() != null) {
115             JsonElement dataTypesJsonElement =
116                     new ToscaDataTypesJsonAdapter().serialize(serviceTemplate.getDataTypes(), type, context);
117             serviceTemplateJsonObject.add(DATA_TYPES, dataTypesJsonElement);
118         }
119
120         return serviceTemplateJsonObject;
121     }
122 }