'Encode into a valid MSDP command.
Args:
cmdname (str): Name of send instruction.
args, kwargs (any): Arguments to OOB command.
Notes:
The output of this encoding will be
MSDP structures on these forms:
[cmdname, [], {}]          -> VAR cmdname VAL ""
[cmdname, [arg], {}]       -> VAR cmdname VAL arg
[cmdname, [args],{}]       -> VAR cmdname VAL ARRAYOPEN VAL arg VAL arg ... ARRAYCLOSE
[cmdname, [], {kwargs}]    -> VAR cmdname VAL TABLEOPEN VAR key VAL val ... TABLECLOSE
[cmdname, [args], {kwargs}] -> VAR cmdname VAL ARRAYOPEN VAL arg VAL arg ... ARRAYCLOSE
VAR cmdname VAL TABLEOPEN VAR key VAL val ... TABLECLOSE
Further nesting is not supported, so if an array argument
consists of an array (for example), that array will be
json-converted to a string.'

def encode_msdp(self, cmdname, *args, **kwargs):

    msdp_cmdname = '{msdp_var}{msdp_cmdname}{msdp_val}'.format(msdp_var=MSDP_VAR, msdp_cmdname=cmdname, msdp_val=MSDP_VAL)
    if (not (args or kwargs)):
        return msdp_cmdname
    msdp_args = ''
    if args:
        msdp_args = msdp_cmdname
        if (len(args) == 1):
            msdp_args += args[0]
        else:
            msdp_args += '{msdp_array_open}{msdp_args}{msdp_array_close}'.format(msdp_array_open=MSDP_ARRAY_OPEN, msdp_array_close=MSDP_ARRAY_CLOSE, msdp_args=''.join((('%s%s' % (MSDP_VAL, json.dumps(val))) for val in args)))
    msdp_kwargs = ''
    if kwargs:
        msdp_kwargs = msdp_cmdname
        msdp_kwargs += '{msdp_table_open}{msdp_kwargs}{msdp_table_close}'.format(msdp_table_open=MSDP_TABLE_OPEN, msdp_table_close=MSDP_TABLE_CLOSE, msdp_kwargs=''.join((('%s%s%s%s' % (MSDP_VAR, key, MSDP_VAL, json.dumps(val))) for (key, val) in kwargs.iteritems())))
    msdp_string = (msdp_args + msdp_kwargs)
    return msdp_string
