Пример #1
0
def reconcile():
    l = cps_unit_test_data.cps_gen_ut_data(1)
    dest_obj = cps_object.CPSObject("base-ip/ipv6")

    callback_dict = {'sync': sync_cb, 'error': sync_err_cb}

    print cps.reconcile(l, dest_obj.get(), callback_dict)
def _sync():

    log_funcname = sys._getframe().f_code.co_name

    # Get all cps/node-group objects
    node_grp_objs = _get_node_group_objs()

    # Get all cps/connection-object and mark them as not seen
    conn_objs = _get_connection_objs()
    for conn in conn_objs:
        conn_objs[conn]['seen'] = "no"

    for grp in node_grp_objs:

        # Build group_info dict to fill cps/connectivity-group object
        group_info = {}
        l = {}
        group_info = {'name': grp['cps/node-group/name'], 'node': []}
        if 'cps/node-group/type' in grp:
            group_info['type'] = grp['cps/node-group/type']

        if 'cps/node-group/node' in grp:
            for node_index in grp['cps/node-group/node']:

                node = grp['cps/node-group/node'][node_index]
                l = {
                    'name':
                    node['name'],
                    'ip':
                    node['ip'],
                    'timestamp':
                    '{:%Y-%b-%d %H:%M:%S:%f}'.format(datetime.datetime.now())
                }

                if node['ip'] == default_redis_addrv4:
                    l.update({'tunnel-ip': node['ip']})
                elif node['name'] not in conn_objs:
                    # Create tunnels
                    node_info = {'name': node['name'], 'ip': node['ip']}
                    tunn_port = _node_connect(node_info)
                    if tunn_port == False:
                        log_msg(
                            6,
                            " Tunnel Node connect returned false for node %s : %s"
                            % (node['name'], node['ip']), log_funcname,
                            sys._getframe().f_lineno)
                        continue
                    conn_objs[node['name']] = {
                        "ip":
                        node['ip'],
                        "tunnel":
                        tunn_port,
                        "timestamp":
                        '{:%Y-%b-%d %H:%M:%S:%f}'.format(
                            datetime.datetime.now()),
                        "seen":
                        "yes"
                    }

                    l.update({'tunnel-ip': "127.0.0.1:" + tunn_port})
                else:
                    conn_objs[node['name']]["seen"] = "yes"
                    l.update({
                        'tunnel-ip':
                        "127.0.0.1:" + conn_objs[node['name']]['tunnel'],
                        'timestamp':
                        conn_objs[node['name']]['timestamp']
                    })

                group_info['node'].append(l)

        # Create cps/connectivity-group object in DB
        group_info['timestamp'] = '{:%Y-%b-%d %H:%M:%S:%f}'.format(
            datetime.datetime.now())

        data = {}
        data = _fill_connectivity_group_obj(group_info)
        dest_obj = cps_object.CPSObject(module="cps/connectivity-group",
                                        qual="observed")
        dest_obj.add_attr("name", str(grp['cps/node-group/name']))
        log_msg(
            6, " In sync- about to sync connectivity grp obj src %s dst %s " %
            (str(data), str(dest_obj.get())), log_funcname,
            sys._getframe().f_lineno)
        cps.reconcile([data], dest_obj.get(), callback_dict)

    # Delete tunnels that is not part of any group
    log_msg(
        6, " Delete Tunnels that is not part of any group: conn_objs: %s" %
        (str(conn_objs)), log_funcname,
        sys._getframe().f_lineno)
    for conn in conn_objs:
        if conn_objs[conn]["seen"] == "no":
            node = {'name': conn, 'ip': conn_objs[conn]['ip']}
            _node_disconnect(node)
    return True