Search in sources :

Example 51 with AbstractPropertyValue

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

the class ToscaParserSimpleProfileAlien120Test method testDataTypesVeryComplexWithDefault.

@Test
public void testDataTypesVeryComplexWithDefault() throws ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-very-complex-default.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());
    NodeType commandType = parsingResult.getResult().getNodeTypes().get("alien.test.Command");
    Assert.assertNotNull(commandType);
    PropertyDefinition pd = commandType.getProperties().get("customer");
    Assert.assertNotNull(pd);
    // check the default value
    Object defaultValue = pd.getDefault();
    Assert.assertNotNull(defaultValue);
    Assert.assertTrue(defaultValue instanceof ComplexPropertyValue);
    ComplexPropertyValue cpv = (ComplexPropertyValue) defaultValue;
    Map<String, Object> valueAsMap = cpv.getValue();
    Assert.assertNotNull(valueAsMap);
    Assert.assertTrue(valueAsMap.containsKey("first_name"));
    Assert.assertEquals("Foo", valueAsMap.get("first_name"));
    Assert.assertTrue(valueAsMap.containsKey("last_name"));
    Assert.assertEquals("Bar", valueAsMap.get("last_name"));
    Assert.assertTrue(valueAsMap.containsKey("address"));
    Object addressObj = valueAsMap.get("address");
    Assert.assertNotNull(addressObj);
    Assert.assertTrue(addressObj instanceof Map);
    Map<String, Object> addressMap = (Map<String, Object>) addressObj;
    Assert.assertTrue(addressMap.containsKey("street_name"));
    Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
    Assert.assertTrue(addressMap.containsKey("zipcode"));
    Assert.assertEquals("92130", addressMap.get("zipcode"));
    Assert.assertTrue(addressMap.containsKey("city_name"));
    Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
    Assert.assertTrue(valueAsMap.containsKey("emails"));
    Object emailsObj = valueAsMap.get("emails");
    Assert.assertNotNull(emailsObj);
    Assert.assertTrue(emailsObj instanceof List);
    List<Object> emailsList = (List<Object>) emailsObj;
    Assert.assertEquals(2, emailsList.size());
    Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
    Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
    Object accountsObj = valueAsMap.get("accounts");
    Assert.assertNotNull(accountsObj);
    Assert.assertTrue(accountsObj instanceof Map);
    Map<String, Object> accountsMap = (Map<String, Object>) accountsObj;
    Assert.assertEquals(2, accountsMap.size());
    Assert.assertTrue(accountsMap.containsKey("main"));
    Assert.assertEquals("root", accountsMap.get("main"));
    Assert.assertTrue(accountsMap.containsKey("secondary"));
    Assert.assertEquals("user", accountsMap.get("secondary"));
    Assert.assertEquals(1, parsingResult.getResult().getTopology().getNodeTemplates().size());
    NodeTemplate nodeTemplate = parsingResult.getResult().getTopology().getNodeTemplates().values().iterator().next();
    // on the node, the default value should be set
    Assert.assertNotNull(nodeTemplate.getProperties());
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("customer"));
    AbstractPropertyValue apv = nodeTemplate.getProperties().get("customer");
    Assert.assertNotNull(apv);
    Assert.assertTrue(apv instanceof ComplexPropertyValue);
    cpv = (ComplexPropertyValue) apv;
    valueAsMap = cpv.getValue();
    Assert.assertNotNull(valueAsMap);
    Assert.assertTrue(valueAsMap.containsKey("first_name"));
    Assert.assertEquals("Foo", valueAsMap.get("first_name"));
    Assert.assertTrue(valueAsMap.containsKey("last_name"));
    Assert.assertEquals("Bar", valueAsMap.get("last_name"));
    Assert.assertTrue(valueAsMap.containsKey("address"));
    addressObj = valueAsMap.get("address");
    Assert.assertNotNull(addressObj);
    Assert.assertTrue(addressObj instanceof Map);
    addressMap = (Map<String, Object>) addressObj;
    Assert.assertTrue(addressMap.containsKey("street_name"));
    Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
    Assert.assertTrue(addressMap.containsKey("zipcode"));
    Assert.assertEquals("92130", addressMap.get("zipcode"));
    Assert.assertTrue(addressMap.containsKey("city_name"));
    Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
    Assert.assertTrue(valueAsMap.containsKey("emails"));
    emailsObj = valueAsMap.get("emails");
    Assert.assertNotNull(emailsObj);
    Assert.assertTrue(emailsObj instanceof List);
    emailsList = (List<Object>) emailsObj;
    Assert.assertEquals(2, emailsList.size());
    Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
    Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
    accountsObj = valueAsMap.get("accounts");
    Assert.assertNotNull(accountsObj);
    Assert.assertTrue(accountsObj instanceof Map);
    accountsMap = (Map<String, Object>) accountsObj;
    Assert.assertEquals(2, accountsMap.size());
    Assert.assertTrue(accountsMap.containsKey("main"));
    Assert.assertEquals("root", accountsMap.get("main"));
    Assert.assertTrue(accountsMap.containsKey("secondary"));
    Assert.assertEquals("user", accountsMap.get("secondary"));
}
Also used : PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) List(java.util.List) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 52 with AbstractPropertyValue

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

the class PropertyUtil method merge.

/**
 * Merge the map of two node properties recursively.
 * @param source    the source map
 * @param target    the target map
 * @param overrideNull  a boolean value for override the null value of target
 * @param untouched the list of unmodified key //TODO not used and it needs to be reorganised
 * @return  a merged map
 */
public static Map<String, AbstractPropertyValue> merge(Map<String, AbstractPropertyValue> source, Map<String, AbstractPropertyValue> target, boolean overrideNull, Set<String> untouched) {
    if (target == null || target.isEmpty()) {
        return source;
    }
    for (Map.Entry<String, AbstractPropertyValue> entry : safe(source).entrySet()) {
        String sourceKey = entry.getKey();
        AbstractPropertyValue sourceValue = entry.getValue();
        AbstractPropertyValue targetValue = target.get(sourceKey);
        if ((overrideNull && targetValue == null) || !target.containsKey(sourceKey)) {
            target.put(sourceKey, sourceValue);
        } else if (target.containsKey(sourceKey) && targetValue != null) {
            if (sourceValue instanceof ComplexPropertyValue) {
                Map<String, Object> mergedMap = mergeMap(((ComplexPropertyValue) sourceValue).getValue(), ((ComplexPropertyValue) targetValue).getValue(), overrideNull, untouched);
                target.put(sourceKey, new ComplexPropertyValue(mergedMap));
            } else if (sourceValue instanceof Map) {
                Map<String, Object> mergedMap = mergeMap((Map<String, Object>) sourceValue, (Map<String, Object>) targetValue, overrideNull, untouched);
                target.put(sourceKey, (AbstractPropertyValue) mergedMap);
            }
        }
    }
    return target;
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 53 with AbstractPropertyValue

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

the class DanglingRequirementService method addDanglingNode.

private NodeTemplate addDanglingNode(Topology topology, TopologyContext topologyContext, NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition, NodeType danglingNodeType, RelationshipType danglingRelationshipType, CapabilityDefinition targetCapabilityDefinition) {
    NodeTemplate danglingTemplate = TemplateBuilder.buildNodeTemplate(danglingNodeType);
    // Add the filter as defined in the node type.
    danglingTemplate.setNodeFilter(requirementDefinition.getNodeFilter());
    // generate the dangling template name
    String danglingTemplateName = TopologyUtils.getNexAvailableName(nodeTemplate.getName() + "_" + requirementDefinition.getId(), "_", topology.getNodeTemplates().keySet());
    danglingTemplate.setName(danglingTemplateName);
    topology.getNodeTemplates().put(danglingTemplateName, danglingTemplate);
    workflowsBuilderService.addNode(topologyContext, danglingTemplateName);
    // Add the dangling requirement relationship
    if (nodeTemplate.getRelationships() == null) {
        nodeTemplate.setRelationships(Maps.newHashMap());
    }
    String danglingRelationshipTemplateName = TopologyUtils.getNexAvailableName(nodeTemplate.getName() + "_" + requirementDefinition.getId(), "_", nodeTemplate.getRelationships().keySet());
    RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
    relationshipTemplate.setName(danglingRelationshipTemplateName);
    relationshipTemplate.setTarget(danglingTemplateName);
    String targetCapabilityName = targetCapabilityDefinition == null ? null : targetCapabilityDefinition.getId();
    relationshipTemplate.setTargetedCapabilityName(targetCapabilityName);
    relationshipTemplate.setRequirementName(requirementDefinition.getId());
    relationshipTemplate.setRequirementType(requirementDefinition.getType());
    relationshipTemplate.setType(danglingRelationshipType.getElementId());
    relationshipTemplate.setArtifacts(newLinkedHashMap(safe(danglingRelationshipType.getArtifacts())));
    relationshipTemplate.setAttributes(newLinkedHashMap(safe(danglingRelationshipType.getAttributes())));
    Map<String, AbstractPropertyValue> properties = new LinkedHashMap();
    TemplateBuilder.fillProperties(properties, danglingRelationshipType.getProperties(), null);
    relationshipTemplate.setProperties(properties);
    nodeTemplate.getRelationships().put(danglingRelationshipTemplateName, relationshipTemplate);
    workflowsBuilderService.addRelationship(topologyContext, nodeTemplate.getName(), danglingRelationshipTemplateName);
    // TODO remove this workaround as soon as matchin leverages
    setPropertiesFromFilter(danglingTemplate, danglingNodeType);
    return danglingTemplate;
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) LinkedHashMap(java.util.LinkedHashMap) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap)

Example 54 with AbstractPropertyValue

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

the class PropertyValueChecker method checkProperties.

/**
 * Check that the value of a property has the right type and match constraints.
 *
 * @param type The type that defines the properties (NodeType, CapabilityType, RequirementType).
 * @param propertyValues The map of values.
 * @param templateName The name of the node template /capability template / requirement template.
 */
public void checkProperties(final AbstractInheritableToscaType type, final Map<String, AbstractPropertyValue> propertyValues, final String templateName) {
    if (type == null) {
        // if the type is null we cannot check properties against their definition. Error is managed elsewhere.
        return;
    }
    ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
    Topology topology = archiveRoot.getTopology();
    for (Map.Entry<String, AbstractPropertyValue> propertyEntry : safe(propertyValues).entrySet()) {
        String propertyName = propertyEntry.getKey();
        AbstractPropertyValue propertyValue = propertyEntry.getValue();
        Node propertyValueNode = ParsingContextExecution.getObjectToNodeMap().get(propertyValue);
        if (type.getProperties() == null || !type.getProperties().containsKey(propertyName)) {
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.UNRECOGNIZED_PROPERTY, templateName, propertyValueNode.getStartMark(), "Property " + propertyName + " does not exist in type " + type.getElementId(), propertyValueNode.getEndMark(), propertyName));
            continue;
        }
        PropertyDefinition propertyDefinition = type.getProperties().get(propertyName);
        checkProperty(propertyName, propertyValueNode, propertyValue, propertyDefinition, topology.getInputs(), templateName);
    }
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) Node(org.yaml.snakeyaml.nodes.Node) Topology(org.alien4cloud.tosca.model.templates.Topology) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 55 with AbstractPropertyValue

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

the class TemplateBuilder method fillRequirementsMap.

private static void fillRequirementsMap(Map<String, Requirement> map, List<RequirementDefinition> elements, Map<String, Requirement> mapToMerge, boolean adaptToType) {
    if (elements == null) {
        return;
    }
    for (RequirementDefinition requirement : elements) {
        Requirement toAddRequirement = MapUtils.getObject(mapToMerge, requirement.getId());
        // the type of a requirement is a capability type in TOSCA as they match each other.
        CapabilityType requirementType = ToscaContext.get(CapabilityType.class, requirement.getType());
        if (toAddRequirement == null || !Objects.equals(toAddRequirement.getType(), requirement.getType())) {
            toAddRequirement = new Requirement();
            toAddRequirement.setType(requirement.getType());
        }
        Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
        fillProperties(properties, requirementType != null ? requirementType.getProperties() : null, toAddRequirement.getProperties(), adaptToType);
        toAddRequirement.setProperties(properties);
        map.put(requirement.getId(), toAddRequirement);
    }
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)57 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)23 Map (java.util.Map)18 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)17 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)14 Capability (org.alien4cloud.tosca.model.templates.Capability)14 NotFoundException (alien4cloud.exception.NotFoundException)10 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)9 Test (org.junit.Test)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)7 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)7 NodeType (org.alien4cloud.tosca.model.types.NodeType)7 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)6 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)5 List (java.util.List)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)4 HashMap (java.util.HashMap)3