Search in sources :

Example 1 with RequirementBoundException

use of org.alien4cloud.tosca.editor.exception.RequirementBoundException 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() + "]");
}
Also used : CapabilityBoundException(org.alien4cloud.tosca.editor.exception.CapabilityBoundException) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) NotFoundException(alien4cloud.exception.NotFoundException) LinkedHashMap(java.util.LinkedHashMap) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) RequirementBoundException(org.alien4cloud.tosca.editor.exception.RequirementBoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) TopologyContext(alien4cloud.paas.wf.TopologyContext) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 InvalidNameException (alien4cloud.exception.InvalidNameException)1 NotFoundException (alien4cloud.exception.NotFoundException)1 TopologyContext (alien4cloud.paas.wf.TopologyContext)1 LinkedHashMap (java.util.LinkedHashMap)1 CapabilityBoundException (org.alien4cloud.tosca.editor.exception.CapabilityBoundException)1 RequirementBoundException (org.alien4cloud.tosca.editor.exception.RequirementBoundException)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)1 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)1 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)1