Skip to content

Commit

Permalink
Move command line options and parser to separate classes (sonic-net#541)
Browse files Browse the repository at this point in the history
* Move ntf_queue to proper NotificationQueue class

* Move command line options and parser to separate classes

* Address comments

* Address comments
  • Loading branch information
kcudnik authored Dec 6, 2019
1 parent da71bdb commit 33ac34e
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 241 deletions.
113 changes: 113 additions & 0 deletions syncd/CommandLineOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "CommandLineOptions.h"

#include "swss/logger.h"

#include <sstream>

CommandLineOptions::CommandLineOptions()
{
SWSS_LOG_ENTER();

// default values for command line options

m_enableDiagShell = false;
m_enableTempView = false;
m_disableExitSleep = false;
m_enableUnittests = false;
m_enableConsistencyCheck = false;
m_enableSyncMode = false;

m_startType = SAI_START_TYPE_COLD_BOOT;

m_profileMapFile = "";

#ifdef SAITHRIFT

m_runRPCServer = false;

m_portMapFile = "";

#endif // SAITHRIFT

}

std::string CommandLineOptions::getCommandLineString() const
{
SWSS_LOG_ENTER();

std::stringstream ss;

ss << " EnableDiagShell=" << (m_enableDiagShell ? "YES" : "NO");
ss << " EnableTempView=" << (m_enableTempView ? "YES" : "NO");
ss << " DisableExitSleep=" << (m_disableExitSleep ? "YES" : "NO");
ss << " EnableUnittests=" << (m_enableUnittests ? "YES" : "NO");
ss << " EnableConsistencyCheck=" << (m_enableConsistencyCheck ? "YES" : "NO");
ss << " EnableSyncMode=" << (m_enableSyncMode ? "YES" : "NO");
ss << " StartType=" << startTypeToString(m_startType);
ss << " ProfileMapFile=" << m_profileMapFile;

#ifdef SAITHRIFT

ss << " RunRPCServer=" << (m_runRPCServer ? "YES" ? "NO");
ss << " PortMapFile=" << m_portMapFile;

#endif // SAITHRIFT

return ss.str();
}

sai_start_type_t CommandLineOptions::startTypeStringToStartType(
_In_ const std::string& startType)
{
SWSS_LOG_ENTER();

if (startType == STRING_SAI_START_TYPE_COLD_BOOT)
return SAI_START_TYPE_COLD_BOOT;

if (startType == STRING_SAI_START_TYPE_WARM_BOOT)
return SAI_START_TYPE_WARM_BOOT;

if (startType == STRING_SAI_START_TYPE_FAST_BOOT)
return SAI_START_TYPE_FAST_BOOT;

if (startType == STRING_SAI_START_TYPE_FASTFAST_BOOT)
return SAI_START_TYPE_FASTFAST_BOOT;

if (startType == STRING_SAI_START_TYPE_UNKNOWN)
return SAI_START_TYPE_UNKNOWN;

SWSS_LOG_WARN("unknown startType: '%s'", startType.c_str());

return SAI_START_TYPE_UNKNOWN;
}

std::string CommandLineOptions::startTypeToString(
_In_ sai_start_type_t startType)
{
SWSS_LOG_ENTER();

switch (startType)
{
case SAI_START_TYPE_COLD_BOOT:
return STRING_SAI_START_TYPE_COLD_BOOT;

case SAI_START_TYPE_WARM_BOOT:
return STRING_SAI_START_TYPE_WARM_BOOT;

case SAI_START_TYPE_FAST_BOOT:
return STRING_SAI_START_TYPE_FAST_BOOT;

case SAI_START_TYPE_FASTFAST_BOOT:
return STRING_SAI_START_TYPE_FASTFAST_BOOT;

case SAI_START_TYPE_UNKNOWN:
return STRING_SAI_START_TYPE_UNKNOWN;

default:

SWSS_LOG_WARN("unknown startType '%d'", startType);

return STRING_SAI_START_TYPE_UNKNOWN;
}
}

79 changes: 79 additions & 0 deletions syncd/CommandLineOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include "swss/sal.h"

#include <string>

#define STRING_SAI_START_TYPE_COLD_BOOT "cold"
#define STRING_SAI_START_TYPE_WARM_BOOT "warm"
#define STRING_SAI_START_TYPE_FAST_BOOT "fast"
#define STRING_SAI_START_TYPE_FASTFAST_BOOT "fastfast"
#define STRING_SAI_START_TYPE_UNKNOWN "unknown"

typedef enum _sai_start_type_t
{
SAI_START_TYPE_COLD_BOOT = 0,

SAI_START_TYPE_WARM_BOOT = 1,

SAI_START_TYPE_FAST_BOOT = 2,

/**
* A special type of boot used by Mellanox platforms to start in 'fastfast'
* boot mode
*/
SAI_START_TYPE_FASTFAST_BOOT = 3,

/**
* Set at last, just for error purpose.
*/
SAI_START_TYPE_UNKNOWN

} sai_start_type_t;

class CommandLineOptions
{
public:

CommandLineOptions();

virtual ~CommandLineOptions() = default;

public:

virtual std::string getCommandLineString() const;

public:

static sai_start_type_t startTypeStringToStartType(
_In_ const std::string& startType);

static std::string startTypeToString(
_In_ sai_start_type_t startType);

public:

bool m_enableDiagShell;
bool m_enableTempView;
bool m_disableExitSleep;
bool m_enableUnittests;

/**
* When set to true will enable DB vs ASIC consistency check after
* comparison logic.
*/
bool m_enableConsistencyCheck;

bool m_enableSyncMode;

sai_start_type_t m_startType;

std::string m_profileMapFile;

#ifdef SAITHRIFT
bool m_runRPCServer;
std::string m_portMapFile;
#endif // SAITHRIFT

};

154 changes: 154 additions & 0 deletions syncd/CommandLineOptionsParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include "CommandLineOptionsParser.h"

#include <getopt.h>

std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(
_In_ int argc,
_In_ char **argv)
{
SWSS_LOG_ENTER();

auto options = std::make_shared<CommandLineOptions>();

#ifdef SAITHRIFT
const char* const optstring = "dp:t:uSUCsrm:h";
#else
const char* const optstring = "dp:t:uSUCsh";
#endif // SAITHRIFT

while(true)
{
static struct option long_options[] =
{
{ "diag", no_argument, 0, 'd' },
{ "profile", required_argument, 0, 'p' },
{ "startType", required_argument, 0, 't' },
{ "useTempView", no_argument, 0, 'u' },
{ "disableExitSleep", no_argument, 0, 'S' },
{ "enableUnittests", no_argument, 0, 'U' },
{ "enableConsistencyCheck", no_argument, 0, 'C' },
{ "syncMode", no_argument, 0, 's' },
#ifdef SAITHRIFT
{ "rpcserver", no_argument, 0, 'r' },
{ "portmap", required_argument, 0, 'm' },
#endif // SAITHRIFT
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};

int option_index = 0;

int c = getopt_long(argc, argv, optstring, long_options, &option_index);

if (c == -1)
{
break;
}

switch (c)
{
case 'd':
options->m_enableDiagShell = true;
break;

case 'p':
options->m_profileMapFile = std::string(optarg);
break;

case 't':
options->m_startType = CommandLineOptions::startTypeStringToStartType(optarg);

if (options->m_startType == SAI_START_TYPE_UNKNOWN)
{
SWSS_LOG_ERROR("unknown start type '%s'", optarg);
exit(EXIT_FAILURE);
}
break;

case 'u':
options->m_enableTempView = true;
break;

case 'S':
options->m_disableExitSleep = true;
break;

case 'U':
options->m_enableUnittests = true;
break;

case 'C':
options->m_enableConsistencyCheck = true;
break;

case 's':
options->m_enableSyncMode = true;
break;

#ifdef SAITHRIFT
case 'r':
options->m_runRPCServer = true;
break;
case 'm':
options->m_portMapFile = std::string(optarg);
break;
#endif // SAITHRIFT

case 'h':
printUsage();
exit(EXIT_SUCCESS);

case '?':
SWSS_LOG_WARN("unknown option %c", optopt);
printUsage();
exit(EXIT_FAILURE);

default:
SWSS_LOG_ERROR("getopt_long failure");
exit(EXIT_FAILURE);
}
}

return options;
}

void CommandLineOptionsParser::printUsage()
{
SWSS_LOG_ENTER();

#ifdef SAITHRIFT
std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-r] [-m portmap] [-h]" << std::endl;
#else
std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-h]" << std::endl;
#endif // SAITHRIFT

std::cout << " -d --diag" << std::endl;
std::cout << " Enable diagnostic shell" << std::endl;
std::cout << " -p --profile profile" << std::endl;
std::cout << " Provide profile map file" << std::endl;
std::cout << " -t --startType type" << std::endl;
std::cout << " Specify start type (cold|warm|fast|fastfast) " << std::endl;
std::cout << " -u --useTempView" << std::endl;
std::cout << " Use temporary view between init and apply" << std::endl;
std::cout << " -S --disableExitSleep" << std::endl;
std::cout << " Disable sleep when syncd crashes" << std::endl;
std::cout << " -U --enableUnittests" << std::endl;
std::cout << " Metadata enable unittests" << std::endl;
std::cout << " -C --enableConsistencyCheck" << std::endl;
std::cout << " Enable consisteny check DB vs ASIC after comparison logic" << std::endl;
std::cout << " -s --syncMode" << std::endl;
std::cout << " Enable synchronous mode" << std::endl;

#ifdef SAITHRIFT

std::cout << " -r --rpcserver" << std::endl;
std::cout << " Enable rpcserver" << std::endl;
std::cout << " -m --portmap portmap" << std::endl;
std::cout << " Specify port map file" << std::endl;

#endif // SAITHRIFT

std::cout << " -h --help" << std::endl;
std::cout << " Print out this message" << std::endl;
}

23 changes: 23 additions & 0 deletions syncd/CommandLineOptionsParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "syncd.h"
#include "CommandLineOptions.h"

#include <memory>

class CommandLineOptionsParser
{
private:

CommandLineOptionsParser() = delete;

~CommandLineOptionsParser() = delete;

public:

static std::shared_ptr<CommandLineOptions> parseCommandLine(
_In_ int argc,
_In_ char **argv);

static void printUsage();
};
8 changes: 6 additions & 2 deletions syncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ syncd_SOURCES = \
syncd_applyview.cpp \
syncd_flex_counter.cpp \
TimerWatchdog.cpp \
NotificationQueue.cpp
NotificationQueue.cpp \
CommandLineOptions.cpp \
CommandLineOptionsParser.cpp

syncd_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS)
syncd_LDADD = -lhiredis -lswsscommon $(SAILIB) -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -ldl
Expand All @@ -50,7 +52,9 @@ tests_SOURCES = \
syncd_applyview.cpp \
syncd_flex_counter.cpp \
TimerWatchdog.cpp \
NotificationQueue.cpp
NotificationQueue.cpp \
CommandLineOptions.cpp \
CommandLineOptionsParser.cpp

tests_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON)
tests_LDADD = -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/lib/src/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta
Expand Down
Loading

0 comments on commit 33ac34e

Please sign in to comment.