private Map<String, PolicyType> getPolicyTypes(Topology topology) { Map<String, PolicyType> policyTypes = Maps.newHashMap(); for (PolicyTemplate template : safe(topology.getPolicies()).values()) { if (!policyTypes.containsKey(template.getType())) { PolicyType policyType = ToscaContext.getOrFail(PolicyType.class, template.getType()); policyTypes.put(policyType.getElementId(), policyType); } } return policyTypes; } }
@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()); } }
@Override protected void processSpecificReplacement(PolicyTemplate replacingNode, PolicyTemplate replacedTopologyNode, Set<String> topologyNotMergedProps) { replacingNode.setTargets(replacedTopologyNode.getTargets()); replacingNode.setTriggers(replacedTopologyNode.getTriggers()); } }
@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()); } }
@Override protected void process(Csar csar, Topology topology, SetPolicyPropertyAsSecretOperation operation, PolicyTemplate policyTemplate) { PolicyType policyType = ToscaContext.getOrFail(PolicyType.class, policyTemplate.getType()); AlienUtils.getOrFail(policyType.getProperties(), operation.getPropertyName(), "Property [ {} ] doesn't exists in type [ {} ] for policy [ {} ].", operation.getPropertyName(), policyTemplate.getType(), operation.getPolicyName()); FunctionPropertyValue secretFunction = new FunctionPropertyValue(); secretFunction.setFunction(ToscaFunctionConstants.GET_SECRET); secretFunction.setParameters(Arrays.asList(operation.getSecretPath())); policyTemplate.getProperties().put(operation.getPropertyName(), secretFunction); log.debug("Associate the property [ {} ] of the policy template [ {} ] as secret [ {} ] of the topology [ {} ].", operation.getPropertyName(), operation.getPolicyName(), operation.getSecretPath(), topology.getId()); } }
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)); } } }
assertEquals(1, archiveRoot.getTopology().getPolicies().size()); assertNotNull(archiveRoot.getTopology().getPolicies().get("anti_affinity_policy")); assertEquals("org.alien4cloud.sample.SamplePolicy", archiveRoot.getTopology().getPolicies().get("anti_affinity_policy").getType()); assertEquals(1, archiveRoot.getTopology().getPolicies().get("anti_affinity_policy").getProperties().size()); assertNotNull(archiveRoot.getTopology().getPolicies().get("anti_affinity_policy").getProperties().get("sample_property")); assertEquals(2, archiveRoot.getTopology().getPolicies().get("anti_affinity_policy").getTags().size());
PolicyType policyType = ToscaContext.getOrFail(PolicyType.class, policyTemplate.getType()); String policyImplMeta = TagUtil.getTagValue(policyType.getTags(), "a4c_policy_impl"); 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); context.log().error("Implementation specified for policy type {} that refers to plugin bean {}, {} cannot be found.", policyTemplate.getType(), policyImpl[0], policyImpl[1]);
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); } } }
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; } }
@Override protected void process(Csar csar, Topology topology, RenamePolicyOperation operation, PolicyTemplate policyTemplate) { NameValidationUtils.validateNodeName(operation.getNewName()); AlienUtils.failIfExists(topology.getPolicies(), operation.getNewName(), "A node template with the given name {} already exists in the topology {}.", operation.getNewName(), topology.getId()); log.debug("Renaming policy template <" + operation.getPolicyName() + "> to <" + operation.getNewName() + "> in the topology <" + topology.getId() + "> ."); policyTemplate.setName(operation.getNewName()); topology.getPolicies().put(operation.getNewName(), policyTemplate); topology.getPolicies().remove(operation.getPolicyName()); } }
public static PolicyTemplate buildPolicyTemplate(PolicyType policyType) { policyType = CloneUtil.clone(policyType); PolicyTemplate policyTemplate = new PolicyTemplate(); fillAbstractTemplate(policyTemplate, policyType, null, false); return policyTemplate; }
@Override protected void process(Csar csar, Topology topology, UpdatePolicyPropertyValueOperation operation, PolicyTemplate policyTemplate) { PolicyType policyType = ToscaContext.getOrFail(PolicyType.class, policyTemplate.getType()); PropertyDefinition propertyDefinition = AlienUtils.getOrFail(policyType.getProperties(), operation.getPropertyName(), "Property [ {} ] doesn't exists in type [ {} ] for policy [ {} ].", operation.getPropertyName(), policyTemplate.getType(), operation.getPolicyName()); log.debug("Updating property [ {} ] of the policy template [ {} ] from the topology [ {} ]: changing value from [{}] to [{}].", operation.getPropertyName(), operation.getPolicyName(), topology.getId(), policyTemplate.getProperties().get(operation.getPropertyName()), operation.getPropertyValue()); try { propertyService.setPropertyValue(policyTemplate, propertyDefinition, operation.getPropertyName(), operation.getPropertyValue()); } catch (ConstraintFunctionalException e) { throw new PropertyValueException("Error when setting policy " + operation.getPolicyName() + " property.", e, operation.getPropertyName(), operation.getPropertyValue()); } } }
/** * 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()); }
policyTemplate.setName(policyName); policyTemplatePostProcessor.process(policyTemplate); });
public static PolicyTemplate buildPolicyTemplate(PolicyType policyType, PolicyTemplate templateToMerge, boolean adaptToType) { policyType = CloneUtil.clone(policyType); PolicyTemplate policyTemplate = new PolicyTemplate(); fillAbstractTemplate(policyTemplate, policyType, templateToMerge, !adaptToType); return policyTemplate; }
public Map<String, PolicyType> getPolicyTypesFromTopology(Topology topology, boolean failOnTypeNotFound) { Map<String, PolicyType> types = Maps.newHashMap(); for (PolicyTemplate template : safe(topology.getPolicies()).values()) { types.put(template.getType(), getFromContextIfDefined(PolicyType.class, template.getType(), topology.getDependencies(), failOnTypeNotFound)); } return types; }
/** * 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()); }); }
@Override public void process(Csar csar, Topology topology, AddPolicyOperation operation) { NameValidationUtils.validate("policy", operation.getPolicyName()); AlienUtils.failIfExists(topology.getPolicies(), operation.getPolicyName(), "A policy with the given name {} already exists in the topology {}.", operation.getPolicyName(), topology.getId()); PolicyType policyType = toscaTypeSearchService.findByIdOrFail(PolicyType.class, operation.getPolicyTypeId()); if (topology.getPolicies() == null) { topology.setPolicies(new LinkedHashMap<>()); } PolicyTemplate policyTemplate = TemplateBuilder.buildPolicyTemplate(policyType); policyTemplate.setName(operation.getPolicyName()); log.debug("Adding a new policy template <" + operation.getPolicyName() + "> of type <" + operation.getPolicyTypeId() + "> to the topology <" + topology.getId() + "> ."); topologyService.loadType(topology, policyType); topology.getPolicies().put(operation.getPolicyName(), policyTemplate); } }
/** * @param topology * @param type * @param manageInheritance true if you also want to consider type hierarchy (ie. include that inherit the given type). * @return a set of nodes that are of the given type (or inherit the given type if <code>manageInheritance</code> is true). */ public static Set<PolicyTemplate> getPoliciesOfType(Topology topology, String type, boolean manageInheritance) { Set<PolicyTemplate> result = Sets.newHashSet(); for (PolicyTemplate policyTemplate : safe(topology.getPolicies()).values()) { if (policyTemplate.getType().equals(type)) { result.add(policyTemplate); } else if (manageInheritance) { PolicyType policyType = ToscaContext.get(PolicyType.class, policyTemplate.getType()); if (policyType.getDerivedFrom().contains(type)) { result.add(policyTemplate); } } } return result; }