Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaPoliciesJsonAdapter.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.JsonArray;
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30
31 import java.lang.reflect.Type;
32 import java.util.Iterator;
33 import lombok.NonNull;
34
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
38
39 /**
40  * GSON type adapter for TOSCA policies.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  * @author Chenfei Gao (cgao@research.att.com)
44  */
45 public class ToscaPoliciesJsonAdapter implements JsonSerializer<JpaToscaPolicies>, JsonDeserializer<JpaToscaPolicies> {
46
47     @Override
48     public JpaToscaPolicies deserialize(@NonNull final JsonElement policiesElement, @NonNull final Type type,
49             @NonNull final JsonDeserializationContext context) {
50         // The incoming JSON
51         final JsonArray policiesJsonArray = policiesElement.getAsJsonArray();
52
53         // The outgoing object
54         final PfConceptKey policiesKey = new PfConceptKey("IncomingPolicies", "0.0.1");
55         final JpaToscaPolicies policies = new JpaToscaPolicies(policiesKey);
56
57         // Get the policies
58         for (Iterator<JsonElement> policiesIterator = policiesJsonArray.iterator(); policiesIterator.hasNext(); ) {
59             JpaToscaPolicy policy = new ToscaPolicyJsonAdapter()
60                     .deserialize(policiesIterator.next(), JpaToscaPolicy.class, context);
61
62             policies.getConceptMap().put(policy.getKey(), policy);
63         }
64
65         return policies;
66     }
67
68     @Override
69     public JsonElement serialize(@NonNull final JpaToscaPolicies policies, @NonNull final Type type,
70             @NonNull final JsonSerializationContext context) {
71
72         JsonArray policiesJsonArray = new JsonArray();
73
74         for (JpaToscaPolicy policy: policies.getConceptMap().values()) {
75             policiesJsonArray.add(new ToscaPolicyJsonAdapter().serialize(policy, type, context));
76         }
77         return policiesJsonArray;
78     }
79 }