Search in sources :

Example 31 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class NodeMatcherService method populateLocationResourcesWithServiceResource.

/**
 * Populate this {@link LocationResources} using these {@link ServiceResource}s in order to make them available as {@link LocationResourceTemplate} for
 * matching purpose.
 *
 * TODO: Improve this ugly code to put ServiceResource in LocationResourceTemplates.
 */
private void populateLocationResourcesWithServiceResource(LocationResources locationResources, List<ServiceResource> services, String locationId) {
    for (ServiceResource serviceResource : services) {
        LocationResourceTemplate lrt = new LocationResourceTemplate();
        lrt.setService(true);
        lrt.setEnabled(true);
        // for a service we also want to display the version, so just add it to the name
        lrt.setName(serviceResource.getName() + ":" + serviceResource.getVersion());
        lrt.setId(serviceResource.getId());
        ServiceNodeTemplate serviceNodeTemplate = new ServiceNodeTemplate(serviceResource.getNodeInstance());
        lrt.setTemplate(serviceNodeTemplate);
        lrt.setLocationId(locationId);
        String serviceTypeName = serviceResource.getNodeInstance().getNodeTemplate().getType();
        List<String> types = Lists.newArrayList(serviceTypeName);
        lrt.setTypes(types);
        NodeType serviceType = toscaTypeSearchService.findOrFail(NodeType.class, serviceTypeName, serviceResource.getNodeInstance().getTypeVersion());
        types.addAll(serviceType.getDerivedFrom());
        locationResources.getNodeTypes().put(serviceTypeName, serviceType);
        Csar csar = toscaTypeSearchService.getArchive(serviceType.getArchiveName(), serviceType.getArchiveVersion());
        Set<CSARDependency> dependencies = Sets.newHashSet();
        if (csar.getDependencies() != null) {
            dependencies.addAll(csar.getDependencies());
        }
        dependencies.add(new CSARDependency(csar.getName(), csar.getVersion()));
        if (serviceType.getCapabilities() != null && !serviceType.getCapabilities().isEmpty()) {
            for (CapabilityDefinition capabilityDefinition : serviceType.getCapabilities()) {
                locationResources.getCapabilityTypes().put(capabilityDefinition.getType(), csarRepoSearchService.getRequiredElementInDependencies(CapabilityType.class, capabilityDefinition.getType(), dependencies));
            }
        }
        locationResources.getNodeTemplates().add(lrt);
    }
}
Also used : ServiceNodeTemplate(org.alien4cloud.tosca.model.templates.ServiceNodeTemplate) Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) LocationResourceTemplate(alien4cloud.model.orchestrators.locations.LocationResourceTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) ServiceResource(alien4cloud.model.service.ServiceResource) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 32 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class TopologyService method updateDependencies.

public void updateDependencies(EditionContext context, CSARDependency newDependency) {
    final Set<CSARDependency> oldDependencies = new HashSet<>(context.getTopology().getDependencies());
    final Set<CSARDependency> newDependencies = csarDependencyLoader.getDependencies(newDependency.getName(), newDependency.getVersion());
    newDependencies.add(newDependency);
    // Update context with the new dependencies.
    newDependencies.forEach(csarDependency -> context.getToscaContext().updateDependency(csarDependency));
    // Validate that the dependency change does not induce missing types.
    try {
        this.checkForMissingTypes(context.getTopology());
    } catch (NotFoundException e) {
        // Revert changes made to the Context then throw.
        context.getToscaContext().resetDependencies(oldDependencies);
        context.getTopology().setDependencies(oldDependencies);
        throw new VersionConflictException("Changing the dependency [" + newDependency.getName() + "] to version [" + newDependency.getVersion() + "] induces missing types in the topology. Not found : [" + e.getMessage() + "].", e);
    }
    // Perform the dependency update on the topology.
    context.getTopology().setDependencies(new HashSet<>(context.getToscaContext().getDependencies()));
}
Also used : VersionConflictException(alien4cloud.exception.VersionConflictException) NotFoundException(alien4cloud.exception.NotFoundException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Example 33 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class ToscaTypeLoader method loadType.

/**
 * Add directDependency for the given type from the given archive.
 * If the directDependency is of an deprecated version ( < than found in the existing dependencies), ignore it.
 * If the directDependency is of a more recent version, force the dependency of the topology to the more recent one
 *
 * @param type name of the type
 * @param directDependency the direct directDependency to load the type
 * @return True if the dependency has been upgraded into the topology. False if not.
 */
public boolean loadType(String type, CSARDependency directDependency) {
    boolean upgraded = false;
    if (log.isDebugEnabled()) {
        log.debug("Load type [" + type + "] from dependency [" + directDependency + "]");
    }
    Set<String> typesLoadedByDependency = dependenciesMap.get(directDependency);
    // Increment usage count
    Integer currentUsageCount = typeUsagesMap.get(type);
    if (currentUsageCount == null) {
        currentUsageCount = Integer.valueOf(0);
    }
    typeUsagesMap.put(type, currentUsageCount + 1);
    if (typesLoadedByDependency != null) {
        typesLoadedByDependency.add(type);
        // make sure we replace the key because the Equals on CSARDependency is only based on the name and the version
        dependenciesMap.remove(directDependency);
        dependenciesMap.put(directDependency, typesLoadedByDependency);
    } else {
        upgraded = addNewDependency(directDependency, type);
    }
    Set<CSARDependency> transitiveDependencies = csarDependencyLoader.getDependencies(directDependency.getName(), directDependency.getVersion());
    for (CSARDependency transitiveDependency : transitiveDependencies) {
        Set<String> transitiveTypesLoadedByDependency = dependenciesMap.get(transitiveDependency);
        if (transitiveTypesLoadedByDependency == null) {
            addNewDependency(csarDependencyLoader.buildDependencyBean(transitiveDependency.getName(), transitiveDependency.getVersion()), type);
        } else {
            transitiveTypesLoadedByDependency.add(type);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Type usage [" + typeUsagesMap + "]");
        log.debug("Dependencies usage [" + dependenciesMap + "]");
    }
    return upgraded;
}
Also used : CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 34 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.

the class NodeMatchingReplaceModifier method processServiceResourceReplacement.

@Override
protected void processServiceResourceReplacement(Topology topology, Map<String, NodeTemplate> topologyTemplateMap, String nodeId, String serviceResourceId) {
    ServiceResource serviceResource = getServiceResourceService().getOrFail(serviceResourceId);
    NodeTemplate serviceNodeTemplate = serviceResource.getNodeInstance().getNodeTemplate();
    ServiceNodeTemplate substitutionNodeTemplate = new ServiceNodeTemplate(serviceNodeTemplate.getType(), serviceNodeTemplate.getProperties(), serviceNodeTemplate.getAttributes(), serviceNodeTemplate.getRelationships(), serviceNodeTemplate.getRequirements(), serviceNodeTemplate.getCapabilities(), serviceNodeTemplate.getInterfaces(), serviceNodeTemplate.getArtifacts());
    substitutionNodeTemplate.setServiceResourceId(serviceResource.getId());
    substitutionNodeTemplate.setAttributeValues(serviceResource.getNodeInstance().getAttributeValues());
    NodeTemplate abstractTopologyNode = topologyTemplateMap.put(nodeId, substitutionNodeTemplate);
    substitutionNodeTemplate.setName(abstractTopologyNode.getName());
    substitutionNodeTemplate.setRelationships(abstractTopologyNode.getRelationships());
    // add all the necessary dependencies to the topology
    Csar csar = getToscaTypeSearchService().getArchive(serviceResource.getDependency().getName(), serviceResource.getDependency().getVersion());
    Set<CSARDependency> dependencies = Sets.newHashSet();
    if (csar.getDependencies() != null) {
        dependencies.addAll(csar.getDependencies());
    }
    dependencies.add(new CSARDependency(csar.getName(), csar.getVersion()));
    topology.getDependencies().addAll(dependencies);
}
Also used : ServiceNodeTemplate(org.alien4cloud.tosca.model.templates.ServiceNodeTemplate) Csar(org.alien4cloud.tosca.model.Csar) ServiceNodeTemplate(org.alien4cloud.tosca.model.templates.ServiceNodeTemplate) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) ServiceResource(alien4cloud.model.service.ServiceResource) CSARDependency(org.alien4cloud.tosca.model.CSARDependency)

Example 35 with CSARDependency

use of org.alien4cloud.tosca.model.CSARDependency 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);
    });
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) IncompatibleHalfRelationshipException(org.alien4cloud.alm.service.exceptions.IncompatibleHalfRelationshipException) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) HashSet(java.util.HashSet)

Aggregations

CSARDependency (org.alien4cloud.tosca.model.CSARDependency)50 Csar (org.alien4cloud.tosca.model.Csar)16 Test (org.junit.Test)11 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)8 Set (java.util.Set)7 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)6 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)6 ServiceResource (alien4cloud.model.service.ServiceResource)5 Capability (org.alien4cloud.tosca.model.templates.Capability)5 Topology (org.alien4cloud.tosca.model.templates.Topology)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 NotFoundException (alien4cloud.exception.NotFoundException)4 ParsingError (alien4cloud.tosca.parser.ParsingError)4 HashSet (java.util.HashSet)4 CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)4 LocationResourceTemplate (alien4cloud.model.orchestrators.locations.LocationResourceTemplate)3 ParsingException (alien4cloud.tosca.parser.ParsingException)3 VersionConflictException (alien4cloud.exception.VersionConflictException)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)2 Location (alien4cloud.model.orchestrators.locations.Location)2