use of org.alien4cloud.tosca.model.templates.PolicyTemplate in project alien4cloud by alien4cloud.
the class PolicyMatcherService method match.
/**
* Perform matching of policies from a topology.
*
* @param policyTemplates The policy templates to match.
* @param location The location against which to match policies.
* @return A Map of available matches for every policy template.
*/
public Map<String, List<PolicyLocationResourceTemplate>> match(Map<String, PolicyTemplate> policyTemplates, Map<String, PolicyType> policyTypes, Location location, String environmentId) {
if (MapUtils.isEmpty(policyTemplates)) {
return Maps.newHashMap();
}
Map<String, List<PolicyLocationResourceTemplate>> matches = Maps.newHashMap();
// fetch location resources
LocationResources locationResources = locationResourceService.getLocationResources(location);
// Authorization filtering of location resources
locationResources.getPolicyTemplates().removeIf(securedResource -> !locationSecurityService.isAuthorised(securedResource, environmentId));
for (Entry<String, PolicyTemplate> policyTemplateEntry : policyTemplates.entrySet()) {
PolicyType policyType = policyTypes.get(policyTemplateEntry.getValue().getType());
matches.put(policyTemplateEntry.getKey(), policyMatcher.match(policyTemplateEntry.getValue(), policyType, locationResources.getPolicyTemplates(), locationResources.getPolicyTypes(), locationResources, null));
}
return matches;
}
use of org.alien4cloud.tosca.model.templates.PolicyTemplate in project alien4cloud by alien4cloud.
the class TopologyService method initializeTypeLoader.
private ToscaTypeLoader initializeTypeLoader(Topology topology, boolean failOnTypeNotFound) {
// FIXME we should use ToscaContext here, and why not allowing the caller to pass ona Context?
ToscaTypeLoader loader = new ToscaTypeLoader(csarDependencyLoader);
Map<String, NodeType> nodeTypes = topologyServiceCore.getIndexedNodeTypesFromTopology(topology, false, false, failOnTypeNotFound);
Map<String, RelationshipType> relationshipTypes = topologyServiceCore.getIndexedRelationshipTypesFromTopology(topology, failOnTypeNotFound);
Map<String, PolicyType> policyTypes = topologyServiceCore.getPolicyTypesFromTopology(topology, failOnTypeNotFound);
for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
NodeType nodeType = nodeTypes.get(nodeTemplate.getType());
// just load found types: the type might be null when failOnTypeNotFound is set to false.
if (nodeType != null) {
loader.loadType(nodeTemplate.getType(), csarDependencyLoader.buildDependencyBean(nodeType.getArchiveName(), nodeType.getArchiveVersion()));
}
for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
RelationshipType relationshipType = relationshipTypes.get(relationshipTemplate.getType());
// just load found types: the type might be null when failOnTypeNotFound is set to false.
if (relationshipType != null) {
loader.loadType(relationshipTemplate.getType(), csarDependencyLoader.buildDependencyBean(relationshipType.getArchiveName(), relationshipType.getArchiveVersion()));
}
}
}
for (PolicyTemplate policyTemplate : safe(topology.getPolicies()).values()) {
PolicyType policyType = policyTypes.get(policyTemplate.getType());
if (policyType != null) {
loader.loadType(policyTemplate.getType(), csarDependencyLoader.buildDependencyBean(policyType.getArchiveName(), policyType.getArchiveVersion()));
}
}
if (topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) {
NodeType substitutionType = nodeTypes.get(topology.getSubstitutionMapping().getSubstitutionType());
loader.loadType(substitutionType.getElementId(), csarDependencyLoader.buildDependencyBean(substitutionType.getArchiveName(), substitutionType.getArchiveVersion()));
for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getCapabilities()).values()) {
initializeSubstitutionTarget(loader, relationshipTypes, substitutionTarget);
}
for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getRequirements()).values()) {
initializeSubstitutionTarget(loader, relationshipTypes, substitutionTarget);
}
}
return loader;
}
use of org.alien4cloud.tosca.model.templates.PolicyTemplate in project alien4cloud by alien4cloud.
the class PolicyMatchingCandidateModifier method getPolicyTypes.
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;
}
use of org.alien4cloud.tosca.model.templates.PolicyTemplate in project alien4cloud by alien4cloud.
the class AddPolicyProcessor method process.
@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);
}
use of org.alien4cloud.tosca.model.templates.PolicyTemplate in project alien4cloud by alien4cloud.
the class TemplateBuilder method buildPolicyTemplate.
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;
}
Aggregations