/** * Process a node template to retrieve filters for node replacements search. * * TODO cleanup this method, better return a filter for node rather than adding it to a parameter list. * * @param topology The topology for which to search filters. * @param nodeTempEntry The node template for which to find replacement filter. * @param nodeTemplatesToFilters The map of filters in which to add the filter for the new Node. */ public void processNodeTemplate(final Topology topology, final Entry<String, NodeTemplate> nodeTempEntry, Map<String, Map<String, Set<String>>> nodeTemplatesToFilters) { String capabilityFilterKey = "capabilities.type"; String requirementFilterKey = "requirements.type"; NodeTemplate template = nodeTempEntry.getValue(); Map<String, RelationshipTemplate> relTemplates = template.getRelationships(); nodeTemplatesToFilters.put(nodeTempEntry.getKey(), null); // process the node template source of relationships if (relTemplates != null && !relTemplates.isEmpty()) { for (RelationshipTemplate relationshipTemplate : relTemplates.values()) { addFilters(nodeTempEntry.getKey(), requirementFilterKey, relationshipTemplate.getRequirementType(), nodeTemplatesToFilters); } } // process the node template target of relationships List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTempEntry.getKey(), topology.getNodeTemplates()); for (RelationshipEntry targetRelationshipEntry : targetRelationships) { addFilters(nodeTempEntry.getKey(), capabilityFilterKey, targetRelationshipEntry.getRelationship().getRequirementType(), nodeTemplatesToFilters); } }
/** * Get the number of relationships from a node template that are actually linked to the given requirement. * * @param nodeTemplate The node template for which to count relationships * @param requirementDefinition The requirement definition from the node template's type for which to count related relationships. * @return The number of relationships connected to the given requirement. */ public static int countRelationshipsForRequirement(NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition) { int count = 0; for (Map.Entry<String, RelationshipTemplate> relEntry : safe(nodeTemplate.getRelationships()).entrySet()) { if (relEntry.getValue().getRequirementName().equals(requirementDefinition.getId()) && relEntry.getValue().getRequirementType().equals(requirementDefinition.getType())) { count++; } } return count; } }
/** * validate the requirement:<br> * <ul> * <li>Check that a node still has the requirement, by checking directly from the related node type</li> * <li>Check that the requirement type of the node hasn't changed</li> * </ul> * * @param relationshipTemplate The name of the requirement to check * @param nodeTemplate The node template in which to check for the requirement * @param nodeType * @throws InvalidStateException when something is wrong */ private void checkRequirement(RelationshipTemplate relationshipTemplate, NodeTemplate nodeTemplate, NodeType nodeType) { Map<String, RequirementDefinition> requirementMap = AlienUtils.fromListToMap(nodeType.getRequirements(), "id", true); if (!AlienUtils.safe(requirementMap).containsKey(relationshipTemplate.getRequirementName())) { throw new InvalidStateException( "A requirement with name [" + relationshipTemplate.getName() + "] cannot be found in the node [" + nodeTemplate.getName() + "]."); } // check that the requirement type hasn't changed RequirementDefinition requirement = AlienUtils.safe(requirementMap).get(relationshipTemplate.getRequirementName()); if (!Objects.equals(requirement.getType(), relationshipTemplate.getRequirementType())) { // the requirementType has changed. delete the relationship // TODO check if this requirement is compatible with the previous one, and mabey try to rebuild the relationship throw new InvalidStateException( "The type of the requirement [" + relationshipTemplate.getRequirementName() + "], node [" + nodeTemplate.getName() + "] has changed"); } }
private void updateRelationshipsCapabilitiesRelationships(Topology topology, NodeTemplate nodeTemplate) { List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTemplate.getName(), topology.getNodeTemplates()); for (RelationshipEntry targetRelationshipEntry : targetRelationships) { RelationshipTemplate targetRelationship = targetRelationshipEntry.getRelationship(); Capability capability = safe(nodeTemplate.getCapabilities()).get(targetRelationship.getTargetedCapabilityName()); if (capability == null || isCapabilityNotOfType(capability, targetRelationship.getRequirementType())) { Entry<String, Capability> capabilityEntry = NodeTemplateUtils.getCapabilitEntryyByType(nodeTemplate, targetRelationship.getRequirementType()); targetRelationship.setTargetedCapabilityName(capabilityEntry.getKey()); // check that the relationship type is still valid with the new capability RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, targetRelationship.getType()); if (!isValidRelationship(relationshipType, capabilityEntry.getValue())) { NodeType sourceNodeType = ToscaContext.get(NodeType.class, targetRelationshipEntry.getSource().getType()); RequirementDefinition requirementDefinition = NodeTypeUtils.getRequirementById(sourceNodeType, targetRelationshipEntry.getRelationship().getRequirementName()); NodeType targetNodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType()); CapabilityDefinition capabilityDefinition = NodeTypeUtils.getCapabilityById(targetNodeType, capabilityEntry.getKey()); RelationshipType validRelationshipType = danglingRequirementService.fetchValidRelationshipType(requirementDefinition, capabilityDefinition); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setType(validRelationshipType.getElementId()); targetRelationship.setArtifacts(newLinkedHashMap(safe(validRelationshipType.getArtifacts()))); targetRelationship.setAttributes(newLinkedHashMap(safe(validRelationshipType.getAttributes()))); Map<String, AbstractPropertyValue> properties = new LinkedHashMap(); TemplateBuilder.fillProperties(properties, validRelationshipType.getProperties(), null); targetRelationship.setProperties(properties); } } } }
capability = getCapabilityByType(targetNodeTemplate, relationshipTemplate, relationshipTemplate.getRequirementType()); if (capability == null) { capability = targetNodeTemplate.getCapabilities().get(relationshipTemplate.getRequirementName());
@Then("^I should have (\\d+) relationship with source \"([^\"]*)\" and target \"([^\"]*)\" for type \"([^\"]*)\" with requirement \"([^\"]*)\" of type \"([^\"]*)\"$") public void I_should_have_relationship_with_source_for_requirement_of_type(int relationshipCount, String source, String target, String relType, String requirementName, String requirementType) throws Throwable { String topologyJson = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId()); RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyJson, TopologyDTO.class, Context.getJsonMapper()); NodeTemplate sourceNode = topologyResponse.getData().getTopology().getNodeTemplates().get(source); RelationshipTemplate rel = sourceNode.getRelationships().get(getRelationShipName(relType, target)); assertNotNull(rel); // Only one relationship of this type for the moment : cardinality check soon assertEquals(rel.getRequirementName(), requirementName); assertEquals(rel.getRequirementType(), requirementType); }
@Then("^I should have a relationship \"([^\"]*)\" with type \"([^\"]*)\" from \"([^\"]*)\" to \"([^\"]*)\" in ALIEN$") public void I_should_have_a_relationship_with_type_from_to_in_ALIEN(String relName, String relType, String source, String target) throws Throwable { // I should have a relationship with type String topologyJson = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId()); RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyJson, TopologyDTO.class, Context.getJsonMapper()); NodeTemplate sourceNode = topologyResponse.getData().getTopology().getNodeTemplates().get(source); relName = relName == null || relName.isEmpty() ? getRelationShipName(relType, target) : relName; RelationshipTemplate rel = sourceNode.getRelationships().get(relName); assertNotNull(rel); assertEquals(relType, rel.getType()); assertEquals(target, rel.getTarget()); assertNotNull(rel.getRequirementName()); assertNotNull(rel.getRequirementType()); }