def get_devices_from_db(cred):
    query = '''MATCH (n:node)
               WHERE n.InterfacesSnmp IS NOT NULL
               RETURN n.SystemInfo,n.InterfacesSnmp,n.Neighbour,n.NextHops
            '''
    result = storeData.match_from_neo4j(cred, query)
    snmp_devices = []
    for record in result:
        snmp_dev = dict(system=ast.literal_eval(record["n.SystemInfo"][0]),
                        interfaces=ast.literal_eval(record["n.InterfacesSnmp"][0]),
                        neighbours=ast.literal_eval(record["n.Neighbour"][0]),
                        nextRoute=ast.literal_eval(record["n.NextHops"][0]))
        snmp_devices.append(snmp_dev)
    # print snmp_devices

    query2 = '''MATCH (n:node)
               WHERE n.InterfacesSnmp IS NULL AND n.InterfacesScan IS NOT NULL
               RETURN n.InterfacesScan;
            '''
    result2 = storeData.match_from_neo4j(cred, query2)
    nn_snmp_dev = []
    for record in result2:
        v = ast.literal_eval(record["n.InterfacesScan"][0])
        nn_snmp_dev.append((v['address'], v['mac']))

    # print nn_snmp_dev

    query3 = '''MATCH (n:node)
              WHERE n.mac IS NOT NULL AND n.address IS NOT NULL
              RETURN n.address,n.mac;
            '''
    result3 = storeData.match_from_neo4j(cred, query3)
    for record in result3:
        nn_snmp_dev.append((''.join(record["n.address"]), ''.join(record["n.mac"])))
    return [snmp_devices, nn_snmp_dev]
def get_host_l2l3_connectivity(cred, host, l2l3, nn_snmp, hosts):
    # print host
    # neighbour switchs routers
    nn_vis_neighbour = []
    nn_vis_router = []
    for neighbour in host['neighbours']:
        next_neigh, next_neigh_if = next_interface_connect(neighbour['ipaddress'], neighbour['mac'], l2l3, nn_snmp)
        if next_neigh != None:
            storeData.store_connectivity(cred, host, neighbour['ifname'], next_neigh, next_neigh_if)
        else:
            nn_vis_neighbour.append(neighbour)

    # next route relations nextRoute
    for nextroute in host['nextRoute']:
        if not exist_in_neighbour(nextroute['next_route'], host['neighbours']):
            next_neigh, next_neigh_if = next_interface_connect(nextroute['next_route'], '0x000000000000', l2l3, nn_snmp)
            if next_neigh != None:
                storeData.store_connectivity(cred, host, nextroute['if_next'], next_neigh, next_neigh_if)
            else:
                nn_vis_router.append(nextroute)

    for neighbour in nn_vis_neighbour:
        next_neigh, next_neigh_if = next_host_neighbour(neighbour['ipaddress'], neighbour['mac'], hosts)
        if next_neigh != None:
            storeData.store_connectivity(cred, host, neighbour['ifname'], next_neigh, next_neigh_if)
            vis_neighbour += 1

    for nextroute in nn_vis_router:
        next_neigh, next_neigh_if = next_interface_connect(nextroute['ipaddress'], '0x000000000000', l2l3, nn_snmp)
        if next_neigh != None:
            storeData.store_connectivity(cred, host, nextroute['if_next'], next_neigh, next_neigh_if)

    return 0
def match_from_neo4j(cred):
    query = '''MATCH (n:node)
                   WHERE n.mac IS NOT NULL AND n.address IS NOT NULL
                   RETURN ID(n);
                '''
    result = storeData.match_from_neo4j(cred, query)
    return result
def get_l2l3_connectivity(cred, dev, devices_snmp, devices_non_snmp):
    # neighbour relations
    for neighbour in dev['neighbours']:
        next_neigh, next_neigh_if = next_interface_connect(neighbour['ipaddress'], neighbour['mac'], devices_snmp,
                                                           devices_non_snmp)
        if next_neigh != None:
            storeData.store_connectivity(cred, dev, neighbour['ifname'], next_neigh, next_neigh_if)

    # next route relations
    for nextroute in dev['nextRoute']:
        if not exist_in_neighbour(nextroute['next_route'], dev['neighbours']):
            next_neigh, next_neigh_if = next_interface_connect(nextroute['next_route'], '0x000000000000', devices_snmp,
                                                               devices_non_snmp)
            if next_neigh != None:
                storeData.store_connectivity(cred, dev, nextroute['if_next'], next_neigh, next_neigh_if)

    return 0
def main(args):
    couchdb = db_auth.CouchDB()
    couchdb.dbname = 'test'
    neo4jdb = db_auth.Neo4jDB()
    neo4jdb.password = '******'
    cred = neo4jdb.get_Neo4j_credentials()
    cred1 = couchdb.get_couchdb_credentials()
    hosts = RetrieveData.get_hosts_info(cred1)
    hosts_mac_ip = hosts_address_mac(hosts)
    devices = device_discovery(hosts_mac_ip)
    print "All devices"
    print devices[0]
    print "snmp devices"
    print devices[1]
    print "non snmp devices"
    print devices[2]
    print "scan devices"
    print hosts
    storeData.insert_dev_to_db(cred, devices[0], devices[1], hosts)
    connectivity_discovery(cred, devices[1], devices[2])
    return 0