use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class ReplaceNodeProcessor method removeTheRelationshipsOnSource.
/**
* If the node to replace is an target of relationship, we should swapped the relationship on source and clean used workflow steps
*/
private Map<String, Map<String, RelationshipTemplate>> removeTheRelationshipsOnSource(Topology topology, Csar csar, String nodeToReplace) {
Map<String, Map<String, RelationshipTemplate>> relationshipDeleted = Maps.newLinkedHashMap();
for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
if (!nodeToReplace.equals(nodeTemplate.getName())) {
List<String> relationshipsToRemove = Lists.newArrayList();
for (RelationshipTemplate relationshipTemplate : safe(nodeTemplate.getRelationships()).values()) {
if (nodeToReplace.equals(relationshipTemplate.getTarget())) {
relationshipsToRemove.add(relationshipTemplate.getName());
if (!relationshipDeleted.containsKey(relationshipTemplate.getName())) {
Map<String, RelationshipTemplate> relationships = Maps.newHashMap();
// we save the RelationshipTemplate, so we will preserve the current properties of this relationship
relationshipDeleted.put(nodeTemplate.getName(), relationships);
}
relationshipDeleted.get(nodeTemplate.getName()).put(relationshipTemplate.getName(), relationshipTemplate);
// we should remove this relationship to maintain a great a workflow
workflowBuilderService.removeRelationship(topology, csar, nodeTemplate.getName(), relationshipTemplate.getName(), relationshipTemplate);
}
}
}
}
return relationshipDeleted;
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class DuplicateNodeProcessor method copyAndCleanRelationships.
/**
* Discard all relationship targeting an "external" node. External here in terms of the hostedOn hierarchy
* Copy the valid ones
*
* @param nodeName
* @param validTargets A map of oldNodeName -> duplicatedNodeName, we should keep relationships targeting one of these nodes.
*/
private void copyAndCleanRelationships(String nodeName, Map<String, String> validTargets, Map<String, NodeTemplate> nodeTemplates, TopologyContext topologyContext) {
NodeTemplate nodeTemplate = nodeTemplates.get(nodeName);
if (MapUtils.isNotEmpty(nodeTemplate.getRelationships())) {
Map<String, RelationshipTemplate> relationships = nodeTemplate.getRelationships();
Set<String> keys = Sets.newHashSet(relationships.keySet());
for (String key : keys) {
RelationshipTemplate rel = relationships.remove(key);
// If so, then rename it, its target and keep it
if (validTargets.containsKey(rel.getTarget())) {
rel.setName(copyName(rel.getName(), relationships.keySet()));
rel.setTarget(validTargets.get(rel.getTarget()));
relationships.put(rel.getName(), rel);
workflowBuilderService.addRelationship(topologyContext, nodeName, rel.getName());
}
}
if (relationships.isEmpty()) {
nodeTemplate.setRelationships(null);
}
}
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class AddRelationshipProcessor method processNodeOperation.
@Override
protected void processNodeOperation(Csar csar, Topology topology, AddRelationshipOperation operation, NodeTemplate sourceNode) {
if (operation.getRelationshipName() == null || operation.getRelationshipName().isEmpty()) {
throw new InvalidNameException("relationshipName", operation.getRelationshipName(), "Not null or empty");
}
if (AlienUtils.safe(sourceNode.getRelationships()).containsKey(operation.getRelationshipName())) {
throw new AlreadyExistException("Relationship " + operation.getRelationshipName() + " already exist on node " + operation.getNodeName());
}
if (sourceNode.getRequirements() == null || sourceNode.getRequirements().get(operation.getRequirementName()) == null) {
throw new NotFoundException("Unable to find requirement with name <" + operation.getRequirementName() + "> on the source node" + operation.getNodeName());
}
Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
// ensure that the target node exists
TopologyUtils.getNodeTemplate(topology.getId(), operation.getTarget(), nodeTemplates);
// We don't use the tosca context as the relationship type may not be in dependencies yet (that's why we use the load type below).
RelationshipType indexedRelationshipType = toscaTypeSearchService.find(RelationshipType.class, operation.getRelationshipType(), operation.getRelationshipVersion());
if (indexedRelationshipType == null) {
throw new NotFoundException(RelationshipType.class.getName(), operation.getRelationshipType() + ":" + operation.getRelationshipVersion(), "Unable to find relationship type to create template in topology.");
}
boolean upperBoundReachedSource = topologyRequirementBoundsValidationServices.isRequirementUpperBoundReachedForSource(sourceNode, operation.getRequirementName(), topology.getDependencies());
if (upperBoundReachedSource) {
// throw exception here
throw new RequirementBoundException(operation.getNodeName(), operation.getRequirementName());
}
boolean upperBoundReachedTarget = topologyCapabilityBoundsValidationServices.isCapabilityUpperBoundReachedForTarget(operation.getTarget(), nodeTemplates, operation.getTargetedCapabilityName(), topology.getDependencies());
// return with a rest response error
if (upperBoundReachedTarget) {
throw new CapabilityBoundException(operation.getTarget(), operation.getTargetedCapabilityName());
}
topologyService.loadType(topology, indexedRelationshipType);
NodeTemplate newSourceNode = topology.getNodeTemplates().get(sourceNode.getName());
if (sourceNode != newSourceNode) {
// topology has been reloaded
sourceNode = newSourceNode;
}
Map<String, RelationshipTemplate> relationships = sourceNode.getRelationships();
if (relationships == null) {
relationships = Maps.newHashMap();
sourceNode.setRelationships(relationships);
}
RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
relationshipTemplate.setName(operation.getRelationshipName());
relationshipTemplate.setTarget(operation.getTarget());
relationshipTemplate.setTargetedCapabilityName(operation.getTargetedCapabilityName());
relationshipTemplate.setRequirementName(operation.getRequirementName());
relationshipTemplate.setRequirementType(sourceNode.getRequirements().get(operation.getRequirementName()).getType());
relationshipTemplate.setType(indexedRelationshipType.getElementId());
relationshipTemplate.setArtifacts(newLinkedHashMap(indexedRelationshipType.getArtifacts()));
relationshipTemplate.setAttributes(newLinkedHashMap(indexedRelationshipType.getAttributes()));
Map<String, AbstractPropertyValue> properties = new LinkedHashMap<String, AbstractPropertyValue>();
TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), null);
relationshipTemplate.setProperties(properties);
relationships.put(operation.getRelationshipName(), relationshipTemplate);
TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
workflowBuilderService.addRelationship(topologyContext, operation.getNodeName(), operation.getRelationshipName());
log.debug("Added relationship to the topology [" + topology.getId() + "], node name [" + operation.getNodeName() + "], relationship name [" + operation.getRelationshipName() + "]");
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class NodeFilterValidationService method validateFiltersForNode.
private void validateFiltersForNode(NodeType sourceNodeType, Map<String, RelationshipTemplate> relationshipsMap, Topology topology, Map<String, NodeType> nodeTypes, Map<String, CapabilityType> capabilityTypes, NodeFiltersTask task, boolean skipInputs) {
Map<String, RequirementDefinition> requirementDefinitionMap = getRequirementsAsMap(sourceNodeType);
for (Map.Entry<String, RelationshipTemplate> relationshipEntry : relationshipsMap.entrySet()) {
RequirementDefinition requirementDefinition = requirementDefinitionMap.get(relationshipEntry.getValue().getRequirementName());
NodeFilter nodeFilter = requirementDefinition.getNodeFilter();
if (nodeFilter != null) {
NodeTemplate targetNode = topology.getNodeTemplates().get(relationshipEntry.getValue().getTarget());
NodeType targetType = nodeTypes.get(relationshipEntry.getValue().getTarget());
NodeFilterToSatisfy nodeFilterToSatisfy = new NodeFilterToSatisfy();
nodeFilterToSatisfy.setRelationshipName(relationshipEntry.getKey());
nodeFilterToSatisfy.setTargetName(targetNode.getName());
nodeFilterToSatisfy.setMissingCapabilities(Lists.<String>newArrayList());
nodeFilterToSatisfy.setViolations(Lists.<Violations>newArrayList());
validateNodeFilter(nodeFilter, targetNode, targetType, capabilityTypes, nodeFilterToSatisfy, skipInputs);
if (!nodeFilterToSatisfy.getViolations().isEmpty() || !nodeFilterToSatisfy.getMissingCapabilities().isEmpty()) {
task.getNodeFiltersToSatisfy().add(nodeFilterToSatisfy);
}
}
}
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class TopologyPropertiesValidationService method validateNodeTemplate.
public void validateNodeTemplate(List<PropertiesTask> toReturnTaskList, NodeType relatedIndexedNodeType, NodeTemplate nodeTemplate, String nodeTempalteName, boolean skipInputProperties) {
// Define a task regarding properties
PropertiesTask task = new PropertiesTask();
task.setNodeTemplateName(nodeTempalteName);
task.setComponent(relatedIndexedNodeType);
task.setCode(TaskCode.PROPERTIES);
task.setProperties(Maps.newHashMap());
// Check the properties of node template
if (MapUtils.isNotEmpty(nodeTemplate.getProperties())) {
addRequiredPropertyIdToTaskProperties(null, nodeTemplate.getProperties(), relatedIndexedNodeType.getProperties(), task, skipInputProperties);
}
// Check relationships PD
for (Map.Entry<String, RelationshipTemplate> relationshipEntry : safe(nodeTemplate.getRelationships()).entrySet()) {
RelationshipTemplate relationship = relationshipEntry.getValue();
if (relationship.getProperties() == null || relationship.getProperties().isEmpty()) {
continue;
}
addRequiredPropertyIdToTaskProperties("relationships[" + relationshipEntry.getKey() + "]", relationship.getProperties(), safe(ToscaContext.getOrFail(RelationshipType.class, relationshipEntry.getValue().getType()).getProperties()), task, skipInputProperties);
}
for (Map.Entry<String, Capability> capabilityEntry : safe(nodeTemplate.getCapabilities()).entrySet()) {
Capability capability = capabilityEntry.getValue();
if (capability.getProperties() == null || capability.getProperties().isEmpty()) {
continue;
}
addRequiredPropertyIdToTaskProperties("capabilities[" + capabilityEntry.getKey() + "]", capability.getProperties(), safe(ToscaContext.getOrFail(CapabilityType.class, capabilityEntry.getValue().getType()).getProperties()), task, skipInputProperties);
if (capability.getType().equals(NormativeCapabilityTypes.SCALABLE)) {
Map<String, AbstractPropertyValue> scalableProperties = capability.getProperties();
verifyScalableProperties(scalableProperties, toReturnTaskList, nodeTempalteName, skipInputProperties);
}
}
if (MapUtils.isNotEmpty(task.getProperties())) {
toReturnTaskList.add(task);
}
}
Aggregations