Search in sources :

Example 1 with ParsingException

use of alien4cloud.tosca.parser.ParsingException in project yorc-a4c-plugin by ystia.

the class AbstractLocationConfigurer method getMatchingConfigurations.

public Map<String, MatchingConfiguration> getMatchingConfigurations(String matchingConfigRelativePath) {
    Path matchingConfigPath = selfContext.getPluginPath().resolve(matchingConfigRelativePath);
    MatchingConfigurations matchingConfigurations = null;
    try {
        matchingConfigurations = matchingConfigurationsParser.parseFile(matchingConfigPath).getResult();
    } catch (ParsingException e) {
        return Maps.newHashMap();
    }
    Map<String, MatchingConfiguration> ret = matchingConfigurations.getMatchingConfigurations();
    printMatchingConfigurations(ret);
    return ret;
}
Also used : Path(java.nio.file.Path) MatchingConfigurations(alien4cloud.deployment.matching.services.nodes.MatchingConfigurations) MatchingConfiguration(alien4cloud.model.deployment.matching.MatchingConfiguration) ParsingException(alien4cloud.tosca.parser.ParsingException)

Example 2 with ParsingException

use of alien4cloud.tosca.parser.ParsingException in project alien4cloud by alien4cloud.

the class EditorTopologyUploadService method processTopologyDir.

public void processTopologyDir(Path archivePath, String workspace) {
    // parse the archive.
    try {
        ParsingResult<ArchiveRoot> parsingResult = toscaArchiveParser.parseDir(archivePath);
        processTopologyParseResult(archivePath, parsingResult, workspace);
    } catch (ParsingException e) {
        // Manage parsing error and dispatch them in the right editor exception
        throw new EditorToscaYamlParsingException("The uploaded file to override the topology yaml is not a valid Tosca Yaml.", toParsingResult(e));
    }
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) EditorToscaYamlParsingException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException) ParsingException(alien4cloud.tosca.parser.ParsingException) EditorToscaYamlParsingException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException)

Example 3 with ParsingException

use of alien4cloud.tosca.parser.ParsingException 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
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) RestError(alien4cloud.rest.model.RestError) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ParsingException

use of alien4cloud.tosca.parser.ParsingException in project alien4cloud by alien4cloud.

the class InitialLoader method loadArchives.

private void loadArchives(Path rootDirectory) {
    if (!Files.exists(rootDirectory) || !Files.isDirectory(rootDirectory)) {
        log.warn("Skipping archives' initial loading: directory cannot be found {}.", rootDirectory.toString());
        return;
    }
    // this operation is performed using admin role
    SecurityContextImpl adminContext = new SecurityContextImpl();
    Set<SimpleGrantedAuthority> authorities = Sets.newHashSet();
    authorities.add(new SimpleGrantedAuthority(Role.ADMIN.name()));
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("alien4cloud_init_loader", "alien4cloud_init_loader", authorities);
    adminContext.setAuthentication(auth);
    SecurityContextHolder.setContext(adminContext);
    // archives must be in zip format and placed in the actual folder
    try {
        List<Path> archives = FileUtil.listFiles(rootDirectory, ".+\\.(zip|csar)");
        Collections.sort(archives);
        for (Path archive : archives) {
            try {
                log.debug("Initial load of archives from [ {} ].", archive.toString());
                csarUploadService.upload(archive, CSARSource.ALIEN, AlienConstants.GLOBAL_WORKSPACE_ID);
            } catch (AlreadyExistException e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive has already been loaded.", archive.toString(), e);
            } catch (CSARUsedInActiveDeployment e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive is used in an active depoyment, and then cannot be overrided.", archive.toString(), e);
            } catch (ParsingException e) {
                log.error("Initial upload of archive [ {} ] has failed.", archive.toString(), e);
            } catch (ToscaTypeAlreadyDefinedInOtherCSAR e) {
                log.debug("Skipping initial upload of archive [ {} ], it's archive contain's a tosca type already defined in an other archive." + e.getMessage(), archive.toString(), e);
            }
        }
    } catch (IOException e) {
        log.error("Failed to load initial archives", e);
    } finally {
        // clear the security context
        SecurityContextHolder.clearContext();
    }
}
Also used : Path(java.nio.file.Path) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 5 with ParsingException

use of alien4cloud.tosca.parser.ParsingException 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;
}
Also used : Path(java.nio.file.Path) Csar(org.alien4cloud.tosca.model.Csar) ParsingContext(alien4cloud.tosca.parser.ParsingContext) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) CsarDependenciesBean(org.alien4cloud.tosca.model.CsarDependenciesBean) CSARDependency(org.alien4cloud.tosca.model.CSARDependency) ParsingResult(alien4cloud.tosca.parser.ParsingResult) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Aggregations

ParsingException (alien4cloud.tosca.parser.ParsingException)12 Path (java.nio.file.Path)9 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)5 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)3 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)3 MatchingConfigurations (alien4cloud.deployment.matching.services.nodes.MatchingConfigurations)3 AlreadyExistException (alien4cloud.exception.AlreadyExistException)3 PluginArchive (alien4cloud.orchestrators.plugin.model.PluginArchive)2 ToscaContextual (alien4cloud.tosca.context.ToscaContextual)2 ParsingContext (alien4cloud.tosca.parser.ParsingContext)2 ParsingError (alien4cloud.tosca.parser.ParsingError)2 ParsingResult (alien4cloud.tosca.parser.ParsingResult)2 IOException (java.io.IOException)2 EditorToscaYamlParsingException (org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException)2 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)2 Csar (org.alien4cloud.tosca.model.Csar)2 CsarDependenciesBean (org.alien4cloud.tosca.model.CsarDependenciesBean)2 Audit (alien4cloud.audit.annotation.Audit)1 MatchingConfiguration (alien4cloud.model.deployment.matching.MatchingConfiguration)1 PluginArchiveException (alien4cloud.plugin.exception.PluginArchiveException)1