Search in sources :

Example 1 with PolicyType

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());
}
Also used : PolicyType(org.alien4cloud.tosca.model.types.PolicyType) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)

Example 2 with PolicyType

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());
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) PolicyType(org.alien4cloud.tosca.model.types.PolicyType) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) Test(org.junit.Test)

Example 3 with PolicyType

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());
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) PolicyType(org.alien4cloud.tosca.model.types.PolicyType) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) Test(org.junit.Test)

Example 4 with PolicyType

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));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) PolicyType(org.alien4cloud.tosca.model.types.PolicyType) Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) Test(org.junit.Test)

Example 5 with PolicyType

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());
}
Also used : PolicyType(org.alien4cloud.tosca.model.types.PolicyType) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) Node(org.yaml.snakeyaml.nodes.Node) Topology(org.alien4cloud.tosca.model.templates.Topology) PolicyTemplate(org.alien4cloud.tosca.model.templates.PolicyTemplate)

Aggregations

PolicyType (org.alien4cloud.tosca.model.types.PolicyType)15 PolicyTemplate (org.alien4cloud.tosca.model.templates.PolicyTemplate)7 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)5 Set (java.util.Set)5 Csar (org.alien4cloud.tosca.model.Csar)4 NodeType (org.alien4cloud.tosca.model.types.NodeType)4 Test (org.junit.Test)4 PolicyLocationResourceTemplate (alien4cloud.model.orchestrators.locations.PolicyLocationResourceTemplate)3 List (java.util.List)2 Topology (org.alien4cloud.tosca.model.templates.Topology)2 ILocationMatch (alien4cloud.model.deployment.matching.ILocationMatch)1 Location (alien4cloud.model.orchestrators.locations.Location)1 LocationResourceTemplateWithDependencies (alien4cloud.model.orchestrators.locations.LocationResourceTemplateWithDependencies)1 LocationResources (alien4cloud.model.orchestrators.locations.LocationResources)1 MissingPluginException (alien4cloud.plugin.exception.MissingPluginException)1 ToscaTypeLoader (alien4cloud.tosca.container.ToscaTypeLoader)1 ToscaContext (alien4cloud.tosca.context.ToscaContext)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 AlienUtils.safe (alien4cloud.utils.AlienUtils.safe)1 TagUtil (alien4cloud.utils.TagUtil)1