Restructure for authorative models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaTopologyTemplateJsonAdapter.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.base.PfReferenceKey;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
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 ToscaTopologyTemplateJsonAdapter
47         implements JsonSerializer<JpaToscaTopologyTemplate>, JsonDeserializer<JpaToscaTopologyTemplate> {
48
49     private static final String POLICIES = "policies";
50     private static final String DESCRIPTION = "description";
51
52     @Override
53     public JpaToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement,
54             @NonNull final Type type, @NonNull final JsonDeserializationContext context) {
55
56         // The incoming JSON
57         final JsonObject topologyTemplateJsonObject = toplogyTemplateElement.getAsJsonObject();
58
59         // The outgoing object
60         final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate");
61         final JpaToscaTopologyTemplate topologyTemplate = new JpaToscaTopologyTemplate(topologyTemplateKey);
62
63         if (topologyTemplateJsonObject.has(DESCRIPTION)) {
64             topologyTemplate.setDescription(topologyTemplateJsonObject.get(DESCRIPTION).getAsString());
65         }
66
67         if (topologyTemplateJsonObject.has(POLICIES)) {
68             topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter()
69                     .deserialize(topologyTemplateJsonObject.get(POLICIES), JpaToscaPolicies.class, context));
70         }
71
72         return topologyTemplate;
73     }
74
75     @Override
76     public JsonElement serialize(@NonNull final JpaToscaTopologyTemplate topologyTemplate, @NonNull final Type type,
77             @NonNull final JsonSerializationContext context) {
78
79         JsonObject topologyTemplateJsonObject = new JsonObject();
80         JsonElement policiesJsonElement = new ToscaPoliciesJsonAdapter()
81                 .serialize(topologyTemplate.getPolicies(), type, context);
82
83         topologyTemplateJsonObject.add(POLICIES, policiesJsonElement);
84
85         if (topologyTemplate.getDescription() != null) {
86             topologyTemplateJsonObject.addProperty(DESCRIPTION, topologyTemplate.getDescription());
87         }
88
89         return topologyTemplateJsonObject;
90     }
91 }