3bf98572f92b6e48681dca1f764bf3158805c3b4
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.simple.serialization;
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 import java.lang.reflect.Type;
30 import javax.ws.rs.core.Response;
31
32 import lombok.NonNull;
33
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaProperty;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * GSON type adapter for TOSCA policy types.
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class ToscaPolicyTypeJsonAdapter implements JsonSerializer<ToscaPolicyType>, JsonDeserializer<ToscaPolicyType> {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeJsonAdapter.class);
49
50     private static final String DERIVED_FROM = "derived_from";
51     private static final String DESCRIPTION = "description";
52     private static final String VERSION = "version";
53     private static final String PROPERTIES = "properties";
54     private static final String DEFAULT_VERSION = "1.0.0";
55
56     @Override
57     public ToscaPolicyType deserialize(@NonNull final JsonElement policyTypeElement, @NonNull final Type type,
58             @NonNull final JsonDeserializationContext context) {
59
60         // The incoming JSON
61         final JsonObject policyTypeJsonMapObject = policyTypeElement.getAsJsonObject();
62
63         // We should only have a single entry for the policy type
64         if (policyTypeJsonMapObject.entrySet().size() != 1) {
65             String errorMessage = "a policy type list entry may only contain one and only one policy type";
66             LOGGER.debug(errorMessage);
67             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
68         }
69
70         final String policyTypeName = policyTypeJsonMapObject.entrySet().iterator().next().getKey();
71         final JsonObject policyTypeJsonObject = policyTypeJsonMapObject.entrySet().iterator().next()
72                                             .getValue().getAsJsonObject();
73
74         // Set keys
75         PfConceptKey policyTypeKey;
76         if (policyTypeJsonObject.get(VERSION) == null) {
77             policyTypeKey = new PfConceptKey(policyTypeName, DEFAULT_VERSION);
78         } else {
79             policyTypeKey = new PfConceptKey(policyTypeName, policyTypeJsonObject.get(VERSION).getAsString());
80         }
81         ToscaPolicyType policyType = new ToscaPolicyType(policyTypeKey);
82
83         // Set derived_from
84         policyType.setDerivedFrom(new PfConceptKey(policyTypeJsonObject.get(DERIVED_FROM).getAsString(),
85                 DEFAULT_VERSION));
86
87         // Set description
88         if (policyTypeJsonObject.has(DESCRIPTION)) {
89             final String policyTypeDescription = policyTypeJsonObject.get(DESCRIPTION).getAsString();
90             policyType.setDescription(policyTypeDescription);
91         }
92
93         // Set properties
94         if (policyTypeJsonObject.has(PROPERTIES)) {
95             policyType.setProperties(
96                     new ToscaPropertiesJsonAdapter().deserializeProperties(policyTypeJsonObject.get(PROPERTIES)));
97             for (ToscaProperty property : policyType.getProperties()) {
98                 property.getKey().setParentConceptKey(policyTypeKey);
99                 property.getType().setVersion(policyType.getKey().getVersion());
100             }
101         }
102
103         return policyType;
104     }
105
106     @Override
107     public JsonElement serialize(@NonNull final ToscaPolicyType policyType, @NonNull final Type type,
108             @NonNull final JsonSerializationContext context) {
109
110         JsonObject policyTypeValJsonObject = new JsonObject();
111
112         // Add derived_from
113         if (policyType.getDerivedFrom() != null) {
114             policyTypeValJsonObject.addProperty(DERIVED_FROM, policyType.getDerivedFrom().getName());
115         }
116
117         // Add description
118         if (policyType.getDescription() != null) {
119             policyTypeValJsonObject.addProperty(DESCRIPTION, policyType.getDescription());
120         }
121
122         // Add version
123         if (policyType.getKey().getVersion() != null) {
124             policyTypeValJsonObject.addProperty(VERSION, policyType.getKey().getVersion());
125         }
126
127         // Add properties
128         if (policyType.getProperties() != null) {
129             JsonElement propertiesJsonElement = new ToscaPropertiesJsonAdapter()
130                     .serializeProperties(policyType.getProperties());
131             policyTypeValJsonObject.add(PROPERTIES, propertiesJsonElement);
132         }
133
134         JsonObject policyTypeJsonObject = new JsonObject();
135         policyTypeJsonObject.add(policyType.getKey().getName(), policyTypeValJsonObject);
136         return policyTypeJsonObject;
137     }
138 }