Search in sources :

Example 31 with ScalarPropertyValue

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

the class DefaultNodeMatcherTest method location_resource_should_be_matched_only_if_capabilities_are_satisfied.

@Test
public void location_resource_should_be_matched_only_if_capabilities_are_satisfied() throws Exception {
    // Given
    CapabilityDefinition capabilityArchitecture = new CapabilityDefinition("tosca.capabilities.OperatingSystem");
    computeNodeType.setCapabilities(Arrays.asList(capabilityArchitecture));
    Capability capability = new Capability();
    capability.setType("tosca.capabilities.OperatingSystem");
    capability.setProperties(ImmutableMap.of("architecture", new ScalarPropertyValue("x86")));
    computeNodeTemplate.setCapabilities(ImmutableMap.of("os", capability));
    CapabilityType capabilityType = new CapabilityType();
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("string");
    capabilityType.setElementId("tosca.capabilities.OperatingSystem");
    capabilityType.setProperties(ImmutableMap.of("architecture", propertyDefinition));
    locationResources.setCapabilityTypes(ImmutableMap.of("tosca.capabilities.OperatingSystem", capabilityType));
    // Matching configuration
    Map<String, MatchingConfiguration> capabilityFilterConfiguration = new HashMap<>();
    MatchingConfiguration matchingConfiguration = new MatchingConfiguration();
    MatchingFilterDefinition matchingFilterDefinition = new MatchingConfiguration();
    matchingFilterDefinition.setProperties(ImmutableMap.of("architecture", Arrays.asList(new EqualConstraint())));
    matchingConfiguration.setCapabilities(ImmutableMap.of("os", matchingFilterDefinition));
    capabilityFilterConfiguration.put("org.alien4cloud.nodes.mock.aws.Compute", matchingConfiguration);
    // When
    NodeTemplate wantedNodeTemplate = new NodeTemplate();
    wantedNodeTemplate.setType("tosca.nodes.Compute");
    Capability wantedCapability = new Capability();
    wantedCapability.setType("tosca.capabilities.OperatingSystem");
    wantedCapability.setProperties(ImmutableMap.of("architecture", new ScalarPropertyValue("power_pc")));
    wantedNodeTemplate.setCapabilities(ImmutableMap.of("os", wantedCapability));
    NodeType nodeType = new NodeType();
    List<LocationResourceTemplate> proposition = nodeMatcher.matchNode(wantedNodeTemplate, nodeType, locationResources, capabilityFilterConfiguration);
    // Then
    assertThat(proposition).hasSize(0);
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) LocationResourceTemplate(alien4cloud.model.orchestrators.locations.LocationResourceTemplate) HashMap(java.util.HashMap) MatchingConfiguration(alien4cloud.model.deployment.matching.MatchingConfiguration) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) MatchingFilterDefinition(alien4cloud.model.deployment.matching.MatchingFilterDefinition) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) Test(org.junit.Test)

Example 32 with ScalarPropertyValue

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

the class PostMatchingNodeSetupModifierTest method testPropertiesCleanup.

@Test
public void testPropertiesCleanup() throws ParsingException {
    Mockito.reset(csarRepositorySearchService);
    Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-SNAPSHOT")).thenReturn(Mockito.mock(Csar.class));
    NodeType mockType = Mockito.mock(NodeType.class);
    Mockito.when(mockType.isAbstract()).thenReturn(true);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockType);
    CapabilityType mockCapaType = Mockito.mock(CapabilityType.class);
    Mockito.when(mockCapaType.getElementId()).thenReturn("tosca.capabilities.Root");
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockCapaType);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get("../alien4cloud-test-common/src/test/resources/data/csars/matching-change-cleanup/tosca.yml"));
    for (Entry<String, CapabilityType> capabilityTypeEntry : parsingResult.getResult().getCapabilityTypes().entrySet()) {
        Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(capabilityTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(capabilityTypeEntry.getValue());
    }
    for (Entry<String, NodeType> nodeTypeEntry : parsingResult.getResult().getNodeTypes().entrySet()) {
        Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(nodeTypeEntry.getKey()), Mockito.any(Set.class))).thenReturn(nodeTypeEntry.getValue());
    }
    try {
        ToscaContext.init(Sets.newHashSet());
        PostMatchingNodeSetupModifier postMatchingNodeSetupModifier = new PostMatchingNodeSetupModifier();
        Topology topology = new Topology();
        topology.setNodeTemplates(Maps.newHashMap());
        topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplOne")));
        // Configure the deployment config
        DeploymentMatchingConfiguration matchingConfiguration = new DeploymentMatchingConfiguration();
        matchingConfiguration.setMatchedLocationResources(Maps.newHashMap());
        matchingConfiguration.getMatchedLocationResources().put("my_node", "a_location_resource");
        NodePropsOverride nodePropsOverride = new NodePropsOverride();
        nodePropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
        NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = new NodeCapabilitiesPropsOverride();
        nodeCapabilitiesPropsOverride.getProperties().put("common_property", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("unique_prop", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("type_variant_prop", new ScalarPropertyValue("p_val"));
        nodeCapabilitiesPropsOverride.getProperties().put("constraint_variant_prop", new ScalarPropertyValue("p_val"));
        nodePropsOverride.getCapabilities().put("my_capability", nodeCapabilitiesPropsOverride);
        matchingConfiguration.setMatchedNodesConfiguration(Maps.newHashMap());
        matchingConfiguration.getMatchedNodesConfiguration().put("my_node", nodePropsOverride);
        FlowExecutionContext mockContext = Mockito.mock(FlowExecutionContext.class);
        Mockito.when(mockContext.log()).thenReturn(Mockito.mock(FlowExecutionLog.class));
        Mockito.when(mockContext.getConfiguration(DeploymentMatchingConfiguration.class, AbstractPostMatchingSetupModifier.class.getSimpleName())).thenReturn(Optional.of(matchingConfiguration));
        // All properties resources should be remain as matched type is compliant
        postMatchingNodeSetupModifier.process(topology, mockContext);
        Assert.assertEquals(4, nodePropsOverride.getProperties().size());
        Assert.assertEquals(4, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
        // Change the type and check that properties are cleared
        topology.getNodeTemplates().clear();
        topology.getNodeTemplates().put("my_node", TemplateBuilder.buildNodeTemplate(parsingResult.getResult().getNodeTypes().get("org.alien4cloud.test.matching.nodes.LocationCustomImplTwo")));
        postMatchingNodeSetupModifier.process(topology, mockContext);
        Assert.assertEquals(1, nodePropsOverride.getProperties().size());
        Assert.assertEquals(1, nodePropsOverride.getCapabilities().get("my_capability").getProperties().size());
    } finally {
        ToscaContext.destroy();
    }
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeCapabilitiesPropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodeCapabilitiesPropsOverride) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) DeploymentMatchingConfiguration(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration) FlowExecutionContext(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionContext) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) FlowExecutionLog(org.alien4cloud.alm.deployment.configuration.flow.FlowExecutionLog) Test(org.junit.Test)

Example 33 with ScalarPropertyValue

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

the class ToscaSerializerUtilsTest method testHasCapabilitiesContainingNotNullProperties.

@Test
public void testHasCapabilitiesContainingNotNullProperties() {
    NodeTemplate nt = new NodeTemplate();
    Assert.assertFalse(utils.hasCapabilitiesContainingNotNullProperties(nt));
    Map<String, Capability> capabilities = Maps.newHashMap();
    nt.setCapabilities(capabilities);
    Assert.assertFalse(utils.hasCapabilitiesContainingNotNullProperties(nt));
    Capability capability1 = new Capability();
    capabilities.put("capa1", capability1);
    Assert.assertFalse(utils.hasCapabilitiesContainingNotNullProperties(nt));
    Capability capability2 = new Capability();
    Map<String, AbstractPropertyValue> properties = new HashMap<String, AbstractPropertyValue>();
    capability2.setProperties(properties);
    capabilities.put("capa2", capability2);
    Assert.assertFalse(utils.hasCapabilitiesContainingNotNullProperties(nt));
    properties.put("prop1", null);
    Assert.assertFalse(utils.hasCapabilitiesContainingNotNullProperties(nt));
    properties.put("prop2", new ScalarPropertyValue("value"));
    Assert.assertTrue(utils.hasCapabilitiesContainingNotNullProperties(nt));
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) HashMap(java.util.HashMap) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 34 with ScalarPropertyValue

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

the class ToscaPropertyDefaultValueTypeValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    PropertyValue defaultValue = value.getDefault();
    if (defaultValue == null) {
        // no default value is specified.
        return true;
    }
    if (!(defaultValue instanceof ScalarPropertyValue)) {
        // No constraint can be made on other thing than scalar values
        return false;
    }
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    try {
        toscaType.parse(((ScalarPropertyValue) defaultValue).getValue());
    } catch (InvalidPropertyValueException e) {
        return false;
    }
    return true;
}
Also used : InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Example 35 with ScalarPropertyValue

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

the class ToscaParserSimpleProfileAlien130Test method testNodeType.

@SuppressWarnings("unchecked")
@Test
public void testNodeType() throws FileNotFoundException, ParsingException {
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
    // check node type.
    Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
    Assert.assertEquals("my_company.my_types.MyAppNodeType", entry.getKey());
    NodeType nodeType = entry.getValue();
    Assert.assertEquals(Lists.newArrayList("tosca.nodes.SoftwareComponent", "tosca.nodes.Root"), nodeType.getDerivedFrom());
    Assert.assertEquals("My company’s custom applicaton", nodeType.getDescription());
    // validate properties parsing
    Assert.assertEquals(4, nodeType.getProperties().size());
    PropertyDefinition def1 = new PropertyDefinition();
    def1.setType("string");
    def1.setDefault(new ScalarPropertyValue("default"));
    def1.setDescription("application password");
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(new MinLengthConstraint(6));
    constraints.add(new MaxLengthConstraint(10));
    def1.setConstraints(constraints);
    PropertyDefinition def2 = new PropertyDefinition();
    def2.setType("integer");
    def2.setDescription("application port number");
    PropertyDefinition def3 = new PropertyDefinition();
    def3.setType("scalar-unit.size");
    def3.setDefault(new ScalarPropertyValue("1 GB"));
    LessThanConstraint ltConstraint = new LessThanConstraint();
    ltConstraint.setLessThan("1 TB");
    constraints = Lists.<PropertyConstraint>newArrayList(ltConstraint);
    def3.setConstraints(constraints);
    PropertyDefinition def4 = new PropertyDefinition();
    def4.setType("scalar-unit.time");
    def4.setDefault(new ScalarPropertyValue("1 d"));
    GreaterThanConstraint gtConstraint = new GreaterThanConstraint();
    gtConstraint.setGreaterThan("1 h");
    constraints = Lists.<PropertyConstraint>newArrayList(gtConstraint);
    def4.setConstraints(constraints);
    Assert.assertEquals(MapUtil.newHashMap(new String[] { "my_app_password", "my_app_duration", "my_app_size", "my_app_port" }, new PropertyDefinition[] { def1, def4, def3, def2 }), nodeType.getProperties());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) Test(org.junit.Test)

Aggregations

ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)15 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)14 Test (org.junit.Test)13 Map (java.util.Map)10 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)7 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)7 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Capability (org.alien4cloud.tosca.model.templates.Capability)4