/** * Loads question from a given file * * @param questionFile File containing the question JSON * @return question loaded as a {@link JSONObject} * @throws BatfishException if question does not have instanceName or question cannot be parsed */ static JSONObject loadQuestionFromFile(Path questionFile) { String questionText = CommonUtil.readFile(questionFile); return loadQuestionFromText(questionText, questionFile.toString()); }
@Nullable @Override public String readExternalBgpAnnouncementsFile() { Path externalBgpAnnouncementsPath = _testrigSettings.getExternalBgpAnnouncementsPath(); if (Files.exists(externalBgpAnnouncementsPath)) { String externalBgpAnnouncementsFileContents = CommonUtil.readFile(externalBgpAnnouncementsPath); return externalBgpAnnouncementsFileContents; } else { return null; } }
@Override public @Nonnull String loadQuestion( NetworkId network, QuestionId question, @Nullable AnalysisId analysis) { return CommonUtil.readFile(getQuestionPath(network, question, analysis)); }
@Override public @Nonnull QuestionId getQuestionId( String question, NetworkId networkId, @Nullable AnalysisId analysisId) { if (!hasQuestionId(question, networkId, analysisId)) { throw new IllegalArgumentException( String.format("No ID assigned to non-existent question '%s'", question)); } return new QuestionId(CommonUtil.readFile(getQuestionIdPath(question, networkId, analysisId))); }
@Override public @Nonnull AnalysisId getAnalysisId(String analysis, NetworkId networkId) { if (!hasAnalysisId(analysis, networkId)) { throw new IllegalArgumentException( String.format("No ID assigned to non-existent analysis %s", analysis)); } return new AnalysisId(CommonUtil.readFile(getAnalysisIdPath(analysis, networkId))); }
/** * Reads the {@link ReferenceLibrary} object from {@code dataPath}. If the path does not exist, * initializes a new object. * * @throws IOException If file exists but its contents could not be cast to {@link * ReferenceLibrary} */ public static ReferenceLibrary read(Path dataPath) throws IOException { if (Files.exists(dataPath)) { return BatfishObjectMapper.mapper() .readValue(CommonUtil.readFile(dataPath), ReferenceLibrary.class); } return new ReferenceLibrary(null); }
@Override public @Nonnull IssueSettingsId getIssueSettingsId(String majorIssueType, NetworkId networkId) { if (!hasIssueSettingsId(majorIssueType, networkId)) { throw new IllegalArgumentException( String.format("No ID assigned to non-configured majorIssueType %s", majorIssueType)); } return new IssueSettingsId( CommonUtil.readFile(getIssueSettingsIdPath(majorIssueType, networkId))); }
@Override public @Nonnull NetworkId getNetworkId(String network) { if (!hasNetworkId(network)) { throw new IllegalArgumentException( String.format("No ID assigned to non-existent network %s", network)); } return new NetworkId(CommonUtil.readFile(getNetworkIdPath(network))); }
@Override public @Nonnull QuestionSettingsId getQuestionSettingsId( String questionClassId, NetworkId networkId) { if (!hasQuestionSettingsId(questionClassId, networkId)) { throw new IllegalArgumentException( String.format("No ID assigned to non-configured questionClassId '%s'", questionClassId)); } return new QuestionSettingsId( CommonUtil.readFile(getQuestionSettingsIdPath(questionClassId, networkId))); }
@Override public @Nonnull SnapshotId getSnapshotId(String snapshot, NetworkId networkId) { if (!hasSnapshotId(snapshot, networkId)) { throw new IllegalArgumentException( String.format("No ID assigned to non-existent snapshot '%s'", snapshot)); } return new SnapshotId(CommonUtil.readFile(getSnapshotIdPath(snapshot, networkId))); }
@Override public @Nullable SortedSet<Edge> loadEdgeBlacklist(NetworkId network, SnapshotId snapshot) { Path path = _d.getSnapshotDir(network, snapshot) .resolve(Paths.get(BfConsts.RELPATH_INPUT, BfConsts.RELPATH_EDGE_BLACKLIST_FILE)); if (!Files.exists(path)) { return null; } String fileText = CommonUtil.readFile(path); try { return BatfishObjectMapper.mapper() .readValue(fileText, new TypeReference<SortedSet<Edge>>() {}); } catch (IOException e) { _logger.warnf( "Unexpected exception caught while loading edge blacklist for snapshot %s: %s", snapshot, Throwables.getStackTraceAsString(e)); return null; } }
@VisibleForTesting /* Helper method to add the specified collection to the serialized list at the specified path. */ static <T> void addToSerializedList( Path serializedObjectPath, @Nullable Collection<T> addition, TypeReference<List<T>> type) throws IOException { if (addition == null || addition.isEmpty()) { return; } List<T> baseList; if (serializedObjectPath.toFile().exists()) { baseList = BatfishObjectMapper.mapper().readValue(CommonUtil.readFile(serializedObjectPath), type); baseList.addAll(addition); } else { baseList = ImmutableList.copyOf(addition); } CommonUtil.writeFile(serializedObjectPath, BatfishObjectMapper.writePrettyString(baseList)); }
@Override public @Nullable MajorIssueConfig loadMajorIssueConfig( NetworkId network, IssueSettingsId majorIssueType) { Path path = _d.getMajorIssueConfigDir(network, majorIssueType); if (!Files.exists(path)) { return null; } String majorIssueFileText = CommonUtil.readFile(path); try { return BatfishObjectMapper.mapper().readValue(majorIssueFileText, MajorIssueConfig.class); } catch (IOException e) { _logger.errorf( "ERROR: Could not cast file for major issue settings with ID %s in network %s to MajorIssueConfig: %s", majorIssueType, network, Throwables.getStackTraceAsString(e)); return null; } }
@Override public NodeRolesId getNetworkNodeRolesId(NetworkId networkId) { if (!hasNetworkNodeRolesId(networkId)) { throw new IllegalArgumentException("No assigned node-roles ID"); } return new NodeRolesId(CommonUtil.readFile(getNetworkNodeRolesIdPath(networkId))); }
@Override public Topology getEnvironmentTopology() { try { return BatfishObjectMapper.mapper() .readValue( CommonUtil.readFile(_testrigSettings.getSerializeTopologyPath()), Topology.class); } catch (IOException e) { throw new BatfishException("Could not getEnvironmentTopology: ", e); } }
private SortedMap<Path, String> readConfigurationFiles(Path testRigPath, String configsType) { _logger.infof("\n*** READING %s FILES ***\n", configsType); _logger.resetTimer(); SortedMap<Path, String> configurationData = new TreeMap<>(); Path configsPath = testRigPath.resolve(configsType); List<Path> configFilePaths = listAllFiles(configsPath); AtomicInteger completed = newBatch("Reading network configuration files", configFilePaths.size()); for (Path file : configFilePaths) { _logger.debugf("Reading: \"%s\"\n", file); String fileTextRaw = CommonUtil.readFile(file.toAbsolutePath()); String fileText = fileTextRaw + ((fileTextRaw.length() != 0) ? "\n" : ""); configurationData.put(file, fileText); completed.incrementAndGet(); } _logger.printElapsedTime(); return configurationData; }
private boolean getObject( @Nullable FileWriter outWriter, List<String> options, List<String> parameters, boolean delta) { Command command = delta ? Command.GET_OBJECT_REFERENCE : Command.GET_OBJECT; if (!isValidArgument(options, parameters, 0, 1, 1, command)) { return false; } if (!isSetTestrig() || !isSetContainer(true) || (delta && !isDeltaReady())) { return false; } String testrig = delta ? _currDeltaTestrig : _currTestrig; String objectName = parameters.get(0); String tmpPath = _workHelper.getObject(_currContainerName, testrig, objectName); String objectString = CommonUtil.readFile(Paths.get(tmpPath)); logOutput(outWriter, objectString + "\n"); return true; }
@Test public void testAddToSerializedListNoAddition() throws IOException { TemporaryFolder tmp = new TemporaryFolder(); tmp.create(); File serializedList = tmp.newFile(); Path serializedListPath = serializedList.toPath(); NodeInterfacePair baseInterface = new NodeInterfacePair("n1", "iface1"); // Write base serialized list List<NodeInterfacePair> interfaces = new ArrayList<>(); interfaces.add(baseInterface); writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces)); addToSerializedList( serializedListPath, ImmutableList.of(), new TypeReference<List<NodeInterfacePair>>() {}); // Confirm original interface shows up in the merged list, even if there are no additions assertThat( BatfishObjectMapper.mapper() .readValue( CommonUtil.readFile(serializedListPath), new TypeReference<List<NodeInterfacePair>>() {}), containsInAnyOrder(baseInterface)); }
@Test public void testAddToSerializedListNullAddition() throws IOException { TemporaryFolder tmp = new TemporaryFolder(); tmp.create(); File serializedList = tmp.newFile(); Path serializedListPath = serializedList.toPath(); NodeInterfacePair baseInterface = new NodeInterfacePair("n1", "iface1"); // Write base serialized list List<NodeInterfacePair> interfaces = new ArrayList<>(); interfaces.add(baseInterface); writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces)); addToSerializedList(serializedListPath, null, new TypeReference<List<NodeInterfacePair>>() {}); // Confirm original interface shows up in the merged list, even if addition is null assertThat( BatfishObjectMapper.mapper() .readValue( CommonUtil.readFile(serializedListPath), new TypeReference<List<NodeInterfacePair>>() {}), containsInAnyOrder(baseInterface)); }
@Test public void testRemoveFromSerializedListEmptyBaseList() throws IOException { TemporaryFolder tmp = new TemporaryFolder(); tmp.create(); File serializedList = tmp.newFile(); Path serializedListPath = serializedList.toPath(); // Write empty base serialized list List<NodeInterfacePair> interfaces = new ArrayList<>(); writeFile(serializedListPath, BatfishObjectMapper.writePrettyString(interfaces)); removeFromSerializedList( serializedListPath, ImmutableList.of(), new TypeReference<List<NodeInterfacePair>>() {}); // Confirm no issue if base list didn't exist assertThat( BatfishObjectMapper.mapper() .readValue( CommonUtil.readFile(serializedListPath), new TypeReference<List<NodeInterfacePair>>() {}), iterableWithSize(0)); }