use of org.alien4cloud.tosca.model.types.PolicyType in project alien4cloud by alien4cloud.
the class SetPolicyPropertyAsSecretProcessor method process.
@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());
}
use of org.alien4cloud.tosca.model.types.PolicyType in project alien4cloud by alien4cloud.
the class ToscaParserAlien200Test method testPolicyTemplateParsing.
@Test
public void testPolicyTemplateParsing() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-ALIEN14")).thenReturn(Mockito.mock(Csar.class));
NodeType nodeType = new NodeType();
nodeType.setElementId("tosca.nodes.Root");
nodeType.setArchiveName("tosca-normative-types");
nodeType.setArchiveVersion("1.0.0-ALIEN14");
nodeType.setDerivedFrom(Lists.newArrayList("tosca.nodes.Root"));
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(nodeType);
nodeType = new NodeType();
nodeType.setElementId("tosca.nodes.Root");
nodeType.setArchiveName("tosca-normative-types");
nodeType.setArchiveVersion("1.0.0-ALIEN14");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(nodeType);
PolicyType policyType = Mockito.mock(PolicyType.class);
policyType.setElementId("tosca.nodes.Root");
policyType.setArchiveName("tosca-normative-types");
policyType.setArchiveVersion("1.0.0-ALIEN14");
Mockito.when(policyType.isAbstract()).thenReturn(true);
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(PolicyType.class), Mockito.eq("tosca.policies.Root"), Mockito.any(Set.class))).thenReturn(policyType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-policy-template.yml"));
assertEquals(0, parsingResult.getContext().getParsingErrors().size());
ArchiveRoot archiveRoot = parsingResult.getResult();
assertNotNull(archiveRoot.getArchive());
assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
assertEquals(1, archiveRoot.getPolicyTypes().size());
PolicyType simplePolicyType = archiveRoot.getPolicyTypes().get("org.alien4cloud.sample.SamplePolicy");
assertNotNull(simplePolicyType);
assertEquals("org.alien4cloud.sample.SamplePolicy", 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"));
// Test that the template is correctly parsed
assertNotNull(archiveRoot.getTopology());
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());
}
use of org.alien4cloud.tosca.model.types.PolicyType in project alien4cloud by alien4cloud.
the class ToscaParserAlien200Test method policyParsingWithUnknownTargetTypeShouldFail.
@Test
public void policyParsingWithUnknownTargetTypeShouldFail() 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);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-policy-type.yml"));
assertEquals(4, parsingResult.getContext().getParsingErrors().size());
}
use of org.alien4cloud.tosca.model.types.PolicyType in project alien4cloud by alien4cloud.
the class ToscaParserAlien200Test method policyTemplateParsingWithUnknownTargetShouldFail.
@Test
public void policyTemplateParsingWithUnknownTargetShouldFail() throws ParsingException {
Mockito.reset(csarRepositorySearchService);
Mockito.when(csarRepositorySearchService.getArchive("tosca-normative-types", "1.0.0-ALIEN14")).thenReturn(Mockito.mock(Csar.class));
NodeType nodeType = new NodeType();
nodeType.setElementId("tosca.nodes.Compute");
nodeType.setArchiveName("tosca-normative-types");
nodeType.setArchiveVersion("1.0.0-ALIEN14");
nodeType.setDerivedFrom(Lists.newArrayList("tosca.nodes.Root"));
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(nodeType);
nodeType = new NodeType();
nodeType.setElementId("tosca.nodes.Root");
nodeType.setArchiveName("tosca-normative-types");
nodeType.setArchiveVersion("1.0.0-ALIEN14");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(nodeType);
PolicyType policyType = new PolicyType();
policyType.setAbstract(true);
policyType.setElementId("org.alien4cloud.sample.SamplePolicy");
policyType.setArchiveName("org.alien4cloud.test.policies.PolicyTemplate");
policyType.setArchiveVersion("2.0.0-SNAPSHOT");
Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(PolicyType.class), Mockito.eq("org.alien4cloud.sample.SamplePolicy"), Mockito.any(Set.class))).thenReturn(policyType);
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-policy-template-fail.yml"));
assertEquals(1, parsingResult.getContext().getParsingErrors().size());
assertEquals(1, countErrorByLevelAndCode(parsingResult, ParsingErrorLevel.ERROR, ErrorCode.POLICY_TARGET_NOT_FOUND));
}
use of org.alien4cloud.tosca.model.types.PolicyType in project alien4cloud by alien4cloud.
the class PolicyTemplatePostProcessor method process.
@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) {
// error managed by the reference post processor.
return;
}
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());
}
Aggregations