Search in sources :

Example 1 with AnalysisMetadata

use of org.batfish.datamodel.AnalysisMetadata in project batfish by batfish.

the class WorkMgr method configureAnalysis.

/**
 * Create, update, or truncate an analysis with provided questions or and/or question names
 *
 * @param containerName The container in which the analysis resides
 * @param newAnalysis Whether or not to create a new analysis. Incompatible with {@code
 *     delQuestionsStr}.
 * @param aName The name of the analysis
 * @param questionsToAdd The questions to be added to or initially populate the analysis.
 * @param questionsToDelete A list of question names to be deleted from the analysis. Incompatible
 *     with {@code newAnalysis}.
 * @param suggested An optional Boolean indicating whether analysis is suggested (default: false).
 */
public void configureAnalysis(String containerName, boolean newAnalysis, String aName, Map<String, String> questionsToAdd, List<String> questionsToDelete, @Nullable Boolean suggested) {
    Path containerDir = getdirContainer(containerName);
    Path aDir = containerDir.resolve(Paths.get(BfConsts.RELPATH_ANALYSES_DIR, aName));
    this.configureAnalysisValidityCheck(containerName, newAnalysis, aName, questionsToAdd, questionsToDelete, aDir);
    if (newAnalysis) {
        aDir.toFile().mkdirs();
    }
    // Create metadata if it's a new analysis, or update it if suggested is not null
    if (newAnalysis || suggested != null) {
        AnalysisMetadata metadata;
        if (newAnalysis) {
            metadata = new AnalysisMetadata(Instant.now(), (suggested != null) && suggested);
        } else if (!Files.exists(getpathAnalysisMetadata(containerName, aName))) {
            // Configuring an old analysis with no metadata file; create one. Know suggested != null
            metadata = new AnalysisMetadata(Instant.MIN, suggested);
        } else {
            try {
                metadata = AnalysisMetadataMgr.readMetadata(containerName, aName);
                metadata.setSuggested(suggested);
            } catch (IOException e) {
                throw new BatfishException("Unable to read metadata file for analysis '" + aName + "'", e);
            }
        }
        // Write metadata to file
        try {
            AnalysisMetadataMgr.writeMetadata(metadata, containerName, aName);
        } catch (JsonProcessingException e) {
            throw new BatfishException("Could not write analysisMetadata", e);
        }
    }
    /**
     * Delete questionsToDelete and add questionsToAdd
     */
    Path questionsDir = aDir.resolve(BfConsts.RELPATH_QUESTIONS_DIR);
    for (String qName : questionsToDelete) {
        CommonUtil.deleteDirectory(questionsDir.resolve(qName));
    }
    for (Entry<String, String> entry : questionsToAdd.entrySet()) {
        questionsDir.resolve(entry.getKey()).toFile().mkdirs();
        Path qFile = questionsDir.resolve(Paths.get(entry.getKey(), BfConsts.RELPATH_QUESTION_FILE));
        CommonUtil.writeFile(qFile, entry.getValue());
    }
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) AnalysisMetadata(org.batfish.datamodel.AnalysisMetadata) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 BatfishException (org.batfish.common.BatfishException)1 AnalysisMetadata (org.batfish.datamodel.AnalysisMetadata)1