Hide VsmZone and VsmNetdevType structs from API 48/44748/4
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Mon, 27 Jul 2015 09:20:37 +0000 (11:20 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 28 Jul 2015 07:26:21 +0000 (00:26 -0700)
[Feature]       N/A
[Cause]         Make it easier to change them in the future without
                breaking the ABI. Also remove duplicate related API
                function from the code.
[Solution]      N/A
[Verification]  N/A

Change-Id: Ib0ec7374ee0f94cd88e2998e9d10f18ae094ee98

cli/command-line-interface.cpp
client/vasum-client-impl.cpp
client/vasum-client-impl.hpp
client/vasum-client.cpp
client/vasum-client.h
tests/unit_tests/client/ut-client.cpp
wrapper/wrapper.cpp

index 29dd4a3..1338afe 100644 (file)
@@ -51,17 +51,9 @@ VsmClient CommandLineInterface::client = NULL;
 
 namespace {
 
-template<typename T>
-string stringAsInStream(const T& value)
+std::string zoneStateToString(const VsmZoneState& state)
 {
-    std::ostringstream stream;
-    stream << value;
-    return stream.str();
-}
-
-ostream& operator<<(ostream& out, const VsmZoneState& state)
-{
-    const char* name;
+    std::string name;
     switch (state) {
         case STOPPED: name = "STOPPED"; break;
         case STARTING: name = "STARTING"; break;
@@ -77,33 +69,33 @@ ostream& operator<<(ostream& out, const VsmZoneState& state)
         default: name = "MAX_STATE (ERROR)";
     }
 
-    out << name;
-    return out;
+    return name;
 }
 
-ostream& operator<<(ostream& out, const VsmZone& zone)
+std::string zoneToString(const VsmZone& zone)
 {
-    out << "Name: " << zone->id
-        << "\nTerminal: " << zone->terminal
-        << "\nState: " << zone->state
-        << "\nRoot: " << zone->rootfs_path;
+    std::string out = std::string("Name: ") + vsm_zone_get_id(zone)
+        + std::string("\nTerminal: ") + std::to_string(vsm_zone_get_terminal(zone))
+        + std::string("\nState: ") + zoneStateToString(vsm_zone_get_state(zone))
+        + std::string("\nRoot: ") + vsm_zone_get_rootfs(zone);
     return out;
 }
 
-ostream& operator<<(ostream& out, const VsmNetdevType& netdevType)
+std::string netdevTypeToString(const VsmNetdevType& netdevType)
 {
+    std::string type;
     switch (netdevType) {
-        case VSMNETDEV_VETH: out << "VETH"; break;
-        case VSMNETDEV_PHYS: out << "PHYS"; break;
-        case VSMNETDEV_MACVLAN: out << "MACVLAN"; break;
+        case VSMNETDEV_VETH: type = "VETH"; break;
+        case VSMNETDEV_PHYS: type = "PHYS"; break;
+        case VSMNETDEV_MACVLAN: type = "MACVLAN"; break;
     }
-    return out;
+    return type;
 }
 
-ostream& operator<<(ostream& out, const VsmNetdev& netdev)
+std::string netdevToString(const VsmNetdev& netdev)
 {
-    out << "Name: " << netdev->name
-        << "\nType: " << netdev->type;
+    std::string out = std::string("Name: ") + vsm_netdev_get_name(netdev)
+        + std::string("\nType: ") + netdevTypeToString(vsm_netdev_get_type(netdev));
     return out;
 }
 
@@ -316,12 +308,12 @@ void console_zone(const Args& argv)
     VsmZone zone;
     CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, argv[1].c_str(), &zone));
 
-    if (stringAsInStream(zone->state) != "RUNNING") {
+    if (zoneStateToString(vsm_zone_get_state(zone)) != "RUNNING") {
         vsm_zone_free(zone);
         throw runtime_error("Zone is not running");
     }
 
-    std::string zonesPath = zone->rootfs_path;
+    std::string zonesPath = vsm_zone_get_rootfs(zone);
     std::string zoneRootFs = argv[1] + "/rootfs";
     zonesPath.erase(zonesPath.length()- zoneRootFs.length(), zoneRootFs.length());
 
@@ -374,12 +366,12 @@ void get_zones_status(const Args& /* argv */)
     for (VsmString* id = ids; *id; ++id) {
         VsmZone zone;
         CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, *id, &zone));
-        assert(string(zone->id) == string(*id));
-        table.push_back({string(zone->id) == string(activeId) ? "*" : "",
-                         zone->id,
-                         stringAsInStream(zone->state),
-                         to_string(zone->terminal),
-                         zone->rootfs_path});
+        assert(string(vsm_zone_get_id(zone)) == string(*id));
+        table.push_back({string(vsm_zone_get_id(zone)) == string(activeId) ? "*" : "",
+                         vsm_zone_get_id(zone),
+                         zoneStateToString(vsm_zone_get_state(zone)),
+                         to_string(vsm_zone_get_terminal(zone)),
+                         vsm_zone_get_rootfs(zone)});
         vsm_zone_free(zone);
     }
     vsm_string_free(activeId);
@@ -421,7 +413,7 @@ void lookup_zone_by_id(const Args& argv)
 
     VsmZone zone;
     CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, argv[1].c_str(), &zone));
-    cout << zone << endl;
+    cout << zoneToString(zone) << endl;
     vsm_zone_free(zone);
 }
 
@@ -497,14 +489,14 @@ void lookup_netdev_by_name(const Args& argv)
     if (argv.size() <= 2) {
         throw runtime_error("Not enough parameters");
     }
-    VsmNetdev vsmNetdev = NULL;
+    VsmNetdev netdev = NULL;
     CommandLineInterface::executeCallback(bind(vsm_lookup_netdev_by_name,
                   _1,
                   argv[1].c_str(),
                   argv[2].c_str(),
-                  &vsmNetdev));
-    cout << vsmNetdev << endl;
-    vsm_netdev_free(vsmNetdev);
+                  &netdev));
+    cout << netdevToString(netdev) << endl;
+    vsm_netdev_free(netdev);
 
 }
 
index 78c2d8f..15cccf6 100644 (file)
@@ -93,9 +93,9 @@ void convert(const api::VectorOfStrings& in, VsmArrayString& out)
     }
 }
 
-void convert(const api::ZoneInfoOut& info, VsmZone& zone)
+void convert(const api::ZoneInfoOut& info, Zone& zone)
 {
-    VsmZone vsmZone = reinterpret_cast<VsmZone>(malloc(sizeof(*vsmZone)));
+    Zone vsmZone = static_cast<Zone>(malloc(sizeof(*vsmZone)));
     vsmZone->id = ::strdup(info.id.c_str());
     vsmZone->terminal = info.vt;
     vsmZone->state = getZoneState(info.state.c_str());
@@ -349,19 +349,6 @@ VsmStatus Client::vsm_get_active_zone_id(VsmString* id) noexcept
     });
 }
 
-VsmStatus Client::vsm_get_zone_rootpath(const char* id, VsmString* rootpath) noexcept
-{
-    return coverException([&] {
-        IS_SET(id);
-        IS_SET(rootpath);
-
-        api::ZoneInfoOut info = *mClient->callSync<api::ZoneId, api::ZoneInfoOut>(
-            api::ipc::METHOD_GET_ZONE_INFO,
-            std::make_shared<api::ZoneId>(api::ZoneId{ id }));
-        *rootpath = ::strdup(info.rootPath.c_str());
-    });
-}
-
 VsmStatus Client::vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept
 {
     return coverException([&] {
@@ -383,7 +370,7 @@ VsmStatus Client::vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept
     });
 }
 
-VsmStatus Client::vsm_lookup_zone_by_id(const char* id, VsmZone* zone) noexcept
+VsmStatus Client::vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept
 {
     return coverException([&] {
         IS_SET(id);
@@ -742,7 +729,7 @@ VsmStatus Client::vsm_create_netdev_phys(const char* id, const char* devId) noex
 
 VsmStatus Client::vsm_lookup_netdev_by_name(const char* id,
                                        const char* netdevId,
-                                       VsmNetdev* netdev) noexcept
+                                       Netdev* netdev) noexcept
 {
     using namespace boost::algorithm;
 
@@ -772,7 +759,7 @@ VsmStatus Client::vsm_lookup_netdev_by_name(const char* id,
                 throw InvalidResponseException("Unknown netdev type: " + it->second);
         }
 
-        *netdev = reinterpret_cast<VsmNetdev>(malloc(sizeof(**netdev)));
+        *netdev = static_cast<Netdev>(malloc(sizeof(**netdev)));
         (*netdev)->name = ::strdup(id);
         (*netdev)->type = type;
     });
index 1195b66..4297c95 100644 (file)
 typedef std::function<void (const char *zoneId, const char *dbusAddress, void *data)> VsmZoneDbusStateFunction;
 
 /**
+ * Zone information structure
+ */
+typedef struct ZoneStructure {
+    char *id;
+    int terminal;
+    VsmZoneState state;
+    char *rootfs_path;
+} *Zone;
+
+/**
+ * Network device information structure
+ */
+typedef struct NetdevStructure {
+    char *name;
+    VsmNetdevType type;
+} *Netdev;
+
+/**
  * vasum's client definition.
  *
  * Client uses dbus API.
@@ -131,11 +149,6 @@ public:
     VsmStatus vsm_get_active_zone_id(VsmString* id) noexcept;
 
     /**
-     *  @see ::vsm_get_zone_rootpath
-     */
-    VsmStatus vsm_get_zone_rootpath(const char* id, VsmString* rootpath) noexcept;
-
-    /**
      *  @see ::vsm_lookup_zone_by_pid
      */
     VsmStatus vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept;
@@ -143,7 +156,7 @@ public:
     /**
      * @see ::vsm_lookup_zone_by_id
      */
-    VsmStatus vsm_lookup_zone_by_id(const char* id, VsmZone* zone) noexcept;
+    VsmStatus vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept;
 
     /**
      * @see ::vsm_lookup_zone_by_terminal_id
@@ -295,7 +308,7 @@ public:
      */
     VsmStatus vsm_lookup_netdev_by_name(const char* zone,
                                         const char* netdevId,
-                                        VsmNetdev* netdev) noexcept;
+                                        Netdev* netdev) noexcept;
 
     /**
      *  @see ::vsm_destroy_netdev
index 68496d5..ee3e6d4 100644 (file)
@@ -112,17 +112,55 @@ API void vsm_string_free(VsmString string)
     free(string);
 }
 
+API VsmString vsm_zone_get_id(VsmZone zone)
+{
+    Zone z = static_cast<Zone>(zone);
+    return z->id;
+}
+
+API int vsm_zone_get_terminal(VsmZone zone)
+{
+    Zone z = static_cast<Zone>(zone);
+    return z->terminal;
+}
+
+API VsmZoneState vsm_zone_get_state(VsmZone zone)
+{
+    Zone z = static_cast<Zone>(zone);
+    return z->state;
+}
+
+API VsmString vsm_zone_get_rootfs(VsmZone zone)
+{
+    Zone z = static_cast<Zone>(zone);
+    return z->rootfs_path;
+}
+
 API void vsm_zone_free(VsmZone zone)
 {
-    free(zone->rootfs_path);
-    free(zone->id);
-    free(zone);
+    Zone z = static_cast<Zone>(zone);
+    free(z->rootfs_path);
+    free(z->id);
+    free(z);
+}
+
+API VsmString vsm_netdev_get_name(VsmNetdev netdev)
+{
+    Netdev n = static_cast<Netdev>(netdev);
+    return n->name;
+}
+
+API VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev)
+{
+    Netdev n = static_cast<Netdev>(netdev);
+    return n->type;
 }
 
 API void vsm_netdev_free(VsmNetdev netdev)
 {
-    free(netdev->name);
-    free(netdev);
+    Netdev n = static_cast<Netdev>(netdev);
+    free(n->name);
+    free(n);
 }
 
 API void vsm_client_free(VsmClient client)
@@ -157,11 +195,6 @@ API VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id)
     return getClient(client).vsm_get_active_zone_id(id);
 }
 
-API VsmStatus vsm_get_zone_rootpath(VsmClient client, const char* id, VsmString* rootpath)
-{
-    return getClient(client).vsm_get_zone_rootpath(id, rootpath);
-}
-
 API VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id)
 {
     return getClient(client).vsm_lookup_zone_by_pid(pid, id);
@@ -169,7 +202,8 @@ API VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id)
 
 API VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone)
 {
-    return getClient(client).vsm_lookup_zone_by_id(id, zone);
+    Zone* z = reinterpret_cast<Zone*>(zone);
+    return getClient(client).vsm_lookup_zone_by_id(id, z);
 }
 
 API VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id)
@@ -338,7 +372,8 @@ API VsmStatus vsm_lookup_netdev_by_name(VsmClient client,
                                         const char* netdevId,
                                         VsmNetdev* netdev)
 {
-    return getClient(client).vsm_lookup_netdev_by_name(zone, netdevId, netdev);
+    Netdev* n = reinterpret_cast<Netdev*>(netdev);
+    return getClient(client).vsm_lookup_netdev_by_name(zone, netdevId, n);
 }
 
 API VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId)
index fbdd3b1..b696dcf 100644 (file)
@@ -221,19 +221,9 @@ typedef enum {
 } VsmZoneState;
 
 /**
- * Zone information structure
- */
-typedef struct {
-    char* id;
-    int terminal;
-    VsmZoneState state;
-    char *rootfs_path;
-} VsmZoneStructure;
-
-/**
  * Zone information
  */
-typedef VsmZoneStructure* VsmZone;
+typedef void* VsmZone;
 
 /**
  * Netowrk device type
@@ -245,17 +235,9 @@ typedef enum {
 } VsmNetdevType;
 
 /**
- * Network device information structure
- */
-typedef struct {
-    char* name;
-    VsmNetdevType type;
-} VsmNetdevStructure;
-
-/**
  * Network device information
  */
-typedef VsmNetdevStructure* VsmNetdev;
+typedef void* VsmNetdev;
 
 /**
  * File type
@@ -391,6 +373,38 @@ void vsm_array_string_free(VsmArrayString astring);
 void vsm_string_free(VsmString string);
 
 /**
+ * Get zone id (offline)
+ *
+ * @param zone VsmZone
+ * @return zone id
+ */
+VsmString vsm_zone_get_id(VsmZone zone);
+
+/**
+ * Get zone terminal (offline)
+ *
+ * @param zone VsmZone
+ * @return zone terminal
+ */
+int vsm_zone_get_terminal(VsmZone zone);
+
+/**
+ * Get zone state (offline)
+ *
+ * @param zone VsmZone
+ * @return zone state
+ */
+VsmZoneState vsm_zone_get_state(VsmZone zone);
+
+/**
+ * Get zone rootfs path (offline)
+ *
+ * @param zone VsmZone
+ * @return zone rootfs path
+ */
+VsmString vsm_zone_get_rootfs(VsmZone zone);
+
+/**
  * Release VsmZone
  *
  * @param zone VsmZone
@@ -398,6 +412,22 @@ void vsm_string_free(VsmString string);
 void vsm_zone_free(VsmZone zone);
 
 /**
+ * Get netdev name (offline)
+ *
+ * @param netdev VsmNetdev
+ * @return netdev name
+ */
+VsmString vsm_netdev_get_name(VsmNetdev netdev);
+
+/**
+ * Get netdev type (offline)
+ *
+ * @param netdev VsmNetdev
+ * @return netdev type
+ */
+VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev);
+
+/**
  * Release VsmNetdev
  *
  * @param netdev VsmNetdev
@@ -464,17 +494,6 @@ VsmStatus vsm_get_zone_ids(VsmClient client, VsmArrayString* array);
 VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
 
 /**
- * Get zone rootfs path.
- *
- * @param[in] client vasum-server's client
- * @param[in] id zone name
- * @param[out] rootpath zone rootfs path
- * @return status of this function call
- * @remark Use @p vsm_string_free() to free memory occupied by @p rootpath.
- */
-VsmStatus vsm_get_zone_rootpath(VsmClient client, const char* id, VsmString* rootpath);
-
-/**
  * Get zone name of process with given pid.
  *
  * @param[in] client vasum-server's client
index 1681c36..8b1530f 100644 (file)
@@ -252,23 +252,6 @@ BOOST_AUTO_TEST_CASE(GetActiveZoneId)
     vsm_client_free(client);
 }
 
-BOOST_AUTO_TEST_CASE(GetZoneRootPath)
-{
-    const std::string zoneId = "zone1";
-
-    VsmClient client = vsm_client_create();
-    VsmStatus status = vsm_connect(client);
-    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
-    VsmString rootpath;
-    status = vsm_get_zone_rootpath(client, zoneId.c_str(), &rootpath);
-    BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
-
-    BOOST_CHECK_EQUAL(rootpath, "/tmp/ut-zones/" + zoneId + "/rootfs");
-
-    vsm_string_free(rootpath);
-    vsm_client_free(client);
-}
-
 BOOST_AUTO_TEST_CASE(LookupZoneById)
 {
     const std::string activeZoneId = "zone1";
@@ -280,10 +263,10 @@ BOOST_AUTO_TEST_CASE(LookupZoneById)
     status = vsm_lookup_zone_by_id(client, activeZoneId.c_str(), &info);
     BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
 
-    BOOST_CHECK_EQUAL(info->id, activeZoneId);
-    BOOST_CHECK_EQUAL(info->state, RUNNING);
-    BOOST_CHECK_EQUAL(info->terminal, -1);
-    BOOST_CHECK_EQUAL(info->rootfs_path, "/tmp/ut-zones/" + activeZoneId + "/rootfs");
+    BOOST_CHECK_EQUAL(vsm_zone_get_id(info), activeZoneId);
+    BOOST_CHECK_EQUAL(vsm_zone_get_state(info), RUNNING);
+    BOOST_CHECK_EQUAL(vsm_zone_get_terminal(info), -1);
+    BOOST_CHECK_EQUAL(vsm_zone_get_rootfs(info), "/tmp/ut-zones/" + activeZoneId + "/rootfs");
 
     vsm_zone_free(info);
     vsm_client_free(client);
index a975087..c3a071f 100644 (file)
@@ -105,7 +105,7 @@ void init_wrapper()
     LOGS("");
 }
 
-static struct vsm_zone* wrap_vsm_zone(WrappedContext *w, VsmZone zone, bool create = false)
+static struct vsm_zone* wrap_vsm_zone(WrappedContext *w, Zone zone, bool create = false)
 {
     if (zone == NULL) {
         return NULL;
@@ -303,7 +303,9 @@ API int vsm_destroy_zone(struct vsm_context *ctx, const char *zone_name, int for
     if (!w->client) return VSM_ERROR_GENERIC;
     VsmStatus st = w->client->vsm_destroy_zone(zone_name);
     if (st == VSMCLIENT_SUCCESS) {
-        auto zonebyname = [zone_name](const WrappedZone& v) {return v.zone->id == zone_name;};
+        auto zonebyname = [zone_name](const WrappedZone& v) {
+            return static_cast<Zone>(v.zone)->id == zone_name;
+        };
         auto zonelist = std::remove_if(w->zones.begin(), w->zones.end(), zonebyname);
         w->zones.erase(zonelist);
     }
@@ -390,7 +392,7 @@ API int vsm_iterate_zone(struct vsm_context *ctx, void (*callback)(struct vsm_zo
     if (!w->client) return -VSM_ERROR_GENERIC;
     callback(ctx->root_zone, user_data);
     for (auto& z : w->zones) {
-        LOGI("iterate callback zone: " << z.zone->id);
+        LOGI("iterate callback zone: " << static_cast<Zone>(z.zone)->id);
         callback(&z.vz, user_data);
     }
     return 0;
@@ -401,7 +403,7 @@ API struct vsm_zone *vsm_lookup_zone_by_name(struct vsm_context *ctx, const char
     LOGS("name=" << path);
     callcheck();
     WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
-    VsmZone zone;
+    Zone zone;
     if (!w->client) return NULL;
     //CHECK if path is same as zone_name
     if (w->client->vsm_lookup_zone_by_id(path, &zone) != VSMCLIENT_SUCCESS)
@@ -415,7 +417,7 @@ API struct vsm_zone *vsm_lookup_zone_by_pid(struct vsm_context *ctx, pid_t pid)
     LOGS("pid=" << pid);
     callcheck();
     WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
-    VsmZone zone;
+    Zone zone;
     VsmString id;
     VsmStatus st;
     if (!w->client) return NULL;
@@ -440,7 +442,7 @@ API int vsm_add_state_changed_callback(struct vsm_context *ctx, vsm_zone_state_c
 
     auto dbus_cb = [=](const char* id, const char* dbusAddress, void* data) ->
     void {
-        VsmZone zone;
+        Zone zone;
         //TODO what are valid state, event
         vsm_zone_state_t t = VSM_ZONE_STATE_RUNNING;
         UNUSED(dbusAddress);
@@ -467,7 +469,7 @@ API int vsm_grant_device(struct vsm_zone *dom, const char *name, uint32_t flags)
     callcheck();
     WrappedZone *w = container_of(dom, WrappedZone, vz);
     const char *id = dom->name;
-    VsmZone zone;
+    Zone zone;
     w->client->vsm_lookup_zone_by_id(id, &zone);
     VsmStatus st = w->client->vsm_grant_device(id, name, flags);
     return wrap_error(st, w->client);
@@ -542,7 +544,7 @@ API struct vsm_netdev *vsm_lookup_netdev_by_name(struct vsm_zone *zone, const ch
     LOGS("");
     callcheck();
     WrappedZone *w = container_of(zone, WrappedZone, vz);
-    VsmNetdev nd;
+    Netdev nd;
     VsmStatus st = w->client->vsm_lookup_netdev_by_name(zone->name, name, &nd);
     if (st == VSMCLIENT_SUCCESS) {
         auto devbyname = [name](const vsm_netdev& v) {return ::strcmp(v.name, name) == 0;};
@@ -695,7 +697,7 @@ API struct vsm_zone *vsm_lookup_zone_by_terminal_id(struct vsm_context *ctx, int
     LOGS("terminal=" << terminal);
     callcheck();
     WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
-    VsmZone zone;
+    Zone zone;
     VsmString id;
     if (!w->client) return NULL;
     if (w->client->vsm_lookup_zone_by_terminal_id(terminal, &id) != VSMCLIENT_SUCCESS)