Search in sources :

Example 1 with Property

use of org.yaml.snakeyaml.introspector.Property in project studio by craftercms.

the class AbstractPluginDescriptorUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    Path descriptorFile = getRepositoryPath(site).getParent().resolve(descriptorPath);
    if (Files.notExists(descriptorFile)) {
        logger.info("Plugin descriptor file not found for site {0}", site);
        return;
    }
    try (Reader reader = Files.newBufferedReader(descriptorFile)) {
        PluginDescriptor descriptor = descriptorReader.read(reader);
        if (descriptor.getDescriptorVersion().equals(descriptorVersion)) {
            logger.info("Plugin descriptor already update for site " + site);
            return;
        }
        logger.info("Updating plugin descriptor for site " + site);
        doPluginDescriptorUpdates(descriptor);
        descriptor.setDescriptorVersion(descriptorVersion);
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        Yaml yaml = new Yaml(new DisableClassLoadingConstructor(), new Representer() {

            @Override
            protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
                if (propertyValue != null) {
                    return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
                }
                return null;
            }
        }, options);
        String content = yaml.dumpAsMap(descriptor);
        writeToRepo(site, descriptorPath, new ByteArrayInputStream(content.getBytes()));
        commitAllChanges(site);
    } catch (Exception e) {
        throw new UpgradeException("Plugin descriptor can't be read for site " + site);
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptorReader(org.craftercms.commons.plugin.PluginDescriptorReader) Reader(java.io.Reader) Yaml(org.yaml.snakeyaml.Yaml) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) Representer(org.yaml.snakeyaml.representer.Representer) ByteArrayInputStream(java.io.ByteArrayInputStream) DumperOptions(org.yaml.snakeyaml.DumperOptions) Tag(org.yaml.snakeyaml.nodes.Tag) DisableClassLoadingConstructor(org.craftercms.commons.config.DisableClassLoadingConstructor) Property(org.yaml.snakeyaml.introspector.Property) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Example 2 with Property

use of org.yaml.snakeyaml.introspector.Property in project sdc by onap.

the class ToscaExportHandler method convertInterfaceNodeType.

public Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode, Map<String, ToscaNodeType> nodeTypes, boolean isAssociatedComponent) {
    log.debug("start convert node type for {}", component.getUniqueId());
    ToscaNodeType toscaNodeType = createNodeType(component);
    Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither = interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(component.getModel());
    if (lifecycleTypeEither.isRight() && !StorageOperationStatus.NOT_FOUND.equals(lifecycleTypeEither.right().value())) {
        log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
        return Either.right(ToscaError.GENERAL_ERROR);
    }
    if (lifecycleTypeEither.isLeft()) {
        List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream().map(InterfaceDataDefinition::getType).collect(Collectors.toList());
        toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
    }
    Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = applicationDataTypeCache.getAll(component.getModel());
    if (dataTypesEither.isRight()) {
        log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
        return Either.right(ToscaError.GENERAL_ERROR);
    }
    Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
    List<InputDefinition> inputDef = component.getInputs();
    Map<String, ToscaProperty> mergedProperties = new HashMap<>();
    interfacesOperationsConverter.addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent);
    addInputsToProperties(dataTypes, inputDef, mergedProperties);
    final Map<String, ToscaAttribute> toscaAttributeMap;
    toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes);
    if (!toscaAttributeMap.isEmpty()) {
        toscaNodeType.setAttributes(toscaAttributeMap);
    }
    if (CollectionUtils.isNotEmpty(component.getProperties())) {
        List<PropertyDefinition> properties = component.getProperties();
        Map<String, ToscaProperty> convertedProperties = properties.stream().map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, component.getInputs())).collect(Collectors.toMap(PropertyDataDefinition::getName, property -> propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY)));
        // merge component properties and inputs properties
        mergedProperties.putAll(convertedProperties);
    }
    if (MapUtils.isNotEmpty(mergedProperties)) {
        toscaNodeType.setProperties(mergedProperties);
    }
    /* convert private data_types */
    List<DataTypeDefinition> privateDataTypes = component.getDataTypes();
    if (CollectionUtils.isNotEmpty(privateDataTypes)) {
        Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
        for (DataTypeDefinition dataType : privateDataTypes) {
            log.debug("Emitting private data type: component.name={} dataType.name={}", component.getNormalizedName(), dataType.getName());
            ToscaDataType toscaDataType = new ToscaDataType();
            toscaDataType.setDerived_from(dataType.getDerivedFromName());
            toscaDataType.setDescription(dataType.getDescription());
            toscaDataType.setVersion(dataType.getVersion());
            if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
                toscaDataType.setProperties(dataType.getProperties().stream().collect(Collectors.toMap(PropertyDataDefinition::getName, s -> propertyConvertor.convertProperty(dataTypes, s, PropertyType.PROPERTY), (toscaPropertyTobeValidated, toscaProperty) -> validateToscaProperty(privateDataTypes, toscaPropertyTobeValidated, toscaProperty))));
            }
            toscaDataTypeMap.put(dataType.getName(), toscaDataType);
        }
        toscaNode.setData_types(toscaDataTypeMap);
    }
    // Extracted to method for code reuse
    return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
}
Also used : InputDefinition(org.openecomp.sdc.be.model.InputDefinition) Arrays(java.util.Arrays) BeanAccess(org.yaml.snakeyaml.introspector.BeanAccess) ToscaTemplateRequirement(org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement) ToscaRelationshipTemplate(org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) EcompLoggerErrorCode(org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode) Node(org.yaml.snakeyaml.nodes.Node) ToscaTemplate(org.openecomp.sdc.be.tosca.model.ToscaTemplate) ResourceTypeEnum(org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum) Map(java.util.Map) ToscaRequirement(org.openecomp.sdc.be.tosca.model.ToscaRequirement) CategoryDefinition(org.openecomp.sdc.be.model.category.CategoryDefinition) Path(java.nio.file.Path) Triple(org.apache.commons.lang3.tuple.Triple) ToscaTopolgyTemplate(org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate) PropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition) InputConverter(org.openecomp.sdc.be.tosca.utils.InputConverter) ToscaOperationFacade(org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade) PropertyType(org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType) ToscaAttribute(org.openecomp.sdc.be.tosca.model.ToscaAttribute) Property(org.yaml.snakeyaml.introspector.Property) Set(java.util.Set) InterfaceDataDefinition(org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition) PropertiesUtils.resolvePropertyValueFromInput(org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput) ToscaConversionException(org.openecomp.sdc.be.tosca.exception.ToscaConversionException) PropertyUtils(org.yaml.snakeyaml.introspector.PropertyUtils) OriginTypeEnum(org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum) ModelConverter(org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter) CINodeFilterDataDefinition(org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition) Supplier(java.util.function.Supplier) ToscaNodeType(org.openecomp.sdc.be.tosca.model.ToscaNodeType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DumperOptions(org.yaml.snakeyaml.DumperOptions) ModelOperation(org.openecomp.sdc.be.model.operations.impl.ModelOperation) OutputConverter(org.openecomp.sdc.be.tosca.utils.OutputConverter) ComponentInstanceAttribute(org.openecomp.sdc.be.model.ComponentInstanceAttribute) Either(fj.data.Either) LinkedHashSet(java.util.LinkedHashSet) Logger(org.openecomp.sdc.common.log.wrappers.Logger) CollectionUtils.isNotEmpty(org.apache.commons.collections.CollectionUtils.isNotEmpty) ComponentTypeEnum(org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) GroupInstance(org.openecomp.sdc.be.model.GroupInstance) RequirementNodeFilterCapabilityDataDefinition(org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition) ConfigurationManager(org.openecomp.sdc.be.config.ConfigurationManager) DataTypeDefinition(org.openecomp.sdc.be.model.DataTypeDefinition) RequirementNodeFilterPropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition) RelationshipInfo(org.openecomp.sdc.be.model.RelationshipInfo) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) FlowStyle(org.yaml.snakeyaml.DumperOptions.FlowStyle) GET_PROPERTY(org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_PROPERTY) AttributeDataDefinition(org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition) ApplicationDataTypeCache(org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache) JsonPresentationFields(org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields) ToscaRelationshipBuilder(org.openecomp.sdc.be.tosca.builder.ToscaRelationshipBuilder) ListDataDefinition(org.openecomp.sdc.be.datatypes.elements.ListDataDefinition) MapUtils.isNotEmpty(org.apache.commons.collections.MapUtils.isNotEmpty) NodeFilter(org.openecomp.sdc.be.tosca.model.NodeFilter) JanusGraphOperationStatus(org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus) SdcResourceNotFoundException(org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException) CapabilityFilter(org.openecomp.sdc.be.tosca.model.CapabilityFilter) AttributeDefinition(org.openecomp.sdc.be.model.AttributeDefinition) ComponentInstanceProperty(org.openecomp.sdc.be.model.ComponentInstanceProperty) ResourceMetadataDataDefinition(org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition) GET_ATTRIBUTE(org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_ATTRIBUTE) ToscaNodeTemplate(org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate) Service(org.openecomp.sdc.be.model.Service) ToscaExportException(org.openecomp.sdc.be.exception.ToscaExportException) Collection(java.util.Collection) YamlUtil(org.onap.sdc.tosca.services.YamlUtil) ComponentInstanceInput(org.openecomp.sdc.be.model.ComponentInstanceInput) ToscaPropertyAssignment(org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment) Collectors(java.util.stream.Collectors) ToscaImportByModel(org.openecomp.sdc.be.data.model.ToscaImportByModel) Objects(java.util.Objects) InputDefinition(org.openecomp.sdc.be.model.InputDefinition) List(java.util.List) SubstitutionMapping(org.openecomp.sdc.be.tosca.model.SubstitutionMapping) Entry(java.util.Map.Entry) Optional(java.util.Optional) ToscaPolicyTemplate(org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate) Component(org.openecomp.sdc.be.model.Component) FilenameUtils(org.apache.commons.io.FilenameUtils) RequirementCapabilityRelDef(org.openecomp.sdc.be.model.RequirementCapabilityRelDef) RequirementDefinition(org.openecomp.sdc.be.model.RequirementDefinition) HashMap(java.util.HashMap) ComponentParametersView(org.openecomp.sdc.be.model.ComponentParametersView) ComponentInstance(org.openecomp.sdc.be.model.ComponentInstance) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) InterfaceDefinition(org.openecomp.sdc.be.model.InterfaceDefinition) Yaml(org.yaml.snakeyaml.Yaml) HashSet(java.util.HashSet) CapabilityRequirementRelationship(org.openecomp.sdc.be.model.CapabilityRequirementRelationship) ComponentInstanceInterface(org.openecomp.sdc.be.model.ComponentInstanceInterface) CollectionUtils(org.apache.commons.collections.CollectionUtils) ToscaGroupTemplate(org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate) InterfaceLifecycleOperation(org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation) SubstitutionFilterDataDefinition(org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition) CapabilityDefinition(org.openecomp.sdc.be.model.CapabilityDefinition) Represent(org.yaml.snakeyaml.representer.Represent) Configuration(org.openecomp.sdc.be.config.Configuration) ToscaTemplateArtifact(org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact) GET_INPUT(org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_INPUT) Iterator(java.util.Iterator) ArtifactDefinition(org.openecomp.sdc.be.model.ArtifactDefinition) MapUtils(org.apache.commons.collections.MapUtils) ForwardingPathToscaUtil(org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil) StorageOperationStatus(org.openecomp.sdc.be.model.operations.api.StorageOperationStatus) Tag(org.yaml.snakeyaml.nodes.Tag) Ints(com.google.common.primitives.Ints) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Resource(org.openecomp.sdc.be.model.Resource) ToscaDataType(org.openecomp.sdc.be.tosca.model.ToscaDataType) InterfacesOperationsConverter.addInterfaceTypeElement(org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement) ToscaCapability(org.openecomp.sdc.be.tosca.model.ToscaCapability) PropertyDefinition(org.openecomp.sdc.be.model.PropertyDefinition) RequirementSubstitutionFilterPropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition) ToscaArtifactDataDefinition(org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition) Representer(org.yaml.snakeyaml.representer.Representer) ToscaProperty(org.openecomp.sdc.be.tosca.model.ToscaProperty) Collections(java.util.Collections) ToscaAttribute(org.openecomp.sdc.be.tosca.model.ToscaAttribute) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ToscaDataType(org.openecomp.sdc.be.tosca.model.ToscaDataType) DataTypeDefinition(org.openecomp.sdc.be.model.DataTypeDefinition) PropertyDefinition(org.openecomp.sdc.be.model.PropertyDefinition) StorageOperationStatus(org.openecomp.sdc.be.model.operations.api.StorageOperationStatus) ToscaNodeType(org.openecomp.sdc.be.tosca.model.ToscaNodeType) ToscaProperty(org.openecomp.sdc.be.tosca.model.ToscaProperty) JanusGraphOperationStatus(org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) PropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition) RequirementNodeFilterPropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition) RequirementSubstitutionFilterPropertyDataDefinition(org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition)

Example 3 with Property

use of org.yaml.snakeyaml.introspector.Property in project cassandra by apache.

the class ConfigCompatabilityTest method diff.

private void diff(Loader loader, Map<Class<?>, Map<String, Replacement>> replacements, ClassTree previous, Class<?> type, String prefix, Set<String> missing, Set<String> errors) {
    Map<String, Replacement> replaces = replacements.getOrDefault(type, Collections.emptyMap());
    Map<String, Property> properties = loader.getProperties(type);
    Sets.SetView<String> missingInCurrent = Sets.difference(previous.properties.keySet(), properties.keySet());
    Sets.SetView<String> inBoth = Sets.intersection(previous.properties.keySet(), properties.keySet());
    for (String name : missingInCurrent) {
        Replacement replacement = replaces.get(name);
        // can we find the property in @Replaces?
        if (replacement == null) {
            missing.add(prefix + name);
        } else {
            // do types match?
            Node node = previous.properties.get(name);
            if (node instanceof Leaf && replacement.oldType != null)
                typeCheck(replacement.converter, toString(replacement.oldType), ((Leaf) node).type, name, errors);
        }
    }
    for (String name : inBoth) {
        Property prop = properties.get(name);
        Node node = previous.properties.get(name);
        // if nested, look at sub-fields
        if (node instanceof ClassTree) {
            // current is nested type
            diff(loader, replacements, (ClassTree) node, prop.getType(), prefix + name + ".", missing, errors);
        } else {
            // current is flat type
            Replacement replacement = replaces.get(name);
            if (replacement != null && replacement.oldType != null) {
                typeCheck(replacement.converter, toString(replacement.oldType), ((Leaf) node).type, name, errors);
            } else {
                // previous is leaf, is current?
                Map<String, Property> children = Properties.isPrimitive(prop) || Properties.isCollection(prop) ? Collections.emptyMap() : loader.getProperties(prop.getType());
                if (!children.isEmpty())
                    errors.add(String.format("Property %s used to be a value-type, but now is nested type %s", name, prop.getType()));
                typeCheck(null, toString(prop.getType()), ((Leaf) node).type, name, errors);
            }
        }
    }
}
Also used : Sets(com.google.common.collect.Sets) TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) Property(org.yaml.snakeyaml.introspector.Property)

Example 4 with Property

use of org.yaml.snakeyaml.introspector.Property in project cassandra by apache.

the class ConfigCompatabilityTest method addProperties.

private static void addProperties(Loader loader, ClassTree node, Class<?> type) {
    SortedMap<String, Property> properties = new TreeMap<>(loader.getProperties(type));
    for (Map.Entry<String, Property> e : properties.entrySet()) {
        Property property = e.getValue();
        Map<String, Property> subProperties = Properties.isPrimitive(property) || Properties.isCollection(property) ? Collections.emptyMap() : loader.getProperties(property.getType());
        Node child;
        if (subProperties.isEmpty()) {
            child = new Leaf(toString(property.getType()));
        } else {
            ClassTree subTree = new ClassTree(property.getType());
            addProperties(loader, subTree, property.getType());
            child = subTree;
        }
        node.addProperty(e.getKey(), child);
    }
}
Also used : TreeNode(com.fasterxml.jackson.core.TreeNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) TreeMap(java.util.TreeMap) Property(org.yaml.snakeyaml.introspector.Property) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 5 with Property

use of org.yaml.snakeyaml.introspector.Property in project cassandra by apache.

the class PropertiesTest method backAndForth.

@Test
public void backAndForth() throws Exception {
    Map<String, Property> ps = loader.flatten(Config.class);
    Config config = new Config();
    Set<String> keys = ImmutableSet.of("server_encryption_options.enabled", "client_encryption_options.enabled", "server_encryption_options.optional", "client_encryption_options.optional");
    for (Property prop : ps.values()) {
        // skip these properties as they don't allow get/set within the context of this test
        if (keys.contains(prop.getName()))
            continue;
        Object value = prop.get(config);
        if (value == null)
            continue;
        prop.set(config, value);
        Object back = prop.get(config);
        assertThat(back).isEqualTo(value);
    }
}
Also used : Property(org.yaml.snakeyaml.introspector.Property) Test(org.junit.Test)

Aggregations

Property (org.yaml.snakeyaml.introspector.Property)36 Test (org.junit.Test)13 MethodProperty (org.yaml.snakeyaml.introspector.MethodProperty)13 NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)12 Tag (org.yaml.snakeyaml.nodes.Tag)7 Representer (org.yaml.snakeyaml.representer.Representer)7 Map (java.util.Map)6 Yaml (org.yaml.snakeyaml.Yaml)6 DumperOptions (org.yaml.snakeyaml.DumperOptions)5 HashMap (java.util.HashMap)4 Constructor (org.yaml.snakeyaml.constructor.Constructor)4 PropertyUtils (org.yaml.snakeyaml.introspector.PropertyUtils)4 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 Collectors (java.util.stream.Collectors)3 FieldProperty (org.yaml.snakeyaml.introspector.FieldProperty)3 TreeNode (com.fasterxml.jackson.core.TreeNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 Maps (com.google.common.collect.Maps)2 IntrospectionException (java.beans.IntrospectionException)2