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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.serialization;
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;
30 import java.lang.reflect.Type;
32 import javax.ws.rs.core.Response;
34 import lombok.NonNull;
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;
43 * GSON type adapter for TOSCA policies.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, JsonDeserializer<ToscaPolicy> {
48 // Logger for this class
49 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class);
52 public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
53 @NonNull final JsonDeserializationContext context) {
56 final JsonObject policyJsonMapObject = policyElement.getAsJsonObject();
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);
65 String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
66 JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject();
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);
74 // TODO: Rest of parsing
80 public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type,
81 @NonNull final JsonSerializationContext context) {