Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RDK-54420] added annotation APIs to Dobby #349

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions AppInfrastructure/Public/Dobby/IDobbyProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ class IDobbyProxy : public AICommon::Notifier<IDobbyProxyEvents>

virtual bool removeContainerMount(int32_t descriptor, const std::string& source) const = 0;

virtual bool addAnnotation(int32_t cd,
const std::string& key,
const std::string& value) const = 0;

virtual bool removeAnnotation(int32_t cd,
const std::string& key) const = 0;

virtual bool execInContainer(int32_t cd,
const std::string& options,
const std::string& command) const = 0;
Expand Down
7 changes: 7 additions & 0 deletions client/lib/include/DobbyProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class DobbyProxy : public IDobbyProxy
const std::vector<std::string>& mountFlags,
const std::string& mountData) const override;

bool addAnnotation(int32_t cd,
const std::string& key,
const std::string& value) const override;

bool removeAnnotation(int32_t cd,
const std::string& key) const override;

bool removeContainerMount(int32_t descriptor, const std::string& source) const override;

bool execInContainer(int32_t cd,
Expand Down
64 changes: 64 additions & 0 deletions client/lib/source/DobbyProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,71 @@ bool DobbyProxy::addContainerMount(int32_t cd, const std::string& source, const
AI_LOG_FN_EXIT();
return result;
}
// -----------------------------------------------------------------------------
/**
* @brief adds a key value pair to the container annotation
*
* @param[in] cd The container descriptor.
* @param[in] key The key string
* @param[in] value The value string
*
* @return true on success, false on failure.
*/
bool DobbyProxy::addAnnotation(int32_t cd, const std::string& key, const std::string& value) const
{
AI_LOG_FN_ENTRY();

// Send off the request
const AI_IPC::VariantList params = { cd, key, value};
AI_IPC::VariantList returns;

bool result = false;

if (invokeMethod(DOBBY_CTRL_INTERFACE,
DOBBY_CTRL_METHOD_ANNOTATE,
params, returns))
{
if (!AI_IPC::parseVariantList<bool>(returns, &result))
{
result = false;
}
}

AI_LOG_FN_EXIT();
return result;
}
// -----------------------------------------------------------------------------
/**
* @brief removes a key value pair from the container annotation
*
* @param[in] cd The container descriptor.
* @param[in] key The key string
*
* @return true on success, false on failure.
*/
bool DobbyProxy::removeAnnotation(int32_t cd, const std::string& key) const
{
AI_LOG_FN_ENTRY();

// Send off the request
const AI_IPC::VariantList params = { cd, key};
AI_IPC::VariantList returns;

bool result = false;

if (invokeMethod(DOBBY_CTRL_INTERFACE,
DOBBY_CTRL_METHOD_REMOVE_ANNOTATION,
params, returns))
{
if (!AI_IPC::parseVariantList<bool>(returns, &result))
{
result = false;
}
}

AI_LOG_FN_EXIT();
return result;
}
// -----------------------------------------------------------------------------
/**
* @brief unmounts a directory/device inside the container
Expand Down
98 changes: 97 additions & 1 deletion client/tool/source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,91 @@ static void unmountCommand(const std::shared_ptr<IDobbyProxy>& dobbyProxy,
}
}
}
// -----------------------------------------------------------------------------
/**
* @brief
*
*
*
*/
static void annotateCommand(const std::shared_ptr<IDobbyProxy>& dobbyProxy,
const std::shared_ptr<const IReadLineContext>& readLine,
const std::vector<std::string>& args)
{
if (args.size() < 3 || args[0].empty() || args[1].empty() || args[2].empty())
{
readLine->printLnError("must provide at least 3 args; <id> <key> <value>");
return;
}

std::string id = args[0];
if (id.empty())
{
readLine->printLnError("invalid container id '%s'", id.c_str());
return;
}
std::string annotateKey(args[1]);
std::string annotateValue(args[2]);

int32_t cd = getContainerDescriptor(dobbyProxy, id);
if (cd < 0)
{
readLine->printLnError("failed to find container '%s'", id.c_str());
}
else
{
if (!dobbyProxy->addAnnotation(cd, annotateKey, annotateValue))
{
readLine->printLnError("failed to add %s %s pair inside the container %s", annotateKey.c_str(), annotateValue.c_str(), id.c_str());
}
else
{
readLine->printLn("annotate successful for container '%s'", id.c_str());
}
}
}
// -----------------------------------------------------------------------------
/**
* @brief
*
*
*
*/
static void removeAnnotationCommand(const std::shared_ptr<IDobbyProxy>& dobbyProxy,
const std::shared_ptr<const IReadLineContext>& readLine,
const std::vector<std::string>& args)
{
if (args.size() < 2 || args[0].empty() || args[1].empty())
{
readLine->printLnError("must provide at least 2 args; <id> <key>");
return;
}

std::string id = args[0];
if (id.empty())
{
readLine->printLnError("invalid container id '%s'", id.c_str());
return;
}
std::string annotateKey(args[1]);

int32_t cd = getContainerDescriptor(dobbyProxy, id);
if (cd < 0)
{
readLine->printLnError("failed to find container '%s'", id.c_str());
}
else
{
if (!dobbyProxy->removeAnnotation(cd, annotateKey))
{
readLine->printLnError("failed to remove %s key from the container %s annotations", annotateKey.c_str(), id.c_str());
}
else
{
readLine->printLn("removed %s key from container '%s' annotations", annotateKey.c_str(), id.c_str());
}
}
}
// -----------------------------------------------------------------------------
/**
* @brief
Expand Down Expand Up @@ -1297,7 +1381,19 @@ static void initCommands(const std::shared_ptr<IReadLine>& readLine,
"unmount <id> <source>",
"unmount a directory inside the container with the given id\n",
"\n");


readLine->addCommand("annotate",
std::bind(annotateCommand, dobbyProxy, std::placeholders::_1, std::placeholders::_2),
"annonate <id> <key> <value>",
"annotate the container with a key value pair\n",
"\n");

readLine->addCommand("remove-annotation",
std::bind(removeAnnotationCommand, dobbyProxy, std::placeholders::_1, std::placeholders::_2),
"remove-annotation <id> <key>",
"removes a key from the container's annotations\n",
"\n");

readLine->addCommand("exec",
std::bind(execCommand, dobbyProxy, std::placeholders::_1, std::placeholders::_2),
"exec [options...] <id> <command>",
Expand Down
3 changes: 3 additions & 0 deletions daemon/lib/include/Dobby.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class Dobby
DOBBY_DBUS_METHOD(stopInProcessTracing);
#endif

DOBBY_DBUS_METHOD(addAnnotation);
DOBBY_DBUS_METHOD(removeAnnotation);

#undef DOBBY_DBUS_METHOD

private:
Expand Down
115 changes: 113 additions & 2 deletions daemon/lib/source/Dobby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ void Dobby::initIpcMethods()
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_RESUME, &Dobby::resume },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_HIBERNATE, &Dobby::hibernate },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_WAKEUP, &Dobby::wakeup },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_MOUNT, &Dobby::addMount },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_UNMOUNT, &Dobby::removeMount },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_MOUNT, &Dobby::addMount },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_UNMOUNT, &Dobby::removeMount },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_EXEC, &Dobby::exec },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_GETSTATE, &Dobby::getState },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_GETINFO, &Dobby::getInfo },
Expand All @@ -670,6 +670,8 @@ void Dobby::initIpcMethods()
{ DOBBY_DEBUG_INTERFACE, DOBBY_DEBUG_START_INPROCESS_TRACING, &Dobby::startInProcessTracing },
{ DOBBY_DEBUG_INTERFACE, DOBBY_DEBUG_STOP_INPROCESS_TRACING, &Dobby::stopInProcessTracing },
#endif // defined(AI_ENABLE_TRACING)
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_ANNOTATE, &Dobby::addAnnotation },
{ DOBBY_CTRL_INTERFACE, DOBBY_CTRL_METHOD_REMOVE_ANNOTATION, &Dobby::removeAnnotation },
};

// ... register them all
Expand Down Expand Up @@ -1551,6 +1553,115 @@ void Dobby::removeMount(std::shared_ptr<AI_IPC::IAsyncReplySender> replySender)
AI_LOG_FN_EXIT();
}

// -----------------------------------------------------------------------------
/**
* @brief annotate a container (add a key value pair)
*
*
*
*
*/
void Dobby::addAnnotation(std::shared_ptr<AI_IPC::IAsyncReplySender> replySender)
{
AI_LOG_FN_ENTRY();

// Expecting 3 arguments (int32_t cd, string key, string value)
int32_t descriptor;
std::string key;
std::string value;

if (!AI_IPC::parseVariantList
<int32_t, std::string, std::string>
(replySender->getMethodCallArguments(), &descriptor, &key, &value))
{
AI_LOG_ERROR("error getting the args");
}
else
{
auto doAnnotateLambda =
[manager = mManager, descriptor, key, value, replySender]()
{
//add the key value pair to the annotations
bool result = manager->annotate(descriptor, key, value);

// Fire off the reply
if (!replySender->sendReply({ result }))
{
AI_LOG_ERROR("failed to send reply");
}
};

// Queue the work, if successful then we're done
if (mWorkQueue->postWork(std::move(doAnnotateLambda)))
{
AI_LOG_FN_EXIT();
return;
}
}

// Fire off the reply
AI_IPC::VariantList results = { false };
if (!replySender->sendReply(results))
{
AI_LOG_ERROR("failed to send reply");
}

AI_LOG_FN_EXIT();
}
// -----------------------------------------------------------------------------
/**
* @brief remove a key from container annotations
*
*
*
*
*/
void Dobby::removeAnnotation(std::shared_ptr<AI_IPC::IAsyncReplySender> replySender)
{
AI_LOG_FN_ENTRY();

// Expecting 2 arguments (int32_t cd, string key)
int32_t descriptor;
std::string key;

if (!AI_IPC::parseVariantList
<int32_t, std::string>
(replySender->getMethodCallArguments(), &descriptor, &key))
{
AI_LOG_ERROR("error getting the args");
}
else
{
auto doremoveAnnotationLambda =
[manager = mManager, descriptor, key, replySender]()
{
//remove the key from the annotations
bool result = manager->removeAnnotation(descriptor, key);

// Fire off the reply
if (!replySender->sendReply({ result }))
{
AI_LOG_ERROR("failed to send reply");
}
};

// Queue the work, if successful then we're done
if (mWorkQueue->postWork(std::move(doremoveAnnotationLambda)))
{
AI_LOG_FN_EXIT();
return;
}
}

// Fire off the reply
AI_IPC::VariantList results = { false };
if (!replySender->sendReply(results))
{
AI_LOG_ERROR("failed to send reply");
}

AI_LOG_FN_EXIT();
}
// -----------------------------------------------------------------------------
/**
* @brief Executes a command in a container
Expand Down
Loading
Loading