Search in sources :

Example 21 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testRootCategories.

@Test
public void testRootCategories() throws FileNotFoundException, ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-root-categories.yml"));
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals("Tosca default namespace value", archiveRoot.getArchive().getToscaDefaultNamespace());
    Assert.assertEquals("Template name value", archiveRoot.getArchive().getName());
    Assert.assertEquals("Temlate author value", archiveRoot.getArchive().getTemplateAuthor());
    Assert.assertEquals("1.0.0-SNAPSHOT", archiveRoot.getArchive().getVersion());
    Assert.assertEquals("This is an example of a single line description (no folding).", archiveRoot.getArchive().getDescription());
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) Test(org.junit.Test)

Example 22 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testParseTopologyWithWorkflows.

@Test
public void testParseTopologyWithWorkflows() throws FileNotFoundException, ParsingException {
    Csar csar = new Csar("tosca-normative-types", "1.0.0.wd03-SNAPSHOT");
    NodeType mockedCompute = getMockedCompute();
    NodeType mockedSoftware = getMockedSoftwareComponent();
    CapabilityType mockedContainer = Mockito.mock(CapabilityType.class);
    Mockito.when(mockedContainer.getElementId()).thenReturn("tosca.capabilities.Container");
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedSoftware);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Container"), Mockito.any(Set.class))).thenReturn(mockedContainer);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedCompute);
    RelationshipType hostedOn = Mockito.mock(RelationshipType.class);
    Mockito.when(hostedOn.getElementId()).thenReturn("tosca.relationships.HostedOn");
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-topology-template-workflows.yml"));
    Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
    assertNoBlocker(parsingResult);
    Assert.assertEquals(3, parsingResult.getContext().getParsingErrors().size());
    assertNotNull(parsingResult.getResult().getTopology());
    Assert.assertEquals(5, parsingResult.getResult().getTopology().getWorkflows().size());
    // check invalid names were renamed
    assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalidName_"));
    assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalid_Name"));
    assertTrue(parsingResult.getResult().getTopology().getWorkflows().containsKey("invalid_Name_1"));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) Test(org.junit.Test)

Example 23 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testDescriptionSingleLine.

@Test
public void testDescriptionSingleLine() throws FileNotFoundException, ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "description-single-line.yml"));
    Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    assertNotNull(archiveRoot.getArchive().getDescription());
    Assert.assertEquals("This is an example of a single line description (no folding).", archiveRoot.getArchive().getDescription());
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) Test(org.junit.Test)

Example 24 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien130Test method testAttributesConcatValid.

@SuppressWarnings("unchecked")
@Test
public void testAttributesConcatValid() throws Throwable {
    Csar csar = new Csar("tosca-normative-types", "1.0.0-SNAPSHOT-wd03");
    // Mockito.when(csarRepositorySearchService.getArchive(csar.getId())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getId()).thenReturn("tosca.nodes.Compute:1.0.0-SNAPSHOT-wd03");
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type-inputs.yml"));
    Mockito.verify(csarRepositorySearchService).getArchive(csar.getName(), csar.getVersion());
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    // check nodetype elements
    Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
    Assert.assertEquals("alien.test.TestComputeConcat", entry.getKey());
    NodeType nodeType = entry.getValue();
    nodeType.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID);
    Map<String, IValue> attributes = nodeType.getAttributes();
    IValue simpleDefinition = attributes.get("simple_definition");
    IValue ipAddressDefinition = attributes.get("ip_address");
    IValue simpleConcat = attributes.get("simple_concat");
    IValue complexConcat = attributes.get("complex_concat");
    // check attributes types
    assertTrue(simpleDefinition.getClass().equals(AttributeDefinition.class));
    assertTrue(ipAddressDefinition.getClass().equals(AttributeDefinition.class));
    assertTrue(simpleConcat.getClass().equals(ConcatPropertyValue.class));
    assertTrue(complexConcat.getClass().equals(ConcatPropertyValue.class));
    // Test nodeType serialization
    String nodeTypeJson = JsonUtil.toString(nodeType);
    // recover node from serialized string
    NodeType nodeTypeDeserialized = JsonUtil.readObject(nodeTypeJson, NodeType.class);
    assertNotNull(nodeTypeDeserialized);
    attributes = nodeTypeDeserialized.getAttributes();
    simpleDefinition = attributes.get("simple_definition");
    ipAddressDefinition = attributes.get("ip_address");
    simpleConcat = attributes.get("simple_concat");
    complexConcat = attributes.get("complex_concat");
    // check attributes types after deserialization
    assertTrue(simpleDefinition.getClass().equals(AttributeDefinition.class));
    assertTrue(ipAddressDefinition.getClass().equals(AttributeDefinition.class));
    assertTrue(simpleConcat.getClass().equals(ConcatPropertyValue.class));
    assertTrue(complexConcat.getClass().equals(ConcatPropertyValue.class));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) IValue(org.alien4cloud.tosca.model.definitions.IValue) NodeType(org.alien4cloud.tosca.model.types.NodeType) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) Test(org.junit.Test)

Example 25 with ArchiveRoot

use of alien4cloud.tosca.model.ArchiveRoot in project alien4cloud by alien4cloud.

the class RelationshipPostProcessor method process.

public void process(NodeType nodeTemplateType, Map.Entry<String, RelationshipTemplate> instance) {
    RelationshipTemplate relationshipTemplate = instance.getValue();
    if (relationshipTemplate.getTarget() == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(instance);
        // the node template name is required
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NODE_TEMPLATE_NAME_REQUIRED, null, node.getStartMark(), null, node.getEndMark(), null));
    }
    RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    propertyValueChecker.checkProperties(relationshipType, relationshipTemplate.getProperties(), instance.getKey());
    RequirementDefinition rd = getRequirementDefinitionByName(nodeTemplateType, relationshipTemplate.getRequirementName());
    if (rd == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getRequirementName());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    if (relationshipTemplate.getType() == null) {
        // if the relationship type has not been defined on the requirement assignment it may be defined on the requirement definition.
        relationshipTemplate.setType(rd.getRelationshipType());
    }
    referencePostProcessor.process(new ReferencePostProcessor.TypeReference(relationshipTemplate, relationshipTemplate.getType(), RelationshipType.class));
    relationshipTemplate.setRequirementType(rd.getType());
    ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
    // now find the target of the relation
    NodeTemplate targetNodeTemplate = archiveRoot.getTopology().getNodeTemplates().get(relationshipTemplate.getTarget());
    if (targetNodeTemplate == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate.getTarget());
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_TARGET_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getTarget()));
        return;
    }
    // alien actually supports a capability type in the TOSCA yaml
    String capabilityStr = relationshipTemplate.getTargetedCapabilityName();
    Capability capability = null;
    if (capabilityStr == null) {
        // the capability type is not known, we assume that we are parsing a Short notation (node only)
        if (targetNodeTemplate.getCapabilities() != null) {
            // let's try to find all match for a given type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, relationshipTemplate.getRequirementType());
            if (capability == null) {
                capability = targetNodeTemplate.getCapabilities().get(relationshipTemplate.getRequirementName());
                if (capability != null) {
                    relationshipTemplate.setTargetedCapabilityName(rd.getId());
                }
            }
        }
    } else {
        // Let's try to find if the target node has a capability as named in the capability string of the relationship (requirement assignment)
        if (targetNodeTemplate.getCapabilities() != null) {
            capability = targetNodeTemplate.getCapabilities().get(capabilityStr);
        }
        if (capability == null) {
            // The capabilityStr may be the name of a type
            capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, capabilityStr);
        }
    }
    if (capability == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(relationshipTemplate);
        // we should fail
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.REQUIREMENT_CAPABILITY_NOT_FOUND, null, node.getStartMark(), null, node.getEndMark(), relationshipTemplate.getRequirementName()));
        return;
    }
    RelationshipType indexedRelationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
    if (indexedRelationshipType == null) {
        // Error managed by the reference post processor.
        return;
    }
    Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
    TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), relationshipTemplate.getProperties(), false);
    relationshipTemplate.setProperties(properties);
    relationshipTemplate.setAttributes(indexedRelationshipType.getAttributes());
    // FIXME we should check that the artifact is defined at the type level.
    safe(instance.getValue().getArtifacts()).values().forEach(templateDeploymentArtifactPostProcessor);
    Map<String, DeploymentArtifact> mergedArtifacts = instance.getValue().getArtifacts();
    if (mergedArtifacts == null) {
        mergedArtifacts = new HashMap<>();
    }
    mergedArtifacts.putAll(safe(indexedRelationshipType.getArtifacts()));
    relationshipTemplate.setArtifacts(mergedArtifacts);
    // TODO Manage interfaces inputs to copy them to all operations.
    for (Interface anInterface : safe(instance.getValue().getInterfaces()).values()) {
        safe(anInterface.getOperations()).values().stream().map(Operation::getImplementationArtifact).filter(Objects::nonNull).forEach(implementationArtifactPostProcessor);
    }
}
Also used : Capability(org.alien4cloud.tosca.model.templates.Capability) Node(org.yaml.snakeyaml.nodes.Node) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) ParsingError(alien4cloud.tosca.parser.ParsingError) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate)

Aggregations

ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)78 Test (org.junit.Test)45 Csar (org.alien4cloud.tosca.model.Csar)27 Set (java.util.Set)26 NodeType (org.alien4cloud.tosca.model.types.NodeType)26 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)16 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)14 ParsingError (alien4cloud.tosca.parser.ParsingError)13 Path (java.nio.file.Path)11 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 Node (org.yaml.snakeyaml.nodes.Node)8 PluginArchive (alien4cloud.orchestrators.plugin.model.PluginArchive)6 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)6 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)6 ParsingException (alien4cloud.tosca.parser.ParsingException)5 Map (java.util.Map)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4