Search in sources :

Example 11 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class ToscaSerializerTest method simpleTest.

@Ignore
@Test
public void simpleTest() throws IOException, URISyntaxException {
    Topology topology = new Topology();
    topology.setDependencies(new HashSet<CSARDependency>());
    topology.getDependencies().add(new CSARDependency("name1", "1.0"));
    topology.getDependencies().add(new CSARDependency("name2", "2.0"));
    topology.setInputs(new HashMap<String, PropertyDefinition>());
    PropertyDefinition pd1 = new PropertyDefinition();
    pd1.setType("string");
    pd1.setConstraints(getConstraintList());
    pd1.setDescription("A description");
    topology.getInputs().put("input1", pd1);
    PropertyDefinition pd2 = new PropertyDefinition();
    pd2.setType("integer");
    pd2.setRequired(false);
    pd2.setDefault(new ScalarPropertyValue("10"));
    topology.getInputs().put("input2", pd2);
    PropertyDefinition pd3 = new PropertyDefinition();
    pd3.setType("map");
    pd3.setRequired(false);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType("integer");
    pd3.setEntrySchema(entrySchema);
    topology.getInputs().put("input3", pd3);
    topology.setNodeTemplates(new HashMap<String, NodeTemplate>());
    topology.getNodeTemplates().put("node1", new NodeTemplate());
    topology.getNodeTemplates().get("node1").setType("the.node.Type");
    topology.getNodeTemplates().get("node1").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setRelationships(new HashMap<String, RelationshipTemplate>());
    topology.getNodeTemplates().get("node1").getRelationships().put("hostedOn", new RelationshipTemplate());
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setTarget("compute2");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementType("capabilities.Capa");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setRequirementName("host");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setType("relationship.Rel");
    topology.getNodeTemplates().get("node1").getRelationships().get("hostedOn").setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").setCapabilities(new HashMap<String, Capability>());
    Capability capability = new Capability();
    capability.setProperties(buildSamplePropertyValueMap());
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa1", capability);
    // this capability should not appear
    topology.getNodeTemplates().get("node1").getCapabilities().put("capa2", new Capability());
    topology.getNodeTemplates().get("node1").setArtifacts(new HashMap<String, DeploymentArtifact>());
    DeploymentArtifact da = new DeploymentArtifact();
    da.setArtifactName("artifact.war");
    da.setArtifactRef("010203904872876723");
    da.setArtifactType("artifacttypes.Artifact");
    topology.getNodeTemplates().get("node1").getArtifacts().put("artifact1", da);
    topology.setOutputProperties(new HashMap<String, Set<String>>());
    topology.getOutputProperties().put("node1", Sets.newHashSet("prop1", "prop2"));
    topology.setOutputAttributes(new HashMap<String, Set<String>>());
    topology.getOutputAttributes().put("node1", Sets.newHashSet("att1", "att2"));
    Map<String, Object> velocityCtx = new HashMap<String, Object>();
    velocityCtx.put("topology", topology);
    velocityCtx.put("template_name", "template-id");
    velocityCtx.put("template_version", "1.0.0-SNAPSHOT");
    velocityCtx.put("template_author", "Foo Bar");
    velocityCtx.put("application_description", "Here is a \nmultiline description");
    StringWriter writer = new StringWriter();
    VelocityUtil.generate("org/alien4cloud/tosca/exporter/topology-alien_dsl_1_4_0.yml.vm", writer, velocityCtx);
    System.out.println(writer.toString());
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) HashMap(java.util.HashMap) Topology(org.alien4cloud.tosca.model.templates.Topology) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) StringWriter(java.io.StringWriter) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class AntiAffinityModifier method apply.

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) {
        // Some targets are not instances of org.alien4cloud.nodes.mock.aws.Compute
        return;
    }
    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));
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 13 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class FunctionEvaluator method parseAttribute.

/**
 * Parse an attribute value that can be : {@link ConcatPropertyValue} / {@link AttributeDefinition}
 *
 * @param attributeId
 * @param attributeValue
 * @param topology
 * @param runtimeInformations
 * @param currentInstance
 * @param basePaaSTemplate
 * @param builtPaaSTemplates
 * @return
 */
public static String parseAttribute(String attributeId, IValue attributeValue, Topology topology, Map<String, Map<String, InstanceInformation>> runtimeInformations, String currentInstance, IPaaSTemplate<? extends AbstractToscaType> basePaaSTemplate, Map<String, PaaSNodeTemplate> builtPaaSTemplates) {
    if (attributeValue == null) {
        return null;
    }
    // handle AttributeDefinition type
    if (attributeValue instanceof AttributeDefinition) {
        String runtimeAttributeValue = extractRuntimeInformationAttribute(runtimeInformations, currentInstance, Lists.newArrayList(basePaaSTemplate), attributeId);
        if (runtimeAttributeValue != null) {
            if (!runtimeAttributeValue.contains("=Error!]") && !runtimeAttributeValue.equals("")) {
                return runtimeAttributeValue;
            }
        }
        return ((AttributeDefinition) attributeValue).getDefault();
    }
    // handle concat function
    if (attributeValue instanceof ConcatPropertyValue) {
        StringBuilder evaluatedAttribute = new StringBuilder();
        ConcatPropertyValue concatPropertyValue = (ConcatPropertyValue) attributeValue;
        for (IValue concatParam : concatPropertyValue.getParameters()) {
            // scalar type
            if (concatParam instanceof ScalarPropertyValue) {
                // scalar case
                evaluatedAttribute.append(((ScalarPropertyValue) concatParam).getValue());
            } else if (concatParam instanceof PropertyDefinition) {
                // Definition case
                // TODO : ?? what should i do here ?? currently returns default value in the definition
                evaluatedAttribute.append(((PropertyDefinition) concatParam).getDefault());
            } else if (concatParam instanceof FunctionPropertyValue) {
                // Function case
                FunctionPropertyValue functionPropertyValue = (FunctionPropertyValue) concatParam;
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, functionPropertyValue.getTemplateName(), builtPaaSTemplates);
                switch(functionPropertyValue.getFunction()) {
                    case ToscaFunctionConstants.GET_ATTRIBUTE:
                        evaluatedAttribute.append(extractRuntimeInformationAttribute(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue.getElementNameToFetch()));
                        break;
                    case ToscaFunctionConstants.GET_PROPERTY:
                        evaluatedAttribute.append(extractRuntimeInformationProperty(topology, functionPropertyValue.getElementNameToFetch(), paasTemplates));
                        break;
                    case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                        String defaultValue = "<" + functionPropertyValue.getElementNameToFetch() + ">";
                        evaluatedAttribute.append(extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, functionPropertyValue, defaultValue));
                        break;
                    default:
                        log.warn("Function [{}] is not yet handled in concat operation.", functionPropertyValue.getFunction());
                        break;
                }
            }
        }
        return evaluatedAttribute.toString();
    }
    // handle functions. For now, only support Get_OPERATION_OUTPUT on attributes scope
    if (attributeValue instanceof FunctionPropertyValue) {
        FunctionPropertyValue function = (FunctionPropertyValue) attributeValue;
        switch(function.getFunction()) {
            case ToscaFunctionConstants.GET_OPERATION_OUTPUT:
                List<? extends IPaaSTemplate> paasTemplates = getPaaSTemplatesFromKeyword(basePaaSTemplate, function.getTemplateName(), builtPaaSTemplates);
                return extractRuntimeInformationOperationOutput(runtimeInformations, currentInstance, paasTemplates, function, null);
            default:
                return null;
        }
    }
    return null;
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) AttributeDefinition(org.alien4cloud.tosca.model.definitions.AttributeDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 14 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class FunctionEvaluator method getPropertyValue.

private static AbstractPropertyValue getPropertyValue(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertyDefinitions, String propertyAccessPath) {
    if (properties == null || !properties.containsKey(propertyAccessPath)) {
        String propertyName = PropertyUtil.getPropertyNameFromComplexPath(propertyAccessPath);
        if (propertyName == null) {
            // Non complex
            return PropertyUtil.getDefaultFromPropertyDefinitions(propertyAccessPath, propertyDefinitions);
        } else {
            // Complex
            PropertyDefinition propertyDefinition = propertyDefinitions.get(propertyName);
            AbstractPropertyValue rawValue;
            if (propertyDefinition == null) {
                return null;
            } else if (ToscaTypes.isSimple(propertyDefinition.getType())) {
                // It's a complex path (with '.') but the type in definition is finally simple
                return null;
            } else if (properties != null && (rawValue = properties.get(propertyName)) != null) {
                if (!(rawValue instanceof PropertyValue)) {
                    throw new NotSupportedException("Only support static value in a get_property");
                }
                Object value = MapUtil.get(((PropertyValue) rawValue).getValue(), propertyAccessPath.substring(propertyName.length() + 1));
                return new ScalarPropertyValue(PropertyUtil.serializePropertyValue(value));
            } else {
                return null;
            }
        }
    } else {
        return properties.get(propertyAccessPath);
    }
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) NotSupportedException(alien4cloud.paas.exception.NotSupportedException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 15 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class ToscaParserSimpleProfileAlien120Test method testDataTypesExtendsNative.

@Test
public void testDataTypesExtendsNative() throws ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-extends-native.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    Assert.assertEquals(3, parsingResult.getResult().getDataTypes().size());
    Assert.assertEquals(2, parsingResult.getResult().getNodeTypes().size());
    Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
    Assert.assertEquals(1, parsingResult.getResult().getTopology().getNodeTemplates().size());
    NodeTemplate nodeTemplate = parsingResult.getResult().getTopology().getNodeTemplates().values().iterator().next();
    Assert.assertEquals(3, nodeTemplate.getProperties().size());
    // check url property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("url"));
    AbstractPropertyValue url = nodeTemplate.getProperties().get("url");
    Assert.assertTrue(url instanceof ScalarPropertyValue);
    Assert.assertEquals("https://kikoo.com", ((ScalarPropertyValue) url).getValue());
    // check ipv6_addresses property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("ipv6_addresses"));
    AbstractPropertyValue ipv6_addresses = nodeTemplate.getProperties().get("ipv6_addresses");
    Assert.assertTrue(ipv6_addresses instanceof ListPropertyValue);
    List<Object> ipv6_addresses_list = ((ListPropertyValue) ipv6_addresses).getValue();
    Assert.assertEquals(2, ipv6_addresses_list.size());
    Assert.assertEquals("192.168.0.10", ipv6_addresses_list.get(0));
    Assert.assertEquals("10.0.0.10", ipv6_addresses_list.get(1));
    // check passwords property
    Assert.assertTrue(nodeTemplate.getProperties().containsKey("passwords"));
    AbstractPropertyValue passwords = nodeTemplate.getProperties().get("passwords");
    Assert.assertTrue(passwords instanceof ComplexPropertyValue);
    Map<String, Object> passwords_map = ((ComplexPropertyValue) passwords).getValue();
    Assert.assertEquals(2, passwords_map.size());
    Assert.assertEquals("123456789", passwords_map.get("user1"));
    Assert.assertEquals("abcdefghij", passwords_map.get("user2"));
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Aggregations

ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)15 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)14 Test (org.junit.Test)13 Map (java.util.Map)10 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)7 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)7 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Capability (org.alien4cloud.tosca.model.templates.Capability)4