From 8fdf84c7f2bd03332e69d13e35414020053c2977 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 15 Mar 2019 12:36:38 +0000 Subject: Add serialization for Tosca Model Added tests for Yaml policy monitoring Added provider interface calls in provider and structure for handling legacy APIs. Issue-ID: POLICY-1195 Change-Id: I4825272e0713b9e6a4b89753828de8905bcffbd1 Signed-off-by: liamfallon --- .../tosca/concepts/ToscaTopologyTemplate.java | 10 +-- .../legacy/LegacyGuardMessageBodyHandler.java | 56 ++++++++++++++ .../serialization/legacy/LegacyGuardPolicy.java | 30 ++++++++ .../LegacyOperationalMessageBodyHandler.java | 56 ++++++++++++++ .../legacy/LegacyOperationalPolicy.java | 30 ++++++++ .../simple/ToscaPoliciesJsonAdapter.java | 72 ++++++++++++++++++ .../simple/ToscaPolicyJsonAdapter.java | 85 ++++++++++++++++++++++ .../simple/ToscaServiceTemplateJsonAdapter.java | 75 +++++++++++++++++++ .../ToscaServiceTemplateMessageBodyHandler.java | 64 ++++++++++++++++ .../simple/ToscaTopologyTemplateJsonAdapter.java | 72 ++++++++++++++++++ 10 files changed, 545 insertions(+), 5 deletions(-) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardMessageBodyHandler.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardPolicy.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalMessageBodyHandler.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalPolicy.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPoliciesJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPolicyJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateJsonAdapter.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateMessageBodyHandler.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaTopologyTemplateJsonAdapter.java (limited to 'models-tosca/src/main/java') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java index e8541f514..aed181f94 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/concepts/ToscaTopologyTemplate.java @@ -38,8 +38,8 @@ import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationMessage; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.base.PfValidationResult.ValidationResult; @@ -59,7 +59,7 @@ public class ToscaTopologyTemplate extends PfConcept { private static final long serialVersionUID = 8969698734673232603L; @EmbeddedId - private PfConceptKey key; + private PfReferenceKey key; @Column(name = "description") private String description; @@ -71,7 +71,7 @@ public class ToscaTopologyTemplate extends PfConcept { * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key. */ public ToscaTopologyTemplate() { - this(new PfConceptKey()); + this(new PfReferenceKey()); } /** @@ -80,7 +80,7 @@ public class ToscaTopologyTemplate extends PfConcept { * * @param key the key */ - public ToscaTopologyTemplate(@NonNull final PfConceptKey key) { + public ToscaTopologyTemplate(@NonNull final PfReferenceKey key) { this.key = key; } @@ -169,7 +169,7 @@ public class ToscaTopologyTemplate extends PfConcept { Assertions.instanceOf(copyObject, PfConcept.class); final ToscaTopologyTemplate copy = ((ToscaTopologyTemplate) copyObject); - copy.setKey(new PfConceptKey(key)); + copy.setKey(new PfReferenceKey(key)); copy.setDescription(description); copy.setPolicies(policies != null ? new ToscaPolicies(policies) : null); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardMessageBodyHandler.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardMessageBodyHandler.java new file mode 100644 index 000000000..b343c9e66 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardMessageBodyHandler.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.legacy; + +import com.google.gson.GsonBuilder; + +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provider used to serialize and deserialize TOSCA objects using GSON. + */ +public class LegacyGuardMessageBodyHandler extends GsonMessageBodyHandler { + + public static final Logger logger = LoggerFactory.getLogger(LegacyGuardMessageBodyHandler.class); + + /** + * Constructs the object. + */ + public LegacyGuardMessageBodyHandler() { + this(new GsonBuilder()); + + logger.info("Using GSON with TOSCA for REST calls"); + } + + /** + * Constructs the object. + * + * @param builder builder to use to create the gson object + */ + public LegacyGuardMessageBodyHandler(final GsonBuilder builder) { + // @formatter:off + super(builder + .create() + ); + // @formatter:on + } + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardPolicy.java new file mode 100644 index 000000000..a365696c3 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyGuardPolicy.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.legacy; + +/** + * Definition of a legacy guard policy stored as a TOSCA policy. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyGuardPolicy { + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalMessageBodyHandler.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalMessageBodyHandler.java new file mode 100644 index 000000000..f51a427f0 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalMessageBodyHandler.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.legacy; + +import com.google.gson.GsonBuilder; + +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provider used to serialize and deserialize TOSCA objects using GSON. + */ +public class LegacyOperationalMessageBodyHandler extends GsonMessageBodyHandler { + + public static final Logger logger = LoggerFactory.getLogger(LegacyOperationalMessageBodyHandler.class); + + /** + * Constructs the object. + */ + public LegacyOperationalMessageBodyHandler() { + this(new GsonBuilder()); + + logger.info("Using GSON with TOSCA for REST calls"); + } + + /** + * Constructs the object. + * + * @param builder builder to use to create the gson object + */ + public LegacyOperationalMessageBodyHandler(final GsonBuilder builder) { + // @formatter:off + super(builder + .create() + ); + // @formatter:on + } + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalPolicy.java new file mode 100644 index 000000000..0b63f2d68 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/legacy/LegacyOperationalPolicy.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.legacy; + +/** + * Definition of a legacy operational policy stored as a TOSCA policy. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyOperationalPolicy { + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPoliciesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPoliciesJsonAdapter.java new file mode 100644 index 000000000..a12ba8be9 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPoliciesJsonAdapter.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.simple; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; +import java.util.Iterator; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.concepts.ToscaPolicy; + +/** + * GSON type adapter for TOSCA policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPoliciesJsonAdapter implements JsonSerializer, JsonDeserializer { + @Override + public ToscaPolicies deserialize(@NonNull final JsonElement policiesElement, @NonNull final Type type, + @NonNull final JsonDeserializationContext context) { + // The incoming JSON + final JsonArray policiesJsonArray = policiesElement.getAsJsonArray(); + + // The outgoing object + final PfConceptKey policiesKey = new PfConceptKey("IncomingPolicies", "0.0.1"); + final ToscaPolicies policies = new ToscaPolicies(policiesKey); + + // Get the policies + for (Iterator policiesIterator = policiesJsonArray.iterator(); policiesIterator.hasNext(); ) { + ToscaPolicy policy = new ToscaPolicyJsonAdapter() + .deserialize(policiesIterator.next(), ToscaPolicy.class, context); + + policies.getConceptMap().put(policy.getKey(), policy); + } + + return policies; + } + + @Override + public JsonElement serialize(@NonNull final ToscaPolicies policy, @NonNull final Type type, + @NonNull final JsonSerializationContext context) { + + return null; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPolicyJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPolicyJsonAdapter.java new file mode 100644 index 000000000..ae5e596ba --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaPolicyJsonAdapter.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.simple; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; + +import javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.concepts.ToscaPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * GSON type adapter for TOSCA policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyJsonAdapter implements JsonSerializer, JsonDeserializer { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class); + + @Override + public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type, + @NonNull final JsonDeserializationContext context) { + + // The incoming JSON + final JsonObject policyJsonMapObject = policyElement.getAsJsonObject(); + + // We should only have a single entry for the policy + if (policyJsonMapObject.entrySet().size() != 1) { + String errorMessage = "a policy list entry may only contain one and only one policy"; + LOGGER.debug(errorMessage); + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage); + } + + String policyName = policyJsonMapObject.entrySet().iterator().next().getKey(); + JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject(); + + PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get("version").getAsString()); + PfConceptKey policyTypeKey = new PfConceptKey( + policyJsonObject.get("type").getAsString(), + policyJsonObject.get("version").getAsString()); + ToscaPolicy policy = new ToscaPolicy(policyKey, policyTypeKey); + + // TODO: Rest of parsing + + return policy; + } + + @Override + public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type, + @NonNull final JsonSerializationContext context) { + + return null; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateJsonAdapter.java new file mode 100644 index 000000000..b08a33d64 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateJsonAdapter.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.simple; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.concepts.ToscaTopologyTemplate; + +/** + * GSON type adapter for TOSCA policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaServiceTemplateJsonAdapter + implements JsonSerializer, JsonDeserializer { + @Override + public ToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, @NonNull final Type type, + @NonNull final JsonDeserializationContext context) { + + // The incoming JSON + final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject(); + + // The outgoing object + final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1"); + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey); + serviceTemplate + .setToscaDefinitionsVersion(serviceTemplateJsonObject.get("tosca_definitions_version").getAsString()); + + if (serviceTemplateJsonObject.has("topology_template")) { + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize( + serviceTemplateJsonObject.get("topology_template"), ToscaTopologyTemplate.class, context)); + } + + // Set the parent key of the topology template to be this service template + serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey); + + return serviceTemplate; + } + + @Override + public JsonElement serialize(@NonNull final ToscaServiceTemplate serviceTemplate, @NonNull final Type type, + @NonNull final JsonSerializationContext context) { + + return null; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateMessageBodyHandler.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateMessageBodyHandler.java new file mode 100644 index 000000000..9abbf1c72 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaServiceTemplateMessageBodyHandler.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.simple; + +import com.google.gson.GsonBuilder; + +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.models.tosca.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.concepts.ToscaTopologyTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provider used to serialize and deserialize TOSCA objects using GSON. + */ +public class ToscaServiceTemplateMessageBodyHandler extends GsonMessageBodyHandler { + + public static final Logger logger = LoggerFactory.getLogger(ToscaServiceTemplateMessageBodyHandler.class); + + /** + * Constructs the object. + */ + public ToscaServiceTemplateMessageBodyHandler() { + this(new GsonBuilder()); + + logger.info("Using GSON with TOSCA for REST calls"); + } + + /** + * Constructs the object. + * + * @param builder builder to use to create the gson object + */ + public ToscaServiceTemplateMessageBodyHandler(final GsonBuilder builder) { + // @formatter:off + super(builder + .registerTypeAdapter(ToscaServiceTemplate.class, new ToscaServiceTemplateJsonAdapter()) + .registerTypeAdapter(ToscaTopologyTemplate.class, new ToscaTopologyTemplateJsonAdapter()) + .registerTypeAdapter(ToscaPolicies.class, new ToscaPoliciesJsonAdapter()) + .registerTypeAdapter(ToscaPolicy.class, new ToscaPolicyJsonAdapter()) + .create() + ); + // @formatter:on + } + +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaTopologyTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaTopologyTemplateJsonAdapter.java new file mode 100644 index 000000000..4f2f590b7 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/serialization/simple/ToscaTopologyTemplateJsonAdapter.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.serialization.simple; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.tosca.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.concepts.ToscaTopologyTemplate; + +/** + * GSON type adapter for TOSCA policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaTopologyTemplateJsonAdapter + implements JsonSerializer, JsonDeserializer { + + @Override + public ToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement, + @NonNull final Type type, @NonNull final JsonDeserializationContext context) { + + // The incoming JSON + final JsonObject topologyTemplateJsonObject = toplogyTemplateElement.getAsJsonObject(); + + // The outgoing object + final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate"); + final ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate(topologyTemplateKey); + + if (topologyTemplateJsonObject.has("policies")) { + topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter() + .deserialize(topologyTemplateJsonObject.get("policies"), ToscaPolicies.class, context)); + } + + return topologyTemplate; + } + + @Override + public JsonElement serialize(@NonNull final ToscaTopologyTemplate topologyTemplate, @NonNull final Type type, + @NonNull final JsonSerializationContext context) { + + return null; + } +} -- cgit 1.2.3-korg