use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class TopologyServiceCore method getIndexedRelationshipTypesFromTopology.
/**
* Get IndexedRelationshipType in a topology
*
* @param topology the topology to find all relationship types
* @param failOnTypeNotFound
* @return the map containing rel
*/
public Map<String, RelationshipType> getIndexedRelationshipTypesFromTopology(Topology topology, boolean failOnTypeNotFound) {
Map<String, RelationshipType> relationshipTypes = Maps.newHashMap();
if (topology.getNodeTemplates() == null) {
return relationshipTypes;
}
for (Map.Entry<String, NodeTemplate> templateEntry : topology.getNodeTemplates().entrySet()) {
NodeTemplate template = templateEntry.getValue();
if (template.getRelationships() != null) {
for (Map.Entry<String, RelationshipTemplate> relationshipEntry : template.getRelationships().entrySet()) {
RelationshipTemplate relationship = relationshipEntry.getValue();
if (!relationshipTypes.containsKey(relationship.getType())) {
RelationshipType relationshipType = getFromContextIfDefined(RelationshipType.class, relationship.getType(), topology.getDependencies(), failOnTypeNotFound);
relationshipTypes.put(relationship.getType(), relationshipType);
}
}
}
}
if (topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) {
for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getCapabilities()).values()) {
addRelationshipTypeFromSubstitutionTarget(topology, relationshipTypes, substitutionTarget, failOnTypeNotFound);
}
for (SubstitutionTarget substitutionTarget : safe(topology.getSubstitutionMapping().getRequirements()).values()) {
addRelationshipTypeFromSubstitutionTarget(topology, relationshipTypes, substitutionTarget, failOnTypeNotFound);
}
}
return relationshipTypes;
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class TopologyModifierSupport method addRelationshipTemplate.
protected RelationshipTemplate addRelationshipTemplate(Csar csar, Topology topology, NodeTemplate sourceNode, String targetNodeName, String relationshipTypeName, String requirementName, String capabilityName) {
AddRelationshipOperation addRelationshipOperation = new AddRelationshipOperation();
addRelationshipOperation.setNodeName(sourceNode.getName());
addRelationshipOperation.setTarget(targetNodeName);
RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTypeName);
addRelationshipOperation.setRelationshipType(relationshipType.getElementId());
addRelationshipOperation.setRelationshipVersion(relationshipType.getArchiveVersion());
addRelationshipOperation.setRequirementName(requirementName);
addRelationshipOperation.setTargetedCapabilityName(capabilityName);
String relationShipName = TopologyCompositionService.ensureNodeNameIsUnique(safe(sourceNode.getRelationships()).keySet(), sourceNode.getName() + "_" + targetNodeName, 0);
addRelationshipOperation.setRelationshipName(relationShipName);
addRelationshipProcessor.process(csar, topology, addRelationshipOperation);
return sourceNode.getRelationships().get(relationShipName);
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class ServiceResourceService method validateRelationshipTypes.
private void validateRelationshipTypes(ServiceResource serviceResource, final NodeType nodeType) {
safe(serviceResource.getCapabilitiesRelationshipTypes()).forEach((capabilityName, relationshipTypeId) -> {
RelationshipType relationshipType = toscaTypeSearchService.findByIdOrFail(RelationshipType.class, relationshipTypeId);
String[] validTargets = relationshipType.getValidTargets();
if (ArrayUtils.isNotEmpty(validTargets)) {
CapabilityDefinition capabilityDefinition = nodeType.getCapabilities().stream().filter(c -> c.getId().equals(capabilityName)).findFirst().get();
Csar csar = toscaTypeSearchService.getArchive(nodeType.getArchiveName(), nodeType.getArchiveVersion());
Set<CSARDependency> allDependencies = new HashSet<>(safe(csar.getDependencies()));
allDependencies.add(new CSARDependency(csar.getName(), csar.getVersion(), csar.getHash()));
CapabilityType capabilityType = toscaTypeSearchService.getElementInDependencies(CapabilityType.class, capabilityDefinition.getType(), allDependencies);
Set<String> allAcceptedTypes = new HashSet<>();
allAcceptedTypes.addAll(capabilityType.getDerivedFrom());
allAcceptedTypes.add(capabilityType.getElementId());
boolean isValid = false;
for (String validTarget : validTargets) {
if (allAcceptedTypes.contains(validTarget)) {
isValid = true;
break;
}
}
if (!isValid) {
throw new IncompatibleHalfRelationshipException("[" + relationshipType.getId() + "] is not compatible with [" + capabilityType.getId() + "]");
}
}
});
safe(serviceResource.getRequirementsRelationshipTypes()).forEach((k, v) -> {
toscaTypeSearchService.findByIdOrFail(RelationshipType.class, v);
});
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class ReplaceNodeProcessor method updateRelationshipsCapabilitiesRelationships.
private void updateRelationshipsCapabilitiesRelationships(Topology topology, NodeTemplate nodeTemplate) {
List<RelationshipEntry> targetRelationships = TopologyUtils.getTargetRelationships(nodeTemplate.getName(), topology.getNodeTemplates());
for (RelationshipEntry targetRelationshipEntry : targetRelationships) {
RelationshipTemplate targetRelationship = targetRelationshipEntry.getRelationship();
Capability capability = safe(nodeTemplate.getCapabilities()).get(targetRelationship.getTargetedCapabilityName());
if (capability == null || isCapabilityNotOfType(capability, targetRelationship.getRequirementType())) {
Entry<String, Capability> capabilityEntry = NodeTemplateUtils.getCapabilitEntryyByType(nodeTemplate, targetRelationship.getRequirementType());
targetRelationship.setTargetedCapabilityName(capabilityEntry.getKey());
// check that the relationship type is still valid with the new capability
RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, targetRelationship.getType());
if (!isValidRelationship(relationshipType, capabilityEntry.getValue())) {
NodeType sourceNodeType = ToscaContext.get(NodeType.class, targetRelationshipEntry.getSource().getType());
RequirementDefinition requirementDefinition = NodeTypeUtils.getRequirementById(sourceNodeType, targetRelationshipEntry.getRelationship().getRequirementName());
NodeType targetNodeType = ToscaContext.get(NodeType.class, nodeTemplate.getType());
CapabilityDefinition capabilityDefinition = NodeTypeUtils.getCapabilityById(targetNodeType, capabilityEntry.getKey());
RelationshipType validRelationshipType = danglingRequirementService.fetchValidRelationshipType(requirementDefinition, capabilityDefinition);
targetRelationship.setType(validRelationshipType.getElementId());
targetRelationship.setType(validRelationshipType.getElementId());
targetRelationship.setArtifacts(newLinkedHashMap(safe(validRelationshipType.getArtifacts())));
targetRelationship.setAttributes(newLinkedHashMap(safe(validRelationshipType.getAttributes())));
Map<String, AbstractPropertyValue> properties = new LinkedHashMap();
TemplateBuilder.fillProperties(properties, validRelationshipType.getProperties(), null);
targetRelationship.setProperties(properties);
}
}
}
}
use of org.alien4cloud.tosca.model.types.RelationshipType in project alien4cloud by alien4cloud.
the class SetRelationshipPropertyAsInputProcessor method processRelationshipOperation.
@Override
protected void processRelationshipOperation(Csar csar, Topology topology, SetRelationshipPropertyAsInputOperation operation, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate) {
PropertyDefinition inputPropertyDefinition = getOrFail(topology.getInputs(), operation.getInputName(), "Input {} not found in topology", operation.getInputName());
RelationshipType relationshipType = ToscaContext.get(RelationshipType.class, relationshipTemplate.getType());
PropertyDefinition relationshipPropertyDefinition = getOrFail(relationshipType.getProperties(), operation.getPropertyName(), "Property {} do not exist for relationship {} of node {}", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName());
// Check that the property definition of the input is indeed compatible with the property definition of the capability.
inputPropertyDefinition.checkIfCompatibleOrFail(relationshipPropertyDefinition);
FunctionPropertyValue getInput = new FunctionPropertyValue();
getInput.setFunction(ToscaFunctionConstants.GET_INPUT);
getInput.setParameters(Arrays.asList(operation.getInputName()));
relationshipTemplate.getProperties().put(operation.getPropertyName(), getInput);
log.debug("Associate the property [ {} ] of relationship template [ {} ] of node [ {} ] to an input of the topology [ {} ].", operation.getPropertyName(), operation.getRelationshipName(), operation.getNodeName(), topology.getId());
}
Aggregations