Search in sources :

Example 6 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class ToscaSerializerTest method simpleTest.

@Ignore
@Test
public void simpleTest() throws IOException, URISyntaxException {
    Topology topology = new Topology();
    topology.setDependencies(new HashSet<CSARDependency>());
    topology.getDependencies().add(new CSARDependency("name1", "1.0"));
    topology.getDependencies().add(new CSARDependency("name2", "2.0"));
    topology.setInputs(new HashMap<String, PropertyDefinition>());
    PropertyDefinition pd1 = new PropertyDefinition();
    pd1.setType("string");
    pd1.setConstraints(getConstraintList());
    pd1.setDescription("A description");
    topology.getInputs().put("input1", pd1);
    PropertyDefinition pd2 = new PropertyDefinition();
    pd2.setType("integer");
    pd2.setRequired(false);
    pd2.setDefault(new ScalarPropertyValue("10"));
    topology.getInputs().put("input2", pd2);
    PropertyDefinition pd3 = new PropertyDefinition();
    pd3.setType("map");
    pd3.setRequired(false);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType("integer");
    pd3.setEntrySchema(entrySchema);
    topology.getInputs().put("input3", pd3);
    topology.setNodeTemplates(new HashMap<String, NodeTemplate>());
    topology.getNodeTemplates().put("node1", new NodeTemplate());
    topology.getNodeTemplates().get("node1").setType("the.node.Type");
    topology.getNodeTemplates().get("node1").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setRelationships(new HashMap<String, RelationshipTemplate>());
    topology.getNodeTemplates().get("node1").getRelationships().put("hostedOn", new RelationshipTemplate());
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setTarget("compute2");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementType("capabilities.Capa");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementName("host");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setType("relationship.Rel");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setCapabilities(new HashMap<String, Capability>());
    Capability capability = new Capability();
    capability.setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa1", capability);
    // this capability should not appear
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa2", new Capability());
    topology.getNodeTemplates().get("node1").setArtifacts(new HashMap<String, DeploymentArtifact>());
    DeploymentArtifact da = new DeploymentArtifact();
    da.setArtifactName("artifact.war");
    da.setArtifactRef("010203904872876723");
    da.setArtifactType("artifacttypes.Artifact");
    topology.getNodeTemplates().get("node1").getArtifacts().put("artifact1", da);
    topology.setOutputProperties(new HashMap<String, Set<String>>());
    topology.getOutputProperties().put("node1", Sets.newHashSet("prop1", "prop2"));
    topology.setOutputAttributes(new HashMap<String, Set<String>>());
    topology.getOutputAttributes().put("node1", Sets.newHashSet("att1", "att2"));
    Map<String, Object> velocityCtx = new HashMap<String, Object>();
    velocityCtx.put("topology", topology);
    velocityCtx.put("template_name", "template-id");
    velocityCtx.put("template_version", "1.0.0-SNAPSHOT");
    velocityCtx.put("template_author", "Foo Bar");
    velocityCtx.put("application_description", "Here is a \nmultiline description");
    StringWriter writer = new StringWriter();
    VelocityUtil.generate("org/alien4cloud/tosca/exporter/topology-alien_dsl_1_4_0.yml.vm", writer, velocityCtx);
    System.out.println(writer.toString());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) HashMap(java.util.HashMap) Topology(org.alien4cloud.tosca.model.templates.Topology) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) StringWriter(java.io.StringWriter) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class LocationResourceService method setTemplateCapabilityProperty.

private void setTemplateCapabilityProperty(LocationResourceTemplate resourceTemplate, String capabilityName, String propertyName, Object propertyValue) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    Location location = locationService.getOrFail(resourceTemplate.getLocationId());
    NodeType resourceType = csarRepoSearchService.getRequiredElementInDependencies(NodeType.class, resourceTemplate.getTemplate().getType(), location.getDependencies());
    Capability capability = getOrFailCapability(resourceTemplate.getTemplate(), capabilityName);
    CapabilityDefinition capabilityDefinition = getOrFailCapabilityDefinition(resourceType, capabilityName);
    CapabilityType capabilityType = csarRepoSearchService.getRequiredElementInDependencies(CapabilityType.class, capabilityDefinition.getType(), location.getDependencies());
    PropertyDefinition propertyDefinition = getOrFailCapabilityPropertyDefinition(capabilityType, propertyName);
    propertyService.setCapabilityPropertyValue(location.getDependencies(), capability, propertyDefinition, propertyName, propertyValue);
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) NodeType(org.alien4cloud.tosca.model.types.NodeType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Location(alien4cloud.model.orchestrators.locations.Location)

Example 8 with Capability

use of org.alien4cloud.tosca.model.templates.Capability in project alien4cloud by alien4cloud.

the class TopologyTreeBuilderService method buildPaaSNodeTemplates.

/**
 * Fetch information from the repository to complete the topology node template informations with additional data such as artifacts paths etc.
 *
 * @param topology The topology for which to build PaaSNodeTemplate map.
 * @return A map of PaaSNodeTemplate that match the one of the NodeTempaltes in the given topology (and filled with artifact paths etc.).
 */
public Map<String, PaaSNodeTemplate> buildPaaSNodeTemplates(Topology topology) {
    Map<String, PaaSNodeTemplate> nodeTemplates = Maps.newHashMap();
    // Fill in PaaSNodeTemplate by fetching node types and CSAR path from the repositories.
    if (topology.getNodeTemplates() != null) {
        for (Entry<String, NodeTemplate> templateEntry : topology.getNodeTemplates().entrySet()) {
            NodeTemplate template = templateEntry.getValue();
            PaaSNodeTemplate paaSNodeTemplate = new PaaSNodeTemplate(templateEntry.getKey(), template);
            fillType(topology, template, paaSNodeTemplate, NodeType.class);
            mergeInterfaces(paaSNodeTemplate, template);
            if (template.getRelationships() != null) {
                for (Map.Entry<String, RelationshipTemplate> relationshipEntry : template.getRelationships().entrySet()) {
                    RelationshipTemplate relationshipTemplate = relationshipEntry.getValue();
                    PaaSRelationshipTemplate paaSRelationshipTemplate = new PaaSRelationshipTemplate(relationshipEntry.getKey(), relationshipTemplate, paaSNodeTemplate.getId());
                    fillType(topology, relationshipTemplate, paaSRelationshipTemplate, RelationshipType.class);
                    mergeInterfaces(paaSRelationshipTemplate, relationshipTemplate);
                    paaSNodeTemplate.getRelationshipTemplates().add(paaSRelationshipTemplate);
                }
            }
            Capability scalableCapability = TopologyUtils.getScalableCapability(topology, templateEntry.getKey(), false);
            if (scalableCapability != null) {
                ScalingPolicy scalingPolicy = TopologyUtils.getScalingPolicy(scalableCapability);
                // A node with a scaling policy 1, 1, 1 is a simple node and so do not set scaling policy
                if (!ScalingPolicy.NOT_SCALABLE_POLICY.equals(scalingPolicy)) {
                    paaSNodeTemplate.setScalingPolicy(scalingPolicy);
                }
            }
            if (topology.getGroups() != null) {
                Set<String> nodeGroups = Sets.newHashSet();
                for (Map.Entry<String, NodeGroup> groupEntry : topology.getGroups().entrySet()) {
                    if (groupEntry.getValue().getMembers() != null && groupEntry.getValue().getMembers().contains(templateEntry.getKey())) {
                        nodeGroups.add(groupEntry.getKey());
                    }
                }
                paaSNodeTemplate.setGroups(nodeGroups);
            }
            nodeTemplates.put(templateEntry.getKey(), paaSNodeTemplate);
        }
    }
    return nodeTemplates;
}
Also used : ScalingPolicy(org.alien4cloud.tosca.model.templates.ScalingPolicy) Capability(org.alien4cloud.tosca.model.templates.Capability) PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) PaaSNodeTemplate(alien4cloud.paas.model.PaaSNodeTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) PaaSRelationshipTemplate(alien4cloud.paas.model.PaaSRelationshipTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) PaaSRelationshipTemplate(alien4cloud.paas.model.PaaSRelationshipTemplate) Map(java.util.Map) HashMap(java.util.HashMap) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 9 with Capability

use of org.alien4cloud.tosca.model.templates.Capability 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 10 with Capability

use of org.alien4cloud.tosca.model.templates.Capability 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

Capability (org.alien4cloud.tosca.model.templates.Capability)50 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)27 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)26 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)14 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)12 Map (java.util.Map)10 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)10 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeType (org.alien4cloud.tosca.model.types.NodeType)9 Test (org.junit.Test)9 Set (java.util.Set)8 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)5 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)5 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)5 HashMap (java.util.HashMap)4 List (java.util.List)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 Location (alien4cloud.model.orchestrators.locations.Location)3 PaaSNodeTemplate (alien4cloud.paas.model.PaaSNodeTemplate)3