b08a33d647662972e30d3162361ce609f3df82f8
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.serialization.simple;
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
32 import lombok.NonNull;
33
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.tosca.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.concepts.ToscaTopologyTemplate;
37
38 /**
39  * GSON type adapter for TOSCA policies.
40  *
41  * @author Liam Fallon (liam.fallon@est.tech)
42  */
43 public class ToscaServiceTemplateJsonAdapter
44         implements JsonSerializer<ToscaServiceTemplate>, JsonDeserializer<ToscaServiceTemplate> {
45     @Override
46     public ToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, @NonNull final Type type,
47             @NonNull final JsonDeserializationContext context) {
48
49         // The incoming JSON
50         final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject();
51
52         // The outgoing object
53         final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1");
54         final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
55         serviceTemplate
56                 .setToscaDefinitionsVersion(serviceTemplateJsonObject.get("tosca_definitions_version").getAsString());
57
58         if (serviceTemplateJsonObject.has("topology_template")) {
59             serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize(
60                     serviceTemplateJsonObject.get("topology_template"), ToscaTopologyTemplate.class, context));
61         }
62
63         // Set the parent key of the topology template to be this service template
64         serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey);
65
66         return serviceTemplate;
67     }
68
69     @Override
70     public JsonElement serialize(@NonNull final ToscaServiceTemplate serviceTemplate, @NonNull final Type type,
71             @NonNull final JsonSerializationContext context) {
72
73         return null;
74     }
75 }