Add serialization/deserialization for tosca policy
[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.ToscaServiceTemplate;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
38
39 /**
40  * GSON type adapter for TOSCA policies.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  * @author Chenfei Gao (cgao@research.att.com)
44  */
45 public class ToscaServiceTemplateJsonAdapter
46         implements JsonSerializer<ToscaServiceTemplate>, JsonDeserializer<ToscaServiceTemplate> {
47
48     private static final String TOPOLOGY_TEMPLATE = "topology_template";
49     private static final String TOSCA_DEFINITIONS_VERSION = "tosca_definitions_version";
50
51     @Override
52     public ToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, @NonNull final Type type,
53             @NonNull final JsonDeserializationContext context) {
54
55         // The incoming JSON
56         final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject();
57
58         // The outgoing object
59         final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1");
60         final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
61         serviceTemplate
62                 .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString());
63
64         if (serviceTemplateJsonObject.has(TOPOLOGY_TEMPLATE)) {
65             serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize(
66                     serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE), ToscaTopologyTemplate.class, context));
67         }
68
69         // Set the parent key of the topology template to be this service template
70         serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey);
71
72         return serviceTemplate;
73     }
74
75     @Override
76     public JsonElement serialize(@NonNull final ToscaServiceTemplate serviceTemplate, @NonNull final Type type,
77             @NonNull final JsonSerializationContext context) {
78
79         JsonObject serviceTemplateJsonObject = new JsonObject();
80         JsonElement topologyTemplateJsonElement = new ToscaTopologyTemplateJsonAdapter()
81                 .serialize(serviceTemplate.getTopologyTemplate(), type, context);
82
83         serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION, serviceTemplate.getToscaDefinitionsVersion());
84         serviceTemplateJsonObject.add(TOPOLOGY_TEMPLATE, topologyTemplateJsonElement);
85
86         return serviceTemplateJsonObject;
87     }
88 }