Skip to content

Commit

Permalink
Merge pull request #769 from fledge-iot/2.0.0RC
Browse files Browse the repository at this point in the history
v2.0.0 release
  • Loading branch information
ashish-jabble authored Sep 15, 2022
2 parents f1452e8 + 6d2870d commit fb94566
Show file tree
Hide file tree
Showing 835 changed files with 38,052 additions and 12,707 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ docs/_build
docs/__pycache__/
docs/.cache/
docs/plugins
docs/services
docs/fledge_plugins.rst

# Compiled Object files
Expand Down Expand Up @@ -87,3 +88,9 @@ python/fledge/plugins/south/*
python/fledge/plugins/filter/*
python/fledge/plugins/notificationDelivery/*
python/fledge/plugins/notificationRule/*

# doxygen build
doxy/

# aspell backups
*.bak
63 changes: 63 additions & 0 deletions C/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
cmake_minimum_required(VERSION 2.4.0)

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

# Get the os name
execute_process(COMMAND bash -c "cat /etc/os-release | grep -w ID | cut -f2 -d'='"
OUTPUT_VARIABLE
OS_NAME
OUTPUT_STRIP_TRAILING_WHITESPACE)

if( POLICY CMP0007 )
cmake_policy( SET CMP0007 NEW )
endif()
project(common-lib)

set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
Expand All @@ -17,18 +30,68 @@ endif()
find_package(Boost 1.53.0 COMPONENTS ${BOOST_COMPONENTS} REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIR})

# Find python3.x dev/lib package
find_package(PkgConfig REQUIRED)
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
pkg_check_modules(PYTHON REQUIRED python3)
else()
if("${OS_NAME}" STREQUAL "mendel")
# We will explicitly set include path later for NumPy.
find_package(Python3 REQUIRED COMPONENTS Interpreter Development )
else()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development NumPy)
endif()
endif()

# Find source files
file(GLOB SOURCES *.cpp)

# Include header files
include_directories(include ../services/common/include ../common/include ../thirdparty/rapidjson/include ../thirdparty/Simple-Web-Server)

# Add Python 3.x header files
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
include_directories(${PYTHON_INCLUDE_DIRS})
else()
if("${OS_NAME}" STREQUAL "mendel")
# The following command gets the location of NumPy.
execute_process(
COMMAND python3
-c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE Python3_NUMPY_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Now we can add include directories as usual.
include_directories(${Python3_INCLUDE_DIRS} ${Python3_NUMPY_INCLUDE_DIRS})
else()
include_directories(${Python3_INCLUDE_DIRS} ${Python3_NUMPY_INCLUDE_DIRS})
endif()
endif()

if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
link_directories(${PYTHON_LIBRARY_DIRS})
else()
link_directories(${Python3_LIBRARY_DIRS})
endif()

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../lib)

# Create shared library
add_library(${PROJECT_NAME} SHARED ${SOURCES})
# Add Python 3.5 library
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})
else()
if("${OS_NAME}" STREQUAL "mendel")
target_link_libraries(${PROJECT_NAME} ${Python3_LIBRARIES})
else()
target_link_libraries(${PROJECT_NAME} ${Python3_LIBRARIES} Python3::NumPy)
endif()
endif()

target_link_libraries(${PROJECT_NAME} ${UUIDLIB})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
target_link_libraries(${PROJECT_NAME} -lcrypto)

set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 1)

Expand Down
166 changes: 166 additions & 0 deletions C/common/acl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Fledge category management
*
* Copyright (c) 2022 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Massimiliano Pinto
*/

#include <logger.h>
#include <stdexcept>
#include <acl.h>
#include <rapidjson/document.h>
#include "rapidjson/error/error.h"
#include "rapidjson/error/en.h"
#include <storage_client.h>

using namespace std;
using namespace rapidjson;

/**
* ACLReason constructor:
* parse input JSON for ACL change reason.
*
* JSON should have string attributes 'reason' and 'argument'
*
* @param json The JSON reason string to parse
* @throws exception ACLReasonMalformed
*/
ACL::ACLReason::ACLReason(const string& json)
{
Document doc;
doc.Parse(json.c_str());
if (doc.HasParseError())
{
Logger::getLogger()->error("ACL Reason parse error in %s: %s at %d",
json.c_str(),
GetParseError_En(doc.GetParseError()),
(unsigned)doc.GetErrorOffset());
throw new ACLReasonMalformed();
}

if (!doc.IsObject())
{
Logger::getLogger()->error("ACL Reason is not a JSON object: %sd",
json.c_str());
throw new ACLReasonMalformed();
}

if (doc.HasMember("reason") && doc["reason"].IsString())
{
m_reason = doc["reason"].GetString();
}
if (doc.HasMember("argument") && doc["argument"].IsString())
{
m_argument = doc["argument"].GetString();
}
}

/**
* ACL constructor:
* parse input JSON for ACL content.
*
* JSON should have string attributes 'name' and 'service' and 'url' arrays
*
* @param json The JSON ACL content to parse
* @throws exception ACLMalformed
*/
ACL::ACL(const string& json)
{
Document doc;
doc.Parse(json.c_str());
if (doc.HasParseError())
{
Logger::getLogger()->error("ACL parse error in %s: %s at %d",
json.c_str(),
GetParseError_En(doc.GetParseError()),
(unsigned)doc.GetErrorOffset());
throw new ACLMalformed();
}

Logger::getLogger()->debug("ACL content is %s", json.c_str());

if (!doc.HasMember("name"))
{
Logger::getLogger()->error("Missing 'name' attribute in ACL JSON data");
throw new ACLMalformed();
}
if (doc.HasMember("name") && doc["name"].IsString())
{
m_name = doc["name"].GetString();
}

// Check for service array item
if (doc.HasMember("service") && doc["service"].IsArray())
{
auto &items = doc["service"];
for (auto& item : items.GetArray())
{
if (!item.IsObject())
{
throw new ACLMalformed();
}
for (Value::ConstMemberIterator itr = item.MemberBegin();
itr != item.MemberEnd();
++itr)
{
// Construct KeyValueItem object
KeyValueItem i(itr->name.GetString(),
itr->value.GetString());

// Add object to the vector
m_service.push_back(i);
}
}
}

// Check for url array item
if (doc.HasMember("url") && doc["url"].IsArray())
{
auto &items = doc["url"];
for (auto& item : items.GetArray())
{
if (!item.IsObject())
{
throw new ACLMalformed();
}

string url = item["url"].GetString();
Value &acl = item["acl"];
vector<KeyValueItem> v_acl;

// Check for acl array
if (acl.IsArray())
{
for (auto& item : acl.GetArray())
{
if (!item.IsObject())
{
throw new ACLMalformed();
}

for (Value::ConstMemberIterator itr = item.MemberBegin();
itr != item.MemberEnd();
++itr)
{
// Construct KeyValueItem object
KeyValueItem item(itr->name.GetString(),
itr->value.GetString());

// Add object to the ACL vector
v_acl.push_back(item);
}
}

}

// Construct UrlItem with url and ACL vector
UrlItem u(url, v_acl);

// Add object to the URL vector
m_url.push_back(u);
}
}
}
60 changes: 58 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 Expand Up @@ -126,3 +144,41 @@ void AssetTracker::addAssetTrackingTuple(string plugin, string asset, string eve
addAssetTrackingTuple(tuple);
}

/**
* Return the name of the service responsible for particulr event of the named asset
*
* @param event The event of interest
* @param asset The asset we are interested in
* @return string The service name of the service that ingests the asset
* @throws exception If the service could not be found
*/
string AssetTracker::getService(const std::string& event, const std::string& asset)
{
// Fetch all asset tracker records
std::vector<AssetTrackingTuple*>& vec = m_mgtClient->getAssetTrackingTuples();
string foundService;
for (AssetTrackingTuple* &rec : vec)
{
// Return first service name with given asset and event
if (rec->m_assetName == asset && rec->m_eventName == event)
{
foundService = rec->m_serviceName;
break;
}
}

delete (&vec);

// Return found service or raise an exception
if (foundService != "")
{
return foundService;
}
else
{
Logger::getLogger()->error("No service found for asset '%s' and event '%s'",
event.c_str(),
asset.c_str());
throw runtime_error("Fetching service for asset not yet implemented");
}
}
Loading

0 comments on commit fb94566

Please sign in to comment.