Example #1
0
def parse_cps_parameters(module_name,
                         qualifier,
                         attr_type,
                         attr_data,
                         operation=None,
                         db=None,
                         commit_event=None):

    obj = cps_object.CPSObject(module=module_name, qual=qualifier)

    if operation:
        obj.set_property('oper', operation)

    if attr_type:
        for key, val in iteritems(attr_type):
            cps_utils.cps_attr_types_map.add_type(key, val)

    for key, val in iteritems(attr_data):

        embed_attrs = key.split(',')
        embed_attrs_len = len(embed_attrs)
        if embed_attrs_len >= 3:
            obj.add_embed_attr(embed_attrs, val, embed_attrs_len - 2)
        else:
            if isinstance(val, str):
                val_list = val.split(',')
                # Treat as list if value contains ',' but is not
                # enclosed within {}
                if len(val_list) == 1 or val.startswith('{'):
                    obj.add_attr(key, val)
                else:
                    obj.add_attr(key, val_list)
            else:
                obj.add_attr(key, val)

    if db:
        cps.set_ownership_type(obj.get_key(), 'db')
        obj.set_property('db', True)
    else:
        obj.set_property('db', False)

    if commit_event:
        cps.set_auto_commit_event(obj.get_key(), True)
        obj.set_property('commit-event', True)
    return obj
Example #2
0
def object_from_parameters(prog,description, optional_fields=[]):
    """
    Uses Argparse to parse the program's command line arguments into an object.
    @param prog the name of the program (passed into argparse)
    @param description of the program (also passed to argparse)
    @param list_of_required_fields is used to determine which field is mandatory/not required valid values include "mod","qual","oper","attr"
    """

    _qualifiers = cps.QUALIFIERS

    parser = argparse.ArgumentParser(prog=prog, description=description)

    parser.add_argument('module',help='The object\'s name and optional qualifier.  For instance: cps/node-group or if/interfaces/interface.  A qualifier can optionally be placed at the beginning')
    parser.add_argument('additional',help='This field can contain a series of object attributes in the form of attr=value combinations',action='append',nargs='*')

    parser.add_argument('-mod',help='An alternate way to specify the module name', metavar='module',required=False,action='store',nargs=1)
    parser.add_argument('-d',help='Print some additional details about the objects parsed and sent to the backend', required=False, action='store_true')

    parser.add_argument('-qua',choices=cps.QUALIFIERS,help='The object\'s qualifier',required=False,action='store')
    parser.add_argument('-attr',help='Object attributes in the form of attr=value', required=False,action='append')

    parser.add_argument('-db',help='Attempt to use the db directly to satisfy the request instad of the normally registered object', required=False,action='store_true')

    if 'oper' in optional_fields :
        parser.add_argument('-oper',choices=cps.OPERATIONS, help='The operation types.  This is only used in CPS commit operations', required=True,action='store')

    if 'commit-event' in optional_fields :
        parser.add_argument('-commit-event',help='This flag will try to force the default state of the auto-commit event to true.  This is only used in CPS commit operations', required=False,action='store_true')

    _args = vars(parser.parse_args())
    if 'd' in _args and _args['d']:
        print _args

    _qual_list = cps.QUALIFIERS
    _class_name = _args['module']
    _attrs=[]

    qual = _qual_list[0]

    if len(_class_name) > 0:
        _lst = _class_name.split(cps.PATH_SEP,1);

        if len(_lst)>0 and _lst[0] in _qual_list:
            _class_name = _lst[1]
            qual = _lst[0]

    if 'additional' in  _args and _args['additional']!=None:
        _lst = _args['additional'][0]
        for i in range(0,len(_lst)):
            if _lst[i] in _qual_list:
                qual = _lst[i]
            else:
                _attrs.append(_lst[i])

    if 'attr' in _args and _args['attr']!=None :
        for i in _args['attr']:
            _attrs.append(i)

    if 'qua' in _args and _args['qua']!=None:
        qual = _args['qua']

    if len(_class_name)>0 and _class_name[len(_class_name)-1]=='/':
        _class_name = _class_name[0:-1]

    obj = CPSObject(_class_name,qual)

    if 'oper' in _args and _args['oper']!=None:
        if 'd' in _args and _args['d']: print('Operation type is: %s' %_args['oper'])
        obj.set_property('oper',_args['oper'])

    for i in _attrs:
        if i.find('=')==-1:
            continue
        _data = i.split('=', 1)

        # When value for attribute is empty, use None to indicate attribute delete
        _val = None
        if len(_data[1]) != 0:
            _val = _data[1]

        # For embedded attribute check if comma seperated attribute list is given
        # then add it as embedded
        embed_attrs = _data[0].split(',')
        if len(embed_attrs) == 3:
            obj.add_embed_attr(embed_attrs,_val)
        else:
            val_list = _data[1].split(',')
            # Treat as leaf list if value contains ',' but is not
            # enclosed within {}
            if len(val_list) == 1 or _data[1][0] == '{':
                obj.add_attr(_data[0],_val)
            else:
                obj.add_attr(_data[0],val_list)

    if 'db' in _args and _args['db']:
        if 'd' in _args and _args['d']:
            print('Attempt to force database use')
        cps.set_ownership_type(obj.get_key(),'db')
        obj.set_property('db',True)
    else:
        obj.set_property('db',False)

    if 'commit_event' in _args and _args['commit_event']:
        if 'd' in _args and _args['d']:
            print('Attempt to force use of auto-event for requested change on %s' % obj.get_key())

        cps.set_auto_commit_event(obj.get_key(),True)
        obj.set_property('commit-event',True)

    if 'd' in _args and _args['d']:
        print obj.get()
    return obj