diff --git a/demo/bird_app/Makefile b/demo/bird_app/Makefile new file mode 100644 index 00000000..39e7bb60 --- /dev/null +++ b/demo/bird_app/Makefile @@ -0,0 +1,17 @@ +CXX := g++ +CXX_FLAGS := -Wall -Wextra -std=c++17 + +BIN := build +SRC := bird_app.cpp ../../controlplane/libbird.cpp +INCLUDE := -I . -I ../.. -I ../../subprojects/json/include/ +LIB := lib +EXECUTABLE := bird_app + + +all: $(BIN)/$(EXECUTABLE) + +$(BIN)/$(EXECUTABLE): $(SRC) + $(CXX) $(CXX_FLAGS) $(INCLUDE) -L$(LIB) $^ -o $@ + +clean: + -rm $(BIN)/* diff --git a/demo/bird_app/bird_app.cpp b/demo/bird_app/bird_app.cpp new file mode 100644 index 00000000..5ef08d9e --- /dev/null +++ b/demo/bird_app/bird_app.cpp @@ -0,0 +1,134 @@ +// #include "bird_app.h" +#include "common/define.h" +#include "controlplane/libbird.h" + +#include +#include + +common::log::LogPriority common::log::logPriority = common::log::TLOG_DEBUG; + +std::ostream& operator<<(std::ostream& os, const common::community_t& lc) +{ + os << lc.toString(); + return os; +} + +std::ostream& operator<<(std::ostream& os, const common::large_community_t& lc) +{ + os << lc.toString(); + return os; +} + +template +std::ostream& PrintCollection(std::ostream& os, T& data, char left, char right) +{ + if (data.empty()) + { + os << "null"; + return os; + } + + os << left; + bool first = true; + for (const auto& value : data) + { + if (first) + { + first = false; + } + else + { + os << ", "; + } + os << value; + } + os << right; + + return os; +} + +template +std::ostream& operator<<(std::ostream& os, const std::set& data) +{ + return PrintCollection(os, data, '{', '}'); +} + +template +std::ostream& operator<<(std::ostream& os, const std::vector& data) +{ + return PrintCollection(os, data, '[', ']'); +} + +void PrintRibRequests(common::icp::rib_update::request& requests) +{ + std::cout << "--------------------------------------------\n"; + std::cout << "new requests: " << requests.size() << "\n"; + for (const auto& action : requests) + { + if (std::holds_alternative(action)) + { + const auto& [protocol, vrf, priority, insert_data] = std::get(action); + std::cout << "insert, protocol=" << protocol << ", vrf=" << vrf << ", priority=" << priority << "\n"; + for (const auto& [key, tables] : insert_data) + { + const auto& [peer, origin, med, aspath, community, large_community, local_preference] = key; + std::cout << "\tpeer=" << peer.toString() << ", origin=" << origin << ", med=" << med + << ", aspath=" << aspath << ", community=" << community + << ", large_community=" << large_community << "\n"; + for (const auto& [table_name, nexthops] : tables) + { + std::cout << "\t\ttable_name=" << table_name << "\n"; + for (const auto& [nexthop, prefixes] : nexthops) + { + std::cout << "\t\t\tnexthop=" << nexthop.toString() << "\n"; + for (const auto& [prefix, path_information, labels] : prefixes) + { + std::cout << "\t\t\t\tprefix=" << prefix.toString() << ", path_information=" + << path_information << ", labels=" << labels << "\n"; + } + } + } + } + } + else if (std::holds_alternative(action)) + { + const auto& [protocol, vrf, priority, peers] = std::get(action); + std::cout << "remove, protocol=" << protocol << ", vrf=" << vrf << ", priority=" << priority << "\n"; + for (const auto& [peer, tables] : peers) + { + std::cout << "\tpeer=" << peer.toString() << "\n"; + for (const auto& [table_name, prefixes] : tables) + { + std::cout << "\t\ttable_name=" << table_name << "\n"; + for (const auto& [prefix, path_information, labels] : prefixes) + { + std::cout << "\t\t\tprefix=" << prefix.toString() << ", path_information=" + << path_information << ", labels=" << labels << "\n"; + } + } + } + } + else + { + std::cout << "bad action type\n"; + } + } + + requests.clear(); + std::cout.flush(); +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cout << "Usage: " << argv[0] << " \n"; + return 1; + } + + const char* sock_name = argv[1]; + const char* vrf = "default"; + read_bird_feed(sock_name, vrf, PrintRibRequests); + + return 0; +} diff --git a/demo/bird_app/build/.gitignore b/demo/bird_app/build/.gitignore new file mode 100644 index 00000000..5816188c --- /dev/null +++ b/demo/bird_app/build/.gitignore @@ -0,0 +1 @@ +bird_app