2b61b6861c2856cd39f2f86c602021df1e08b68c
[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.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
33 import lombok.NonNull;
34
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
38
39 /**
40  * GSON type adapter for TOSCA policies.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>, JsonDeserializer<ToscaPolicies> {
45     @Override
46     public ToscaPolicies deserialize(@NonNull final JsonElement policiesElement, @NonNull final Type type,
47             @NonNull final JsonDeserializationContext context) {
48         // The incoming JSON
49         final JsonArray policiesJsonArray = policiesElement.getAsJsonArray();
50
51         // The outgoing object
52         final PfConceptKey policiesKey = new PfConceptKey("IncomingPolicies", "0.0.1");
53         final ToscaPolicies policies = new ToscaPolicies(policiesKey);
54
55         // Get the policies
56         for (Iterator<JsonElement> policiesIterator = policiesJsonArray.iterator(); policiesIterator.hasNext(); ) {
57             ToscaPolicy policy = new ToscaPolicyJsonAdapter()
58                     .deserialize(policiesIterator.next(), ToscaPolicy.class, context);
59
60             policies.getConceptMap().put(policy.getKey(), policy);
61         }
62
63         return policies;
64     }
65
66     @Override
67     public JsonElement serialize(@NonNull final ToscaPolicies policy, @NonNull final Type type,
68             @NonNull final JsonSerializationContext context) {
69
70         return null;
71     }
72 }