use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class AddCapabilitySubstitutionTypeProcessor method process.
@Override
public void process(Csar csar, Topology topology, AddCapabilitySubstitutionTypeOperation operation) {
if (topology.getNodeTemplates() == null || !topology.getNodeTemplates().containsKey(operation.getNodeTemplateName())) {
throw new NotFoundException("Node " + operation.getNodeTemplateName() + " do not exist");
}
NodeTemplate nodeTemplate = topology.getNodeTemplates().get(operation.getNodeTemplateName());
if (nodeTemplate.getCapabilities() == null || !nodeTemplate.getCapabilities().containsKey(operation.getCapabilityId())) {
throw new NotFoundException("Capability " + operation.getCapabilityId() + " do not exist for node " + operation.getNodeTemplateName());
}
if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
throw new NotFoundException("No substitution type has been found");
}
Map<String, SubstitutionTarget> substitutionCapabilities = topology.getSubstitutionMapping().getCapabilities();
if (substitutionCapabilities == null) {
substitutionCapabilities = Maps.newHashMap();
topology.getSubstitutionMapping().setCapabilities(substitutionCapabilities);
} else if (substitutionCapabilities.containsKey(operation.getSubstitutionCapabilityId())) {
// ensure name unicity
throw new AlreadyExistException(String.format("A substitution with capability id <%s> already exists", operation.getSubstitutionCapabilityId()));
}
substitutionCapabilities.put(operation.getSubstitutionCapabilityId(), new SubstitutionTarget(operation.getNodeTemplateName(), operation.getCapabilityId()));
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class UpdateCapabilitySubstitutionTypeProcessor method process.
@Override
public void process(Csar csar, Topology topology, UpdateCapabilitySubstitutionTypeOperation operation) {
if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
throw new NotFoundException("No substitution type has been found");
}
Map<String, SubstitutionTarget> substitutionCapabilities = topology.getSubstitutionMapping().getCapabilities();
if (substitutionCapabilities == null) {
throw new NotFoundException("No substitution capabilities has been found");
}
SubstitutionTarget target = substitutionCapabilities.remove(operation.getSubstitutionCapabilityId());
if (target == null) {
throw new NotFoundException("No substitution capability has been found for key " + operation.getSubstitutionCapabilityId());
}
if (substitutionCapabilities.containsKey(operation.getNewCapabilityId())) {
throw new AlreadyExistException(String.format("Can not rename from <%s> to <%s> since capability <%s> already exists", operation.getSubstitutionCapabilityId(), operation.getNewCapabilityId(), operation.getNewCapabilityId()));
}
substitutionCapabilities.put(operation.getNewCapabilityId(), target);
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class LocationService method autoConfigure.
/**
* Auto-configure locations using the given location auto-configurer.
*
* @param orchestrator The id of the orchestrator that own the locations.
* @param locationAutoConfigurer The auto-configurer to use for getting locations.
*/
public void autoConfigure(Orchestrator orchestrator, ILocationAutoConfigurer locationAutoConfigurer) {
List<Location> locations = locationAutoConfigurer.getLocations();
for (Location location : locations) {
//
location.setId(UUID.randomUUID().toString());
location.setOrchestratorId(orchestrator.getId());
try {
createLocation(orchestrator, location, location.getInfrastructureType());
} catch (AlreadyExistException e) {
log.debug("Location <" + location.getName() + "> is already configured for this location - skipping", e);
}
}
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class PluginManager method uploadPlugin.
/**
* Upload a plugin from a given path.
*
* @param uploadedPluginPath The path of the plugin to upload.<br>
* The state of the new uploaded plugin will be determined as follow:
* <ul>
* <li>plugin doesn't exists: load and enable</li>
* <li>plugin exists: keep the state (reload if enabled)</li>
* </ul>
* @return the uploaded plugin
* @throws IOException In case there is an issue with the access to the plugin file.
* @throws PluginLoadingException
* @throws AlreadyExistException if a plugin with the same id already exists in the repository
* @throws MissingPlugingDescriptorFileException
*/
public Plugin uploadPlugin(Path uploadedPluginPath) throws PluginLoadingException, IOException, MissingPlugingDescriptorFileException {
// load the plugin descriptor
FileSystem fs = FileSystems.newFileSystem(uploadedPluginPath, null);
PluginDescriptor descriptor;
try {
try {
descriptor = YamlParserUtil.parseFromUTF8File(fs.getPath(PLUGIN_DESCRIPTOR_FILE), PluginDescriptor.class);
} catch (IOException e) {
if (e instanceof NoSuchFileException) {
throw new MissingPlugingDescriptorFileException();
} else {
throw e;
}
}
String pluginPathId = getPluginPathId();
Plugin plugin = new Plugin(descriptor, pluginPathId);
// check plugin already exists and is loaded
if (pluginContexts.get(plugin.getId()) != null) {
log.warn("Uploading Plugin [ {} ] impossible (already exists and enabled)", plugin.getId());
throw new AlreadyExistException("A plugin with the given id already exists and is enabled.");
}
Plugin oldPlugin = alienDAO.findById(Plugin.class, plugin.getId());
if (oldPlugin != null) {
// remove all files for the old plugin but keep configuration.
removePlugin(plugin.getId(), false);
}
Path pluginPath = getPluginPath(pluginPathId);
FileUtil.unzip(uploadedPluginPath, pluginPath);
// copy ui directory in case it exists
Path pluginUiSourcePath = pluginPath.resolve(UI_DIRECTORY);
Path pluginUiPath = getPluginUiPath(pluginPathId);
if (Files.exists(pluginUiSourcePath)) {
FileUtil.copy(pluginUiSourcePath, pluginUiPath);
}
alienDAO.save(plugin);
if (oldPlugin == null || oldPlugin.isEnabled()) {
enablePlugin(plugin);
}
return plugin;
} finally {
fs.close();
}
}
use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.
the class CloudServiceArchiveController method uploadCSAR.
@ApiOperation(value = "Upload a csar zip file.")
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<CsarUploadResult> uploadCSAR(@RequestParam(required = false) String workspace, @RequestParam("file") MultipartFile csar) throws IOException {
Path csarPath = null;
try {
if (workspace == null) {
workspace = AlienConstants.GLOBAL_WORKSPACE_ID;
}
// Perform check that the user has one of ARCHITECT, COMPONENT_MANAGER or ADMIN role
archiveIndexerAuthorizationFilter.preCheckAuthorization(workspace);
log.info("Serving file upload with name [" + csar.getOriginalFilename() + "]");
csarPath = Files.createTempFile(tempDirPath, null, '.' + CsarFileRepository.CSAR_EXTENSION);
// save the archive in the temp directory
FileUploadUtil.safeTransferTo(csarPath, csar);
// load, parse the archive definitions and save on disk
ParsingResult<Csar> result = csarUploadService.upload(csarPath, CSARSource.UPLOAD, workspace);
RestError error = null;
if (result.hasError(ParsingErrorLevel.ERROR)) {
error = RestErrorBuilder.builder(RestErrorCode.CSAR_PARSING_ERROR).build();
}
return RestResponseBuilder.<CsarUploadResult>builder().error(error).data(CsarUploadUtil.toUploadResult(result)).build();
} catch (ParsingException e) {
log.error("Error happened while parsing csar file <" + e.getFileName() + ">", e);
String fileName = e.getFileName() == null ? csar.getOriginalFilename() : e.getFileName();
CsarUploadResult uploadResult = new CsarUploadResult();
uploadResult.getErrors().put(fileName, e.getParsingErrors());
return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.CSAR_INVALID_ERROR).build()).data(uploadResult).build();
} catch (AlreadyExistException | CSARUsedInActiveDeployment | ToscaTypeAlreadyDefinedInOtherCSAR e) {
CsarUploadResult uploadResult = new CsarUploadResult();
uploadResult.getErrors().put(csar.getOriginalFilename(), Lists.newArrayList(UploadExceptionUtil.parsingErrorFromException(e)));
return RestResponseBuilder.<CsarUploadResult>builder().error(RestErrorBuilder.builder(RestErrorCode.ALREADY_EXIST_ERROR).build()).data(uploadResult).build();
} finally {
if (csarPath != null) {
// Clean up
try {
FileUtil.delete(csarPath);
} catch (IOException e) {
// The repository might just move the file instead of copying to save IO disk access
}
}
}
}
Aggregations