Add enum/string conversions for allocd types

Creates common interfaces to convert enum types for requests
into strings and from strings into enums.

Bug: 148823285
Test: None
Change-Id: I1d87c6abf3146eaa5551b6fe821d7be786069a7e
diff --git a/host/libs/allocd/utils.cpp b/host/libs/allocd/utils.cpp
index d90f48c..603c8d0 100644
--- a/host/libs/allocd/utils.cpp
+++ b/host/libs/allocd/utils.cpp
@@ -19,15 +19,65 @@
 #include <cstdint>
 #include <optional>
 
-#include "common/libs/fs/shared_fd.h"
 #include "common/libs/fs/shared_buf.h"
+#include "common/libs/fs/shared_fd.h"
 #include "host/libs/allocd/request.h"
 
 namespace cuttlefish {
 
+// While the JSON schema and payload structure are designed to be extensible,
+// and avoid version incompatibility. However, should project requirements
+// change, it is necessary that we have a mechanism to handle incompatibilities
+// that arise over time. If an incompatibility should come about, the
+// kMinHeaderVersion constant should be increased to match the new minimal set
+// of features that are supported.
+
+/// Current supported Header version number
 constexpr uint16_t kCurHeaderVersion = 1;
+
+/// Oldest compatible header version number
 constexpr uint16_t kMinHeaderVersion = 1;
 
+const std::map<RequestType, const char*> RequestTyToStrMap = {
+    {RequestType::ID, "alloc_id"},
+    {RequestType::CreateInterface, "create_interface"},
+    {RequestType::DestroyInterface, "destroy_interface"},
+    {RequestType::StopSession, "stop_session"},
+    {RequestType::Shutdown, "shutdown"},
+    {RequestType::Invalid, "invalid"}};
+
+const std::map<std::string, RequestType> StrToRequestTyMap = {
+    {"alloc_id", RequestType::ID},
+    {"create_interface", RequestType::CreateInterface},
+    {"destroy_interface", RequestType::DestroyInterface},
+    {"stop_session", RequestType::StopSession},
+    {"shutdown", RequestType::Shutdown},
+    {"invalid", RequestType::Invalid}};
+
+const std::map<std::string, IfaceType> StrToIfaceTyMap = {
+    {"invalid", IfaceType::Invalid},
+    {"mtap", IfaceType::mtap},
+    {"wtap", IfaceType::wtap},
+    {"wbr", IfaceType::wbr}};
+
+const std::map<IfaceType, std::string> IfaceTyToStrMap = {
+    {IfaceType::Invalid, "invalid"},
+    {IfaceType::mtap, "mtap"},
+    {IfaceType::wtap, "wtap"},
+    {IfaceType::wbr, "wbr"}};
+
+const std::map<RequestStatus, std::string> ReqStatusToStrMap = {
+    {RequestStatus::Invalid, "invalid"},
+    {RequestStatus::Pending, "pending"},
+    {RequestStatus::Failure, "failure"},
+    {RequestStatus::Success, "success"}};
+
+const std::map<std::string, RequestStatus> StrToReqStatusMap = {
+    {"invalid", RequestStatus::Invalid},
+    {"pending", RequestStatus::Pending},
+    {"failure", RequestStatus::Failure},
+    {"success", RequestStatus::Success}};
+
 bool SendJsonMsg(SharedFD client_socket, const Json::Value& resp) {
   LOG(INFO) << "Sending JSON message";
   Json::FastWriter writer;
@@ -48,7 +98,7 @@
 std::optional<Json::Value> RecvJsonMsg(SharedFD client_socket) {
   LOG(INFO) << "Receiving JSON message";
   RequestHeader header;
-  client_socket->Recv(&header, sizeof(header), recv_flags);
+  client_socket->Recv(&header, sizeof(header), kRecvFlags);
 
   if (header.version < kMinHeaderVersion) {
     LOG(WARNING) << "bad request header version: " << header.version;
@@ -61,4 +111,74 @@
   return reader.parse(payload);
 }
 
+std::string ReqTyToStr(RequestType req_ty) {
+  switch (req_ty) {
+    case RequestType::Invalid:
+      return "invalid";
+    case RequestType::Shutdown:
+      return "shutdown";
+    case RequestType::StopSession:
+      return "stop_session";
+    case RequestType::DestroyInterface:
+      return "destroy_interface";
+    case RequestType::CreateInterface:
+      return "create_interface";
+    case RequestType::ID:
+      return "id";
+  }
+}
+
+RequestType StrToReqTy(const std::string& req) {
+  auto it = StrToRequestTyMap.find(req);
+  if (it == StrToRequestTyMap.end()) {
+    return RequestType::Invalid;
+  } else {
+    return it->second;
+  }
+}
+
+RequestStatus StrToStatus(const std::string& st) {
+  auto it = StrToReqStatusMap.find(st);
+  if (it == StrToReqStatusMap.end()) {
+    return RequestStatus::Invalid;
+  } else {
+    return it->second;
+  }
+}
+
+std::string StatusToStr(RequestStatus st) {
+  switch (st) {
+    case RequestStatus::Invalid:
+      return "invalid";
+    case RequestStatus::Pending:
+      return "pending";
+    case RequestStatus::Success:
+      return "success";
+    case RequestStatus::Failure:
+      return "failure";
+  }
+}
+
+std::string IfaceTyToStr(IfaceType iface) {
+  switch (iface) {
+    case IfaceType::Invalid:
+      return "invalid";
+    case IfaceType::mtap:
+      return "mtap";
+    case IfaceType::wtap:
+      return "wtap";
+    case IfaceType::wbr:
+      return "wbr";
+  }
+}
+
+IfaceType StrToIfaceTy(const std::string& iface) {
+  auto it = StrToIfaceTyMap.find(iface);
+  if (it == StrToIfaceTyMap.end()) {
+    return IfaceType::Invalid;
+  } else {
+    return it->second;
+  }
+}
+
 }  // namespace cuttlefish
diff --git a/host/libs/allocd/utils.h b/host/libs/allocd/utils.h
index e426dae..f173935 100644
--- a/host/libs/allocd/utils.h
+++ b/host/libs/allocd/utils.h
@@ -28,12 +28,37 @@
 
 namespace cuttlefish {
 
-static constexpr int send_flags = 0;
-static constexpr int recv_flags = 0;
+constexpr char kDefaultLocation[] =
+    "/var/run/cuttlefish/cuttlefish_allocd.sock";
 
-// returns true if successfully sent the whole message
+// Default flags for send and receive.
+static constexpr int kSendFlags = 0;
+static constexpr int kRecvFlags = 0;
+
+/// Sends a Json value over client_socket
+///
+/// returns true if successfully sent the whole JSON object
+/// returns false otherwise
 bool SendJsonMsg(cuttlefish::SharedFD client_socket, const Json::Value& resp);
 
+/// Receives a single Json value over client_socket
+///
+/// The returned option will contain the JSON object when successful,
+/// or an std::nullopt if an error is reported
 std::optional<Json::Value> RecvJsonMsg(cuttlefish::SharedFD client_socket);
 
+// Helper functions mapping between Enum types and std::string
+
+RequestType StrToReqTy(const std::string& req);
+
+std::string ReqTyToStr(RequestType req_ty);
+
+IfaceType StrToIfaceTy(const std::string& iface);
+
+std::string IfaceTyToStr(IfaceType iface);
+
+RequestStatus StrToStatus(const std::string& st);
+
+std::string StatusToStr(RequestStatus st);
+
 }  // namespace cuttlefish