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 protected void process(Csar csar, Topology topology, UpdatePolicyTargetsOperation operation, PolicyTemplate policyTemplate) { for (String target : operation.getTargets()) { AlienUtils.getOrFail(topology.getNodeTemplates(), target, "The node with name [ {} ] and assigned as policy [ {} ] target cannot be found in the topology.", target, policyTemplate.getName()); // FIXME if the policy type defines some restriction on the policy targets then it should be checked here } policyTemplate.setTargets(operation.getTargets()); } }
if (policyImplMeta == null) { context.log().warn("Matched policy {} for {} does not define an alien topology modifier implementation, it may not be taken in account.", policyTemplate.getType(), policyTemplate.getName()); continue; context.log().error( "Matched policy {} for policy {} defines an invalid modifier implementation {}, format should be policy_plugin_id:policy_plugin_bean:injection_phase", policyTemplate.getType(), policyTemplate.getName(), policyImplMeta);
@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()); } }