public static boolean hasRelationship(NodeTemplate sourceNode, String targetNodeName, String requirementName, String capabilityName) { for (RelationshipTemplate relationshipTemplate : safe(sourceNode.getRelationships()).values()) { if (relationshipTemplate.getTarget().equals(targetNodeName) && relationshipTemplate.getRequirementName().equals(requirementName) && relationshipTemplate.getTargetedCapabilityName().equals(capabilityName)) { return true; } } return false; }
private String buildRelationShipTemplateName(RelationshipTemplate relationshipTemplate) { String value = relationshipTemplate.getType(); if (value == null) { return null; } if (value.contains(".")) { value = value.substring(value.lastIndexOf(".") + 1); } value = StringUtils.uncapitalize(value); value = value + StringUtils.capitalize(relationshipTemplate.getTarget()); return value; }
/** * Returns all the nodes that this node template targets with the given requirement. */ public static Set<NodeTemplate> getTargetNodes(Topology topology, NodeTemplate nodeTemplate, String requirementName) { Set<NodeTemplate> result = Sets.newHashSet(); for (RelationshipTemplate relationshipTemplate : getTargetRelationships(nodeTemplate, requirementName)) { result.add(topology.getNodeTemplates().get(relationshipTemplate.getTarget())); } return result; }
/** * Returns all the nodes that target this node template with the given requirement. */ public static Set<NodeTemplate> getSourceNodes(Topology topology, NodeTemplate nodeTemplate, String capabilityName) { Set<NodeTemplate> result = Sets.newHashSet(); for (NodeTemplate node : topology.getNodeTemplates().values()) { for (RelationshipTemplate relationshipTemplate : safe(node.getRelationships()).values()) { if (relationshipTemplate.getTargetedCapabilityName().equals(capabilityName) && relationshipTemplate.getTarget().equals(nodeTemplate.getName())) { result.add(node); } } } return result; }
/** * Utility method to find the immediate host template of the current given template. * * @param template The template for wich to get the immediate host. * @return */ public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template) { RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON); if (host == null) { return null; } return topology.getNodeTemplates().get(host.getTarget()); }
/** * checks if a relationship is still valid * * @param nodeTemplate The source node of the relationship * @param relationshipTemplate The relationship to validate * @param topology The related topology */ private void validateRelationShip(NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate, Topology topology) { // This call should never throw a NotFoundException NodeType nodeType = ToscaContext.getOrFail(NodeType.class, nodeTemplate.getType()); // validate the requirement checkRequirement(relationshipTemplate, nodeTemplate, nodeType); // validate the targeted capability checkCapability(relationshipTemplate.getTargetedCapabilityName(), topology.getNodeTemplates().get(relationshipTemplate.getTarget())); }
public static List<NodeTemplate> getHostedNodes(Topology topology, String nodeName) { return safe(topology.getNodeTemplates()).values().stream().filter(nodeTemplate -> safe(nodeTemplate.getRelationships()).values().stream() .anyMatch(relTemp -> relTemp.getTarget().equals(nodeName) && isHostedOnRelationship(relTemp.getType()))).collect(Collectors.toList()); }
private void processNetwork(PaaSNodeTemplate paaSNodeTemplate, Map<String, PaaSNodeTemplate> nodeTemplates) { List<PaaSRelationshipTemplate> networkRelationships = getPaaSRelationshipsTemplateFromType(paaSNodeTemplate, NormativeRelationshipConstants.NETWORK); List<PaaSNodeTemplate> networks = Lists.newArrayList(); if (networkRelationships != null && !networkRelationships.isEmpty()) { for (PaaSRelationshipTemplate networkRelationship : networkRelationships) { String target = networkRelationship.getTemplate().getTarget(); PaaSNodeTemplate network = nodeTemplates.get(target); networks.add(network); network.setParent(paaSNodeTemplate); } } paaSNodeTemplate.setNetworkNodes(networks); }
public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template, IToscaTypeFinder toscaTypeFinder) { RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON, id -> toscaTypeFinder.findElement(RelationshipType.class, id)); if (host == null) { return null; } return topology.getNodeTemplates().get(host.getTarget()); }
private void doRemoveRelationships(Workflow wf, String sourceNodeId, Map<String, RelationshipTemplate> sourceRelationships) { // Remove relationships steps Collection<WorkflowStep> relationshipWorkflowSteps = wf.getSteps().values().stream() .filter(step -> sourceRelationships.entrySet().stream() .anyMatch(relationshipTemplateEntry -> WorkflowUtils.isRelationshipStep(step, sourceNodeId, relationshipTemplateEntry.getKey()))) .collect(Collectors.toList()); relationshipWorkflowSteps.forEach(step -> removeStep(wf, step.getName(), true)); // Find all links from and to the node's steps and unlink them all if they come from relationship template wf.getSteps().values().stream().filter(step -> sourceNodeId.equals(step.getTarget())) .forEach(nodeStep -> new ArrayList<>(AlienUtils.safe(nodeStep.getOnSuccess())).stream().map(followingId -> wf.getSteps().get(followingId)) .filter(followingStep -> sourceRelationships.entrySet().stream().anyMatch( relationshipTemplateEntry -> WorkflowUtils.isNodeStep(followingStep, relationshipTemplateEntry.getValue().getTarget()))) .forEach(followingStep -> unlinkSteps(nodeStep, followingStep))); wf.getSteps().values().stream().filter(step -> sourceNodeId.equals(step.getTarget())) .forEach(nodeStep -> new ArrayList<>(AlienUtils.safe(nodeStep.getPrecedingSteps())).stream().map(precedingId -> wf.getSteps().get(precedingId)) .filter(precedingStep -> sourceRelationships.entrySet().stream().anyMatch( relationshipTemplateEntry -> WorkflowUtils.isNodeStep(precedingStep, relationshipTemplateEntry.getValue().getTarget()))) .forEach(precedingStep -> unlinkSteps(precedingStep, nodeStep))); }
private void processBlockStorage(PaaSNodeTemplate paaSNodeTemplate, Map<String, PaaSNodeTemplate> nodeTemplates) { PaaSRelationshipTemplate attachTo = getPaaSRelationshipTemplateFromType(paaSNodeTemplate, NormativeRelationshipConstants.ATTACH_TO); if (attachTo != null) { String target = attachTo.getTemplate().getTarget(); PaaSNodeTemplate parent = nodeTemplates.get(target); parent.getStorageNodes().add(paaSNodeTemplate); paaSNodeTemplate.setParent(parent); } }
private void doScaledUpNode(ScalingVisitor scalingVisitor, String nodeTemplateId, Map<String, NodeTemplate> nodeTemplates) { scalingVisitor.visit(nodeTemplateId); for (Entry<String, NodeTemplate> nEntry : nodeTemplates.entrySet()) { if (nEntry.getValue().getRelationships() != null) { for (Entry<String, RelationshipTemplate> rt : nEntry.getValue().getRelationships().entrySet()) { RelationshipType relType = getRelationshipType(rt.getValue().getType()); if (nodeTemplateId.equals(rt.getValue().getTarget()) && ToscaTypeUtils.isOfType(relType, NormativeRelationshipConstants.HOSTED_ON)) { doScaledUpNode(scalingVisitor, nEntry.getKey(), nodeTemplates); } } } } }
private void processRelationship(PaaSNodeTemplate paaSNodeTemplate, Map<String, PaaSNodeTemplate> nodeTemplates) { PaaSRelationshipTemplate hostedOnRelationship = getPaaSRelationshipTemplateFromType(paaSNodeTemplate, NormativeRelationshipConstants.HOSTED_ON); if (hostedOnRelationship != null) { String target = hostedOnRelationship.getTemplate().getTarget(); PaaSNodeTemplate parent = nodeTemplates.get(target); parent.getChildren().add(paaSNodeTemplate); paaSNodeTemplate.setParent(parent); } // Relationships are defined from sources to target. We have to make sure that target node also has the relationship injected. List<PaaSRelationshipTemplate> allRelationships = getPaaSRelationshipsTemplateFromType(paaSNodeTemplate, NormativeRelationshipConstants.ROOT); for (PaaSRelationshipTemplate relationship : allRelationships) { // inject the relationship in it's target. String target = relationship.getTemplate().getTarget(); nodeTemplates.get(target).getRelationshipTemplates().add(relationship); } }
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); } } } }
/** * Returns all the nodes that target this node template with a relationship of the given type. */ public static Set<NodeTemplate> getSourceNodesByRelationshipType(Topology topology, NodeTemplate nodeTemplate, String relationshipTypeName) { Set<NodeTemplate> result = Sets.newHashSet(); for (NodeTemplate node : topology.getNodeTemplates().values()) { for (RelationshipTemplate relationshipTemplate : safe(node.getRelationships()).values()) { if (relationshipTemplate.getTarget().equals(nodeTemplate.getName())) { RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType()); if (ToscaTypeUtils.isOfType(relationshipType, relationshipTypeName)) { result.add(node); } } } } return result; }
private static PaaSNodeTemplate getTargetNode(IPaaSTemplate<? extends AbstractToscaType> basePaaSTemplate, Map<String, PaaSNodeTemplate> builtPaaSTemplates) { if (basePaaSTemplate instanceof PaaSRelationshipTemplate) { return getPaaSNodeOrFail(((PaaSRelationshipTemplate) basePaaSTemplate).getTemplate().getTarget(), builtPaaSTemplates); } throw new BadUsageKeywordException("The keyword <" + ToscaFunctionConstants.TARGET + "> can only be used on a Relationship level's parameter. Node<" + basePaaSTemplate.getId() + ">."); }
public static String getConcernedNodeName(WorkflowStep stepFound, Topology topology) { if (stepFound instanceof NodeWorkflowStep) { return stepFound.getTarget(); } else if (stepFound instanceof RelationshipWorkflowStep) { RelationshipWorkflowStep relationshipWorkflowStepFound = (RelationshipWorkflowStep) stepFound; if (RelationshipOperationHost.SOURCE.toString().equals(relationshipWorkflowStepFound.getOperationHost())) { return relationshipWorkflowStepFound.getTarget(); } else if (RelationshipOperationHost.TARGET.toString().equals(relationshipWorkflowStepFound.getOperationHost())) { return topology.getNodeTemplates().get(relationshipWorkflowStepFound.getTarget()).getRelationships() .get(relationshipWorkflowStepFound.getTargetRelationship()).getTarget(); } } throw new NotFoundException("This is a bug, no node can be found concerning the step " + stepFound.getStepAsString()); }
private static String getRelationshipTarget(String source, String relationshipId, TopologyContext topologyContext) { NodeTemplate sourceNode = safe(topologyContext.getTopology().getNodeTemplates()).get(source); if (sourceNode == null) { throw new NotFoundException("Source " + source + " cannot be found in the topology " + topologyContext.getTopology().getId()); } RelationshipTemplate relationshipTemplate = safe(sourceNode.getRelationships()).get(relationshipId); if (relationshipTemplate == null) { throw new NotFoundException( "Source " + source + " does not have the relationship " + relationshipId + " in the topology " + topologyContext.getTopology().getId()); } return relationshipTemplate.getTarget(); }
/** * Deeply explore the hosted_on hierarchy of the given node to find a node of the given type. */ public static NodeTemplate getHostOfTypeInHostingHierarchy(Topology topology, NodeTemplate nodeTemplate, String hostType) { if (nodeTemplate.getRelationships() != null) { for (RelationshipTemplate relationshipTemplate : nodeTemplate.getRelationships().values()) { RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType()); if (isOfType(relationshipType, NormativeRelationshipConstants.HOSTED_ON)) { NodeTemplate hostNode = topology.getNodeTemplates().get(relationshipTemplate.getTarget()); NodeType hostNodeType = ToscaContext.get(NodeType.class, hostNode.getType()); if (isOfType(hostNodeType, hostType)) { return hostNode; } else { return getHostOfTypeInHostingHierarchy(topology, hostNode, hostType); } } } } return null; }
@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()); }