Skip to content

Commit

Permalink
Merge pull request #765 from fledge-iot/FOGL-6693
Browse files Browse the repository at this point in the history
FOGL-6693
  • Loading branch information
ashish-jabble authored Aug 3, 2022
2 parents 9284c34 + c9f4dd0 commit 4fb9395
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
26 changes: 24 additions & 2 deletions python/fledge/plugins/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import datetime

__author__ = "Amarendra Kumar Sinha"
from fledge.services.core.api import utils as api_utils

__author__ = "Amarendra Kumar Sinha, Ashish Jabble"
__copyright__ = "Copyright (c) 2017 OSIsoft, LLC"
__license__ = "Apache 2.0"
__version__ = "${VERSION}"

DEPRECATED_BIT_POSITION = 7
DEPRECATED_BIT_MASK_VALUE = 1 << DEPRECATED_BIT_POSITION
PERSIST_DATA_BIT_POSITION = 3


def get_diff(old, new):
Expand All @@ -31,7 +34,7 @@ def get_diff(old, new):
def local_timestamp():
"""
:return: str - current time stamp with microseconds and machine timezone info
:example '2018-05-08 14:06:40.517313+05:30'
:example: '2018-05-08 14:06:40.51731305:30'
"""
return str(datetime.datetime.now(datetime.timezone.utc).astimezone())

Expand All @@ -51,3 +54,22 @@ def bit_at_given_position_set_or_unset(n, k):
"""
new_num = n >> k
return new_num & 1


def get_persist_plugins():
""" Get a list of south, north, filter types plugins that can persist data for a service
:return: list - plugins
:example: ["OMF"]
"""
plugin_list = []
supported_persist_dirs = ["south", "north", "filter"]
for plugin_type in supported_persist_dirs:
libs = api_utils.find_c_plugin_libs(plugin_type)
for name, _type in libs:
if _type == 'binary':
jdoc = api_utils.get_plugin_info(name, dir=plugin_type)
if jdoc:
if 'flag' in jdoc:
if bit_at_given_position_set_or_unset(jdoc['flag'], PERSIST_DATA_BIT_POSITION):
plugin_list.append(jdoc['name'])
return plugin_list
31 changes: 28 additions & 3 deletions python/fledge/services/core/api/plugins/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

from fledge.common.plugin_discovery import PluginDiscovery
from fledge.common.storage_client.payload_builder import PayloadBuilder
from fledge.plugins.common import utils as common_utils
from fledge.services.core import connect
from fledge.services.core.service_registry.service_registry import ServiceRegistry


__author__ = "Mark Riddoch, Ashish Jabble"
__copyright__ = "Copyright (c) 2022 Dianomic Systems Inc."
Expand All @@ -19,9 +22,23 @@

_help = """
---------------------------------------------------------------------------------------
| GET | /fledge/service/{service_name}/persist |
| GET POST DELETE | /fledge/service/{service_name}/plugin/{plugin_name}/data |
---------------------------------------------------------------------------------------
"""
FORBIDDEN_MSG = "Resource you were trying to reach is absolutely forbidden!"

async def get_persist_plugins(request: web.Request) -> web.Response:
"""
Args:
request:
Returns:
list of plugins that have SP_PERSIST_DATA flag set in plugin info
:Example:
curl -sX GET "http://localhost:8081/fledge/service/{service_name}/persist"
"""
plugins = common_utils.get_persist_plugins()
return web.json_response({'persistent': plugins})


async def get(request: web.Request) -> web.Response:
Expand Down Expand Up @@ -70,6 +87,10 @@ async def add(request: web.Request) -> web.Response:
try:
service = request.match_info.get('service_name', None)
plugin = request.match_info.get('plugin_name', None)
svc_records = ServiceRegistry.all()
for service_record in svc_records:
if service_record._name == service and int(service_record._status) == 1:
raise web.HTTPForbidden(reason=FORBIDDEN_MSG)
storage_client = connect.get_storage_async()
await _find_svc_and_plugin(storage_client, service, plugin)
key = "{}{}".format(service, plugin)
Expand All @@ -78,13 +99,13 @@ async def add(request: web.Request) -> web.Response:
if response:
msg = "{} key already exist.".format(key)
return web.HTTPConflict(reason=msg, body=json.dumps({"message": msg}))
data = await request.json()
data = data["data"]
payload = await request.json()
data = payload.get("data")
if data is not None:
payload = PayloadBuilder().INSERT(key=key, data=data)
await storage_client.insert_into_tbl('plugin_data', payload.payload())
else:
raise web.HTTPBadRequest(reason=msg, body=json.dumps({"message": "Malformed data in payload"}))
raise KeyError('Malformed data in payload!')
except KeyError as err:
msg = str(err)
raise web.HTTPBadRequest(reason=msg, body=json.dumps({"message": msg}))
Expand All @@ -111,6 +132,10 @@ async def delete(request: web.Request) -> web.Response:
service = request.match_info.get('service_name', None)
service = urllib.parse.unquote(service) if service is not None else None
plugin = request.match_info.get('plugin_name', None)
svc_records = ServiceRegistry.all()
for service_record in svc_records:
if service_record._name == service and int(service_record._status) == 1:
raise web.HTTPForbidden(reason=FORBIDDEN_MSG)
storage_client = connect.get_storage_async()
key = "{}{}".format(service, plugin)
payload = PayloadBuilder().WHERE(['key', '=', key])
Expand Down
1 change: 1 addition & 0 deletions python/fledge/services/core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def setup(app):
app.router.add_route('DELETE', '/fledge/plugins/{type}/{name}', plugins_remove.remove_plugin)

# plugin data
app.router.add_route('GET', '/fledge/service/{service_name}/persist', plugin_data.get_persist_plugins)
app.router.add_route('GET', '/fledge/service/{service_name}/plugin/{plugin_name}/data', plugin_data.get)
app.router.add_route('POST', '/fledge/service/{service_name}/plugin/{plugin_name}/data', plugin_data.add)
app.router.add_route('DELETE', '/fledge/service/{service_name}/plugin/{plugin_name}/data', plugin_data.delete)
Expand Down

0 comments on commit 4fb9395

Please sign in to comment.