use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class DefaultWorkflowBuilder method addRelationship.
@Override
public void addRelationship(Workflow workflow, String nodeId, NodeTemplate nodeTemplate, String relationshipName, RelationshipTemplate relationshipTemplate, TopologyContext topologyContext) {
boolean sourceIsNative = WorkflowUtils.isNativeOrSubstitutionNode(nodeId, topologyContext);
boolean targetIsNative = WorkflowUtils.isNativeOrSubstitutionNode(relationshipTemplate.getTarget(), topologyContext);
if (!sourceIsNative || !targetIsNative) {
// source or target is native or abstract
// for native types we don't care about relation ships in workflows
RelationshipDeclarativeWorkflow relationshipDeclarativeWorkflow = defaultDeclarativeWorkflows.getRelationshipWorkflows().get(workflow.getName());
// only trigger this method if it's a default workflow
if (relationshipDeclarativeWorkflow != null) {
Map<String, WorkflowStep> relationshipOperationSteps = safe(relationshipDeclarativeWorkflow.getOperations()).entrySet().stream().filter(operationEntry -> !targetIsNative || operationEntry.getValue().getOperationHost() == RelationshipOperationHost.SOURCE).filter(operationEntry -> !sourceIsNative || operationEntry.getValue().getOperationHost() == RelationshipOperationHost.TARGET).collect(Collectors.toMap(Map.Entry::getKey, operationEntry -> WorkflowUtils.addRelationshipOperationStep(workflow, nodeId, relationshipTemplate.getName(), ToscaRelationshipLifecycleConstants.CONFIGURE_SHORT, operationEntry.getKey(), operationEntry.getValue().getOperationHost().toString())));
Steps sourceSteps = new Steps(workflow, nodeId);
Steps targetSteps = new Steps(workflow, relationshipTemplate.getTarget());
safe(relationshipDeclarativeWorkflow.getOperations()).forEach((relationshipOperationName, relationshipOperationDependencies) -> {
WorkflowStep currentStep = relationshipOperationSteps.get(relationshipOperationName);
if (currentStep != null) {
// It might be filtered if source or target is native
declareStepDependencies(relationshipOperationDependencies.getSource(), currentStep, sourceSteps);
declareStepDependencies(relationshipOperationDependencies.getTarget(), currentStep, targetSteps);
declareStepDependencies(relationshipOperationDependencies, currentStep, new Steps(relationshipOperationSteps, Collections.emptyMap(), null));
}
});
RelationshipWeavingDeclarativeWorkflow relationshipWeavingDeclarativeWorkflow = getRelationshipWeavingDeclarativeWorkflow(relationshipTemplate.getType(), topologyContext, workflow.getName());
declareWeaving(relationshipWeavingDeclarativeWorkflow.getSource(), sourceSteps, targetSteps);
declareWeaving(relationshipWeavingDeclarativeWorkflow.getTarget(), targetSteps, sourceSteps);
}
} else {
// both source and target are native then the relationship does not have any operation implemented
// we will just try to declare weaving between source node operations and target node operations
Steps sourceSteps = new Steps(workflow, nodeId);
Steps targetSteps = new Steps(workflow, relationshipTemplate.getTarget());
RelationshipWeavingDeclarativeWorkflow relationshipWeavingDeclarativeWorkflow = getRelationshipWeavingDeclarativeWorkflow(relationshipTemplate.getType(), topologyContext, workflow.getName());
declareWeaving(relationshipWeavingDeclarativeWorkflow.getSource(), sourceSteps, targetSteps);
declareWeaving(relationshipWeavingDeclarativeWorkflow.getTarget(), targetSteps, sourceSteps);
}
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class WorkflowsBuilderService method removeRelationship.
public void removeRelationship(Topology topology, Csar csar, String sourceNodeId, String relationshipName, RelationshipTemplate relationshipTemplate) {
TopologyContext topologyContext = buildTopologyContext(topology, csar);
topologyContext.getTopology().getWorkflows().putAll(topologyContext.getTopology().getUnprocessedWorkflows());
NodeTemplate sourceNode = topology.getNodeTemplates().get(sourceNodeId);
String targetNodeId = relationshipTemplate.getTarget();
NodeTemplate targetNode = topologyContext.getTopology().getNodeTemplates().get(targetNodeId);
for (Workflow wf : topology.getWorkflows().values()) {
AbstractWorkflowBuilder builder = getWorkflowBuilder(topologyContext.getDSLVersion(), wf);
// Remove relationships from source to target
// Remove relationships from target to source
Map<String, RelationshipTemplate> sourceRelationships = sourceNode.getRelationships().entrySet().stream().filter(relationshipEntry -> relationshipEntry.getValue().getTarget().equals(targetNodeId)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Map<String, RelationshipTemplate> targetRelationships = AlienUtils.safe(targetNode.getRelationships()).entrySet().stream().filter(relationshipEntry -> relationshipEntry.getValue().getTarget().equals(sourceNodeId)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
builder.removeRelationships(wf, sourceNodeId, sourceRelationships, targetNodeId, targetRelationships);
sourceRelationships.entrySet().stream().filter(entry -> !entry.getKey().equals(relationshipName)).forEach(entry -> builder.addRelationship(wf, sourceNode.getName(), sourceNode, entry.getKey(), entry.getValue(), topologyContext));
targetRelationships.forEach((id, relationship) -> builder.addRelationship(wf, targetNodeId, targetNode, id, relationship, topologyContext));
// Remove unique relationship that we really want to remove
WorkflowUtils.fillHostId(wf, topologyContext);
}
postProcessTopologyWorkflows(topologyContext);
debugWorkflow(topologyContext.getTopology());
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class TopologyCompositionService method renameNodeTemplate.
private void renameNodeTemplate(Topology topology, String oldName, String newName) {
// quite improbable but ...
if (topology.getNodeTemplates().containsKey(newName)) {
throw new AlreadyExistException(String.format("A node with name '%s' already exists in this topology", newName));
}
NodeTemplate nodeTemplate = topology.getNodeTemplates().remove(oldName);
// manage relationships that target this node
for (NodeTemplate otherNodes : topology.getNodeTemplates().values()) {
if (otherNodes.getRelationships() == null || otherNodes.getRelationships().isEmpty()) {
continue;
}
for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
if (relationshipTemplate.getTarget().equals(oldName)) {
relationshipTemplate.setTarget(newName);
}
}
}
// all output stuffs
MapUtil.replaceKey(topology.getOutputProperties(), oldName, newName);
MapUtil.replaceKey(topology.getOutputCapabilityProperties(), oldName, newName);
MapUtil.replaceKey(topology.getOutputAttributes(), oldName, newName);
// group members must be updated
if (topology.getGroups() != null) {
for (NodeGroup nodeGroup : topology.getGroups().values()) {
Set<String> members = nodeGroup.getMembers();
if (members != null && members.remove(oldName)) {
members.add(newName);
}
}
}
// substitutions
if (topology.getSubstitutionMapping() != null) {
renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getCapabilities(), oldName, newName);
renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getRequirements(), oldName, newName);
}
// finally the node itself
topology.getNodeTemplates().put(newName, nodeTemplate);
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class TopologyCompositionService method processComposition.
/**
* Process the composition:
* <ul>
* <li>remove the 'proxy' node from the parent.
* <li>merge the child topology nodes into the parent nodes.
* <li>
* </ul>
*
* @param compositionCouple
*/
private void processComposition(CompositionCouple compositionCouple) {
// first of all, remove the proxy node from the parent
NodeTemplate proxyNodeTemplate = compositionCouple.parent.getNodeTemplates().remove(compositionCouple.nodeName);
// properties of the proxy are used to feed the property values of child node that use get_input
for (NodeTemplate childNodeTemplate : compositionCouple.child.getNodeTemplates().values()) {
for (Entry<String, AbstractPropertyValue> propertyEntry : childNodeTemplate.getProperties().entrySet()) {
AbstractPropertyValue pValue = propertyEntry.getValue();
if (isGetInput(pValue)) {
String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
}
}
for (Entry<String, Capability> capabilityEntry : childNodeTemplate.getCapabilities().entrySet()) {
if (capabilityEntry.getValue().getProperties() != null) {
for (Entry<String, AbstractPropertyValue> propertyEntry : capabilityEntry.getValue().getProperties().entrySet()) {
AbstractPropertyValue pValue = propertyEntry.getValue();
if (isGetInput(pValue)) {
String inputName = ((FunctionPropertyValue) pValue).getTemplateName();
propertyEntry.setValue(proxyNodeTemplate.getProperties().get(inputName));
}
}
}
}
}
// all relations from the proxy must now start from the corresponding node
if (proxyNodeTemplate.getRelationships() != null) {
for (Entry<String, RelationshipTemplate> e : proxyNodeTemplate.getRelationships().entrySet()) {
String relationShipKey = e.getKey();
RelationshipTemplate proxyRelationShip = e.getValue();
String requirementName = proxyRelationShip.getRequirementName();
SubstitutionTarget substitutionTarget = compositionCouple.child.getSubstitutionMapping().getRequirements().get(requirementName);
NodeTemplate nodeTemplate = compositionCouple.child.getNodeTemplates().get(substitutionTarget.getNodeTemplateName());
if (nodeTemplate.getRelationships() == null) {
Map<String, RelationshipTemplate> relationships = Maps.newHashMap();
nodeTemplate.setRelationships(relationships);
}
nodeTemplate.getRelationships().put(relationShipKey, proxyRelationShip);
proxyRelationShip.setRequirementName(substitutionTarget.getTargetId());
}
}
// all relations that target the proxy must be redirected to the corresponding child node
for (NodeTemplate otherNodes : compositionCouple.parent.getNodeTemplates().values()) {
if (otherNodes.getRelationships() != null) {
for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
if (relationshipTemplate.getTarget().equals(compositionCouple.nodeName)) {
SubstitutionTarget st = compositionCouple.child.getSubstitutionMapping().getCapabilities().get(relationshipTemplate.getTargetedCapabilityName());
relationshipTemplate.setTarget(st.getNodeTemplateName());
relationshipTemplate.setTargetedCapabilityName(st.getTargetId());
}
}
}
}
if (compositionCouple.parent.getOutputAttributes() != null) {
Set<String> outputAttributes = compositionCouple.parent.getOutputAttributes().remove(compositionCouple.nodeName);
if (outputAttributes != null) {
for (String proxyAttributeName : outputAttributes) {
sustituteGetAttribute(compositionCouple.child, compositionCouple.parent, proxyAttributeName);
}
}
}
// the parent itself expose stuffs, we eventually need to replace substitution targets
if (compositionCouple.parent.getSubstitutionMapping() != null) {
if (compositionCouple.parent.getSubstitutionMapping().getCapabilities() != null) {
for (Entry<String, SubstitutionTarget> substitutionCapabilityEntry : compositionCouple.parent.getSubstitutionMapping().getCapabilities().entrySet()) {
if (substitutionCapabilityEntry.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
String targetCapability = substitutionCapabilityEntry.getValue().getTargetId();
// just substitute the substitution target
substitutionCapabilityEntry.setValue(compositionCouple.child.getSubstitutionMapping().getCapabilities().get(targetCapability));
}
}
}
if (compositionCouple.parent.getSubstitutionMapping().getRequirements() != null) {
for (Entry<String, SubstitutionTarget> e : compositionCouple.parent.getSubstitutionMapping().getRequirements().entrySet()) {
if (e.getValue().getNodeTemplateName().equals(compositionCouple.nodeName)) {
String targetCapability = e.getValue().getTargetId();
// just substitute the substitution target
e.setValue(compositionCouple.child.getSubstitutionMapping().getRequirements().get(targetCapability));
}
}
}
}
// merge each child nodes into the parent
compositionCouple.parent.getNodeTemplates().putAll(compositionCouple.child.getNodeTemplates());
}
use of org.alien4cloud.tosca.model.templates.RelationshipTemplate in project alien4cloud by alien4cloud.
the class TopologyStepDefinitions method I_should_have_a_relationship_with_type_from_to_in_ALIEN.
@Then("^I should have a relationship \"([^\"]*)\" with type \"([^\"]*)\" from \"([^\"]*)\" to \"([^\"]*)\" in ALIEN$")
public void I_should_have_a_relationship_with_type_from_to_in_ALIEN(String relName, String relType, String source, String target) throws Throwable {
// I should have a relationship with type
String topologyJson = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId());
RestResponse<TopologyDTO> topologyResponse = JsonUtil.read(topologyJson, TopologyDTO.class, Context.getJsonMapper());
NodeTemplate sourceNode = topologyResponse.getData().getTopology().getNodeTemplates().get(source);
relName = relName == null || relName.isEmpty() ? getRelationShipName(relType, target) : relName;
RelationshipTemplate rel = sourceNode.getRelationships().get(relName);
assertNotNull(rel);
assertEquals(relType, rel.getType());
assertEquals(target, rel.getTarget());
assertNotNull(rel.getRequirementName());
assertNotNull(rel.getRequirementType());
}
Aggregations