use of org.alien4cloud.tosca.model.types.PolicyType 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.types.PolicyType 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.types.PolicyType in project alien4cloud by alien4cloud.
the class UpdatePolicyPropertyValueProcessor method process.
@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());
}
}
use of org.alien4cloud.tosca.model.types.PolicyType 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.types.PolicyType in project alien4cloud by alien4cloud.
the class ToscaParserAlien200Test method testPolicyTypeParsing.
@Test
public void testPolicyTypeParsing() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-ALIEN14")).thenReturn(Mockito.mock(Csar.class));
PolicyType mockRoot = Mockito.mock(PolicyType.class);
Mockito.when(mockRoot.isAbstract()).thenReturn(true);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(PolicyType.class), Mockito.eq("tosca.policies.Root"), Mockito.any(Set.class))).thenReturn(mockRoot);
NodeType mockType = Mockito.mock(NodeType.class);
Mockito.when(mockType.isAbstract()).thenReturn(true);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockType);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-policy-type.yml"));
assertEquals(0, parsingResult.getContext().getParsingErrors().size());
ArchiveRoot archiveRoot = parsingResult.getResult();
assertNotNull(archiveRoot.getArchive());
assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
assertEquals(3, archiveRoot.getPolicyTypes().size());
PolicyType minPolicyType = archiveRoot.getPolicyTypes().get("org.alien4cloud.sample.MinimalPolicyType");
assertNotNull(minPolicyType);
assertEquals("org.alien4cloud.test.policies.PolicyTypes", minPolicyType.getArchiveName());
assertEquals("2.0.0-SNAPSHOT", minPolicyType.getArchiveVersion());
assertEquals("org.alien4cloud.sample.MinimalPolicyType", minPolicyType.getElementId());
assertEquals("This is a sample policy type with minimal definition", minPolicyType.getDescription());
assertEquals(1, minPolicyType.getDerivedFrom().size());
assertEquals("tosca.policies.Root", minPolicyType.getDerivedFrom().get(0));
PolicyType simplePolicyType = archiveRoot.getPolicyTypes().get("org.alien4cloud.sample.SimpleConditionPolicyType");
assertNotNull(simplePolicyType);
assertEquals("org.alien4cloud.test.policies.PolicyTypes", simplePolicyType.getArchiveName());
assertEquals("2.0.0-SNAPSHOT", simplePolicyType.getArchiveVersion());
assertEquals("org.alien4cloud.sample.SimpleConditionPolicyType", simplePolicyType.getElementId());
assertEquals("This is a sample policy type with simple definition", simplePolicyType.getDescription());
assertEquals(1, simplePolicyType.getDerivedFrom().size());
assertEquals("tosca.policies.Root", simplePolicyType.getDerivedFrom().get(0));
assertEquals(2, simplePolicyType.getTags().size());
assertEquals("sample_meta", simplePolicyType.getTags().get(0).getName());
assertEquals("a meta data", simplePolicyType.getTags().get(0).getValue());
assertEquals("anoter_meta", simplePolicyType.getTags().get(1).getName());
assertEquals("another meta data", simplePolicyType.getTags().get(1).getValue());
assertEquals(1, simplePolicyType.getProperties().size());
assertNotNull(simplePolicyType.getProperties().get("sample_property"));
assertEquals("string", simplePolicyType.getProperties().get("sample_property").getType());
assertEquals(2, simplePolicyType.getTargets().size());
assertTrue(simplePolicyType.getTargets().contains("tosca.nodes.Compute"));
assertTrue(simplePolicyType.getTargets().contains("tosca.nodes.Root"));
PolicyType policyType = archiveRoot.getPolicyTypes().get("org.alien4cloud.sample.PolicyType");
assertNotNull(policyType);
assertEquals("org.alien4cloud.test.policies.PolicyTypes", policyType.getArchiveName());
assertEquals("2.0.0-SNAPSHOT", policyType.getArchiveVersion());
assertEquals("org.alien4cloud.sample.PolicyType", policyType.getElementId());
assertEquals("This is a sample policy type", policyType.getDescription());
assertEquals(1, policyType.getDerivedFrom().size());
assertEquals("tosca.policies.Root", policyType.getDerivedFrom().get(0));
assertEquals(2, policyType.getTags().size());
assertEquals("sample_meta", policyType.getTags().get(0).getName());
assertEquals("a meta data", policyType.getTags().get(0).getValue());
assertEquals("anoter_meta", policyType.getTags().get(1).getName());
assertEquals("another meta data", policyType.getTags().get(1).getValue());
assertEquals(1, policyType.getProperties().size());
assertNotNull(policyType.getProperties().get("sample_property"));
assertEquals("string", policyType.getProperties().get("sample_property").getType());
assertEquals(2, policyType.getTargets().size());
assertTrue(simplePolicyType.getTargets().contains("tosca.nodes.Compute"));
assertTrue(simplePolicyType.getTargets().contains("tosca.nodes.Root"));
}
Aggregations