Search in sources :

Example 1 with ListPropertyValue

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

the class ToscaTypeConverter method toPropertyValue.

@SuppressWarnings("unchecked")
public PropertyValue toPropertyValue(Object resolvedPropertyValue, PropertyDefinition propertyDefinition) {
    if (resolvedPropertyValue == null) {
        return null;
    }
    if (ToscaTypes.isSimple(propertyDefinition.getType())) {
        return new ScalarPropertyValue(resolvedPropertyValue.toString());
    }
    switch(propertyDefinition.getType()) {
        case ToscaTypes.MAP:
            if (resolvedPropertyValue instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) resolvedPropertyValue;
                Map<String, Object> resultMap = Maps.newHashMap();
                map.forEach((key, value) -> resultMap.put(key, toPropertyValue(value, propertyDefinition.getEntrySchema())));
                return new ComplexPropertyValue(resultMap);
            } else {
                throw new IllegalStateException("Property value: expected type [" + Map.class.getSimpleName() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
        case ToscaTypes.LIST:
            if (resolvedPropertyValue instanceof Collection) {
                List list = (List) resolvedPropertyValue;
                List resultList = new LinkedList();
                for (Object item : list) {
                    resultList.add(toPropertyValue(item, propertyDefinition.getEntrySchema()));
                }
                return new ListPropertyValue(resultList);
            } else {
                throw new IllegalStateException("Property value: expected type [" + Collection.class.getSimpleName() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
        default:
            DataType dataType = findDataType(propertyDefinition.getType());
            if (dataType == null) {
                throw new NotFoundException("Data type  [" + propertyDefinition.getType() + "] cannot be found");
            }
            if (dataType.isDeriveFromSimpleType()) {
                return new ScalarPropertyValue(resolvedPropertyValue.toString());
            } else if (resolvedPropertyValue instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) resolvedPropertyValue;
                /*
                 * Map<String, Object> resultMap = Maps.newHashMap();
                 * 
                 * map.forEach((key, value) -> {
                 * PropertyDefinition entryDefinition = dataType.getProperties().get(key);
                 * if(entryDefinition == null){
                 * throw new IllegalStateException("DataType [" + propertyDefinition.getType() + "] does not contains any definition for entry [" + key + "]");
                 * }
                 * resultMap.put(key, toPropertyValue(value, entryDefinition));
                 * });
                 * return new ComplexPropertyValue(resultMap);
                 */
                return new ComplexPropertyValue(map);
            } else {
                throw new IllegalStateException("Property value: expected type [" + propertyDefinition.getType() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
    }
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) LinkedList(java.util.LinkedList) Collection(java.util.Collection) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) List(java.util.List) LinkedList(java.util.LinkedList) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map)

Example 2 with ListPropertyValue

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

the class AntiAffinityModifier method apply.

private void apply(PolicyTemplate policy, Topology topology, FlowExecutionContext context) {
    AbstractPropertyValue value = policy.getProperties().get("availability_zones");
    List<NodeTemplate> targets = getTargets(policy, topology, context);
    if (targets == null) {
        // Some targets are not instances of org.alien4cloud.nodes.mock.aws.Compute
        return;
    }
    if (safe(policy.getTargets()).size() < 2) {
        context.log().error("Anti-affinity policy {} is not correctly configured, at least 2 targets are required.", policy.getName());
        return;
    }
    if (!(value instanceof ListPropertyValue) || ((ListPropertyValue) value).getValue().size() < 2) {
        context.log().error("Anti-affinity policy {} is not correctly configured, zones property is required and must contains at least 2 values.", policy.getName());
        return;
    }
    ListPropertyValue propertyValue = (ListPropertyValue) value;
    for (int i = 0; i < targets.size(); i++) {
        NodeTemplate nodeTemplate = targets.get(i);
        String nodeZone = (String) propertyValue.getValue().get(i % propertyValue.getValue().size());
        if (AWS_MOCK_COMPUTE_TYPE.equals(nodeTemplate.getType())) {
            context.log().info("Anti-affinity policy {} inject zone property {} to node {}", policy.getName(), nodeZone, nodeTemplate.getName());
            nodeTemplate.getProperties().put("zone", new ScalarPropertyValue(nodeZone));
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 3 with ListPropertyValue

use of org.alien4cloud.tosca.model.definitions.ListPropertyValue 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 4 with ListPropertyValue

use of org.alien4cloud.tosca.model.definitions.ListPropertyValue 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 5 with ListPropertyValue

use of org.alien4cloud.tosca.model.definitions.ListPropertyValue 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)

Aggregations

ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)6 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)5 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 Map (java.util.Map)4 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)4 List (java.util.List)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)2 NotFoundException (alien4cloud.exception.NotFoundException)1 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)1 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)1 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)1 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)1 DataType (org.alien4cloud.tosca.model.types.DataType)1 PrimitiveDataType (org.alien4cloud.tosca.model.types.PrimitiveDataType)1 Test (org.junit.Test)1