Search in sources :

Example 6 with ComplexPropertyValue

use of org.alien4cloud.tosca.model.definitions.ComplexPropertyValue in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien120Test method testDataTypesExtendsNative.

@Test
public void testDataTypesExtendsNative() throws ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-extends-native.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    Assert.assertEquals(3, parsingResult.getResult().getDataTypes().size());
    Assert.assertEquals(2, parsingResult.getResult().getNodeTypes().size());
    Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
    Assert.assertEquals(1, parsingResult.getResult().getTopology().getNodeTemplates().size());
    NodeTemplate nodeTemplate = parsingResult.getResult().getTopology().getNodeTemplates().values().iterator().next();
    Assert.assertEquals(3, nodeTemplate.getProperties().size());
    // check url property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("url"));
    AbstractPropertyValue url = nodeTemplate.getProperties().get("url");
    Assert.assertTrue(url instanceof ScalarPropertyValue);
    Assert.assertEquals("https://kikoo.com", ((ScalarPropertyValue) url).getValue());
    // check ipv6_addresses property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("ipv6_addresses"));
    AbstractPropertyValue ipv6_addresses = nodeTemplate.getProperties().get("ipv6_addresses");
    Assert.assertTrue(ipv6_addresses instanceof ListPropertyValue);
    List<Object> ipv6_addresses_list = ((ListPropertyValue) ipv6_addresses).getValue();
    Assert.assertEquals(2, ipv6_addresses_list.size());
    Assert.assertEquals("192.168.0.10", ipv6_addresses_list.get(0));
    Assert.assertEquals("10.0.0.10", ipv6_addresses_list.get(1));
    // check passwords property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("passwords"));
    AbstractPropertyValue passwords = nodeTemplate.getProperties().get("passwords");
    Assert.assertTrue(passwords instanceof ComplexPropertyValue);
    Map<String, Object> passwords_map = ((ComplexPropertyValue) passwords).getValue();
    Assert.assertEquals(2, passwords_map.size());
    Assert.assertEquals("123456789", passwords_map.get("user1"));
    Assert.assertEquals("abcdefghij", passwords_map.get("user2"));
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 7 with ComplexPropertyValue

use of org.alien4cloud.tosca.model.definitions.ComplexPropertyValue in project alien4cloud by alien4cloud.

the class TopologyPropertiesValidationService method addRequiredPropertyIdToTaskProperties.

private void addRequiredPropertyIdToTaskProperties(String prefix, Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> relatedProperties, PropertiesTask task, boolean skipInputProperties) {
    for (Map.Entry<String, AbstractPropertyValue> propertyEntry : properties.entrySet()) {
        PropertyDefinition propertyDef = relatedProperties.get(propertyEntry.getKey());
        String propertyErrorKey = prefix == null ? propertyEntry.getKey() : prefix + "." + propertyEntry.getKey();
        AbstractPropertyValue value = propertyEntry.getValue();
        if (propertyDef != null && propertyDef.isRequired()) {
            if (value == null) {
                addRequiredPropertyError(task, propertyErrorKey);
            } else if (value instanceof ScalarPropertyValue) {
                String propertyValue = ((ScalarPropertyValue) value).getValue();
                if (StringUtils.isBlank(propertyValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ComplexPropertyValue) {
                Map<String, Object> mapValue = ((ComplexPropertyValue) value).getValue();
                if (MapUtils.isEmpty(mapValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ListPropertyValue) {
                List<Object> listValue = ((ListPropertyValue) value).getValue();
                if (listValue.isEmpty()) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (FunctionEvaluator.containGetSecretFunction(value)) {
                // this is a get_secret function, we should not validate the get_secret here
                continue;
            } else if (skipInputProperties) {
                // get_input Will be validated later on
                continue;
            } else {
                addRequiredPropertyError(task, propertyErrorKey);
            }
        }
    }
}
Also used : ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 8 with ComplexPropertyValue

use of org.alien4cloud.tosca.model.definitions.ComplexPropertyValue in project alien4cloud by alien4cloud.

the class PropertyService method asFunctionPropertyValue.

public static <T extends AbstractPropertyValue> T asFunctionPropertyValue(Object propertyValue) {
    if (propertyValue instanceof Map) {
        Map valueAsMap = (Map) propertyValue;
        if (valueAsMap.keySet().contains("function") && valueAsMap.keySet().contains("parameters")) {
            if (!ToscaFunctionConstants.GET_SECRET.equals(valueAsMap.get("function"))) {
                throw new InvalidArgumentException("Property function " + valueAsMap.get("function") + " is invalid");
            }
            FunctionPropertyValue functionPropertyValue = new FunctionPropertyValue();
            functionPropertyValue.setFunction((String) valueAsMap.get("function"));
            functionPropertyValue.setParameters((List<String>) valueAsMap.get("parameters"));
            return (T) functionPropertyValue;
        }
    }
    return (T) new ComplexPropertyValue((Map<String, Object>) propertyValue);
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map)

Example 9 with ComplexPropertyValue

use of org.alien4cloud.tosca.model.definitions.ComplexPropertyValue in project alien4cloud by alien4cloud.

the class TopologyModifierSupport method feedPropertyValue.

// TODO: move elsewhere ?
public static String feedPropertyValue(Map propertyValues, String propertyPath, Object propertyValue, boolean lastPropertyIsAList) {
    String nodePropertyName = null;
    if (propertyPath.contains(".")) {
        String[] paths = propertyPath.split("\\.");
        nodePropertyName = paths[0];
        Map<String, Object> currentMap = null;
        for (int i = 0; i < paths.length; i++) {
            if (i == 0) {
                Object currentPropertyValue = propertyValues.get(paths[i]);
                if (currentPropertyValue != null && currentPropertyValue instanceof ComplexPropertyValue) {
                    currentMap = ((ComplexPropertyValue) currentPropertyValue).getValue();
                } else {
                    // FIXME OVERRIDING PROP VALUE This overrides the nodePropertyName property value!!!. We should instead fail if currentPropertyValue not
                    // instanceof ComplexPropertyValue
                    // FIXME and do this only if currentPropertyValue is null
                    currentMap = Maps.newHashMap();
                    propertyValues.put(nodePropertyName, new ComplexPropertyValue(currentMap));
                }
            } else if (i == paths.length - 1) {
                // TODO: find a better way to manage this
                if (lastPropertyIsAList) {
                    Object currentEntry = currentMap.get(paths[i]);
                    ListPropertyValue listPropertyValue = null;
                    if (currentEntry != null && currentEntry instanceof ListPropertyValue) {
                        listPropertyValue = (ListPropertyValue) currentEntry;
                    } else {
                        // FIXME Same as OVERRIDING PROP VALUE above
                        listPropertyValue = new ListPropertyValue(Lists.newArrayList());
                        currentMap.put(paths[i], listPropertyValue);
                    }
                    listPropertyValue.getValue().add(propertyValue);
                } else {
                    currentMap.put(paths[i], propertyValue);
                }
            } else {
                Map<String, Object> currentPropertyValue = null;
                Object currentPropertyValueObj = currentMap.get(paths[i]);
                if (currentPropertyValueObj != null && currentPropertyValueObj instanceof Map<?, ?>) {
                    currentPropertyValue = (Map<String, Object>) currentPropertyValueObj;
                } else {
                    // FIXME Same as OVERRIDING PROP VALUE above
                    currentPropertyValue = Maps.newHashMap();
                    currentMap.put(paths[i], currentPropertyValue);
                }
                currentMap = currentPropertyValue;
            }
        }
    } else {
        nodePropertyName = propertyPath;
        propertyValues.put(nodePropertyName, propertyValue);
    }
    return nodePropertyName;
}
Also used : ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map)

Example 10 with ComplexPropertyValue

use of org.alien4cloud.tosca.model.definitions.ComplexPropertyValue in project alien4cloud by alien4cloud.

the class FunctionEvaluator method getFromPath.

private static AbstractPropertyValue getFromPath(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, Map<String, AbstractPropertyValue> properties, String propertyPath) {
    if (propertyPath.contains(".")) {
        String propertyName = propertyPath.split("\\.")[0];
        AbstractPropertyValue propertyValue = properties.get(propertyName);
        if (!(propertyValue instanceof PropertyValue)) {
            // if the value is not a property value resolve it first
            propertyValue = tryResolveValue(evaluatorContext, targetTemplate, properties, propertyValue);
            if (propertyValue == null) {
                return null;
            }
        }
        // now it is a property value
        Object value = MapUtil.get(((PropertyValue) propertyValue).getValue(), propertyPath.substring(propertyName.length() + 1));
        if (value == null) {
            return null;
        } else if (value instanceof String) {
            return new ScalarPropertyValue((String) value);
        } else if (value instanceof List) {
            return new ListPropertyValue((List<Object>) value);
        } else if (value instanceof Map) {
            return new ComplexPropertyValue((Map<String, Object>) value);
        }
        throw new IllegalArgumentException("The value of a property must be a scalar, a list or a map.");
    }
    return safe(properties).get(propertyPath);
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Map(java.util.Map)

Aggregations

ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)15 Map (java.util.Map)10 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)9 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 Test (org.junit.Test)8 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)7 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)7 List (java.util.List)5 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)4 DataType (org.alien4cloud.tosca.model.types.DataType)4 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 Application (alien4cloud.model.application.Application)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Maps (com.google.common.collect.Maps)2 Arrays (java.util.Arrays)2 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)2 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)2