Skip to content

Commit

Permalink
Add option to gbp_inspect to ignore all observables
Browse files Browse the repository at this point in the history
This is especially useful when dumping MODB

Signed-off-by: Tom Flynn <tom.flynn@gmail.com>
  • Loading branch information
tomflynn committed Mar 28, 2024
1 parent 9944b2a commit 0e8b729
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 37 deletions.
2 changes: 1 addition & 1 deletion agent-ovs/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ if RENDERER_OVS
ovs/OvsdbMessage.cpp \
ovs/OvsdbMonitorMessage.cpp \
ovs/CtZoneManager.cpp \
ovs/DnsManager.cpp \
ovs/DnsManager.cpp \
ovs/NatStatsManager.cpp

librenderer_openvswitch_la_CFLAGS = \
Expand Down
5 changes: 5 additions & 0 deletions agent-ovs/cmd/gbp_inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int main(int argc, char** argv) {
("props,p", "Include object properties in output")
("width,w", po::value<int>()->default_value(w.ws_col - 1),
"Truncate output to the specified number of characters")
("exclude-observables,x", "Exclude observables from output")
;
} catch (const boost::bad_lexical_cast& e) {
LOG(ERROR) << "exception while processing description: " << e.what();
Expand All @@ -107,6 +108,7 @@ int main(int argc, char** argv) {
bool followRefs = false;
int truncate = 0;
bool unresolved = false;
bool excludeObservables = false;
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).
Expand All @@ -131,6 +133,8 @@ int main(int argc, char** argv) {
followRefs = true;
if (vm.count("unresolved"))
unresolved = true;
if (vm.count("exclude-observables"))
excludeObservables = true;

log_file = vm["log"].as<string>();
level_str = vm["level"].as<string>();
Expand Down Expand Up @@ -175,6 +179,7 @@ int main(int argc, char** argv) {
modelgbp::getMetadata()));
client->setRecursive(recursive);
client->setFollowRefs(followRefs);
client->setExcludeObservables(excludeObservables);

if(unresolved) {
client->setUnresolved(true);
Expand Down
2 changes: 1 addition & 1 deletion agent-ovs/cmd/test/mock_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int main(int argc, char** argv) {
Policies::writeBasicInit(mframework);
Policies::writeTestPolicy(mframework);

mframework.dumpMODB(sample_file);
mframework.dumpMODB(sample_file, false);

mframework.stop();
return 0;
Expand Down
8 changes: 6 additions & 2 deletions libopflex/engine/InspectorClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void InspectorClientImpl::dumpToFile(FILE* file) {
if (unresolved) {
serializer.dumpUnResolvedMODB(file);
} else {
serializer.dumpMODB(file);
serializer.dumpMODB(file, excludeObservables);
}
}

Expand All @@ -191,7 +191,7 @@ void InspectorClientImpl::prettyPrint(std::ostream& output,
if (unresolved) {
serializer.displayUnresolved(output, tree, utf8);
} else {
serializer.displayMODB(output, tree, includeProps, utf8, truncate);
serializer.displayMODB(output, tree, includeProps, utf8, truncate, excludeObservables);
}
}

Expand All @@ -208,6 +208,10 @@ void InspectorClientImpl::setUnresolved(bool enabled) {
followRefs = enabled;
}

void InspectorClientImpl::setExcludeObservables(bool enabled) {
excludeObservables = enabled;
}

static std::string getRefSubj(const modb::ObjectStore& store,
const modb::reference_t& ref) {
try {
Expand Down
35 changes: 25 additions & 10 deletions libopflex/engine/MOSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void MOSerializer::dumpUnResolvedMODB(FILE* pfile) {
fwrite("\n", 1, 1, pfile);
}

void MOSerializer::dumpMODB(FILE* pfile) {
void MOSerializer::dumpMODB(FILE* pfile, bool excludeObservables) {
Region::obj_set_t roots;
getRoots(store, roots);
char buffer[1024];
Expand All @@ -387,21 +387,27 @@ void MOSerializer::dumpMODB(FILE* pfile) {
StoreClient& client = store->getReadOnlyStoreClient();
for (const Region::obj_set_t::value_type& r : roots) {
try {
serialize(r.first, r.second, client, writer, true);
if (excludeObservables) {
const modb::ClassInfo &ci = store->getClassInfo(r.first);
if (ci.getType() == modb::ClassInfo::class_type_t::OBSERVABLE) {
continue;
}
}
serialize(r.first, r.second, client, writer, true, excludeObservables);
} catch (const std::out_of_range& e) { }
}
writer.EndArray();
fwrite("\n", 1, 1, pfile);
}

void MOSerializer::dumpMODB(const std::string& file) {
void MOSerializer::dumpMODB(const std::string& file, bool excludeObservable) {
FILE* pfile = fopen(file.c_str(), "w");
if (pfile == NULL) {
LOG(ERROR) << "Could not open MODB file "
<< file << " for writing";
return;
}
dumpMODB(pfile);
dumpMODB(pfile, excludeObservable);
fclose(pfile);
LOG(INFO) << "Wrote MODB to " << file;
}
Expand Down Expand Up @@ -530,7 +536,8 @@ void MOSerializer::displayObject(std::ostream& ostream,
bool includeProps,
bool last, const std::string& prefix,
size_t prefixCharCount,
bool utf8, size_t truncate) {
bool utf8, size_t truncate,
bool excludeObservables) {
StoreClient& client = store->getReadOnlyStoreClient();
const modb::ClassInfo& ci = store->getClassInfo(class_id);
const std::shared_ptr<const modb::mointernal::ObjectInstance>
Expand Down Expand Up @@ -590,7 +597,9 @@ void MOSerializer::displayObject(std::ostream& ostream,
std::vector<modb::URI> >::iterator clsit;
std::vector<modb::URI>::const_iterator cit;
for (clsit = children.begin(); clsit != children.end(); ) {
if (clsit->second.empty())
const modb::ClassInfo& cci = store->getClassInfo(clsit->first);
if (clsit->second.empty() ||
(excludeObservables && cci.getType() != modb::ClassInfo::class_type_t::OBSERVABLE))
children.erase(clsit++);
else {
hasChildren = true;
Expand Down Expand Up @@ -721,22 +730,28 @@ void MOSerializer::displayObject(std::ostream& ostream,
}
displayObject(ostream, clsit->first, *cit,
tree, false, includeProps, islast, nprefix,
nPrefixCharCount, utf8, truncate);
nPrefixCharCount, utf8, truncate, excludeObservables);
}
}
}

void MOSerializer::displayMODB(std::ostream& ostream,
bool tree, bool includeProps, bool utf8,
size_t truncate) {
size_t truncate, bool excludeObservables) {
Region::obj_set_t roots;
getRoots(store, roots);

for (const Region::obj_set_t::value_type& r : roots) {
try {
if (excludeObservables) {
const modb::ClassInfo &ci = store->getClassInfo(r.first);
if (ci.getType() == modb::ClassInfo::class_type_t::OBSERVABLE) {
continue;
}
}
displayObject(ostream, r.first, r.second,
tree, true, includeProps,
true, "", 0, utf8, truncate);
tree, true, includeProps, true, "",
0, utf8, truncate, excludeObservables);
} catch (const std::out_of_range& e) { }
}
}
Expand Down
2 changes: 2 additions & 0 deletions libopflex/engine/include/opflex/engine/InspectorClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class InspectorClientImpl : public ofcore::InspectorClient,
virtual void setFollowRefs(bool enabled);
virtual void setRecursive(bool enabled);
virtual void setUnresolved(bool enabled);
virtual void setExcludeObservables(bool enabled);
virtual void addQuery(const std::string& subject,
const modb::URI& uri);
virtual void addClassQuery(const std::string& subject);
Expand Down Expand Up @@ -111,6 +112,7 @@ class InspectorClientImpl : public ofcore::InspectorClient,
bool followRefs;
bool recursive;
bool unresolved;
bool excludeObservables;
friend class internal::InspectorClientHandler;

void executeCommands();
Expand Down
29 changes: 19 additions & 10 deletions libopflex/engine/include/opflex/engine/internal/MOSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class MOSerializer {
const modb::URI& uri,
modb::mointernal::StoreClient& client,
T& writer,
bool recursive = true) {
bool recursive = true,
bool excludeObservables = false) {
const modb::ClassInfo& ci = store->getClassInfo(class_id);
const std::shared_ptr<const modb::mointernal::ObjectInstance>
oi(client.get(class_id, uri));
Expand Down Expand Up @@ -334,8 +335,11 @@ class MOSerializer {
std::vector<modb::URI> >::const_iterator clsit;
std::vector<modb::URI>::const_iterator cit;
for (clsit = children.begin(); clsit != children.end(); ++clsit) {
for (cit = clsit->second.begin(); cit != clsit->second.end(); ++cit) {
writer.String(cit->toString().c_str());
const modb::ClassInfo& cci = store->getClassInfo(clsit->first);
if (!excludeObservables || cci.getType() != modb::ClassInfo::class_type_t::OBSERVABLE) {
for (cit = clsit->second.begin(); cit != clsit->second.end(); ++cit) {
writer.String(cit->toString().c_str());
}
}
}
writer.EndArray();
Expand All @@ -362,9 +366,12 @@ class MOSerializer {
writer.EndObject();
if (recursive) {
for (clsit = children.begin(); clsit != children.end(); ++clsit) {
for (cit = clsit->second.begin();
cit != clsit->second.end(); ++cit) {
serialize(clsit->first, *cit, client, writer);
const modb::ClassInfo& cci = store->getClassInfo(clsit->first);
if (!excludeObservables || cci.getType() != modb::ClassInfo::class_type_t::OBSERVABLE) {
for (cit = clsit->second.begin();
cit != clsit->second.end(); ++cit) {
serialize(clsit->first, *cit, client, writer, recursive, excludeObservables);
}
}
}
}
Expand Down Expand Up @@ -394,15 +401,15 @@ class MOSerializer {
*
* @param file the file to write to.
*/
void dumpMODB(const std::string& file);
void dumpMODB(const std::string& file, bool excludeObservables);

/**
* Dump the managed object database to the file specified as a
* JSON blob.
*
* @param file the file to write to.
*/
void dumpMODB(FILE* file);
void dumpMODB(FILE* file, bool excludeObservables);

/**
* Dump the unresolved managed object database to the file specified as a
Expand Down Expand Up @@ -446,7 +453,8 @@ class MOSerializer {
*/
void displayMODB(std::ostream& ostream,
bool tree = true, bool includeProps = false,
bool utf8 = true, size_t truncate = 0);
bool utf8 = true, size_t truncate = 0,
bool excludeObservables = false);

/**
* Display the unresolved refrence in managed object database in a human-readable format
Expand Down Expand Up @@ -542,7 +550,8 @@ class MOSerializer {
bool tree, bool root, bool includeProps,
bool last, const std::string& prefix,
size_t prefixCharCount,
bool utf8, size_t truncate = 0);
bool utf8, size_t truncate = 0,
bool excludeObservables = false);

/**
* Display a particular unresolved relation object
Expand Down
2 changes: 1 addition & 1 deletion libopflex/engine/test/MOSerialize_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ BOOST_FIXTURE_TEST_CASE( mo_deserialize , BaseFixture ) {
BOOST_CHECK_EQUAL(0, notifs.size());

string modbFilename("/tmp/mo.db");
serializer.dumpMODB(modbFilename);
serializer.dumpMODB(modbFilename, true);
string unresolvedModbFilename("/tmp/unresolvedmo.db");
FILE* unresolvedMoFile = fopen(unresolvedModbFilename.c_str(), "w");
if (unresolvedMoFile == NULL) {
Expand Down
7 changes: 7 additions & 0 deletions libopflex/include/opflex/ofcore/InspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class InspectorClient : private boost::noncopyable {
*/
virtual void setUnresolved(bool enabled) = 0;

/**
* exclude observables from output
*
* @param enabled set to true to exclude observables
*/
virtual void setExcludeObservables(bool enabled) = 0;

/**
* Query for a particular managed object
*
Expand Down
7 changes: 4 additions & 3 deletions libopflex/include/opflex/ofcore/OFFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,15 @@ class OFFramework : private boost::noncopyable {
*
* @param file the file to write to.
*/
virtual void dumpMODB(const std::string& file);
virtual void dumpMODB(const std::string& file, bool excludeObservables);

/**
* Dump the managed object database to the file specified as a
* JSON blob.
*
* @param file the file to write to.
*/
virtual void dumpMODB(FILE* file);
virtual void dumpMODB(FILE* file, bool excludeObservables);

/**
* Pretty print the current MODB to the provided output stream.
Expand All @@ -806,7 +806,8 @@ class OFFramework : private boost::noncopyable {
bool tree = true,
bool includeProps = true,
bool utf8 = true,
size_t truncate = 0);
size_t truncate = 0,
bool excludeObservables = false);

/**
* Enable SSL for connections to opflex peers
Expand Down
13 changes: 7 additions & 6 deletions libopflex/ofcore/OFFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,24 @@ void OFFramework::stop() {
pimpl->started = false;
}

void OFFramework::dumpMODB(const string& file) {
void OFFramework::dumpMODB(const string& file, bool excludeObservables) {
MOSerializer& serializer = pimpl->processor.getSerializer();
serializer.dumpMODB(file);
serializer.dumpMODB(file, excludeObservables);
}

void OFFramework::dumpMODB(FILE* file) {
void OFFramework::dumpMODB(FILE* file, bool excludeObservables) {
MOSerializer& serializer = pimpl->processor.getSerializer();
serializer.dumpMODB(file);
serializer.dumpMODB(file, excludeObservables);
}

void OFFramework::prettyPrintMODB(std::ostream& output,
bool tree,
bool includeProps,
bool utf8,
size_t truncate) {
size_t truncate,
bool excludeObservables) {
MOSerializer& serializer = pimpl->processor.getSerializer();
serializer.displayMODB(output, tree, includeProps, utf8, truncate);
serializer.displayMODB(output, tree, includeProps, utf8, truncate, excludeObservables);
}

void OFFramework::setOpflexIdentity(const string& name,
Expand Down
3 changes: 0 additions & 3 deletions libopflex/ofcore/test/OFFramework_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
# include <config.h>
#endif


#include "config.h"

#include <boost/test/unit_test.hpp>

#include "opflex/ofcore/OFFramework.h"
Expand Down

0 comments on commit 0e8b729

Please sign in to comment.