private static void updatePolicyMembers(Topology topology, String nodeName, String newName) { // Update the policy members when a node template is renamed. for (PolicyTemplate policyTemplate : safe(topology.getPolicies()).values()) { boolean removed = policyTemplate.getTargets().remove(nodeName); if (removed && newName != null) { policyTemplate.getTargets().add(newName); } } }
/** * Get the policies that target this node template. */ public static Set<PolicyTemplate> getTargetedPolicies(Topology topology, NodeTemplate nodeTemplate) { return safe(topology.getPolicies()).values().stream() .filter(policyTemplate -> policyTemplate.getTargets() != null && policyTemplate.getTargets().contains(nodeTemplate.getName())) .collect(Collectors.toSet()); }
/** * 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()); }
/** * 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()); }); }
private void removeNodeFromPolicies(String removedNode, Topology topology) { for (PolicyTemplate policyTemplate : safe(topology.getPolicies()).values()) { policyTemplate.getTargets().remove(removedNode); } }
public static boolean isPolicyTarget(NodeTemplate nodeTemplate, PolicyTemplate policyTemplate) { return safe(policyTemplate.getTargets()).contains(nodeTemplate.getName()); }
@Override protected void processSpecificReplacement(PolicyTemplate replacingNode, PolicyTemplate replacedTopologyNode, Set<String> topologyNotMergedProps) { replacingNode.setTargets(replacedTopologyNode.getTargets()); replacingNode.setTriggers(replacedTopologyNode.getTriggers()); } }
private List<NodeTemplate> getTargets(PolicyTemplate policy, Topology topology, FlowExecutionContext context) { List<NodeTemplate> targets = Lists.newArrayList(); for (String targetName : safe(policy.getTargets())) { NodeTemplate target = safe(topology.getNodeTemplates()).get(targetName); // This modifier is injected after matching phase. Nodes must have been matched against valid type. if (!AWS_MOCK_COMPUTE_TYPE.equals(target.getType())) { context.log().error("Anti-affinity policy {} is not correctly configured, target {} is not an instance of {}.", policy.getName(), target.getName(), AWS_MOCK_COMPUTE_TYPE); return null; } targets.add(target); } return targets; } }
private void apply(PolicyTemplate policy, Topology topology, FlowExecutionContext context) { AbstractPropertyValue value = policy.getProperties().get("availability_zones"); List<NodeTemplate> targets = getTargets(policy, topology, context); if (targets == null) { return; // Some targets are not instances of org.alien4cloud.nodes.mock.aws.Compute } if (safe(policy.getTargets()).size() < 2) { context.log().error("Anti-affinity policy {} is not correctly configured, at least 2 targets are required.", policy.getName()); return; } if (!(value instanceof ListPropertyValue) || ((ListPropertyValue) value).getValue().size() < 2) { context.log().error("Anti-affinity policy {} is not correctly configured, zones property is required and must contains at least 2 values.", policy.getName()); return; } ListPropertyValue propertyValue = (ListPropertyValue) value; for (int i = 0; i < targets.size(); i++) { NodeTemplate nodeTemplate = targets.get(i); String nodeZone = (String) propertyValue.getValue().get(i % propertyValue.getValue().size()); if (AWS_MOCK_COMPUTE_TYPE.equals(nodeTemplate.getType())) { context.log().info("Anti-affinity policy {} inject zone property {} to node {}", policy.getName(), nodeZone, nodeTemplate.getName()); nodeTemplate.getProperties().put("zone", new ScalarPropertyValue(nodeZone)); } } }
@Override public void process(final PolicyTemplate instance) { // ensure type exists referencePostProcessor.process(new ReferencePostProcessor.TypeReference(instance, instance.getType(), PolicyType.class)); final PolicyType policyType = ToscaContext.get(PolicyType.class, instance.getType()); if (policyType == null) { return; // error managed by the reference post processor. } final Topology topology = ((ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance()).getTopology(); // check that the targets are exiting node templates // TODO should we also check the type of the target, see if it matches with one provided in PolicyType.getTargets() ? safe(instance.getTargets()).forEach(target -> { if (!safe((topology.getNodeTemplates())).containsKey(target)) { // Dispatch an error. Node node = ParsingContextExecution.getObjectToNodeMap().get(instance.getTargets()); ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.POLICY_TARGET_NOT_FOUND, instance.getName(), node.getStartMark(), null, node.getEndMark(), target)); } }); // Merge the policy template with data coming from the type (default values etc.). PolicyTemplate tempObject = TemplateBuilder.buildPolicyTemplate(policyType, instance, false); instance.setProperties(tempObject.getProperties()); propertyValueChecker.checkProperties(policyType, instance.getProperties(), instance.getName()); } }