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);
}
}
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()));
}
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;
}
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);
}
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);
});
}
Aggregations