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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.serialization;
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;
30 import java.lang.reflect.Type;
32 import lombok.NonNull;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfReferenceKey;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
40 * GSON type adapter for TOSCA policies.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class ToscaTopologyTemplateJsonAdapter
45 implements JsonSerializer<ToscaTopologyTemplate>, JsonDeserializer<ToscaTopologyTemplate> {
48 public ToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement,
49 @NonNull final Type type, @NonNull final JsonDeserializationContext context) {
52 final JsonObject topologyTemplateJsonObject = toplogyTemplateElement.getAsJsonObject();
54 // The outgoing object
55 final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate");
56 final ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate(topologyTemplateKey);
58 if (topologyTemplateJsonObject.has("policies")) {
59 topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter()
60 .deserialize(topologyTemplateJsonObject.get("policies"), ToscaPolicies.class, context));
63 return topologyTemplate;
67 public JsonElement serialize(@NonNull final ToscaTopologyTemplate topologyTemplate, @NonNull final Type type,
68 @NonNull final JsonSerializationContext context) {