use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien120Test method testDataTypesVeryComplexWithDefault.
@Test
public void testDataTypesVeryComplexWithDefault() throws ParsingException {
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-very-complex-default.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());
NodeType commandType = parsingResult.getResult().getNodeTypes().get("alien.test.Command");
Assert.assertNotNull(commandType);
PropertyDefinition pd = commandType.getProperties().get("customer");
Assert.assertNotNull(pd);
// check the default value
Object defaultValue = pd.getDefault();
Assert.assertNotNull(defaultValue);
Assert.assertTrue(defaultValue instanceof ComplexPropertyValue);
ComplexPropertyValue cpv = (ComplexPropertyValue) defaultValue;
Map<String, Object> valueAsMap = cpv.getValue();
Assert.assertNotNull(valueAsMap);
Assert.assertTrue(valueAsMap.containsKey("first_name"));
Assert.assertEquals("Foo", valueAsMap.get("first_name"));
Assert.assertTrue(valueAsMap.containsKey("last_name"));
Assert.assertEquals("Bar", valueAsMap.get("last_name"));
Assert.assertTrue(valueAsMap.containsKey("address"));
Object addressObj = valueAsMap.get("address");
Assert.assertNotNull(addressObj);
Assert.assertTrue(addressObj instanceof Map);
Map<String, Object> addressMap = (Map<String, Object>) addressObj;
Assert.assertTrue(addressMap.containsKey("street_name"));
Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
Assert.assertTrue(addressMap.containsKey("zipcode"));
Assert.assertEquals("92130", addressMap.get("zipcode"));
Assert.assertTrue(addressMap.containsKey("city_name"));
Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
Assert.assertTrue(valueAsMap.containsKey("emails"));
Object emailsObj = valueAsMap.get("emails");
Assert.assertNotNull(emailsObj);
Assert.assertTrue(emailsObj instanceof List);
List<Object> emailsList = (List<Object>) emailsObj;
Assert.assertEquals(2, emailsList.size());
Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
Object accountsObj = valueAsMap.get("accounts");
Assert.assertNotNull(accountsObj);
Assert.assertTrue(accountsObj instanceof Map);
Map<String, Object> accountsMap = (Map<String, Object>) accountsObj;
Assert.assertEquals(2, accountsMap.size());
Assert.assertTrue(accountsMap.containsKey("main"));
Assert.assertEquals("root", accountsMap.get("main"));
Assert.assertTrue(accountsMap.containsKey("secondary"));
Assert.assertEquals("user", accountsMap.get("secondary"));
Assert.assertEquals(1, parsingResult.getResult().getTopology().getNodeTemplates().size());
NodeTemplate nodeTemplate = parsingResult.getResult().getTopology().getNodeTemplates().values().iterator().next();
// on the node, the default value should be set
Assert.assertNotNull(nodeTemplate.getProperties());
Assert.assertTrue(nodeTemplate.getProperties().containsKey("customer"));
AbstractPropertyValue apv = nodeTemplate.getProperties().get("customer");
Assert.assertNotNull(apv);
Assert.assertTrue(apv instanceof ComplexPropertyValue);
cpv = (ComplexPropertyValue) apv;
valueAsMap = cpv.getValue();
Assert.assertNotNull(valueAsMap);
Assert.assertTrue(valueAsMap.containsKey("first_name"));
Assert.assertEquals("Foo", valueAsMap.get("first_name"));
Assert.assertTrue(valueAsMap.containsKey("last_name"));
Assert.assertEquals("Bar", valueAsMap.get("last_name"));
Assert.assertTrue(valueAsMap.containsKey("address"));
addressObj = valueAsMap.get("address");
Assert.assertNotNull(addressObj);
Assert.assertTrue(addressObj instanceof Map);
addressMap = (Map<String, Object>) addressObj;
Assert.assertTrue(addressMap.containsKey("street_name"));
Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
Assert.assertTrue(addressMap.containsKey("zipcode"));
Assert.assertEquals("92130", addressMap.get("zipcode"));
Assert.assertTrue(addressMap.containsKey("city_name"));
Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
Assert.assertTrue(valueAsMap.containsKey("emails"));
emailsObj = valueAsMap.get("emails");
Assert.assertNotNull(emailsObj);
Assert.assertTrue(emailsObj instanceof List);
emailsList = (List<Object>) emailsObj;
Assert.assertEquals(2, emailsList.size());
Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
accountsObj = valueAsMap.get("accounts");
Assert.assertNotNull(accountsObj);
Assert.assertTrue(accountsObj instanceof Map);
accountsMap = (Map<String, Object>) accountsObj;
Assert.assertEquals(2, accountsMap.size());
Assert.assertTrue(accountsMap.containsKey("main"));
Assert.assertEquals("root", accountsMap.get("main"));
Assert.assertTrue(accountsMap.containsKey("secondary"));
Assert.assertEquals("user", accountsMap.get("secondary"));
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class PropertyUtil method merge.
/**
* Merge the map of two node properties recursively.
* @param source the source map
* @param target the target map
* @param overrideNull a boolean value for override the null value of target
* @param untouched the list of unmodified key //TODO not used and it needs to be reorganised
* @return a merged map
*/
public static Map<String, AbstractPropertyValue> merge(Map<String, AbstractPropertyValue> source, Map<String, AbstractPropertyValue> target, boolean overrideNull, Set<String> untouched) {
if (target == null || target.isEmpty()) {
return source;
}
for (Map.Entry<String, AbstractPropertyValue> entry : safe(source).entrySet()) {
String sourceKey = entry.getKey();
AbstractPropertyValue sourceValue = entry.getValue();
AbstractPropertyValue targetValue = target.get(sourceKey);
if ((overrideNull && targetValue == null) || !target.containsKey(sourceKey)) {
target.put(sourceKey, sourceValue);
} else if (target.containsKey(sourceKey) && targetValue != null) {
if (sourceValue instanceof ComplexPropertyValue) {
Map<String, Object> mergedMap = mergeMap(((ComplexPropertyValue) sourceValue).getValue(), ((ComplexPropertyValue) targetValue).getValue(), overrideNull, untouched);
target.put(sourceKey, new ComplexPropertyValue(mergedMap));
} else if (sourceValue instanceof Map) {
Map<String, Object> mergedMap = mergeMap((Map<String, Object>) sourceValue, (Map<String, Object>) targetValue, overrideNull, untouched);
target.put(sourceKey, (AbstractPropertyValue) mergedMap);
}
}
}
return target;
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class DanglingRequirementService method addDanglingNode.
private NodeTemplate addDanglingNode(Topology topology, TopologyContext topologyContext, NodeTemplate nodeTemplate, RequirementDefinition requirementDefinition, NodeType danglingNodeType, RelationshipType danglingRelationshipType, CapabilityDefinition targetCapabilityDefinition) {
NodeTemplate danglingTemplate = TemplateBuilder.buildNodeTemplate(danglingNodeType);
// Add the filter as defined in the node type.
danglingTemplate.setNodeFilter(requirementDefinition.getNodeFilter());
// generate the dangling template name
String danglingTemplateName = TopologyUtils.getNexAvailableName(nodeTemplate.getName() + "_" + requirementDefinition.getId(), "_", topology.getNodeTemplates().keySet());
danglingTemplate.setName(danglingTemplateName);
topology.getNodeTemplates().put(danglingTemplateName, danglingTemplate);
workflowsBuilderService.addNode(topologyContext, danglingTemplateName);
// Add the dangling requirement relationship
if (nodeTemplate.getRelationships() == null) {
nodeTemplate.setRelationships(Maps.newHashMap());
}
String danglingRelationshipTemplateName = TopologyUtils.getNexAvailableName(nodeTemplate.getName() + "_" + requirementDefinition.getId(), "_", nodeTemplate.getRelationships().keySet());
RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
relationshipTemplate.setName(danglingRelationshipTemplateName);
relationshipTemplate.setTarget(danglingTemplateName);
String targetCapabilityName = targetCapabilityDefinition == null ? null : targetCapabilityDefinition.getId();
relationshipTemplate.setTargetedCapabilityName(targetCapabilityName);
relationshipTemplate.setRequirementName(requirementDefinition.getId());
relationshipTemplate.setRequirementType(requirementDefinition.getType());
relationshipTemplate.setType(danglingRelationshipType.getElementId());
relationshipTemplate.setArtifacts(newLinkedHashMap(safe(danglingRelationshipType.getArtifacts())));
relationshipTemplate.setAttributes(newLinkedHashMap(safe(danglingRelationshipType.getAttributes())));
Map<String, AbstractPropertyValue> properties = new LinkedHashMap();
TemplateBuilder.fillProperties(properties, danglingRelationshipType.getProperties(), null);
relationshipTemplate.setProperties(properties);
nodeTemplate.getRelationships().put(danglingRelationshipTemplateName, relationshipTemplate);
workflowsBuilderService.addRelationship(topologyContext, nodeTemplate.getName(), danglingRelationshipTemplateName);
// TODO remove this workaround as soon as matchin leverages
setPropertiesFromFilter(danglingTemplate, danglingNodeType);
return danglingTemplate;
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class PropertyValueChecker method checkProperties.
/**
* Check that the value of a property has the right type and match constraints.
*
* @param type The type that defines the properties (NodeType, CapabilityType, RequirementType).
* @param propertyValues The map of values.
* @param templateName The name of the node template /capability template / requirement template.
*/
public void checkProperties(final AbstractInheritableToscaType type, final Map<String, AbstractPropertyValue> propertyValues, final String templateName) {
if (type == null) {
// if the type is null we cannot check properties against their definition. Error is managed elsewhere.
return;
}
ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
Topology topology = archiveRoot.getTopology();
for (Map.Entry<String, AbstractPropertyValue> propertyEntry : safe(propertyValues).entrySet()) {
String propertyName = propertyEntry.getKey();
AbstractPropertyValue propertyValue = propertyEntry.getValue();
Node propertyValueNode = ParsingContextExecution.getObjectToNodeMap().get(propertyValue);
if (type.getProperties() == null || !type.getProperties().containsKey(propertyName)) {
ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.UNRECOGNIZED_PROPERTY, templateName, propertyValueNode.getStartMark(), "Property " + propertyName + " does not exist in type " + type.getElementId(), propertyValueNode.getEndMark(), propertyName));
continue;
}
PropertyDefinition propertyDefinition = type.getProperties().get(propertyName);
checkProperty(propertyName, propertyValueNode, propertyValue, propertyDefinition, topology.getInputs(), templateName);
}
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class TemplateBuilder method fillRequirementsMap.
private static void fillRequirementsMap(Map<String, Requirement> map, List<RequirementDefinition> elements, Map<String, Requirement> mapToMerge, boolean adaptToType) {
if (elements == null) {
return;
}
for (RequirementDefinition requirement : elements) {
Requirement toAddRequirement = MapUtils.getObject(mapToMerge, requirement.getId());
// the type of a requirement is a capability type in TOSCA as they match each other.
CapabilityType requirementType = ToscaContext.get(CapabilityType.class, requirement.getType());
if (toAddRequirement == null || !Objects.equals(toAddRequirement.getType(), requirement.getType())) {
toAddRequirement = new Requirement();
toAddRequirement.setType(requirement.getType());
}
Map<String, AbstractPropertyValue> properties = Maps.newLinkedHashMap();
fillProperties(properties, requirementType != null ? requirementType.getProperties() : null, toAddRequirement.getProperties(), adaptToType);
toAddRequirement.setProperties(properties);
map.put(requirement.getId(), toAddRequirement);
}
}
Aggregations