/** * @return true if this node is hosted on another. */ public static boolean isHosted(Topology topology, NodeTemplate nodeTemplate) { return getImmediateHostTemplate(topology, nodeTemplate) != null; }
/** * Get all incoming relationships of a node that match the requested type. * * @param template The node template for which to get relationships. * @param type The expected type. * @return */ public static RelationshipTemplate getRelationshipFromType(NodeTemplate template, String type) { return getRelationshipFromType(template, type, id -> ToscaContext.getOrFail(RelationshipType.class, id)); }
/** * 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; }
public static int getDefaultInstanceCount(Topology topology, NodeTemplate template, int multiplicator) { Capability scalableCapability = TopologyUtils.getScalableCapability(topology, template.getName(), false); int defaultInstanceCount = 1; if (scalableCapability != null) { ScalingPolicy scalingPolicy = TopologyUtils.getScalingPolicy(scalableCapability); if (!ScalingPolicy.NOT_SCALABLE_POLICY.equals(scalingPolicy)) { defaultInstanceCount = scalingPolicy.getInitialInstances(); } } // now look for the host NodeTemplate host = getImmediateHostTemplate(topology, template); if (host != null) { return getDefaultInstanceCount(topology, host, multiplicator * defaultInstanceCount); } else { return multiplicator * defaultInstanceCount; } }
if (template instanceof NodeTemplate) { return doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) template), function); } else { throw new IllegalArgumentException("HOST keyname cannot be used if not in a node template context (or capability/requirement)."); Set<NodeTemplate> targetNodes = TopologyNavigationUtil.getTargetNodes(evaluatorContext.getTopology(), (NodeTemplate) template, function.getCapabilityOrRequirementName()); if (targetNodes != null && targetNodes.size() == 1) { NodeTemplate firstNode = targetNodes.iterator().next();
protected void removeNode(Topology topology, NodeTemplate nodeTemplate) { // keep track of the hosted nodes List<NodeTemplate> hostedNodes = TopologyNavigationUtil.getHostedNodes(topology, nodeTemplate.getName()); Csar csar = new Csar(topology.getArchiveName(), topology.getArchiveVersion()); DeleteNodeOperation deleteNodeOperation = new DeleteNodeOperation(); deleteNodeOperation.setNodeName(nodeTemplate.getName()); deleteNodeProcessor.process(csar, topology, deleteNodeOperation); // remove all hosted node also safe(hostedNodes).forEach(hostedNodeTemplate -> removeNode(topology, hostedNodeTemplate)); } }
/** * Get the members targeted by this policy. */ public static Set<NodeTemplate> getTargetedMembers(Topology topology, PolicyTemplate policyTemplate) { return CollectionUtils.isEmpty(policyTemplate.getTargets()) ? Sets.newHashSet() : safe(topology.getNodeTemplates()).values().stream().filter(nodeTemplate -> isPolicyTarget(nodeTemplate, policyTemplate)) .collect(Collectors.toSet()); }
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()); }
/** * Change policies that target the sourceTemplate and make them target the targetTemplate. * * TODO: move elsewhere ? */ public static void changePolicyTarget(Topology topology, NodeTemplate sourceTemplate, NodeTemplate targetTemplate) { Set<PolicyTemplate> policies = TopologyNavigationUtil.getTargetedPolicies(topology, sourceTemplate); policies.forEach(policyTemplate -> { policyTemplate.getTargets().remove(sourceTemplate.getName()); policyTemplate.getTargets().add(targetTemplate.getName()); }); }
int instanceCount = TopologyNavigationUtil.getDefaultInstanceCount(topology, nodeTemplate, 1); nodeInstanceCount.put(nodeName, instanceCount); });
/** * 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; }
private void duplicateNodeTemplate(NodeTemplate nodeTemplateToDuplicate, Map<String, String> duplicatedNodesNameMappings, Map<String, NodeTemplate> nodeTemplates, Topology topology, Csar csar) { // Build the new one NodeTemplate newNodeTemplate = CloneUtil.clone(nodeTemplateToDuplicate); newNodeTemplate.setName(copyName(nodeTemplateToDuplicate.getName(), nodeTemplates.keySet())); // load type NodeType type = ToscaContext.getOrFail(NodeType.class, nodeTemplateToDuplicate.getType()); topologyService.loadType(topology, type); log.debug("Duplicating node template [ {} ] into [ {} ] on the topology [ {} ] .", nodeTemplateToDuplicate.getName(), newNodeTemplate.getName(), topology.getId()); // Put the new one in the topology nodeTemplates.put(newNodeTemplate.getName(), newNodeTemplate); // register the name mapping for further use duplicatedNodesNameMappings.put(nodeTemplateToDuplicate.getName(), newNodeTemplate.getName()); // copy outputs copyOutputs(topology, nodeTemplateToDuplicate.getName(), newNodeTemplate.getName()); TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar); // add the new node to the workflow workflowBuilderService.addNode(topologyContext, newNodeTemplate.getName()); // copy hosted nodes safe(TopologyNavigationUtil.getHostedNodes(topology, nodeTemplateToDuplicate.getName())) .forEach(nodeTemplate -> duplicateNodeTemplate(nodeTemplate, duplicatedNodesNameMappings, nodeTemplates, topology, csar)); }
private static String getRootHostNode(String nodeId, TopologyContext topologyContext) { NodeTemplate nodeTemplate = topologyContext.getTopology().getNodeTemplates().get(nodeId); if (nodeTemplate == null) { return null; } NodeTemplate hostTemplate = TopologyNavigationUtil.getImmediateHostTemplate(topologyContext.getTopology(), nodeTemplate, topologyContext); if (hostTemplate == null) { return nodeId; } return getRootHostNode(hostTemplate.getName(), topologyContext); }
/** * 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()); }
TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function); if (propertyValue == null && targetTemplate instanceof NodeTemplate) { propertyValue = doGetProperty(evaluatorContext, TopologyNavigationUtil.getImmediateHostTemplate(evaluatorContext.getTopology(), (NodeTemplate) targetTemplate), function);
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()); }