|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.tosca.parser; |
| 18 | + |
| 19 | +import org.apache.cloudstack.tosca.functions.ToscaBooleanFunctions; |
| 20 | +import org.apache.cloudstack.tosca.functions.ToscaFunction; |
| 21 | +import org.apache.cloudstack.tosca.model.ToscaAttributeDefinition; |
| 22 | +import org.apache.cloudstack.tosca.model.ToscaCollectionType; |
| 23 | +import org.apache.cloudstack.tosca.model.ToscaDataTypeDefinition; |
| 24 | +import org.apache.cloudstack.tosca.model.ToscaFieldDefinition; |
| 25 | +import org.apache.cloudstack.tosca.model.ToscaPrimitiveType; |
| 26 | +import org.apache.cloudstack.tosca.model.ToscaPropertyDefinition; |
| 27 | +import org.apache.cloudstack.tosca.model.ToscaTypeDefinition; |
| 28 | +import org.apache.commons.collections.MapUtils; |
| 29 | +import org.apache.commons.lang3.EnumUtils; |
| 30 | +import org.apache.logging.log4j.LogManager; |
| 31 | +import org.apache.logging.log4j.Logger; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.stream.Collectors; |
| 36 | + |
| 37 | +public class ToscaFieldParser { |
| 38 | + private final Logger logger = LogManager.getLogger(ToscaFieldParser.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * Parses a field (property or attribute) of a TOSCA resource. |
| 42 | + * @param rawFields The body of the field. Example: "{ name: { type: string, description: Name } }" |
| 43 | + * @param typeOfToscaField The type of the field that will be parsed. |
| 44 | + * @param dataTypes Available data types. If null, then the type of the fields must be a primitive or a collection. |
| 45 | + * @return a map of {@link ToscaFieldDefinition} representing the fields, whose key is the name of the field and whose value is the field itself. |
| 46 | + */ |
| 47 | + protected Map<String, ? extends ToscaFieldDefinition> parseField(Object rawFields, ToscaConstants.TypeOfToscaField typeOfToscaField, Map<String, ToscaDataTypeDefinition> dataTypes) { |
| 48 | + Map<String, Object> fields = ToscaYamlHelper.asMap(rawFields); |
| 49 | + logger.debug("Parsing the following {}: {}.", |
| 50 | + () -> typeOfToscaField == ToscaConstants.TypeOfToscaField.ATTRIBUTE ? "attributes" : "properties", fields::keySet); |
| 51 | + |
| 52 | + return fields.entrySet().stream().map((field) -> { |
| 53 | + String name = field.getKey(); |
| 54 | + Map<String, Object> fieldBody = ToscaYamlHelper.asMap(field.getValue()); |
| 55 | + String description = ToscaYamlHelper.asString(fieldBody.get(ToscaConstants.FIELDS_DESCRIPTION_KEY)); |
| 56 | + ToscaTypeDefinition type = parseType(fieldBody, dataTypes); |
| 57 | + |
| 58 | + if (typeOfToscaField == ToscaConstants.TypeOfToscaField.ATTRIBUTE) { |
| 59 | + ToscaAttributeDefinition attributeDefinition = new ToscaAttributeDefinition(name, description, type); |
| 60 | + logger.debug("Successfully parsed the following attribute: [{}].", attributeDefinition::toString); |
| 61 | + return attributeDefinition; |
| 62 | + } |
| 63 | + |
| 64 | + boolean required = ToscaYamlHelper.asBoolean(fieldBody.get(ToscaConstants.FIELDS_REQUIRED_KEY)); |
| 65 | + ToscaFunction.ToscaBooleanFunction validation = parseToscaBooleanFunction(ToscaYamlHelper.asMap(fieldBody.get(ToscaConstants.FIELDS_VALIDATION_KEY))); |
| 66 | + ToscaPropertyDefinition propertyDefinition = new ToscaPropertyDefinition(name, description, type, required, validation); |
| 67 | + logger.debug("Successfully parsed the following property: [{}].", propertyDefinition::toString); |
| 68 | + return propertyDefinition; |
| 69 | + }).collect(Collectors.toMap(ToscaFieldDefinition::getName, (field) -> field)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Parses the type of TOSCA resource. Types can be primitive, collection, or data type, and are represented by the {@link ToscaTypeDefinition} class. |
| 74 | + * @param typeBody The body of the type. Example: "{ type: list, entry_schema: { type: NodeOffering } }" |
| 75 | + * @param dataTypes The available data types. If null, then the type being parsed must be either a primitive or a collection. |
| 76 | + * @return a {@link ToscaTypeDefinition} representing the type of the field. Null if the type is not recognized. |
| 77 | + */ |
| 78 | + protected ToscaTypeDefinition parseType(Map<String, Object> typeBody, Map<String, ToscaDataTypeDefinition> dataTypes) { |
| 79 | + String rawType = ToscaYamlHelper.asString(typeBody.get(ToscaConstants.FIELDS_TYPE_KEY)); |
| 80 | + logger.debug("Parsing the following type: [{}].", rawType); |
| 81 | + ToscaPrimitiveType primitiveType = EnumUtils.getEnumIgnoreCase(ToscaPrimitiveType.class, rawType); |
| 82 | + if (primitiveType != null) { |
| 83 | + logger.debug("The type is a primitive, returning its corresponding ToscaTypeDefinition."); |
| 84 | + return ToscaTypeDefinition.ofPrimitive(primitiveType); |
| 85 | + } |
| 86 | + |
| 87 | + ToscaCollectionType collectionType = EnumUtils.getEnumIgnoreCase(ToscaCollectionType.class, rawType); |
| 88 | + if (collectionType != null) { |
| 89 | + logger.debug("The type is a collection, returning its corresponding ToscaTypeDefinition."); |
| 90 | + return ToscaTypeDefinition.ofCollection(collectionType, parseType(ToscaYamlHelper.asMap(typeBody.get(ToscaConstants.FIELDS_ENTRY_SCHEMA_KEY)), dataTypes)); |
| 91 | + } |
| 92 | + |
| 93 | + if (MapUtils.isEmpty(dataTypes) || !dataTypes.containsKey(rawType)) { |
| 94 | + logger.debug("The [{}] type is not recognized, returning null.", rawType); |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + logger.debug("The type is a data type, returning its corresponding ToscaTypeDefinition based in the declared data types: {}.", dataTypes::keySet); |
| 99 | + return ToscaTypeDefinition.ofDataType(dataTypes.get(rawType)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Parses a TOSCA boolean ({@link ToscaFunction.ToscaBooleanFunction}) function. |
| 104 | + * @param validationBody The body of the validation field. Example: "{ $valid_values: [ $value, [ CloudManaged, ExternalManaged ] ] }") |
| 105 | + * @return a {@link ToscaFunction.ToscaBooleanFunction} representing the boolean function. If the function is not recognized, returns null. |
| 106 | + */ |
| 107 | + protected ToscaFunction.ToscaBooleanFunction parseToscaBooleanFunction(Map<String, Object> validationBody) { |
| 108 | + if (MapUtils.isEmpty(validationBody)) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + Map.Entry<String, Object> function = validationBody.entrySet().iterator().next(); |
| 113 | + String name = function.getKey(); |
| 114 | + Object arguments = function.getValue(); |
| 115 | + logger.debug("Parsing the following TOSCA boolean function: [{}].", name); |
| 116 | + switch (name) { |
| 117 | + case "$valid_values": |
| 118 | + return parseValidValuesFunction(arguments); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Parses the $valid_values TOSCA function. |
| 125 | + * @param arguments The arguments of the $valid_values function. Example: "{ $valid_values: [ $value, [ CloudManaged, ExternalManaged ] ] }" |
| 126 | + * @return a {@link ToscaBooleanFunctions.ValidValues} (typed as {@link ToscaFunction.ToscaBooleanFunction}) representing the $valid_values function. |
| 127 | + */ |
| 128 | + private ToscaFunction.ToscaBooleanFunction parseValidValuesFunction(Object arguments) { |
| 129 | + List<Object> args = (List<Object>) arguments; |
| 130 | + List<Object> validValues = (List<Object>) args.get(1); |
| 131 | + return new ToscaBooleanFunctions.ValidValues(validValues); |
| 132 | + } |
| 133 | +} |
0 commit comments