Skip to content

Commit fb94566

Browse files
Merge pull request #769 from fledge-iot/2.0.0RC
v2.0.0 release
2 parents f1452e8 + 6d2870d commit fb94566

File tree

835 files changed

+38052
-12707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

835 files changed

+38052
-12707
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ docs/_build
3939
docs/__pycache__/
4040
docs/.cache/
4141
docs/plugins
42+
docs/services
4243
docs/fledge_plugins.rst
4344

4445
# Compiled Object files
@@ -87,3 +88,9 @@ python/fledge/plugins/south/*
8788
python/fledge/plugins/filter/*
8889
python/fledge/plugins/notificationDelivery/*
8990
python/fledge/plugins/notificationRule/*
91+
92+
# doxygen build
93+
doxy/
94+
95+
# aspell backups
96+
*.bak

C/common/CMakeLists.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
cmake_minimum_required(VERSION 2.4.0)
22

3+
if(COMMAND cmake_policy)
4+
cmake_policy(SET CMP0003 NEW)
5+
endif(COMMAND cmake_policy)
6+
7+
# Get the os name
8+
execute_process(COMMAND bash -c "cat /etc/os-release | grep -w ID | cut -f2 -d'='"
9+
OUTPUT_VARIABLE
10+
OS_NAME
11+
OUTPUT_STRIP_TRAILING_WHITESPACE)
12+
13+
if( POLICY CMP0007 )
14+
cmake_policy( SET CMP0007 NEW )
15+
endif()
316
project(common-lib)
417

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

33+
# Find python3.x dev/lib package
34+
find_package(PkgConfig REQUIRED)
35+
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
36+
pkg_check_modules(PYTHON REQUIRED python3)
37+
else()
38+
if("${OS_NAME}" STREQUAL "mendel")
39+
# We will explicitly set include path later for NumPy.
40+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development )
41+
else()
42+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development NumPy)
43+
endif()
44+
endif()
45+
2046
# Find source files
2147
file(GLOB SOURCES *.cpp)
2248

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

52+
# Add Python 3.x header files
53+
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
54+
include_directories(${PYTHON_INCLUDE_DIRS})
55+
else()
56+
if("${OS_NAME}" STREQUAL "mendel")
57+
# The following command gets the location of NumPy.
58+
execute_process(
59+
COMMAND python3
60+
-c "import numpy; print(numpy.get_include())"
61+
OUTPUT_VARIABLE Python3_NUMPY_INCLUDE_DIRS
62+
OUTPUT_STRIP_TRAILING_WHITESPACE
63+
)
64+
# Now we can add include directories as usual.
65+
include_directories(${Python3_INCLUDE_DIRS} ${Python3_NUMPY_INCLUDE_DIRS})
66+
else()
67+
include_directories(${Python3_INCLUDE_DIRS} ${Python3_NUMPY_INCLUDE_DIRS})
68+
endif()
69+
endif()
70+
71+
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
72+
link_directories(${PYTHON_LIBRARY_DIRS})
73+
else()
74+
link_directories(${Python3_LIBRARY_DIRS})
75+
endif()
76+
2677
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../lib)
2778

2879
# Create shared library
2980
add_library(${PROJECT_NAME} SHARED ${SOURCES})
81+
# Add Python 3.5 library
82+
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
83+
target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})
84+
else()
85+
if("${OS_NAME}" STREQUAL "mendel")
86+
target_link_libraries(${PROJECT_NAME} ${Python3_LIBRARIES})
87+
else()
88+
target_link_libraries(${PROJECT_NAME} ${Python3_LIBRARIES} Python3::NumPy)
89+
endif()
90+
endif()
91+
3092
target_link_libraries(${PROJECT_NAME} ${UUIDLIB})
3193
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
94+
target_link_libraries(${PROJECT_NAME} -lcrypto)
3295

3396
set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 1)
3497

C/common/acl.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Fledge category management
3+
*
4+
* Copyright (c) 2022 Dianomic Systems
5+
*
6+
* Released under the Apache 2.0 Licence
7+
*
8+
* Author: Massimiliano Pinto
9+
*/
10+
11+
#include <logger.h>
12+
#include <stdexcept>
13+
#include <acl.h>
14+
#include <rapidjson/document.h>
15+
#include "rapidjson/error/error.h"
16+
#include "rapidjson/error/en.h"
17+
#include <storage_client.h>
18+
19+
using namespace std;
20+
using namespace rapidjson;
21+
22+
/**
23+
* ACLReason constructor:
24+
* parse input JSON for ACL change reason.
25+
*
26+
* JSON should have string attributes 'reason' and 'argument'
27+
*
28+
* @param json The JSON reason string to parse
29+
* @throws exception ACLReasonMalformed
30+
*/
31+
ACL::ACLReason::ACLReason(const string& json)
32+
{
33+
Document doc;
34+
doc.Parse(json.c_str());
35+
if (doc.HasParseError())
36+
{
37+
Logger::getLogger()->error("ACL Reason parse error in %s: %s at %d",
38+
json.c_str(),
39+
GetParseError_En(doc.GetParseError()),
40+
(unsigned)doc.GetErrorOffset());
41+
throw new ACLReasonMalformed();
42+
}
43+
44+
if (!doc.IsObject())
45+
{
46+
Logger::getLogger()->error("ACL Reason is not a JSON object: %sd",
47+
json.c_str());
48+
throw new ACLReasonMalformed();
49+
}
50+
51+
if (doc.HasMember("reason") && doc["reason"].IsString())
52+
{
53+
m_reason = doc["reason"].GetString();
54+
}
55+
if (doc.HasMember("argument") && doc["argument"].IsString())
56+
{
57+
m_argument = doc["argument"].GetString();
58+
}
59+
}
60+
61+
/**
62+
* ACL constructor:
63+
* parse input JSON for ACL content.
64+
*
65+
* JSON should have string attributes 'name' and 'service' and 'url' arrays
66+
*
67+
* @param json The JSON ACL content to parse
68+
* @throws exception ACLMalformed
69+
*/
70+
ACL::ACL(const string& json)
71+
{
72+
Document doc;
73+
doc.Parse(json.c_str());
74+
if (doc.HasParseError())
75+
{
76+
Logger::getLogger()->error("ACL parse error in %s: %s at %d",
77+
json.c_str(),
78+
GetParseError_En(doc.GetParseError()),
79+
(unsigned)doc.GetErrorOffset());
80+
throw new ACLMalformed();
81+
}
82+
83+
Logger::getLogger()->debug("ACL content is %s", json.c_str());
84+
85+
if (!doc.HasMember("name"))
86+
{
87+
Logger::getLogger()->error("Missing 'name' attribute in ACL JSON data");
88+
throw new ACLMalformed();
89+
}
90+
if (doc.HasMember("name") && doc["name"].IsString())
91+
{
92+
m_name = doc["name"].GetString();
93+
}
94+
95+
// Check for service array item
96+
if (doc.HasMember("service") && doc["service"].IsArray())
97+
{
98+
auto &items = doc["service"];
99+
for (auto& item : items.GetArray())
100+
{
101+
if (!item.IsObject())
102+
{
103+
throw new ACLMalformed();
104+
}
105+
for (Value::ConstMemberIterator itr = item.MemberBegin();
106+
itr != item.MemberEnd();
107+
++itr)
108+
{
109+
// Construct KeyValueItem object
110+
KeyValueItem i(itr->name.GetString(),
111+
itr->value.GetString());
112+
113+
// Add object to the vector
114+
m_service.push_back(i);
115+
}
116+
}
117+
}
118+
119+
// Check for url array item
120+
if (doc.HasMember("url") && doc["url"].IsArray())
121+
{
122+
auto &items = doc["url"];
123+
for (auto& item : items.GetArray())
124+
{
125+
if (!item.IsObject())
126+
{
127+
throw new ACLMalformed();
128+
}
129+
130+
string url = item["url"].GetString();
131+
Value &acl = item["acl"];
132+
vector<KeyValueItem> v_acl;
133+
134+
// Check for acl array
135+
if (acl.IsArray())
136+
{
137+
for (auto& item : acl.GetArray())
138+
{
139+
if (!item.IsObject())
140+
{
141+
throw new ACLMalformed();
142+
}
143+
144+
for (Value::ConstMemberIterator itr = item.MemberBegin();
145+
itr != item.MemberEnd();
146+
++itr)
147+
{
148+
// Construct KeyValueItem object
149+
KeyValueItem item(itr->name.GetString(),
150+
itr->value.GetString());
151+
152+
// Add object to the ACL vector
153+
v_acl.push_back(item);
154+
}
155+
}
156+
157+
}
158+
159+
// Construct UrlItem with url and ACL vector
160+
UrlItem u(url, v_acl);
161+
162+
// Add object to the URL vector
163+
m_url.push_back(u);
164+
}
165+
}
166+
}

C/common/asset_tracking.cpp

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ AssetTracker::AssetTracker(ManagementClient *mgtClient, string service)
4141
/**
4242
* Fetch all asset tracking tuples from DB and populate local cache
4343
*
44+
* Return the vector of deprecated asset names
45+
*
4446
* @param plugin Plugin name
4547
* @param event Event name
4648
*/
@@ -51,7 +53,9 @@ void AssetTracker::populateAssetTrackingCache(string /*plugin*/, string /*event*
5153
for (AssetTrackingTuple* & rec : vec)
5254
{
5355
assetTrackerTuplesCache.insert(rec);
54-
//Logger::getLogger()->info("Added asset tracker tuple to cache: '%s'", rec->assetToString().c_str());
56+
57+
Logger::getLogger()->debug("Added asset tracker tuple to cache: '%s'",
58+
rec->assetToString().c_str());
5559
}
5660
delete (&vec);
5761
}
@@ -60,8 +64,9 @@ void AssetTracker::populateAssetTrackingCache(string /*plugin*/, string /*event*
6064
Logger::getLogger()->error("Failed to populate asset tracking tuples' cache");
6165
return;
6266
}
63-
}
6467

68+
return;
69+
}
6570

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

89+
AssetTrackingTuple* AssetTracker::findAssetTrackingCache(AssetTrackingTuple& tuple)
90+
{
91+
AssetTrackingTuple *ptr = &tuple;
92+
std::unordered_set<AssetTrackingTuple*>::const_iterator it = assetTrackerTuplesCache.find(ptr);
93+
if (it == assetTrackerTuplesCache.end())
94+
{
95+
return NULL;
96+
}
97+
else
98+
{
99+
return *it;
100+
}
101+
}
84102

85103
/**
86104
* Add asset tracking tuple via microservice management API and in cache
@@ -126,3 +144,41 @@ void AssetTracker::addAssetTrackingTuple(string plugin, string asset, string eve
126144
addAssetTrackingTuple(tuple);
127145
}
128146

147+
/**
148+
* Return the name of the service responsible for particulr event of the named asset
149+
*
150+
* @param event The event of interest
151+
* @param asset The asset we are interested in
152+
* @return string The service name of the service that ingests the asset
153+
* @throws exception If the service could not be found
154+
*/
155+
string AssetTracker::getService(const std::string& event, const std::string& asset)
156+
{
157+
// Fetch all asset tracker records
158+
std::vector<AssetTrackingTuple*>& vec = m_mgtClient->getAssetTrackingTuples();
159+
string foundService;
160+
for (AssetTrackingTuple* &rec : vec)
161+
{
162+
// Return first service name with given asset and event
163+
if (rec->m_assetName == asset && rec->m_eventName == event)
164+
{
165+
foundService = rec->m_serviceName;
166+
break;
167+
}
168+
}
169+
170+
delete (&vec);
171+
172+
// Return found service or raise an exception
173+
if (foundService != "")
174+
{
175+
return foundService;
176+
}
177+
else
178+
{
179+
Logger::getLogger()->error("No service found for asset '%s' and event '%s'",
180+
event.c_str(),
181+
asset.c_str());
182+
throw runtime_error("Fetching service for asset not yet implemented");
183+
}
184+
}

0 commit comments

Comments
 (0)