/*- * ============LICENSE_START======================================================= * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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.simple.serialization; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; import javax.ws.rs.core.Response; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues; import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntrySchema; import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * GSON type adapter for TOSCA properties. * * @author Chenfei Gao (cgao@research.att.com) */ public class ToscaPropertiesJsonAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPropertiesJsonAdapter.class); private static final String DESCRIPTION = "description"; private static final String REQUIRED = "required"; private static final String DEFAULT = "default"; private static final String TYPE = "type"; private static final String ENTRY_SCHEMA = "entry_schema"; private static final String CONSTRAINTS = "constraints"; private static final String EQUAL = "equal"; private static final String VALID_VALUES = "valid_values"; private static final String DEFAULT_VERSION = "1.0.0"; /** * Deserializes the properties. * * @param propertiesElement the properties in JsonElement * * @return deserialized ToscaProperty list */ public List deserializeProperties(JsonElement propertiesElement) { final JsonObject propertiesMapObject = propertiesElement.getAsJsonObject(); List properties = new LinkedList<>(); for (Entry entry : propertiesMapObject.entrySet()) { final String propertyEntryKey = entry.getKey(); final JsonElement propertyEntryVal = entry.getValue(); // Set property: key and type JpaToscaProperty property = new JpaToscaProperty( new PfReferenceKey(new PfConceptKey(), propertyEntryKey), new PfConceptKey(propertyEntryVal.getAsJsonObject().get(TYPE).getAsString(), DEFAULT_VERSION)); // Set property: description JsonObject propertyJsonObject = propertyEntryVal.getAsJsonObject(); if (propertyJsonObject.has(DESCRIPTION)) { property.setDescription(propertyJsonObject.get(DESCRIPTION).getAsString()); } // Set property: required if (propertyJsonObject.has(REQUIRED)) { property.setRequired(propertyJsonObject.get(REQUIRED).getAsBoolean()); } // Set property: default if (propertyJsonObject.has(DEFAULT)) { property.setDefaultValue(propertyJsonObject.get(DEFAULT).getAsString()); } // Set property: entry_schema if (propertyJsonObject.has(ENTRY_SCHEMA)) { checkEntrySchemaCompatibility(property.getType().getName()); property.setEntrySchema(deserializeEntrySchema(propertyJsonObject.get(ENTRY_SCHEMA))); property.getEntrySchema().getKey().setParentConceptKey(property.getType()); property.getEntrySchema().getType().setVersion(property.getType().getVersion()); } // Set property: constraints if (propertyJsonObject.has(CONSTRAINTS)) { property.setConstraints(deserializeConstraints(propertyJsonObject.get(CONSTRAINTS))); for (JpaToscaConstraint c : property.getConstraints()) { c.getKey().setParentConceptKey(property.getType()); } } // Add property to properties list properties.add(property); } return properties; } /** * Serializes the properties. * * @param properties the list of ToscaProperty * * @return serialized JsonElement */ public JsonElement serializeProperties(List properties) { JsonObject propertiesJsonObject = new JsonObject(); for (JpaToscaProperty property : properties) { JsonObject propertyValJsonObject = new JsonObject(); // Add type propertyValJsonObject.addProperty(TYPE, property.getType().getName()); // Add description if (property.getDescription() != null) { propertyValJsonObject.addProperty(DESCRIPTION, property.getDescription()); } // Add required propertyValJsonObject.addProperty(REQUIRED, property.isRequired()); // Add defaultValue if (property.getDefaultValue() != null) { propertyValJsonObject.addProperty(DEFAULT, property.getDefaultValue()); } // Add constraints if (property.getConstraints() != null) { propertyValJsonObject.add(CONSTRAINTS, serializeConstraints(property.getConstraints())); } // Add entry_schema if (property.getEntrySchema() != null) { propertyValJsonObject.add(ENTRY_SCHEMA, serializeEntrySchema(property.getEntrySchema())); } propertiesJsonObject.add(property.getKey().getLocalName(), propertyValJsonObject); } return propertiesJsonObject; } private JsonElement serializeConstraints(List constraints) { JsonArray constraintsValJsonArray = new JsonArray(); for (JpaToscaConstraint c : constraints) { JsonObject constraintJsonObject = new JsonObject(); // Check which type of constraint it is // TODO: here we only support valid_values and equal if (c instanceof JpaToscaConstraintValidValues) { JsonArray validValuesJsonArray = new JsonArray(); for (String validValue : ((JpaToscaConstraintValidValues)c).getValidValues()) { validValuesJsonArray.add(validValue); } constraintJsonObject.add(VALID_VALUES, validValuesJsonArray); } else if (c instanceof JpaToscaConstraintLogicalString) { constraintJsonObject.addProperty(EQUAL, ((JpaToscaConstraintLogicalString)c).getCompareToString()); } else { String errorMessage = "constraint is neither valid_values nor equal"; LOGGER.debug(errorMessage); throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage); } constraintsValJsonArray.add(constraintJsonObject); } return constraintsValJsonArray; } private JsonElement serializeEntrySchema(JpaToscaEntrySchema entrySchema) { JsonObject entrySchemaValJsonObject = new JsonObject(); // Add type entrySchemaValJsonObject.addProperty(TYPE, entrySchema.getType().getName()); // Add description if (entrySchema.getDescription() != null) { entrySchemaValJsonObject.addProperty(DESCRIPTION, entrySchema.getDescription()); } // Add constraints if (entrySchema.getConstraints() != null) { entrySchemaValJsonObject.add(CONSTRAINTS, serializeConstraints(entrySchema.getConstraints())); } return entrySchemaValJsonObject; } private JpaToscaEntrySchema deserializeEntrySchema(JsonElement entrySchemaElement) { JsonObject entrySchemaJsonObject = entrySchemaElement.getAsJsonObject(); // Set entry_schema: key and type JpaToscaEntrySchema entrySchema = new JpaToscaEntrySchema( new PfReferenceKey(new PfConceptKey(), ENTRY_SCHEMA), new PfConceptKey(entrySchemaJsonObject.get(TYPE).getAsString(), DEFAULT_VERSION)); // Set entry_schema: description if (entrySchemaJsonObject.has(DESCRIPTION)) { entrySchema.setDescription(entrySchemaJsonObject.get(DESCRIPTION).getAsString()); } // Set entry_schema: constraints if (entrySchemaJsonObject.has(CONSTRAINTS)) { entrySchema.setConstraints(deserializeConstraints(entrySchemaJsonObject.get(CONSTRAINTS))); for (JpaToscaConstraint c : entrySchema.getConstraints()) { c.getKey().setParentConceptKey(entrySchema.getType()); } } return entrySchema; } private List deserializeConstraints(JsonElement constraintsElement) { JsonArray constraintsJsonArray = constraintsElement.getAsJsonArray(); List constraints = new LinkedList<>(); for (Iterator constraintsIter = constraintsJsonArray.iterator(); constraintsIter.hasNext(); ) { JsonObject constraintJsonObject = constraintsIter.next().getAsJsonObject(); // Check which type of constraint it is // TODO: here we only check 'valid_values' and 'equal' if (constraintJsonObject.get(VALID_VALUES) != null) { List validValues = new LinkedList<>(); for (Iterator validValuesIter = constraintJsonObject.get(VALID_VALUES).getAsJsonArray() .iterator(); validValuesIter.hasNext(); ) { validValues.add(validValuesIter.next().getAsString()); } JpaToscaConstraint constraint = new JpaToscaConstraintValidValues( new PfReferenceKey(new PfConceptKey(), VALID_VALUES), validValues); constraints.add(constraint); } else if (constraintJsonObject.get(EQUAL) != null) { JpaToscaConstraint constraint = new JpaToscaConstraintLogicalString(new PfReferenceKey( new PfConceptKey(), EQUAL), Operation.EQ, constraintJsonObject.get(EQUAL).getAsString()); constraints.add(constraint); } else { String errorMessage = "specified constraint is neither valid_values nor equal"; LOGGER.debug(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } return constraints; } private void checkEntrySchemaCompatibility(String type) { if (!("list".equalsIgnoreCase(type)) && !("map".equalsIgnoreCase(type))) { String errorMessage = "entry schema can only be specified for list or map property"; LOGGER.debug(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } }