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