/** * Adds the reference books in {@code newBooks} to the ReferenceLibrary at {@code path}. Books * with the same name are overwritten */ public static void mergeReferenceBooks( @Nonnull Path path, @Nonnull SortedSet<ReferenceBook> newBooks) throws IOException { ReferenceLibrary originalLibrary = read(path); List<ReferenceBook> booksToKeep = originalLibrary._books.stream() .filter( originalBook -> !newBooks.stream() .anyMatch(newBook -> newBook.getName().equals(originalBook.getName()))) .collect(Collectors.toList()); booksToKeep.addAll(newBooks); ReferenceLibrary newLibrary = new ReferenceLibrary(booksToKeep); write(newLibrary, path); }
/** * From the {@link ReferenceLibrary} at {@code dataPath}, get the {@link ReferenceBook} with name * {@code bookName}. * * @throws IOException If the contents of the file could not be cast to {@link ReferenceLibrary} */ public static Optional<ReferenceBook> getReferenceBook( @Nonnull Path dataPath, @Nonnull String bookName) throws IOException { ReferenceLibrary data = read(dataPath); return data.getReferenceBook(bookName); }
@VisibleForTesting public static Set<String> getAddressBooks(ReferenceLibrary referenceLibrary) { ImmutableSet.Builder<String> addressBooks = ImmutableSet.builder(); referenceLibrary.getReferenceBooks().stream() .map(ReferenceBook::getName) .forEach(addressBooks::add); return addressBooks.build(); }
@JsonProperty(PROP_SERVICE_OBJECTS) List<ServiceObject> serviceObjects) { checkArgument(name != null, "Reference book name cannot be null"); ReferenceLibrary.checkValidName(name, "book"); ReferenceLibrary.checkDuplicates("address group", addressGroupNames); ReferenceLibrary.checkDuplicates("filter group", filterGroupNames); ReferenceLibrary.checkDuplicates("interface group", interfaceGroupNames); ReferenceLibrary.checkDuplicates("service endpoint", serviceEndpointNames); ReferenceLibrary.checkDuplicates("service object group", serviceObjectGroupNames); ReferenceLibrary.checkDuplicates("service objects", serviceObjectNames); ReferenceLibrary.checkDuplicates("service object or group", allServiceNames);
Path tempPath = CommonUtil.createTempFile("referencelibrary", "tmp"); ReferenceLibrary library = new ReferenceLibrary( ImmutableList.of( ReferenceBook.builder("book1").build(), ReferenceBook.builder("book2").build())); ReferenceLibrary.write(library, tempPath); ReferenceBook.builder("book3").build()); ReferenceLibrary.mergeReferenceBooks(tempPath, newBooks); ReferenceLibrary.read(tempPath).getReferenceBooks(), equalTo( ImmutableSortedSet.of(
@Test public void delAddressBook() throws IOException { String container = "someContainer"; Main.getWorkMgr().initNetwork(container, null); // write a library to the right place ReferenceLibrary.write( new ReferenceLibrary(ImmutableList.of(ReferenceBook.builder("book1").build())), Main.getWorkMgr().getReferenceLibraryPath(container)); Response response = getAddressBookTarget(container, "book1").delete(); // response should be OK and book1 should have disappeared assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( Main.getWorkMgr().getReferenceLibrary(container).getReferenceBook("book1").isPresent(), equalTo(false)); // deleting again should fail Response response2 = getAddressBookTarget(container, "book1").delete(); assertThat(response2.getStatus(), equalTo(NOT_FOUND.getStatusCode())); }
referenceLibraryData = true; try { ReferenceLibrary testrigData = ReferenceLibrary.read(subFile); Path path = networkDir.resolve(BfConsts.RELPATH_REFERENCE_LIBRARY_PATH); ReferenceLibrary.mergeReferenceBooks(path, testrigData.getReferenceBooks()); } catch (IOException e) {
@Test public void getAddressBook() throws JsonProcessingException { String container = "someContainer"; Main.getWorkMgr().initNetwork(container, null); // write a library to the right place ReferenceLibrary.write( new ReferenceLibrary(ImmutableList.of(ReferenceBook.builder("book1").build())), Main.getWorkMgr().getReferenceLibraryPath(container)); // we only check that the right type of object is returned at the expected URL target // we rely on ReferenceBookBean to have created the object with the right content Response response = getAddressBookTarget(container, "book1").get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(ReferenceBook.class), equalTo(ReferenceBook.builder("book1").build())); // should get 404 for non-existent dimension Response response2 = getAddressBookTarget(container, "book2").get(); assertThat(response2.getStatus(), equalTo(NOT_FOUND.getStatusCode())); } }
/** * 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 Optional<ReferenceBook> getReferenceBook(String bookName) { return _batfish.getReferenceLibraryData().getReferenceBook(bookName); }
@DELETE public Response delReferenceBook() { _logger.infof("WMS2: delReferenceBook '%s' '%s'\n", _container, _bookName); try { ReferenceLibrary library = Main.getWorkMgr().getReferenceLibrary(_container); library.delAddressBook(_bookName); ReferenceLibrary.write(library, Main.getWorkMgr().getReferenceLibraryPath(_container)); return Response.ok().build(); } catch (IOException e) { throw new InternalServerErrorException("Reference library is corrupted"); } catch (NoSuchElementException e) { throw new NotFoundException("Book not found: " + _bookName); } }
/** * Gets the {@link ReferenceLibrary} for the {@code network}. * * @throws IOException The contents of reference library file cannot be converted to {@link * ReferenceLibrary} */ public ReferenceLibrary getReferenceLibrary(String network) throws IOException { return ReferenceLibrary.read(getReferenceLibraryPath(network)); }
/** Adds a new {@link ReferenceBook} to the network's {@link ReferenceLibrary} */ @POST public Response addReferenceBook(ReferenceBookBean referenceBookBean) { _logger.infof("WMS2: addReferenceBook '%s'\n", _network); if (referenceBookBean.name == null) { throw new BadRequestException("ReferenceBook must have a name"); } try { ReferenceLibrary library = Main.getWorkMgr().getReferenceLibrary(_network); if (library.getReferenceBook(referenceBookBean.name).isPresent()) { throw new BadRequestException("Duplicate bookname: " + referenceBookBean.name); } ReferenceLibrary.mergeReferenceBooks( Main.getWorkMgr().getReferenceLibraryPath(_network), ImmutableSortedSet.of(referenceBookBean.toAddressBook())); return Response.ok().build(); } catch (IOException e) { throw new InternalServerErrorException("ReferenceLibrary resource is corrupted"); } }
/** * The argument to this constructor is a List (not a SortedSet) to prevent Jackson from silently * removing duplicate entries. */ @JsonCreator public ReferenceLibrary(@JsonProperty(PROP_BOOKS) List<ReferenceBook> books) { List<ReferenceBook> nnBooks = firstNonNull(books, ImmutableList.of()); checkDuplicates( "book", nnBooks.stream().map(ReferenceBook::getName).collect(Collectors.toList())); _books = nnBooks.stream() .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); }
/** Test that we get back the reference library */ @Test public void getReferenceLibrary() { String network = "someNetwork"; Main.getWorkMgr().initNetwork(network, null); // we only check that the right type of object is returned at the expected URL target // we rely on ReferenceLibraryBean to have created the object with the right content Response response = getReferenceLibraryTarget(network).get(); assertThat(response.getStatus(), equalTo(OK.getStatusCode())); assertThat( response.readEntity(ReferenceLibraryBean.class), equalTo(new ReferenceLibraryBean(new ReferenceLibrary(ImmutableList.of())))); } }
/** Deletes the book */ public void delAddressBook(String bookName) { ReferenceBook book = getReferenceBook(bookName) .orElseThrow(() -> new NoSuchElementException("Book not found: " + bookName)); _books = _books.stream() .filter(b -> !b.equals(book)) .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); }
/** Gets the {@link ReferenceLibrary} for the network */ @Override public ReferenceLibrary getReferenceLibraryData() { Path libraryPath = _settings .getStorageBase() .resolve(_settings.getContainer().getId()) .resolve(BfConsts.RELPATH_REFERENCE_LIBRARY_PATH); try { return ReferenceLibrary.read(libraryPath); } catch (IOException e) { _logger.errorf("Could not read reference library data from %s: %s", libraryPath, e); return null; } }
/** check that we do NOT barf on duplicates */ @Test public void checkDuplicatesNoDups() { checkDuplicates("test", ImmutableList.of("")); checkDuplicates("test", ImmutableList.of("a")); checkDuplicates("test", ImmutableList.of("a", "b", "c")); }
/** Test if empty library is bean'd properly */ @Test public void constructorEmpty() throws IOException { ReferenceLibrary library = new ReferenceLibrary(ImmutableList.of()); assertThat(new ReferenceLibraryBean(library).books, equalTo(ImmutableSortedSet.of())); }
public ReferenceLibraryBean(ReferenceLibrary library) { books = library.getReferenceBooks().stream() .map(book -> new ReferenceBookBean(book)) .collect(Collectors.toSet()); }