Search in sources :

Example 6 with RequirementDefinition

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

the class NodeFilterValidationService method validateFiltersForNode.

private void validateFiltersForNode(NodeType sourceNodeType, Map<String, RelationshipTemplate> relationshipsMap, Topology topology, Map<String, NodeType> nodeTypes, Map<String, CapabilityType> capabilityTypes, NodeFiltersTask task, boolean skipInputs) {
    Map<String, RequirementDefinition> requirementDefinitionMap = getRequirementsAsMap(sourceNodeType);
    for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipsMap.entrySet()) {
        RequirementDefinition requirementDefinition = requirementDefinitionMap.get(relationshipEntry.getValue().getRequirementName());
        NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
        if (nodeFilter != null) {
            NodeTemplate targetNode = topology.getNodeTemplates().get(relationshipEntry.getValue().getTarget());
            NodeType targetType = nodeTypes.get(relationshipEntry.getValue().getTarget());
            NodeFilterToSatisfy nodeFilterToSatisfy = new NodeFilterToSatisfy();
            nodeFilterToSatisfy.setRelationshipName(relationshipEntry.getKey());
            nodeFilterToSatisfy.setTargetName(targetNode.getName());
            nodeFilterToSatisfy.setMissingCapabilities(Lists.<String>newArrayList());
            nodeFilterToSatisfy.setViolations(Lists.<Violations>newArrayList());
            validateNodeFilter(nodeFilter, targetNode, targetType, capabilityTypes, nodeFilterToSatisfy, skipInputs);
            if (!nodeFilterToSatisfy.getViolations().isEmpty() || !nodeFilterToSatisfy.getMissingCapabilities().isEmpty()) {
                task.getNodeFiltersToSatisfy().add(nodeFilterToSatisfy);
            }
        }
    }
}
Also used : RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) Map(java.util.Map) NodeFilterToSatisfy(alien4cloud.topology.task.NodeFilterToSatisfy) NodeFilter(org.alien4cloud.tosca.model.definitions.NodeFilter)

Example 7 with RequirementDefinition

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

the class TopologyRequirementBoundsValidationServices method validateRequirementsLowerBounds.

/**
 * Perform validation of requirements bounds/occurences for the given topology.
 *
 * @param topology The topology to check
 * @return A list of validation errors (tasks to be done to make the topology compliant).
 */
public List<RequirementsTask> validateRequirementsLowerBounds(Topology topology) {
    List<RequirementsTask> toReturnTaskList = Lists.newArrayList();
    Map<String, NodeTemplate> nodeTemplates = topology.getNodeTemplates();
    for (Map.Entry<String, NodeTemplate> nodeTempEntry : nodeTemplates.entrySet()) {
        NodeTemplate nodeTemp = nodeTempEntry.getValue();
        if (nodeTemp.getRequirements() == null) {
            continue;
        }
        NodeType relatedIndexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemp.getType(), topology.getDependencies());
        // do pass if abstract node
        if (relatedIndexedNodeType.isAbstract()) {
            continue;
        }
        RequirementsTask task = new RequirementsTask();
        task.setNodeTemplateName(nodeTempEntry.getKey());
        task.setCode(TaskCode.SATISFY_LOWER_BOUND);
        task.setComponent(relatedIndexedNodeType);
        task.setRequirementsToImplement(Lists.<RequirementToSatisfy>newArrayList());
        if (CollectionUtils.isNotEmpty(relatedIndexedNodeType.getRequirements())) {
            for (RequirementDefinition reqDef : relatedIndexedNodeType.getRequirements()) {
                int count = countRelationshipsForRequirement(nodeTemp, reqDef);
                if (count < reqDef.getLowerBound()) {
                    task.getRequirementsToImplement().add(new RequirementToSatisfy(reqDef.getId(), reqDef.getType(), reqDef.getLowerBound() - count));
                }
            }
            if (CollectionUtils.isNotEmpty(task.getRequirementsToImplement())) {
                toReturnTaskList.add(task);
            }
        }
    }
    return toReturnTaskList.isEmpty() ? null : toReturnTaskList;
}
Also used : RequirementsTask(alien4cloud.topology.task.RequirementsTask) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) RequirementToSatisfy(alien4cloud.topology.task.RequirementToSatisfy) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) Map(java.util.Map)

Example 8 with RequirementDefinition

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

the class SuggestionService method postProcessSuggestionFromArchive.

public void postProcessSuggestionFromArchive(ParsingResult<ArchiveRoot> parsingResult) {
    ArchiveRoot archiveRoot = parsingResult.getResult();
    ParsingContext context = parsingResult.getContext();
    if (archiveRoot.hasToscaTopologyTemplate()) {
        Topology topology = archiveRoot.getTopology();
        Map<String, NodeTemplate> nodeTemplateMap = topology.getNodeTemplates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplateMap.entrySet()) {
            NodeTemplate nodeTemplate = nodeTemplateEntry.getValue();
            String nodeName = nodeTemplateEntry.getKey();
            if (MapUtils.isNotEmpty(nodeTemplate.getProperties())) {
                checkProperties(nodeName, nodeTemplate.getProperties(), NodeType.class, nodeTemplate.getType(), context);
            }
            Map<String, Capability> capabilityMap = nodeTemplate.getCapabilities();
            if (MapUtils.isNotEmpty(capabilityMap)) {
                for (Map.Entry<String, Capability> capabilityEntry : capabilityMap.entrySet()) {
                    String capabilityName = capabilityEntry.getKey();
                    Capability capability = capabilityEntry.getValue();
                    if (MapUtils.isNotEmpty(capability.getProperties())) {
                        checkProperties(nodeName + ".capabilities." + capabilityName, capability.getProperties(), CapabilityType.class, capability.getType(), context);
                    }
                }
            }
            Map<String, RelationshipTemplate> relationshipTemplateMap = nodeTemplate.getRelationships();
            if (MapUtils.isNotEmpty(relationshipTemplateMap)) {
                for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipTemplateMap.entrySet()) {
                    String relationshipName = relationshipEntry.getKey();
                    RelationshipTemplate relationship = relationshipEntry.getValue();
                    if (MapUtils.isNotEmpty(relationship.getProperties())) {
                        checkProperties(nodeName + ".relationships." + relationshipName, relationship.getProperties(), RelationshipType.class, relationship.getType(), context);
                    }
                }
            }
        }
    }
    if (archiveRoot.hasToscaTypes()) {
        Map<String, NodeType> allNodeTypes = archiveRoot.getNodeTypes();
        if (MapUtils.isNotEmpty(allNodeTypes)) {
            for (Map.Entry<String, NodeType> nodeTypeEntry : allNodeTypes.entrySet()) {
                NodeType nodeType = nodeTypeEntry.getValue();
                if (nodeType.getRequirements() != null && !nodeType.getRequirements().isEmpty()) {
                    for (RequirementDefinition requirementDefinition : nodeType.getRequirements()) {
                        NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
                        if (nodeFilter != null) {
                            Map<String, FilterDefinition> capabilitiesFilters = nodeFilter.getCapabilities();
                            if (MapUtils.isNotEmpty(capabilitiesFilters)) {
                                for (Map.Entry<String, FilterDefinition> capabilityFilterEntry : capabilitiesFilters.entrySet()) {
                                    FilterDefinition filterDefinition = capabilityFilterEntry.getValue();
                                    for (Map.Entry<String, List<PropertyConstraint>> constraintEntry : filterDefinition.getProperties().entrySet()) {
                                        List<PropertyConstraint> constraints = constraintEntry.getValue();
                                        checkPropertyConstraints("node_filter.capabilities", CapabilityType.class, capabilityFilterEntry.getKey(), constraintEntry.getKey(), constraints, context);
                                    }
                                }
                            }
                        // FIXME check also the value properties filter of a node filter
                        }
                    }
                }
            }
        }
    }
}
Also used : RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) FilterDefinition(org.alien4cloud.tosca.model.definitions.FilterDefinition) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) List(java.util.List) ArrayList(java.util.ArrayList) ParsingContext(alien4cloud.tosca.parser.ParsingContext) Capability(org.alien4cloud.tosca.model.templates.Capability) Topology(org.alien4cloud.tosca.model.templates.Topology) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) Map(java.util.Map) NodeFilter(org.alien4cloud.tosca.model.definitions.NodeFilter)

Example 9 with RequirementDefinition

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

the class SearchDefinitionSteps method createAndIndexComponent.

private void createAndIndexComponent(int count, String type, String baseName, int countHavingProperty, String property, String propertyValue) throws Exception {
    testDataList.clear();
    Class<?> clazz = QUERY_TYPES.get(type).getIndexedToscaElementClass();
    String typeName = MappingBuilder.indexTypeFromClass(clazz);
    int remaining = countHavingProperty;
    baseName = baseName == null || baseName.isEmpty() ? typeName : baseName;
    for (int i = 0; i < count; i++) {
        AbstractToscaType componentTemplate = (AbstractToscaType) clazz.newInstance();
        String elementId = baseName + "_" + i;
        componentTemplate.setElementId(elementId);
        componentTemplate.setArchiveVersion(DEFAULT_ARCHIVE_VERSION);
        componentTemplate.setWorkspace(AlienConstants.GLOBAL_WORKSPACE_ID);
        if (property != null && remaining > 0) {
            if (type.equalsIgnoreCase("node types")) {
                switch(property) {
                    case "capability":
                        ((NodeType) componentTemplate).setCapabilities(Lists.newArrayList(new CapabilityDefinition(propertyValue, propertyValue, 1)));
                        break;
                    case "requirement":
                        ((NodeType) componentTemplate).setRequirements((Lists.newArrayList(new RequirementDefinition(propertyValue, propertyValue))));
                        break;
                    case "default capability":
                        ((NodeType) componentTemplate).setDefaultCapabilities((Lists.newArrayList(propertyValue)));
                        break;
                    case "elementId":
                        ((NodeType) componentTemplate).setElementId(propertyValue);
                        break;
                    default:
                        break;
                }
            } else if (type.equalsIgnoreCase("relationship types")) {
                ((RelationshipType) componentTemplate).setValidSources(new String[] { propertyValue });
            }
            remaining -= 1;
        }
        String serializeDatum = JsonUtil.toString(componentTemplate);
        log.debug("Saving in ES: " + serializeDatum);
        esClient.prepareIndex(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, typeName).setSource(serializeDatum).setRefresh(true).execute().actionGet();
        if (componentTemplate instanceof NodeType) {
            testDataList.add((NodeType) (componentTemplate));
        }
    }
    indexedComponentTypes.put(type, typeName);
}
Also used : AbstractToscaType(org.alien4cloud.tosca.model.types.AbstractToscaType) NodeType(org.alien4cloud.tosca.model.types.NodeType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition)

Example 10 with RequirementDefinition

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

the class TopologyRequirementBoundsValidationServices method isRequirementUpperBoundReachedForSource.

/**
 * Check if the upperBound of a requirement is reached on a node template
 *
 * @param nodeTemplate the node to check for requirement bound
 * @param requirementName the name of the requirement
 * @param dependencies the dependencies of the topology
 * @return true if requirement upper bound is reached, false otherwise
 */
public boolean isRequirementUpperBoundReachedForSource(NodeTemplate nodeTemplate, String requirementName, Set<CSARDependency> dependencies) {
    NodeType relatedIndexedNodeType = toscaTypeSearchService.getRequiredElementInDependencies(NodeType.class, nodeTemplate.getType(), dependencies);
    Requirement requirement = nodeTemplate.getRequirements().get(requirementName);
    if (nodeTemplate.getRelationships() == null || nodeTemplate.getRelationships().isEmpty()) {
        return false;
    }
    RequirementDefinition requirementDefinition = getRequirementDefinition(relatedIndexedNodeType.getRequirements(), requirementName, requirement.getType());
    if (requirementDefinition.getUpperBound() == Integer.MAX_VALUE) {
        return false;
    }
    int count = countRelationshipsForRequirement(nodeTemplate, requirementDefinition);
    return count >= requirementDefinition.getUpperBound();
}
Also used : Requirement(org.alien4cloud.tosca.model.templates.Requirement) NodeTemplateUtils.countRelationshipsForRequirement(org.alien4cloud.tosca.utils.NodeTemplateUtils.countRelationshipsForRequirement) NodeType(org.alien4cloud.tosca.model.types.NodeType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition)

Aggregations

RequirementDefinition (org.alien4cloud.tosca.model.definitions.RequirementDefinition)20 NodeType (org.alien4cloud.tosca.model.types.NodeType)14 CapabilityDefinition (org.alien4cloud.tosca.model.definitions.CapabilityDefinition)11 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)5 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)4 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)4 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)3 Date (java.util.Date)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 Set (java.util.Set)2 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)2 NodeFilter (org.alien4cloud.tosca.model.definitions.NodeFilter)2 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)2 Capability (org.alien4cloud.tosca.model.templates.Capability)2 Requirement (org.alien4cloud.tosca.model.templates.Requirement)2 Test (org.junit.Test)2 FacetedSearchResult (alien4cloud.dao.model.FacetedSearchResult)1 NodeFilterToSatisfy (alien4cloud.topology.task.NodeFilterToSatisfy)1