075445d019517f9d38db70f5155026c1228155ad
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / serialization / ToscaPolicyJsonAdapter.java
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.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
30 import java.lang.reflect.Type;
31
32 import javax.ws.rs.core.Response;
33
34 import lombok.NonNull;
35
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * GSON type adapter for TOSCA policies.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, JsonDeserializer<ToscaPolicy> {
48     // Logger for this class
49     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class);
50
51     @Override
52     public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
53             @NonNull final JsonDeserializationContext context) {
54
55         // The incoming JSON
56         final JsonObject policyJsonMapObject = policyElement.getAsJsonObject();
57
58         // We should only have a single entry for the policy
59         if (policyJsonMapObject.entrySet().size() != 1) {
60             String errorMessage = "a policy list entry may only contain one and only one policy";
61             LOGGER.debug(errorMessage);
62             throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage);
63         }
64
65         String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
66         JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject();
67
68         PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get("version").getAsString());
69         PfConceptKey policyTypeKey = new PfConceptKey(
70                 policyJsonObject.get("type").getAsString(),
71                 policyJsonObject.get("version").getAsString());
72         ToscaPolicy policy = new ToscaPolicy(policyKey, policyTypeKey);
73
74         // TODO: Rest of parsing
75
76         return policy;
77     }
78
79     @Override
80     public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type,
81             @NonNull final JsonSerializationContext context) {
82
83         return null;
84     }
85 }