Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "Code for control-node retrieving config via cassandra."
- Loading branch information
Showing
12 changed files
with
299 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include "config_cass2json_adapter.h" | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
const string ConfigCass2JsonAdapter::prop_prefix = "prop:"; | ||
const string ConfigCass2JsonAdapter::meta_prefix = "META:"; | ||
const string ConfigCass2JsonAdapter::comma_str = ","; | ||
|
||
ConfigCass2JsonAdapter::ConfigCass2JsonAdapter(const CassColumnKVVec &cdvec) | ||
: prop_plen_(prop_prefix.size()), | ||
meta_plen_(meta_prefix.size()) { | ||
CreateJsonString(cdvec); | ||
} | ||
|
||
// Return true if the caller needs to append a comma. False otherwise. | ||
bool ConfigCass2JsonAdapter::AddOneEntry(const CassColumnKVVec &cdvec, int i) { | ||
// If the key has 'prop:' at the start, remove it. | ||
if (cdvec.at(i).key.substr(0, prop_plen_) == prop_prefix) { | ||
doc_string_ += string( | ||
"\"" + cdvec.at(i).key.substr(prop_plen_) + | ||
"\"" + ": " + cdvec.at(i).value); | ||
} else if (cdvec.at(i).key.substr(0, meta_plen_) == meta_prefix) { | ||
// If the key has 'META:' at the start, ignore the column. | ||
return false; | ||
} else if (cdvec.at(i).key.compare("type") == 0) { | ||
// Prepend the 'type'. This is "our key", with value being the json | ||
// sub-document containing all other columns. | ||
doc_string_ = string("{\n" + cdvec.at(i).value + ":" + "{\n") + | ||
doc_string_; | ||
return false; | ||
} else { | ||
doc_string_ += string("\"" + cdvec.at(i).key + "\"" + ": " + | ||
cdvec.at(i).value); | ||
} | ||
return true; | ||
} | ||
|
||
bool ConfigCass2JsonAdapter::CreateJsonString(const CassColumnKVVec &cdvec) { | ||
for (size_t i = 0; i < cdvec.size(); ++i) { | ||
if (AddOneEntry(cdvec, i)) { | ||
doc_string_ += comma_str; | ||
} | ||
} | ||
|
||
// Remove the comma after the last entry. | ||
if (doc_string_[doc_string_.size() - 1] == ',') { | ||
doc_string_.erase(doc_string_.size() - 1); | ||
} | ||
|
||
// Add one brace to close out the type's value and one to close out the | ||
// whole json document. | ||
doc_string_ += string("\n}\n}"); | ||
|
||
return true; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#ifndef ctrlplane_config_cass2json_adapter_h | ||
#define ctrlplane_config_cass2json_adapter_h | ||
|
||
#include "json_adapter_data.h" | ||
|
||
// The purpose of this class is to convert key-value pairs received from | ||
// cassandra into one single json string. | ||
// The user will pass a vector of key-value while creating the object. The | ||
// constructor will create a json string, which will then be accessible via the | ||
// doc_string() accessor. | ||
class ConfigCass2JsonAdapter { | ||
public: | ||
ConfigCass2JsonAdapter(const CassColumnKVVec &cdvec); | ||
const std::string &doc_string() { return doc_string_; } | ||
|
||
private: | ||
static const std::string prop_prefix; | ||
static const std::string meta_prefix; | ||
static const std::string comma_str; | ||
bool CreateJsonString(const CassColumnKVVec &cdvec); | ||
bool AddOneEntry(const CassColumnKVVec &cdvec, int i); | ||
|
||
std::string doc_string_; | ||
int prop_plen_; | ||
int meta_plen_; | ||
}; | ||
|
||
#endif // ctrlplane_config_cass2json_adapter_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.