Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaPolicyTypesJsonAdapter.java
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.JsonArray;
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29
30 import java.lang.reflect.Type;
31 import java.util.Iterator;
32 import javax.ws.rs.core.Response;
33 import lombok.NonNull;
34
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * GSON type adapter for TOSCA policy types.
44  *
45  * @author Chenfei Gao (cgao@research.att.com)
46  */
47 public class ToscaPolicyTypesJsonAdapter implements JsonSerializer<JpaToscaPolicyTypes>,
48                                                     JsonDeserializer<JpaToscaPolicyTypes> {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypesJsonAdapter.class);
51
52     @Override
53     public JpaToscaPolicyTypes deserialize(@NonNull final JsonElement policyTypesElement, @NonNull final Type type,
54             @NonNull final JsonDeserializationContext context) {
55
56         // The incoming JSON
57         final JsonArray policyTypesJsonArray = policyTypesElement.getAsJsonArray();
58
59         // The outgoing object
60         final PfConceptKey policyTypesKey = new PfConceptKey("IncomingPolicyTypes", "0.0.1");
61         final JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(policyTypesKey);
62
63         // Get the policyTypes
64         Iterator<JsonElement> policyTypesIterator = policyTypesJsonArray.iterator();
65         while (policyTypesIterator.hasNext()) {
66             JpaToscaPolicyType policyType = new ToscaPolicyTypeJsonAdapter()
67                     .deserialize(policyTypesIterator.next(), JpaToscaPolicyType.class, context);
68
69             policyTypes.getConceptMap().put(policyType.getKey(), policyType);
70         }
71
72         return policyTypes;
73     }
74
75     @Override
76     public JsonElement serialize(@NonNull final JpaToscaPolicyTypes policyTypes, @NonNull final Type type,
77             @NonNull final JsonSerializationContext context) {
78
79         JsonArray policyTypesJsonArray = new JsonArray();
80
81         if (policyTypes.getConceptMap().isEmpty()) {
82             String errorMessage = "policy type list is empty";
83             LOGGER.debug(errorMessage);
84             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage);
85         }
86
87         for (JpaToscaPolicyType policyType: policyTypes.getConceptMap().values()) {
88             JsonElement policyTypeEntry = new  ToscaPolicyTypeJsonAdapter().serialize(policyType, type, context);
89             policyTypesJsonArray.add(policyTypeEntry);
90         }
91
92         return policyTypesJsonArray;
93     }
94 }