use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.
the class ToscaTypeLoader method addNewDependency.
/**
* Add a dependency
*
* @param dependency
* @param type
* @return True if the dependecy has been upgraded into the topology. False if not.
*/
private boolean addNewDependency(CSARDependency dependency, String type) {
CSARDependency currentDependency = getDependencyWithName(dependency.getName());
// New dependency that never exists before
if (currentDependency == null) {
dependenciesMap.put(dependency, Sets.newHashSet(type));
return false;
}
// The new version is more recent, we will override with new version with warning
if (VersionUtil.compare(dependency.getVersion(), currentDependency.getVersion()) > 0) {
Set<String> typesLoadedByConflictingArchive = dependenciesMap.remove(currentDependency);
typesLoadedByConflictingArchive.add(type);
dependenciesMap.put(dependency, typesLoadedByConflictingArchive);
log.warn("Version conflicting for archive [" + dependency.getName() + "] override current version [" + currentDependency.getVersion() + "] with [" + dependency.getVersion() + "]");
return true;
}
log.warn("Version conflicting for archive [" + dependency.getName() + "] do not override and use current version [" + currentDependency.getVersion() + "] ignore old version [" + dependency.getVersion() + "]");
dependenciesMap.get(currentDependency).add(type);
return false;
}
use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.
the class TopologyService method loadType.
/**
* Load a type into the topology (add dependency for this new type, upgrade if necessary ...)
*
* If the dependency added has been upgraded into the topology, then recover the topology
*
* @param topology the topology
* @param element the element to load
* @param <T> tosca element type
* @return The real loaded element if element given in argument is from older archive than topology's dependencies
*/
@SuppressWarnings("unchecked")
public <T extends AbstractToscaType> T loadType(Topology topology, T element) {
String type = element.getElementId();
String archiveName = element.getArchiveName();
String archiveVersion = element.getArchiveVersion();
CSARDependency toLoadDependency = csarDependencyLoader.buildDependencyBean(archiveName, archiveVersion);
// FIXME Transitive dependencies could change here and thus types be affected ?
ToscaTypeLoader typeLoader = initializeTypeLoader(topology, true);
boolean upgraded = typeLoader.loadType(type, toLoadDependency);
ToscaContext.get().resetDependencies(typeLoader.getLoadedDependencies());
// Validate does not induce missing types
try {
this.checkForMissingTypes(topology);
} catch (NotFoundException e) {
// Revert changes made to the Context then throw.
ToscaContext.get().resetDependencies(topology.getDependencies());
throw new VersionConflictException("Adding the type [" + element.getId() + "] from archive [" + element.getArchiveName() + ":" + element.getArchiveVersion() + "] changes the topology dependencies and induces missing types. " + "Try with another version instead. Not found : [" + e.getMessage() + "].", e);
}
topology.setDependencies(typeLoader.getLoadedDependencies());
// recover the topology if needed
if (upgraded) {
recover(topology, toLoadDependency);
}
return ToscaContext.getOrFail((Class<T>) element.getClass(), type);
}
use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.
the class CsarGitService method sort.
private List<CsarDependenciesBean> sort(Map<CSARDependency, CsarDependenciesBean> elements) {
List<CsarDependenciesBean> sortedCsars = Lists.newArrayList();
List<CsarDependenciesBean> independents = Lists.newArrayList();
for (Map.Entry<CSARDependency, CsarDependenciesBean> entry : elements.entrySet()) {
CsarDependenciesBean csar = entry.getValue();
if (csar.getDependencies() == null) {
// the element has no dependencies
independents.add(csar);
} else {
// complete the list of dependent elements
List<CSARDependency> toClears = Lists.newArrayList();
for (CSARDependency dependency : csar.getDependencies()) {
CsarDependenciesBean providedDependency = elements.get(dependency);
if (providedDependency == null) {
// remove the dependency as it may be in the alien repo
toClears.add(dependency);
} else {
providedDependency.getDependents().add(csar);
}
}
for (CSARDependency toClear : toClears) {
csar.getDependencies().remove(toClear);
}
if (csar.getDependencies().isEmpty()) {
independents.add(csar);
}
}
}
while (!independents.isEmpty()) {
CsarDependenciesBean independent = independents.remove(0);
// remove from the elements
elements.remove(independent.getSelf());
// element has no more dependencies
sortedCsars.add(independent);
for (CsarDependenciesBean dependent : independent.getDependents()) {
dependent.getDependencies().remove(independent.getSelf());
if (dependent.getDependencies().isEmpty()) {
independents.add(dependent);
}
}
}
if (elements.size() > 0) {
// TODO there is looping dependencies throw exception or ignore ?
}
return sortedCsars;
}
use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.
the class CsarGitService method processImport.
private List<ParsingResult<Csar>> processImport(CsarGitRepository csarGitRepository, CsarGitCheckoutLocation csarGitCheckoutLocation, String gitHash) {
// find all the archives under the given hierarchy and zip them to create archives
Path archiveZipRoot = tempZipDirPath.resolve(csarGitRepository.getId());
Path archiveGitRoot = tempDirPath.resolve(csarGitRepository.getId());
if (csarGitCheckoutLocation.getSubPath() != null && !csarGitCheckoutLocation.getSubPath().isEmpty()) {
archiveGitRoot = archiveGitRoot.resolve(csarGitCheckoutLocation.getSubPath());
}
Set<Path> archivePaths = csarFinderService.prepare(archiveGitRoot, archiveZipRoot);
List<ParsingResult<Csar>> parsingResults = Lists.newArrayList();
Map<CSARDependency, CsarDependenciesBean> csarDependenciesBeans = uploadService.preParsing(archivePaths, parsingResults);
List<CsarDependenciesBean> sorted = sort(csarDependenciesBeans);
for (CsarDependenciesBean csarBean : sorted) {
String archiveRepoPath = archiveZipRoot.relativize(csarBean.getPath().getParent()).toString();
if (csarGitCheckoutLocation.getLastImportedHash() != null && csarGitCheckoutLocation.getLastImportedHash().equals(gitHash) && csarService.get(csarBean.getSelf().getName(), csarBean.getSelf().getVersion()) != null) {
// no commit since last import and the archive still exist in the repo, so do not import
addAlreadyImportParsingResult(archiveRepoPath, parsingResults);
continue;
}
try {
// FIXME Add possibility to choose an workspace
ParsingResult<Csar> result = uploadService.upload(csarBean.getPath(), CSARSource.GIT, AlienConstants.GLOBAL_WORKSPACE_ID);
result.getContext().setFileName(archiveRepoPath + "/" + result.getContext().getFileName());
parsingResults.add(result);
} catch (ParsingException e) {
ParsingResult<Csar> failedResult = new ParsingResult<>();
failedResult.setContext(new ParsingContext(archiveRepoPath));
failedResult.getContext().setParsingErrors(e.getParsingErrors());
parsingResults.add(failedResult);
log.debug("Failed to import archive from git as it cannot be parsed", e);
} catch (AlreadyExistException | ToscaTypeAlreadyDefinedInOtherCSAR | CSARUsedInActiveDeployment e) {
ParsingResult<Csar> failedResult = new ParsingResult<>();
failedResult.setContext(new ParsingContext(archiveRepoPath));
failedResult.getContext().setParsingErrors(Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
parsingResults.add(failedResult);
}
}
return parsingResults;
}
use of org.alien4cloud.tosca.model.CSARDependency in project alien4cloud by alien4cloud.
the class TopologyStepDefinitions method The_topology_should_have_as_dependencies.
@And("^The topology should have as dependencies$")
public void The_topology_should_have_as_dependencies(DataTable dependencies) throws Throwable {
Set<CSARDependency> expectedDependencies = Sets.newHashSet();
for (List<String> row : dependencies.raw()) {
expectedDependencies.add(new CSARDependency(row.get(0), row.get(1)));
}
String topologyResponseText = Context.getInstance().getRestResponse();
Set<CSARDependency> actualDependencies = JsonUtil.read(topologyResponseText, TopologyDTO.class, Context.getJsonMapper()).getData().getTopology().getDependencies();
Assert.assertEquals(expectedDependencies, actualDependencies);
}
Aggregations