Add remove and list to vasum-server and vasum-client 97/34497/7
authorMateusz Malicki <m.malicki2@samsung.com>
Tue, 27 Jan 2015 15:53:35 +0000 (16:53 +0100)
committerPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Thu, 5 Feb 2015 16:35:32 +0000 (08:35 -0800)
[Bug/Feature]   Ability to list and remove provision
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, run tests

Change-Id: I57783a0b79466ef60d781e6206b92fb266fe5f38

12 files changed:
client/vasum-client-impl.cpp
client/vasum-client-impl.hpp
client/vasum-client.cpp
client/vasum-client.h
server/host-connection.cpp
server/host-connection.hpp
server/host-dbus-definitions.hpp
server/zone.cpp
server/zone.hpp
server/zones-manager.cpp
server/zones-manager.hpp
tests/unit_tests/client/ut-client.cpp

index 0af5269..9ca5de5 100644 (file)
@@ -585,14 +585,29 @@ VsmStatus Client::vsm_declare_file(const char* zone,
                                    VsmFileType type,
                                    const char *path,
                                    int32_t flags,
-                                   mode_t mode) noexcept
+                                   mode_t mode,
+                                   VsmString* id) noexcept
 {
     assert(path);
 
+    GVariant* out = NULL;
     GVariant* args_in = g_variant_new("(sisii)", zone, type, path, flags, mode);
-    return callMethod(ZONE_INTERFACE,
-                      api::host::METHOD_DECLARE_FILE,
-                      args_in);
+    VsmStatus ret = callMethod(HOST_INTERFACE,
+                               api::host::METHOD_DECLARE_FILE,
+                               args_in,
+                               "(s)",
+                               &out);
+    if (ret != VSMCLIENT_SUCCESS) {
+        return ret;
+    }
+    GVariant* unpacked;
+    if (id != NULL) {
+        g_variant_get(out, "(*)", &unpacked);
+        toBasic(unpacked, id);
+        g_variant_unref(unpacked);
+    }
+    g_variant_unref(out);
+    return ret;
 }
 
 VsmStatus Client::vsm_declare_mount(const char *source,
@@ -600,7 +615,8 @@ VsmStatus Client::vsm_declare_mount(const char *source,
                                     const char *target,
                                     const char *type,
                                     uint64_t flags,
-                                    const char *data) noexcept
+                                    const char *data,
+                                    VsmString* id) noexcept
 {
     assert(source);
     assert(target);
@@ -609,22 +625,83 @@ VsmStatus Client::vsm_declare_mount(const char *source,
         data = "";
     }
 
+    GVariant* out = NULL;
     GVariant* args_in = g_variant_new("(ssssts)", source, zone, target, type, flags, data);
-    return callMethod(ZONE_INTERFACE,
-                      api::host::METHOD_DECLARE_MOUNT,
-                      args_in);
+    VsmStatus ret = callMethod(HOST_INTERFACE,
+                               api::host::METHOD_DECLARE_MOUNT,
+                               args_in,
+                               "(s)",
+                               &out);
+    if (ret != VSMCLIENT_SUCCESS) {
+        return ret;
+    }
+    GVariant* unpacked;
+    if (id != NULL) {
+        g_variant_get(out, "(*)", &unpacked);
+        toBasic(unpacked, id);
+        g_variant_unref(unpacked);
+    }
+    g_variant_unref(out);
+    return ret;
 }
 
 VsmStatus Client::vsm_declare_link(const char *source,
                                    const char* zone,
-                                   const char *target) noexcept
+                                   const char *target,
+                                   VsmString* id) noexcept
 {
     assert(source);
     assert(target);
 
+    GVariant* out = NULL;
     GVariant* args_in = g_variant_new("(sss)", source, zone, target);
-    return callMethod(ZONE_INTERFACE,
-                      api::host::METHOD_DECLARE_LINK,
+    VsmStatus ret = callMethod(HOST_INTERFACE,
+                               api::host::METHOD_DECLARE_LINK,
+                               args_in,
+                               "(s)",
+                               &out);
+    if (ret != VSMCLIENT_SUCCESS) {
+        return ret;
+    }
+    GVariant* unpacked;
+    if (id != NULL) {
+        g_variant_get(out, "(*)", &unpacked);
+        toBasic(unpacked, id);
+        g_variant_unref(unpacked);
+    }
+    g_variant_unref(out);
+    return ret;
+}
+
+VsmStatus Client::vsm_list_declarations(const char* zone, VsmArrayString* declarations)
+{
+    assert(declarations);
+
+    GVariant* out = NULL;
+    GVariant* args_in = g_variant_new("(s)", zone);
+    VsmStatus ret = callMethod(HOST_INTERFACE,
+                               api::host::METHOD_GET_DECLARATIONS,
+                               args_in,
+                               "(as)",
+                               &out);
+    if (ret != VSMCLIENT_SUCCESS) {
+        return ret;
+    }
+    GVariant* unpacked;
+    g_variant_get(out, "(*)", &unpacked);
+    toArray(unpacked, declarations);
+    g_variant_unref(unpacked);
+    g_variant_unref(out);
+    return ret;
+}
+
+VsmStatus Client::vsm_remove_declaration(const char* zone, VsmString declaration)
+{
+    assert(declaration);
+
+    GVariant* args_in = g_variant_new("(ss)", zone, declaration);
+    return callMethod(HOST_INTERFACE,
+                      api::host::METHOD_REMOVE_DECLARATION,
                       args_in);
 }
 
index f545d77..b5acee7 100644 (file)
@@ -254,7 +254,8 @@ public:
                                VsmFileType type,
                                const char* path,
                                int32_t flags,
-                               mode_t mode) noexcept;
+                               mode_t mode,
+                               VsmString* id) noexcept;
 
     /**
      * @see ::vsm_declare_mount
@@ -264,13 +265,25 @@ public:
                                 const char* target,
                                 const char* type,
                                 uint64_t flags,
-                                const char* data) noexcept;
+                                const char* data,
+                                VsmString* id) noexcept;
     /**
      * @see ::vsm_declare_link
      */
     VsmStatus vsm_declare_link(const char* source,
                                const char* zone,
-                               const char* target) noexcept;
+                               const char* target,
+                               VsmString* id) noexcept;
+
+    /**
+     * @see ::vsm_list_declarations
+     */
+    VsmStatus vsm_list_declarations(const char* zone, VsmArrayString* declarations);
+
+    /**
+     * @see ::vsm_remove_declaration
+     */
+    VsmStatus vsm_remove_declaration(const char* zone, VsmString declaration);
 
     /**
      *  @see ::vsm_notify_active_zone
index 00a4dfb..5310b42 100644 (file)
@@ -279,7 +279,7 @@ API VsmStatus vsm_declare_file(VsmClient client,
                                int32_t flags,
                                mode_t mode)
 {
-    return getClient(client).vsm_declare_file(zone, type, path, flags, mode);
+    return getClient(client).vsm_declare_file(zone, type, path, flags, mode, NULL);
 }
 
 
@@ -291,7 +291,7 @@ API VsmStatus vsm_declare_mount(VsmClient client,
                                 uint64_t flags,
                                 const char* data)
 {
-    return getClient(client).vsm_declare_mount(source, zone, target, type, flags, data);
+    return getClient(client).vsm_declare_mount(source, zone, target, type, flags, data, NULL);
 }
 
 API VsmStatus vsm_declare_link(VsmClient client,
@@ -299,9 +299,22 @@ API VsmStatus vsm_declare_link(VsmClient client,
                                const char* zone,
                                const char* target)
 {
-    return getClient(client).vsm_declare_link(source, zone, target);
+    return getClient(client).vsm_declare_link(source, zone, target, NULL);
 }
 
+API VsmStatus vsm_list_declarations(VsmClient client,
+                                    const char* zone,
+                                    VsmArrayString* declarations)
+{
+    return getClient(client).vsm_list_declarations(zone, declarations);
+}
+
+API VsmStatus vsm_remove_declaration(VsmClient client,
+                                     const char* zone,
+                                     VsmString declaration)
+{
+    return getClient(client).vsm_remove_declaration(zone, declaration);
+}
 
 API VsmStatus vsm_notify_active_zone(VsmClient client,
                                           const char* application,
index bf15f25..6793722 100644 (file)
@@ -653,6 +653,35 @@ VsmStatus vsm_declare_link(VsmClient client,
                            const char* zone,
                            const char *target);
 
+/**
+ * Get all declarations
+ *
+ * Gets all declarations of resourcies
+ * (@see ::vsm_declare_link, @see ::vsm_declare_mount, @see ::vsm_declare_linki)
+ *
+ * @param[in] client vasum-server's client
+ * @param[in] zone zone id
+ * @param[out] declarations array of declarations id
+ * @return status of this function call
+ */
+VsmStatus vsm_list_declarations(VsmClient client,
+                                const char* zone,
+                                VsmArrayString* declarations);
+
+/**
+ * Remove declaration
+ *
+ * Removes given declaration by its id (@see ::vsm_list_declarations)
+ *
+ * @param[in] client vasum-server's client
+ * @param[in] zone zone id
+ * @param[in] declaration declaration id
+ * @return status of this function call
+ */
+VsmStatus vsm_remove_declaration(VsmClient client,
+                                 const char* zone,
+                                 VsmString declaration);
+
 
 /** @} Host API */
 
index db6130f..a90a47c 100644 (file)
@@ -145,6 +145,16 @@ void HostConnection::setDeclareLinkCallback(const DeclareLinkCallback& callback)
     mDeclareLinkCallback = callback;
 }
 
+void HostConnection::setGetDeclarationsCallback(const GetDeclarationsCallback& callback)
+{
+    mGetDeclarationsCallback = callback;
+}
+
+void HostConnection::setRemoveDeclarationCallback(const RemoveDeclarationCallback& callback)
+{
+    mRemoveDeclarationCallback =  callback;
+}
+
 void HostConnection::setSetActiveZoneCallback(const SetActiveZoneCallback& callback)
 {
     mSetActiveZoneCallback = callback;
@@ -319,6 +329,27 @@ void HostConnection::onMessageCall(const std::string& objectPath,
         return;
     }
 
+    if (methodName == api::host::METHOD_GET_DECLARATIONS) {
+        const gchar* zone;
+        g_variant_get(parameters, "(&s)", &zone);
+
+        if (mGetDeclarationsCallback) {
+            mGetDeclarationsCallback(zone, result);
+        }
+        return;
+    }
+
+    if (methodName == api::host::METHOD_REMOVE_DECLARATION) {
+        const gchar* zone;
+        const gchar* declarationId;
+        g_variant_get(parameters, "(&s&s)", &zone, &declarationId);
+
+        if (mRemoveDeclarationCallback) {
+            mRemoveDeclarationCallback(zone, declarationId, result);
+        }
+        return;
+    }
+
     if (methodName == api::host::METHOD_CREATE_ZONE) {
         const gchar* id = NULL;
         g_variant_get(parameters, "(&s)", &id);
index db8cf34..39708ad 100644 (file)
@@ -82,6 +82,13 @@ public:
                               )> DeclareLinkCallback;
     typedef std::function<void(const std::string& id,
                                dbus::MethodResultBuilder::Pointer result
+                              )> GetDeclarationsCallback;
+    typedef std::function<void(const std::string& id,
+                               const std::string& declarationId,
+                               dbus::MethodResultBuilder::Pointer result
+                              )> RemoveDeclarationCallback;
+    typedef std::function<void(const std::string& id,
+                               dbus::MethodResultBuilder::Pointer result
                               )> SetActiveZoneCallback;
     typedef std::function<void(const std::string& id,
                                dbus::MethodResultBuilder::Pointer result
@@ -157,6 +164,16 @@ public:
     void setDeclareLinkCallback(const DeclareLinkCallback& callback);
 
     /**
+     * Register a callback called to list declarations
+     */
+    void setGetDeclarationsCallback(const GetDeclarationsCallback& callback);
+
+    /**
+     * Register a callback called to remove declarartion
+     */
+    void setRemoveDeclarationCallback(const RemoveDeclarationCallback& callback);
+
+    /**
      * Register a callback called to set the active zone
      */
     void setSetActiveZoneCallback(const SetActiveZoneCallback& callback);
@@ -225,6 +242,8 @@ private:
     DeclareFileCallback mDeclareFileCallback;
     DeclareMountCallback mDeclareMountCallback;
     DeclareLinkCallback mDeclareLinkCallback;
+    GetDeclarationsCallback mGetDeclarationsCallback;
+    RemoveDeclarationCallback mRemoveDeclarationCallback;
     SetActiveZoneCallback mSetActiveZoneCallback;
     CreateZoneCallback mCreateZoneCallback;
     DestroyZoneCallback mDestroyZoneCallback;
index ff13843..47922b5 100644 (file)
@@ -45,6 +45,8 @@ const std::string METHOD_GET_ZONE_INFO      = "GetZoneInfo";
 const std::string METHOD_DECLARE_FILE       = "DeclareFile";
 const std::string METHOD_DECLARE_MOUNT      = "DeclareMount";
 const std::string METHOD_DECLARE_LINK       = "DeclareLink";
+const std::string METHOD_GET_DECLARATIONS   = "GetDeclarations";
+const std::string METHOD_REMOVE_DECLARATION = "RemoveDeclaration";
 const std::string METHOD_SET_ACTIVE_ZONE    = "SetActiveZone";
 const std::string METHOD_CREATE_ZONE        = "CreateZone";
 const std::string METHOD_DESTROY_ZONE       = "DestroyZone";
@@ -89,6 +91,7 @@ const std::string DEFINITION =
     "      <arg type='s' name='path' direction='in'/>"
     "      <arg type='i' name='flags' direction='in'/>"
     "      <arg type='i' name='mode' direction='in'/>"
+    "      <arg type='s' name='id' direction='out'/>"
     "    </method>"
     "    <method name='" + METHOD_DECLARE_MOUNT + "'>"
     "      <arg type='s' name='source' direction='in'/>"
@@ -97,11 +100,21 @@ const std::string DEFINITION =
     "      <arg type='s' name='type' direction='in'/>"
     "      <arg type='t' name='flags' direction='in'/>"
     "      <arg type='s' name='data' direction='in'/>"
+    "      <arg type='s' name='id' direction='out'/>"
     "    </method>"
     "    <method name='" + METHOD_DECLARE_LINK + "'>"
     "      <arg type='s' name='source' direction='in'/>"
     "      <arg type='s' name='zone' direction='in'/>"
     "      <arg type='s' name='target' direction='in'/>"
+    "      <arg type='s' name='id' direction='out'/>"
+    "    </method>"
+    "    <method name='" + METHOD_GET_DECLARATIONS + "'>"
+    "      <arg type='s' name='zone' direction='in'/>"
+    "      <arg type='as' name='list' direction='out'/>"
+    "    </method>"
+    "    <method name='" + METHOD_REMOVE_DECLARATION + "'>"
+    "      <arg type='s' name='zone' direction='in'/>"
+    "      <arg type='s' name='declaration' direction='in'/>"
     "    </method>"
     "    <method name='" + METHOD_SET_ACTIVE_ZONE + "'>"
     "      <arg type='s' name='id' direction='in'/>"
index c3f6991..e7ea3d6 100644 (file)
@@ -404,4 +404,14 @@ std::string Zone::declareLink(const std::string& source,
     return mProvision->declareLink(source, target);
 }
 
+std::vector<std::string> Zone::getDeclarations() const
+{
+    return mProvision->list();
+}
+
+void Zone::removeDeclaration(const std::string& declarationId)
+{
+    mProvision->remove(declarationId);
+}
+
 } // namespace vasum
index 6105209..ab24615 100644 (file)
@@ -249,7 +249,17 @@ public:
     std::string declareLink(const std::string& source,
                             const std::string& target);
 
-   /**
+    /**
+     * Gets all declarations
+     */
+    std::vector<std::string> getDeclarations() const;
+
+    /**
+     * Remove declaration
+     */
+    void removeDeclaration(const std::string& declarationId);
+
+    /**
      * Get zone root path
      */
     std::string getRootPath() const;
index f554bdc..34e1531 100644 (file)
@@ -171,6 +171,12 @@ ZonesManager::ZonesManager(const std::string& configPath)
     mHostConnection.setDeclareLinkCallback(bind(&ZonesManager::handleDeclareLinkCall,
                                                 this, _1, _2, _3, _4));
 
+    mHostConnection.setGetDeclarationsCallback(bind(&ZonesManager::handleGetDeclarationsCall,
+                                                    this, _1, _2));
+
+    mHostConnection.setRemoveDeclarationCallback(bind(&ZonesManager::handleRemoveDeclarationCall,
+                                                    this, _1, _2, _3));
+
     mHostConnection.setSetActiveZoneCallback(bind(&ZonesManager::handleSetActiveZoneCall,
                                                   this, _1, _2));
 
@@ -725,7 +731,7 @@ void ZonesManager::handleGetZoneDbuses(dbus::MethodResultBuilder::Pointer result
         entries.push_back(entry);
     }
     GVariant* dict = g_variant_new_array(G_VARIANT_TYPE("{ss}"), entries.data(), entries.size());
-    result->set(g_variant_new("(*)", dict));
+    result->set(g_variant_new("(@a{ss})", dict));
 }
 
 void ZonesManager::handleDbusStateChanged(const std::string& zoneId,
@@ -746,7 +752,7 @@ void ZonesManager::handleGetZoneIdsCall(dbus::MethodResultBuilder::Pointer resul
     GVariant* array = g_variant_new_array(G_VARIANT_TYPE("s"),
                                           zoneIds.data(),
                                           zoneIds.size());
-    result->set(g_variant_new("(*)", array));
+    result->set(g_variant_new("(@as)", array));
 }
 
 void ZonesManager::handleGetActiveZoneIdCall(dbus::MethodResultBuilder::Pointer result)
@@ -804,8 +810,8 @@ void ZonesManager::handleDeclareFileCall(const std::string& zone,
     try {
         Lock lock(mMutex);
 
-        at(mZones, zone).declareFile(type, path, flags, mode);
-        result->setVoid();
+        const std::string id = at(mZones, zone).declareFile(type, path, flags, mode);
+        result->set(g_variant_new("(s)", id.c_str()));
     } catch (const std::out_of_range&) {
         LOGE("No zone with id=" << zone);
         result->setError(api::ERROR_INVALID_ID, "No such zone id");
@@ -828,8 +834,8 @@ void ZonesManager::handleDeclareMountCall(const std::string& source,
     try {
         Lock lock(mMutex);
 
-        at(mZones, zone).declareMount(source, target, type, flags, data);
-        result->setVoid();
+        const std::string id = at(mZones, zone).declareMount(source, target, type, flags, data);
+        result->set(g_variant_new("(s)", id.c_str()));
     } catch (const std::out_of_range&) {
         LOGE("No zone with id=" << zone);
         result->setError(api::ERROR_INVALID_ID, "No such zone id");
@@ -848,8 +854,8 @@ void ZonesManager::handleDeclareLinkCall(const std::string& source,
     try {
         Lock lock(mMutex);
 
-        at(mZones, zone).declareLink(source, target);
-        result->setVoid();
+        const std::string id = at(mZones, zone).declareLink(source, target);
+        result->set(g_variant_new("(s)", id.c_str()));
     } catch (const std::out_of_range&) {
         LOGE("No zone with id=" << zone);
         result->setError(api::ERROR_INVALID_ID, "No such zone id");
@@ -859,6 +865,54 @@ void ZonesManager::handleDeclareLinkCall(const std::string& source,
     }
 }
 
+void ZonesManager::handleGetDeclarationsCall(const std::string& zone,
+                                             dbus::MethodResultBuilder::Pointer result)
+{
+    LOGI("GetDeclarations call Id=" << zone);
+    try {
+        Lock lock(mMutex);
+
+        std::vector<std::string> declarations = at(mZones, zone).getDeclarations();
+
+        std::vector<GVariant*> out;
+        for (auto declaration : declarations) {
+            out.push_back(g_variant_new_string(declaration.c_str()));
+        }
+
+        GVariant* array = g_variant_new_array(G_VARIANT_TYPE("s"),
+                                              out.data(),
+                                              out.size());
+        result->set(g_variant_new("(@as)", array));
+    } catch (const std::out_of_range&) {
+        LOGE("No zone with id=" << zone);
+        result->setError(api::ERROR_INVALID_ID, "No such zone id");
+    } catch (const VasumException& ex) {
+        LOGE(ex.what());
+        result->setError(api::ERROR_INTERNAL, ex.what());
+    }
+
+}
+
+void ZonesManager::handleRemoveDeclarationCall(const std::string& zone,
+                                               const std::string& declarationId,
+                                               dbus::MethodResultBuilder::Pointer result)
+{
+    LOGI("RemoveDeclaration call Id=" << zone);
+    try {
+        Lock lock(mMutex);
+
+        at(mZones, zone).removeDeclaration(declarationId);
+
+        result->setVoid();
+    } catch (const std::out_of_range&) {
+        LOGE("No zone with id=" << zone);
+        result->setError(api::ERROR_INVALID_ID, "No such zone id");
+    } catch (const VasumException& ex) {
+        LOGE(ex.what());
+        result->setError(api::ERROR_INTERNAL, ex.what());
+    }
+}
+
 void ZonesManager::handleSetActiveZoneCall(const std::string& id,
                                            dbus::MethodResultBuilder::Pointer result)
 {
index 740e074..9ede4e9 100644 (file)
@@ -174,6 +174,11 @@ private:
                                const std::string& zone,
                                const std::string& target,
                                dbus::MethodResultBuilder::Pointer result);
+    void handleGetDeclarationsCall(const std::string& zone,
+                                   dbus::MethodResultBuilder::Pointer result);
+    void handleRemoveDeclarationCall(const std::string& zone,
+                                     const std::string& declarationId,
+                                     dbus::MethodResultBuilder::Pointer result);
     void handleSetActiveZoneCall(const std::string& id,
                                  dbus::MethodResultBuilder::Pointer result);
     void handleCreateZoneCall(const std::string& id,
index 49e18d2..a188a9d 100644 (file)
@@ -394,4 +394,24 @@ BOOST_AUTO_TEST_CASE(GrantRevokeTest)
     vsm_client_free(client);
 }
 
+BOOST_AUTO_TEST_CASE(ProvisionTest)
+{
+    VsmClient client = vsm_client_create();
+    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, vsm_connect(client));
+    const std::string zone = cm.getRunningForegroundZoneId();
+    VsmArrayString declarations;
+    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, vsm_list_declarations(client, zone.c_str(), &declarations));
+    BOOST_REQUIRE(declarations != NULL && declarations[0] == NULL);
+    vsm_array_string_free(declarations);
+    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, vsm_declare_link(client, "/tmp/fake", zone.c_str(), "/tmp/fake/"));
+    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, vsm_list_declarations(client, zone.c_str(), &declarations));
+    BOOST_REQUIRE(declarations != NULL && declarations[0] != NULL && declarations[1] == NULL);
+    BOOST_CHECK_EQUAL(VSMCLIENT_SUCCESS, vsm_remove_declaration(client, zone.c_str(), declarations[0]));
+    vsm_array_string_free(declarations);
+    BOOST_CHECK_EQUAL(VSMCLIENT_SUCCESS, vsm_list_declarations(client, zone.c_str(), &declarations));
+    BOOST_CHECK(declarations != NULL && declarations[0] == NULL);
+    vsm_array_string_free(declarations);
+    vsm_client_free(client);
+}
+
 BOOST_AUTO_TEST_SUITE_END()