public static synchronized void write(ReferenceLibrary data, Path dataPath) throws JsonProcessingException { CommonUtil.writeFile(dataPath, BatfishObjectMapper.writePrettyString(data)); } }
/** * 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()); }
@Deprecated @Override public Map<String, BiFunction<Question, IBatfish, Answerer>> getAnswererCreators() { return toImmutableMap(_answererCreators, Entry::getKey, entry -> entry.getValue()::create); }
@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)); }
private void repairEnvironmentBgpTables() { Path answerPath = _testrigSettings.getParseEnvironmentBgpTablesAnswerPath(); Path bgpTablesOutputPath = _testrigSettings.getSerializeEnvironmentBgpTablesPath(); CommonUtil.deleteIfExists(answerPath); CommonUtil.deleteDirectory(bgpTablesOutputPath); computeEnvironmentBgpTables(); }
@Nullable @Deprecated public Path getTestrigObject(String networkName, String testrigName, String objectName) { Path testrigDir = getdirSnapshot(networkName, testrigName); Path file = testrigDir.resolve(Paths.get(BfConsts.RELPATH_OUTPUT, objectName)); /* * Check if we got an object name outside of the testrig folder, perhaps because of ".." in the * name; disallow it */ if (!CommonUtil.getCanonicalPath(file).startsWith(CommonUtil.getCanonicalPath(testrigDir))) { throw new BatfishException("Illegal object name: '" + objectName + "'"); } // Check in output then input directories for backward compatibility // Since inputs and outputs used to be stored together, in the testrig dir if (!file.toFile().exists()) { file = testrigDir.resolve(Paths.get(BfConsts.RELPATH_INPUT, objectName)); } if (Files.isRegularFile(file)) { return file; } else if (Files.isDirectory(file)) { Path zipfile = Paths.get(file + ".zip"); if (Files.exists(zipfile)) { CommonUtil.deleteIfExists(zipfile); } ZipUtility.zipFiles(file, zipfile); // TODO: delete the zipfile return zipfile; } return null; }
CommonUtil.deleteIfExists(failedTestoutPath); testoutWriter.close(); String testOutput = CommonUtil.readFile(Paths.get(testoutFile.getAbsolutePath())); missingReferenceFile ? "" : CommonUtil.readFile(Paths.get(referenceFileName)); testPassed ? "" : patch + "\n"); if (!testPassed) { CommonUtil.writeFile(failedTestoutPath, testOutput); _logger.outputf("Copied output to %s\n", failedTestoutPath);
public static synchronized String salt() { if (SALT == null) { SALT = readResource(BfConsts.ABSPATH_DEFAULT_SALT); } return SALT; }
@JsonProperty(PROP_SRC_OR_DST_IPS) public SortedSet<IpWildcard> getSrcOrDstIps() { return asPositiveIpWildcards(_headerSpace.getSrcOrDstIps()); }
try { client = CommonUtil.createHttpClientBuilder( _mainSettings.getSslDisable(), _mainSettings.getSslTrustAllCerts(), if (CommonUtil.causedBy(e, SSLHandshakeException.class) || CommonUtil.causedByMessage(e, "Unexpected end of file from server")) { throw new BatfishException("Unrecoverable connection error", e);
private void repairDataPlane(boolean compressed) { Path dataPlanePath = compressed ? _testrigSettings.getCompressedDataPlanePath() : _testrigSettings.getDataPlanePath(); Path dataPlaneAnswerPath = compressed ? _testrigSettings.getCompressedDataPlaneAnswerPath() : _testrigSettings.getDataPlaneAnswerPath(); CommonUtil.deleteIfExists(dataPlanePath); CommonUtil.deleteIfExists(dataPlaneAnswerPath); if (compressed) { computeCompressedDataPlane(); } else { computeDataPlane(); } }
return; } catch (SQLException e) { if (CommonUtil.causedByMessage(e, "Access denied for user") || CommonUtil.causedByMessage(e, "No suitable driver found") || CommonUtil.causedByMessage(e, "Unknown database") || CommonUtil.causedBy(e, UnknownHostException.class) || CommonUtil.causedBy(e, ConnectException.class)) { throw new BatfishException( "Unrecoverable SQLException loading JDBC driver: " + _driverClassName, e);
public Settings(String[] args) { super( CommonUtil.getConfig( BfConsts.PROP_ALLINONE_PROPERTIES_PATH, BfConsts.ABSPATH_CONFIG_FILE_NAME_ALLINONE, ConfigurationLocator.class)); initConfigDefaults(); initOptions(); parseCommandLine(args); }
@JsonProperty(PROP_NOT_SRC_IPS) public SortedSet<IpWildcard> getNotSrcIps() { return asNegativeIpWildcards(_headerSpace.getNotSrcIps()); }
public static boolean causedByMessage(Throwable e, String searchTerm) { Set<Throwable> seenCauses = Collections.newSetFromMap(new IdentityHashMap<>()); return causedByMessage(e, searchTerm, seenCauses); }
public static <T extends Throwable> boolean causedBy(Throwable e, Class<T> causeClass) { Set<Throwable> seenCauses = Collections.newSetFromMap(new IdentityHashMap<>()); return causedBy(e, causeClass, seenCauses); }
@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)); }
@Before public void readDisplayHints() throws IOException { String text = CommonUtil.readResource("org/batfish/question/jsonpath/jsonPathDisplayHintsTestHints.json"); _displayHints = BatfishObjectMapper.mapper().readValue(text, DisplayHints.class); }
@JsonProperty(PROP_DST_IPS) public SortedSet<IpWildcard> getDstIps() { return asPositiveIpWildcards(_headerSpace.getDstIps()); }
private void repairEnvironmentRoutingTables() { Path answerPath = _testrigSettings.getParseEnvironmentRoutingTablesAnswerPath(); Path rtOutputPath = _testrigSettings.getSerializeEnvironmentRoutingTablesPath(); CommonUtil.deleteIfExists(answerPath); CommonUtil.deleteDirectory(rtOutputPath); computeEnvironmentRoutingTables(); }