Skip to content

Commit

Permalink
JunOS sensor_path as JSON ...
Browse files Browse the repository at this point in the history
  • Loading branch information
scuzzilla committed Jun 26, 2023
1 parent c464599 commit f11f0d8
Showing 1 changed file with 171 additions and 141 deletions.
312 changes: 171 additions & 141 deletions src/dataManipulation/data_manipulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,161 +270,191 @@ bool DataManipulation::JuniperUpdate(gnmi::SubscribeResponse &juniper_stream,
// ---> string name = 1;
// ---> map<string, string> key = 2;

//std::string value;
std::string sensor_path;
//std::cout << "-------> " << jup.ByteSizeLong() << "\n\n";
// sensor_path as JSON
if (juniper_stream.has_update()) {
const auto &jup = juniper_stream.update();
// The Notification MUST include the timestamp field
std::uint64_t notification_timestamp = jup.timestamp();
//std::cout << "DebugString: " << jup.prefix().Utf8DebugString()
// << "\n";

int path_idx = 0;
sensor_path.clear();
Json::Value sensor_path(Json::arrayValue);

while (path_idx < jup.prefix().elem_size()) {
// first partial path with filters
if (path_idx == 0 &&
jup.prefix().elem().at(path_idx).key_size() > 0) {
//std::cout << "/" << jup.prefix().elem().at(path_idx).name();
sensor_path.append("/");
sensor_path.append(jup.prefix().elem().at(path_idx).name());
int filter = 1;
for (const auto &[key, value] :
jup.prefix().elem().at(path_idx).key()) {
// only one filter
if (jup.prefix().elem().at(path_idx).key_size() == 1) {
//std::cout << "[" << key << "=" << value << "]";
sensor_path.append("[");
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append("]");
path_idx++;
continue;
}
// multiple filters
if (jup.prefix().elem().at(path_idx).key_size() > 1) {
// first filter
if (filter == 1) {
//std::cout << "[" << key << "=" << value
// << " and ";
sensor_path.append("[");
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append(" and ");
filter++;
continue;
}
// last filter
if (filter ==
jup.prefix().elem().at(path_idx).key_size()) {
//std::cout << key << "=" << value << "]";
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append("]");
filter++;
continue;
}
// in-between filters
if (filter > 0) {
//std::cout << key << "=" << value << " and ";
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append(" and ");
filter++;
continue;
}
}
}
//std::cout << "/";
sensor_path.append("/");
path_idx++;
continue;
}
// first partial path without filters
if (path_idx == 0) {
//std::cout << "/" << jup.prefix().elem().at(path_idx).name()
// << "/";
sensor_path.append("/");
sensor_path.append(jup.prefix().elem().at(path_idx).name());
sensor_path.append("/");
path_idx++;
continue;
}
// in-between paths with filters
Json::Value path_element;
path_element["name"] = jup.prefix().elem().at(path_idx).name();

// handling paths with filters
if (jup.prefix().elem().at(path_idx).key_size() > 0) {
//std::cout << jup.prefix().elem().at(path_idx).name();
sensor_path.append(jup.prefix().elem().at(path_idx).name());
int filter = 1;
Json::Value filters;
for (const auto &[key, value] :
jup.prefix().elem().at(path_idx).key()) {
// only one filter
if (jup.prefix().elem().at(path_idx).key_size() == 1) {
//std::cout << "[" << key << "=" << value << "]";
sensor_path.append("[");
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append("]");
path_idx++;
continue;
}
// multiple filters
if (jup.prefix().elem().at(path_idx).key_size() > 1) {
// first filter
if (filter == 1) {
//std::cout << "[" << key << "=" << value
// << " and ";
sensor_path.append("[");
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append(" and ");
filter++;
continue;
}
// last filter
if (filter ==
jup.prefix().elem().at(path_idx).key_size()) {
//std::cout << key << "=" << value << "]";
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append("]");
filter++;
continue;
}
// in-between filters
if (filter > 0) {
//std::cout << key << "=" << value << " and ";
sensor_path.append(key);
sensor_path.append("=");
sensor_path.append(value);
sensor_path.append(" and ");
filter++;
continue;
}
}
jup.prefix().elem().at(path_idx).key()) {
filters[key] = value;
}
//std::cout << "/";
sensor_path.append("/");
path_idx++;
continue;
path_element["filters"] = filters;
}

// no filtering
//std::cout << jup.prefix().elem().at(path_idx).name() << "/";
sensor_path.append(jup.prefix().elem().at(path_idx).name());
sensor_path.append("/");
sensor_path.append(path_element);
path_idx++;
}

root["sensor_path"] = sensor_path;
root["notification_timestamp"] = notification_timestamp;
//std::cout << "sensor_path: " << sensor_path << "\n";

// sensor_path as string
//std::string value;
//std::string sensor_path;

//std::cout << "-------> " << jup.ByteSizeLong() << "\n\n";
//if (juniper_stream.has_update()) {
// const auto &jup = juniper_stream.update();
// // The Notification MUST include the timestamp field
// std::uint64_t notification_timestamp = jup.timestamp();
// //std::cout << "DebugString: " << jup.prefix().Utf8DebugString()
// // << "\n";
// int path_idx = 0;
// sensor_path.clear();
// while (path_idx < jup.prefix().elem_size()) {
// // first partial path with filters
// if (path_idx == 0 &&
// jup.prefix().elem().at(path_idx).key_size() > 0) {
// //std::cout << "/" << jup.prefix().elem().at(path_idx).name();
// sensor_path.append("/");
// sensor_path.append(jup.prefix().elem().at(path_idx).name());
// int filter = 1;
// for (const auto &[key, value] :
// jup.prefix().elem().at(path_idx).key()) {
// // only one filter
// if (jup.prefix().elem().at(path_idx).key_size() == 1) {
// //std::cout << "[" << key << "=" << value << "]";
// sensor_path.append("[");
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append("]");
// path_idx++;
// continue;
// }
// // multiple filters
// if (jup.prefix().elem().at(path_idx).key_size() > 1) {
// // first filter
// if (filter == 1) {
// //std::cout << "[" << key << "=" << value
// // << " and ";
// sensor_path.append("[");
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append(" and ");
// filter++;
// continue;
// }
// // last filter
// if (filter ==
// jup.prefix().elem().at(path_idx).key_size()) {
// //std::cout << key << "=" << value << "]";
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append("]");
// filter++;
// continue;
// }
// // in-between filters
// if (filter > 0) {
// //std::cout << key << "=" << value << " and ";
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append(" and ");
// filter++;
// continue;
// }
// }
// }
// //std::cout << "/";
// sensor_path.append("/");
// path_idx++;
// continue;
// }
// // first partial path without filters
// if (path_idx == 0) {
// //std::cout << "/" << jup.prefix().elem().at(path_idx).name()
// // << "/";
// sensor_path.append("/");
// sensor_path.append(jup.prefix().elem().at(path_idx).name());
// sensor_path.append("/");
// path_idx++;
// continue;
// }
// // in-between paths with filters
// if (jup.prefix().elem().at(path_idx).key_size() > 0) {
// //std::cout << jup.prefix().elem().at(path_idx).name();
// sensor_path.append(jup.prefix().elem().at(path_idx).name());
// int filter = 1;
// for (const auto &[key, value] :
// jup.prefix().elem().at(path_idx).key()) {
// // only one filter
// if (jup.prefix().elem().at(path_idx).key_size() == 1) {
// //std::cout << "[" << key << "=" << value << "]";
// sensor_path.append("[");
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append("]");
// path_idx++;
// continue;
// }
// // multiple filters
// if (jup.prefix().elem().at(path_idx).key_size() > 1) {
// // first filter
// if (filter == 1) {
// //std::cout << "[" << key << "=" << value
// // << " and ";
// sensor_path.append("[");
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append(" and ");
// filter++;
// continue;
// }
// // last filter
// if (filter ==
// jup.prefix().elem().at(path_idx).key_size()) {
// //std::cout << key << "=" << value << "]";
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append("]");
// filter++;
// continue;
// }
// // in-between filters
// if (filter > 0) {
// //std::cout << key << "=" << value << " and ";
// sensor_path.append(key);
// sensor_path.append("=");
// sensor_path.append(value);
// sensor_path.append(" and ");
// filter++;
// continue;
// }
// }
// }
// //std::cout << "/";
// sensor_path.append("/");
// path_idx++;
// continue;
// }

// // no filtering
// //std::cout << jup.prefix().elem().at(path_idx).name() << "/";
// sensor_path.append(jup.prefix().elem().at(path_idx).name());
// sensor_path.append("/");
// path_idx++;
// }

// root["sensor_path"] = sensor_path;
// root["notification_timestamp"] = notification_timestamp;
// std::cout << "sensor_path: " << sensor_path << "\n";

// From the second update().update() extract all the values
// associated with a specific sensor path
Expand Down

0 comments on commit f11f0d8

Please sign in to comment.