Skip to content

Commit

Permalink
Merge pull request #753 from fledge-iot/FOGL-6719
Browse files Browse the repository at this point in the history
FOGL-6719 - Deprecate asset record handling in Rest API
  • Loading branch information
ashish-jabble authored Aug 3, 2022
2 parents 29a1278 + ec33e3b commit 9284c34
Show file tree
Hide file tree
Showing 32 changed files with 863 additions and 256 deletions.
22 changes: 20 additions & 2 deletions C/common/asset_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ AssetTracker::AssetTracker(ManagementClient *mgtClient, string service)
/**
* Fetch all asset tracking tuples from DB and populate local cache
*
* Return the vector of deprecated asset names
*
* @param plugin Plugin name
* @param event Event name
*/
Expand All @@ -51,7 +53,9 @@ void AssetTracker::populateAssetTrackingCache(string /*plugin*/, string /*event*
for (AssetTrackingTuple* & rec : vec)
{
assetTrackerTuplesCache.insert(rec);
//Logger::getLogger()->info("Added asset tracker tuple to cache: '%s'", rec->assetToString().c_str());

Logger::getLogger()->debug("Added asset tracker tuple to cache: '%s'",
rec->assetToString().c_str());
}
delete (&vec);
}
Expand All @@ -60,8 +64,9 @@ void AssetTracker::populateAssetTrackingCache(string /*plugin*/, string /*event*
Logger::getLogger()->error("Failed to populate asset tracking tuples' cache");
return;
}
}

return;
}

/**
* Check local cache for a given asset tracking tuple
Expand All @@ -81,6 +86,19 @@ bool AssetTracker::checkAssetTrackingCache(AssetTrackingTuple& tuple)
return true;
}

AssetTrackingTuple* AssetTracker::findAssetTrackingCache(AssetTrackingTuple& tuple)
{
AssetTrackingTuple *ptr = &tuple;
std::unordered_set<AssetTrackingTuple*>::const_iterator it = assetTrackerTuplesCache.find(ptr);
if (it == assetTrackerTuplesCache.end())
{
return NULL;
}
else
{
return *it;
}
}

/**
* Add asset tracking tuple via microservice management API and in cache
Expand Down
34 changes: 28 additions & 6 deletions C/common/include/asset_tracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,40 @@ class AssetTrackingTuple {
std::string assetToString()
{
std::ostringstream o;
o << "service:" << m_serviceName << ", plugin:" << m_pluginName << ", asset:" << m_assetName << ", event:" << m_eventName;
o << "service:" << m_serviceName <<
", plugin:" << m_pluginName <<
", asset:" << m_assetName <<
", event:" << m_eventName <<
", deprecated:" << m_deprecated;
return o.str();
}

inline bool operator==(const AssetTrackingTuple& x) const
{
return ( x.m_serviceName==m_serviceName && x.m_pluginName==m_pluginName && x.m_assetName==m_assetName && x.m_eventName==m_eventName);
return ( x.m_serviceName==m_serviceName &&
x.m_pluginName==m_pluginName &&
x.m_assetName==m_assetName &&
x.m_eventName==m_eventName);
}

AssetTrackingTuple(const std::string& service, const std::string& plugin,
const std::string& asset, const std::string& event) :
m_serviceName(service), m_pluginName(plugin),
m_assetName(asset), m_eventName(event)
AssetTrackingTuple(const std::string& service,
const std::string& plugin,
const std::string& asset,
const std::string& event,
const bool& deprecated = false) :
m_serviceName(service),
m_pluginName(plugin),
m_assetName(asset),
m_eventName(event),
m_deprecated(deprecated)
{}

std::string& getAssetName() { return m_assetName; };
bool isDeprecated() { return m_deprecated; };
void unDeprecate() { m_deprecated = false; };

private:
bool m_deprecated;
};

struct AssetTrackingTuplePtrEqual {
Expand Down Expand Up @@ -90,6 +110,8 @@ class AssetTracker {
static AssetTracker *getAssetTracker();
void populateAssetTrackingCache(std::string plugin, std::string event);
bool checkAssetTrackingCache(AssetTrackingTuple& tuple);
AssetTrackingTuple*
findAssetTrackingCache(AssetTrackingTuple& tuple);
void addAssetTrackingTuple(AssetTrackingTuple& tuple);
void addAssetTrackingTuple(std::string plugin, std::string asset, std::string event);
std::string
Expand Down
1 change: 1 addition & 0 deletions C/common/include/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Expression {
case JSON_COLUMN:
case BOOL_COLUMN:
case STRING_COLUMN:
case NULL_COLUMN:
break;
case INT_COLUMN:
json << m_value.ival;
Expand Down
16 changes: 16 additions & 0 deletions C/common/include/insert.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class InsertValue {
strncpy(m_value.str, s.c_str(), s.length() + 1);
m_type = JSON_COLUMN;
};

// Insert a NULL value for the given column
InsertValue(const std::string& column) :
m_column(column)
{
m_type = NULL_COLUMN;
m_value.str = NULL;
}

InsertValue(const InsertValue& rhs) : m_column(rhs.m_column)
{
m_type = rhs.m_type;
Expand All @@ -78,6 +87,9 @@ class InsertValue {
case JSON_COLUMN: // Internally stored a a string
m_value.str = strdup(rhs.m_value.str);
break;
case NULL_COLUMN:
m_value.str = NULL;
break;
case BOOL_COLUMN:
// TODO
break;
Expand Down Expand Up @@ -112,6 +124,10 @@ class InsertValue {
case STRING_COLUMN:
json << "\"" << m_value.str << "\"";
break;
case NULL_COLUMN:
// JSON output for NULL value
json << "null";
break;
}
return json.str();
}
Expand Down
3 changes: 3 additions & 0 deletions C/common/include/management_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class ManagementClient {
std::vector<std::pair<std::string, std::string> > >& endpoints);
bool deleteProxy(const std::string& serviceName);
const std::string getUrlbase() { return m_urlbase.str(); }
AssetTrackingTuple* getAssetTrackingTuple(const std::string& serviceName,
const std::string& assetName,
const std::string& event);

private:
std::ostringstream m_urlbase;
Expand Down
3 changes: 2 additions & 1 deletion C/common/include/resultset.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ typedef enum column_type {
NUMBER_COLUMN,
STRING_COLUMN,
BOOL_COLUMN,
JSON_COLUMN
JSON_COLUMN,
NULL_COLUMN
} ColumnType;


Expand Down
123 changes: 121 additions & 2 deletions C/common/management_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,25 @@ std::vector<AssetTrackingTuple*>& ManagementClient::getAssetTrackingTuples(const
{
throw runtime_error("Expected asset tracker tuple to be an object");
}
AssetTrackingTuple *tuple = new AssetTrackingTuple(rec["service"].GetString(), rec["plugin"].GetString(), rec["asset"].GetString(), rec["event"].GetString());

// Note: deprecatedTimestamp NULL value is returned as ""
// otherwise it's a string DATE
bool deprecated = rec.HasMember("deprecatedTimestamp") &&
strlen(rec["deprecatedTimestamp"].GetString());

AssetTrackingTuple *tuple = new AssetTrackingTuple(rec["service"].GetString(),
rec["plugin"].GetString(),
rec["asset"].GetString(),
rec["event"].GetString(),
deprecated);

m_logger->debug("Adding AssetTracker tuple for service %s: %s:%s:%s, " \
"deprecated state is %d",
rec["service"].GetString(),
rec["plugin"].GetString(),
rec["asset"].GetString(),
rec["event"].GetString(),
deprecated);
vec->push_back(tuple);
}
}
Expand Down Expand Up @@ -809,7 +827,9 @@ std::vector<AssetTrackingTuple*>& ManagementClient::getAssetTrackingTuples(const
* @return whether operation was successful
*/
bool ManagementClient::addAssetTrackingTuple(const std::string& service,
const std::string& plugin, const std::string& asset, const std::string& event)
const std::string& plugin,
const std::string& asset,
const std::string& event)
{
ostringstream convert;

Expand Down Expand Up @@ -1327,3 +1347,102 @@ bool ManagementClient::deleteProxy(const std::string& serviceName)
}
return false;
}
/**
* Get the asset tracking tuple
* for a service and asset name
*
* @param serviceName The serviceName to restrict data fetch
* @param assetName The asset name that belongs to the service
* @param event The associated event type
* @return A vector of pointers to AssetTrackingTuple objects allocated on heap
*/
AssetTrackingTuple* ManagementClient::getAssetTrackingTuple(const std::string& serviceName,
const std::string& assetName,
const std::string& event)
{
AssetTrackingTuple* tuple = NULL;
try {
string url = "/fledge/track";
if (serviceName == "" && assetName == "" && event == "")
{
m_logger->error("Failed to fetch asset tracking tuple: " \
"service name, asset name and event type are required.");
throw new exception();
}

url += "?service=" + urlEncode(serviceName);
url += "&asset=" + urlEncode(assetName) + "&event=" + event;

auto res = this->getHttpClient()->request("GET", url.c_str());
Document doc;
string response = res->content.string();
doc.Parse(response.c_str());
if (doc.HasParseError())
{
bool httpError = (isdigit(response[0]) &&
isdigit(response[1]) &&
isdigit(response[2]) &&
response[3]==':');
m_logger->error("%s fetch asset tracking tuple: %s\n",
httpError?"HTTP error during":"Failed to parse result of",
response.c_str());
throw new exception();
}
else if (doc.HasMember("message"))
{
m_logger->error("Failed to fetch asset tracking tuple: %s.",
doc["message"].GetString());
throw new exception();
}
else
{
const rapidjson::Value& trackArray = doc["track"];
if (trackArray.IsArray())
{
// Process every row and create the AssetTrackingTuple object
for (auto& rec : trackArray.GetArray())
{
if (!rec.IsObject())
{
throw runtime_error("Expected asset tracker tuple to be an object");
}

// Note: deprecatedTimestamp NULL value is returned as ""
// otherwise it's a string DATE
bool deprecated = rec.HasMember("deprecatedTimestamp") &&
strlen(rec["deprecatedTimestamp"].GetString());

// Create a new AssetTrackingTuple object, to be freed by the caller
tuple = new AssetTrackingTuple(rec["service"].GetString(),
rec["plugin"].GetString(),
rec["asset"].GetString(),
rec["event"].GetString(),
deprecated);

m_logger->debug("Adding AssetTracker tuple for service %s: %s:%s:%s, " \
"deprecated state is %d",
rec["service"].GetString(),
rec["plugin"].GetString(),
rec["asset"].GetString(),
rec["event"].GetString(),
deprecated);
}
}
else
{
throw runtime_error("Expected array of rows in asset track tuples array");
}

return tuple;
}
} catch (const SimpleWeb::system_error &e) {
m_logger->error("Fetch/parse of asset tracking tuples for service %s failed: %s.",
serviceName.c_str(),
e.what());
} catch (...) {
m_logger->error("Unexpected exception when retrieving asset tuples for service %s",
serviceName.c_str());
}

return tuple;
}
Loading

0 comments on commit 9284c34

Please sign in to comment.