|
| 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.model.ToscaAttributeDefinition; |
| 20 | +import org.apache.cloudstack.tosca.model.ToscaFieldDefinition; |
| 21 | +import org.apache.cloudstack.tosca.model.ToscaNodeType; |
| 22 | +import org.apache.cloudstack.tosca.model.ToscaPrimitiveType; |
| 23 | +import org.apache.cloudstack.tosca.model.ToscaPropertyDefinition; |
| 24 | +import org.apache.commons.lang3.BooleanUtils; |
| 25 | +import org.apache.commons.lang3.EnumUtils; |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | + |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +public class ToscaParser { |
| 35 | + private Logger logger = LogManager.getLogger(ToscaParser.class); |
| 36 | + |
| 37 | + enum FieldDefinitionType { |
| 38 | + ATTRIBUTE, PROPERTY |
| 39 | + } |
| 40 | + |
| 41 | + private static final String NODE_TYPES_KEY = "node_types"; |
| 42 | + private static final String NODE_TYPES_PROPERTIES_KEY = "properties"; |
| 43 | + private static final String NODE_TYPES_ATTRIBUTES_KEY = "attributes"; |
| 44 | + |
| 45 | + @SuppressWarnings("unchecked") |
| 46 | + public ToscaNodeType parseNodeType(Path path) { |
| 47 | + Object rawYaml = ToscaYamlHelper.loadYaml(path); |
| 48 | + Map<String, Object> yamlRoot = ToscaYamlHelper.asMap(rawYaml); |
| 49 | + logger.debug("Trying to extract only one node type from the [{}] file, since it is expected for each node type to be declared separately.", path); |
| 50 | + Optional<Map.Entry<String, Object>> nodeTypeRaw = ToscaYamlHelper.asMap(yamlRoot.get(NODE_TYPES_KEY)).entrySet().stream().findFirst(); |
| 51 | + if (nodeTypeRaw.isEmpty()) { |
| 52 | + logger.error("No node types are declared in [{}].", path); |
| 53 | + return null; |
| 54 | + } |
| 55 | + String nodeTypeName = nodeTypeRaw.get().getKey(); |
| 56 | + logger.debug("Parsing the following node type: [{}].", nodeTypeName); |
| 57 | + Map<String, Object> nodeTypeBody = ToscaYamlHelper.asMap(nodeTypeRaw.get().getValue()); |
| 58 | + Map<String, ToscaPropertyDefinition> propertyDefinitions = (Map<String, ToscaPropertyDefinition>) parseFieldDefinition(nodeTypeBody.get(NODE_TYPES_PROPERTIES_KEY), FieldDefinitionType.PROPERTY); |
| 59 | + Map<String, ToscaAttributeDefinition> attributeDefinitions = (Map<String, ToscaAttributeDefinition>) parseFieldDefinition(nodeTypeBody.get(NODE_TYPES_ATTRIBUTES_KEY), FieldDefinitionType.ATTRIBUTE); |
| 60 | + ToscaNodeType nodeType = new ToscaNodeType(nodeTypeName, propertyDefinitions, attributeDefinitions); |
| 61 | + logger.info("Successfully parsed the following node type: [{}].", nodeType::toString); |
| 62 | + return nodeType; |
| 63 | + } |
| 64 | + |
| 65 | + private Map<String, ? extends ToscaFieldDefinition> parseFieldDefinition(Object rawFields, FieldDefinitionType fieldDefinitionType) { |
| 66 | + Map<String, Object> fields = ToscaYamlHelper.asMap(rawFields); |
| 67 | + logger.debug("Parsing the following {}: [{}].", |
| 68 | + () -> fieldDefinitionType == FieldDefinitionType.ATTRIBUTE ? "attributes" : "properties", fields::keySet); |
| 69 | + |
| 70 | + return fields.entrySet().stream().map((field) -> { |
| 71 | + String fieldName = field.getKey(); |
| 72 | + Map<String, Object> fieldBody = ToscaYamlHelper.asMap(field.getValue()); |
| 73 | + ToscaPrimitiveType fieldType = EnumUtils.getEnumIgnoreCase(ToscaPrimitiveType.class, ToscaYamlHelper.asString(fieldBody.get("type"))); |
| 74 | + String propertyDescription = ToscaYamlHelper.asString(fieldBody.get("description")); |
| 75 | + |
| 76 | + if (fieldDefinitionType == FieldDefinitionType.PROPERTY) { |
| 77 | + ToscaAttributeDefinition attributeDefinition = new ToscaAttributeDefinition(fieldName, propertyDescription, fieldType); |
| 78 | + logger.debug("Successfully parsed the following property: [{}].", attributeDefinition::toString); |
| 79 | + return attributeDefinition; |
| 80 | + } |
| 81 | + |
| 82 | + boolean required = BooleanUtils.toBoolean(ToscaYamlHelper.asString(fieldBody.get("required"))); |
| 83 | + Object validation = fieldBody.get("validation"); |
| 84 | + ToscaPropertyDefinition propertyDefinition = new ToscaPropertyDefinition(fieldName, propertyDescription, fieldType, required, validation);; |
| 85 | + logger.debug("Successfully parsed the following property: [{}].", propertyDefinition::toString); |
| 86 | + return propertyDefinition; |
| 87 | + }).collect(Collectors.toMap(ToscaFieldDefinition::getName, (field) -> field)); |
| 88 | + } |
| 89 | +} |
0 commit comments