Search in sources :

Example 11 with AbstractPropertyValue

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

the class FunctionEvaluator method extractRuntimeInformationProperty.

/**
 * Extract property value from runtime informations
 *
 * @param topology
 * @param propertyOrAttributeName
 * @param nodes
 * @return
 */
private static String extractRuntimeInformationProperty(Topology topology, String propertyOrAttributeName, List<? extends IPaaSTemplate> nodes) {
    AbstractPropertyValue propertyOrAttributeValue;
    NodeTemplate template = null;
    for (IPaaSTemplate node : nodes) {
        String nodeName = node.getId();
        template = topology.getNodeTemplates().get(nodeName);
        if (template != null && template.getProperties() != null) {
            propertyOrAttributeValue = template.getProperties().get(propertyOrAttributeName);
            if (propertyOrAttributeValue != null) {
                return PropertyUtil.getScalarValue(propertyOrAttributeValue);
            }
        }
    }
    log.warn("Couldn't find property [ {} ] of node [ {} ]", propertyOrAttributeName, nodes);
    return "[" + nodes + "." + propertyOrAttributeName + "=Error!]";
}
Also used : PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) IPaaSTemplate(alien4cloud.paas.IPaaSTemplate) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 12 with AbstractPropertyValue

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

the class FunctionEvaluator method getPropertyValue.

private static AbstractPropertyValue getPropertyValue(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertyDefinitions, String propertyAccessPath) {
    if (properties == null || !properties.containsKey(propertyAccessPath)) {
        String propertyName = PropertyUtil.getPropertyNameFromComplexPath(propertyAccessPath);
        if (propertyName == null) {
            // Non complex
            return PropertyUtil.getDefaultFromPropertyDefinitions(propertyAccessPath, propertyDefinitions);
        } else {
            // Complex
            PropertyDefinition propertyDefinition = propertyDefinitions.get(propertyName);
            AbstractPropertyValue rawValue;
            if (propertyDefinition == null) {
                return null;
            } else if (ToscaTypes.isSimple(propertyDefinition.getType())) {
                // It's a complex path (with '.') but the type in definition is finally simple
                return null;
            } else if (properties != null && (rawValue = properties.get(propertyName)) != null) {
                if (!(rawValue instanceof PropertyValue)) {
                    throw new NotSupportedException("Only support static value in a get_property");
                }
                Object value = MapUtil.get(((PropertyValue) rawValue).getValue(), propertyAccessPath.substring(propertyName.length() + 1));
                return new ScalarPropertyValue(PropertyUtil.serializePropertyValue(value));
            } else {
                return null;
            }
        }
    } else {
        return properties.get(propertyAccessPath);
    }
}
Also used : 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) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) NotSupportedException(alien4cloud.paas.exception.NotSupportedException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 13 with AbstractPropertyValue

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

the class ToscaParserAlien140Test method testCapabilitiesComplexProperty.

@Test
public void testCapabilitiesComplexProperty() throws ParsingException {
    Mockito.reset(csarRepositorySearchService);
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN14");
    Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    DataType mockedDataType = new DataType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq("tosca.datatypes.Root"), Mockito.any(Set.class))).thenReturn(mockedDataType);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "capa_complex_props.yml"));
    Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
    ArchiveRoot archiveRoot = parsingResult.getResult();
    // check the capabilityType
    // ////////////
    CapabilityType capaType = archiveRoot.getCapabilityTypes().values().stream().findFirst().get();
    assertNotNull(capaType.getProperties());
    Assert.assertEquals(3, capaType.getProperties().size());
    // map property
    String map = "map";
    PropertyDefinition propertyDefinition = capaType.getProperties().get(map);
    assertNotNull(propertyDefinition.getDefault());
    assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
    Map<String, Object> propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
    assertNotNull(propertyMapValue);
    Assert.assertEquals(2, propertyMapValue.size());
    Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
    Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
    // custom property
    String custom = "custom";
    propertyDefinition = capaType.getProperties().get(custom);
    assertEquals("alien.test.datatypes.Custom", propertyDefinition.getType());
    assertNull(propertyDefinition.getDefault());
    // custom_with_default property
    String custom_with_default = "custom_with_default";
    propertyDefinition = capaType.getProperties().get(custom_with_default);
    assertNotNull(propertyDefinition.getDefault());
    assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("defaultName", propertyMapValue.get("name"));
    Object list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
    // check the node template capability
    // ////////////
    NodeTemplate nodeTemplate = archiveRoot.getTopology().getNodeTemplates().values().stream().findFirst().get();
    Capability capability = nodeTemplate.getCapabilities().values().stream().findFirst().get();
    assertNotNull(capability);
    Assert.assertEquals(3, capability.getProperties().size());
    // map property
    AbstractPropertyValue propertyValue = capability.getProperties().get(map);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    Assert.assertEquals(2, propertyMapValue.size());
    Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
    Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
    // custom property
    propertyValue = capability.getProperties().get(custom);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("manual", propertyMapValue.get("name"));
    list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("manual_alien", "manual_fastconnect")));
    // custom_with_default property
    propertyValue = capability.getProperties().get(custom_with_default);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("defaultName", propertyMapValue.get("name"));
    list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) 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) DataType(org.alien4cloud.tosca.model.types.DataType) List(java.util.List) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 14 with AbstractPropertyValue

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

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

the class PropertyValueChecker method checkProperty.

public void checkProperty(String propertyName, Node propertyValueNode, AbstractPropertyValue propertyValue, PropertyDefinition propertyDefinition, Map<String, PropertyDefinition> inputs, String templateName) {
    if (propertyValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) propertyValue;
        String parameters = function.getParameters().get(0);
        // check get_input only
        if (function.getFunction().equals("get_input")) {
            if (inputs == null || !inputs.keySet().contains(parameters)) {
                ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.MISSING_TOPOLOGY_INPUT, templateName, propertyValueNode.getStartMark(), parameters, propertyValueNode.getEndMark(), propertyName));
            }
        }
    } else if (propertyValue instanceof PropertyValue<?>) {
        checkProperty(propertyName, propertyValueNode, (PropertyValue<?>) propertyValue, propertyDefinition, templateName);
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)

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