示例#1
0
def insert_interface_unit(iface_node, unit, service_id_regex):
    """
    Creates or updates logical interface units.
    """
    user = utils.get_user()
    unit_name = u'{}'.format(unit['unit'])
    # Unit names are unique per interface
    result = iface_node.get_unit(unit_name)
    if result.get('Part_of', None):
        unit_node = result.get('Part_of')[0].get('node')
    else:
        node_handle = utils.create_node_handle(unit_name, 'Unit', 'Logical')
        unit_node = node_handle.get_node()
        helpers.set_part_of(user, iface_node, unit_node.handle_id)
        logger.info('Unit {interface}.{unit} created.'.format(
            interface=iface_node.data['name'], unit=unit_node.data['name']))
    helpers.set_noclook_auto_manage(unit_node, True)
    unit['ip_addresses'] = [
        address.lower() for address in unit.get('address', '')
    ]
    property_keys = ['description', 'ip_addresses', 'vlanid']
    helpers.dict_update_node(user, unit_node.handle_id, unit, property_keys)
    # Auto depend service on unit
    auto_depend_services(unit_node.handle_id, unit.get('description', ''),
                         service_id_regex, 'Unit')
示例#2
0
def get_or_create_port(port_name, node, user):
    result = node.get_port(port_name)
    if 'Has' in result:
        port_node = result.get('Has')[0].get('node')
    else:
        port_handle = utils.create_node_handle(port_name, 'Port', 'Physical')
        port_node = port_handle.get_node()
        helpers.set_has(user, node, port_node.handle_id)
    return port_node
示例#3
0
def get_peering_partner(peering):
    """
    Inserts a new node of the type Peering partner and ensures that this node
    is unique for AS number.
    Returns the created node.
    """
    try:
        return PEER_AS_CACHE[peering['as_number']]
    except KeyError:
        logger.info('Peering Partner {name} not in cache.'.format(
            name=peering.get('description')))
        pass
    user = utils.get_user()
    peer_node = None
    peer_properties = {'name': 'Missing description', 'as_number': '0'}
    if peering.get('description'):
        peer_properties['name'] = peering.get('description')
    if peering.get('as_number'):
        peer_properties['as_number'] = peering.get('as_number')
        hits = nc.get_nodes_by_value(nc.graphdb.manager,
                                     prop='as_number',
                                     value=peer_properties['as_number'])
        found = 0
        for node in hits:
            peer_node = nc.get_node_model(nc.graphdb.manager,
                                          node['handle_id'])
            helpers.set_noclook_auto_manage(peer_node, True)
            if peer_node.data[
                    'name'] == 'Missing description' and peer_properties[
                        'name'] != 'Missing description':
                helpers.dict_update_node(user, peer_node.handle_id,
                                         peer_properties)
            logger.info('Peering Partner {name} fetched.'.format(
                name=peer_properties['name']))
            found += 1
        if found > 1:
            logger.error(
                'Found more then one Peering Partner with AS number {!s}'.
                format(peer_properties['as_number']))

    if not peer_node:
        node_handle = utils.create_node_handle(peer_properties['name'],
                                               'Peering Partner', 'Relation')
        peer_node = node_handle.get_node()
        helpers.set_noclook_auto_manage(peer_node, True)
        helpers.dict_update_node(user, peer_node.handle_id, peer_properties,
                                 peer_properties.keys())
        logger.info('Peering Partner {name} created.'.format(
            name=peer_properties['name']))
    PEER_AS_CACHE[peering['as_number']] = peer_node
    return peer_node
示例#4
0
def insert_juniper_interfaces(router_node, interfaces):
    """
    Insert all interfaces in the interfaces list with a Has
    relationship from the router_node. Some filtering is done for
    interface names that are not interesting.
    """
    p = r"""
        .*\*|\.|all|tap|fxp.*|pfe.*|pfh.*|mt.*|pd.*|pe.*|vt.*|bcm.*|dsc.*|em.*|gre.*|ipip.*|lsi.*|mtun.*|pimd.*|pime.*|
        pp.*|pip.*|irb.*|demux.*|cbp.*|me.*|lo.*
        """
    not_interesting_interfaces = re.compile(p, re.VERBOSE)
    user = utils.get_user()

    cleanup_hardware_v1(router_node, user)
    service_id_regex = _service_id_regex()

    for interface in interfaces:
        port_name = interface['name']
        if port_name and not not_interesting_interfaces.match(
                port_name) and not interface.get('inactive', False):
            result = router_node.get_port(port_name)
            if 'Has' in result:
                port_node = result.get('Has')[0].get('node')
            else:
                node_handle = utils.create_node_handle(port_name, 'Port',
                                                       'Physical')
                port_node = node_handle.get_node()
                helpers.set_has(user, router_node, port_node.handle_id)
            helpers.set_noclook_auto_manage(port_node, True)
            property_keys = ['description', 'name']
            helpers.dict_update_node(user, port_node.handle_id, interface,
                                     property_keys)
            # Update interface units
            for unit in interface['units']:
                if not unit.get('inactive', False):
                    insert_interface_unit(port_node, unit, service_id_regex)
            # Auto depend services
            auto_depend_services(port_node.handle_id,
                                 interface.get('description', ''),
                                 service_id_regex)
            logger.info('{router} {interface} done.'.format(
                router=router_node.data['name'], interface=port_name))
        else:
            logger.info('Interface {name} ignored.'.format(name=port_name))
示例#5
0
def get_peering_partner(peering):
    """
    Inserts a new node of the type Peering partner and ensures that this node
    is unique for AS number.
    Returns the created node.
    """
    try:
        return PEER_AS_CACHE[peering['as_number']]
    except KeyError:
        logger.info('Peering Partner {name} not in cache.'.format(
            name=peering.get('description')))
        pass
    user = utils.get_user()
    peer_node = None
    peer_properties = {'name': 'Missing description', 'as_number': '0'}
    # neither description or as_number error and return
    if not (peering.get('description') or peering.get('as_number')):
        logger.error('Neither AS number nor description in peering %s',
                     peering)
        return None
    if peering.get('description'):
        peer_properties['name'] = peering.get('description')
    if peering.get('as_number'):
        peer_properties['as_number'] = peering.get('as_number')
        # as number is most important
        hits = nc.get_nodes_by_value(nc.graphdb.manager,
                                     prop='as_number',
                                     value=peer_properties['as_number'])
        found = 0
        for node in hits:
            peer_node = nc.get_node_model(nc.graphdb.manager,
                                          node['handle_id'])
            helpers.set_noclook_auto_manage(peer_node, True)
            if peer_node.data[
                    'name'] == 'Missing description' and peer_properties[
                        'name'] != 'Missing description':
                helpers.dict_update_node(user, peer_node.handle_id,
                                         peer_properties)
            logger.info('Peering Partner {name} fetched.'.format(
                name=peer_properties['name']))
            found += 1
        if found > 1:
            logger.error(
                'Found more then one Peering Partner with AS number {!s}'.
                format(peer_properties['as_number']))

        if not peer_node:
            # since we have a AS number we will create a new Peering Partner, even if name is missing or exists
            node_handle = utils.create_node_handle(peer_properties['name'],
                                                   'Peering Partner',
                                                   'Relation')
            peer_node = node_handle.get_node()
            helpers.set_noclook_auto_manage(peer_node, True)
            helpers.dict_update_node(user, peer_node.handle_id,
                                     peer_properties, peer_properties.keys())
            logger.info('Peering Partner %s AS(%s) created.',
                        peer_properties['name'], peer_properties['as_number'])

    # Handle peer with name only
    if not peer_node and peering.get('description'):
        # Try and get peer_partners
        res = NodeHandle.objects.filter(
            node_name__iexact=peer_properties['name'],
            node_type__type='Peering Partner').order_by('-modified')
        for ph in res:
            peer_node = ph.get_node()
            break
        if not peer_node:
            # create
            peer_nh = utils.get_unique_node_handle(peer_properties['name'],
                                                   'Peering Partner',
                                                   'Relation')
            peer_node = peer_nh.get_node()
        if not peer_node.data.get('as_number'):
            # Peering partner did not exist
            logger.warning(
                'Peering Partner %s without AS number created for peering: %s',
                peer_node.data.get('name'), peering)
            # AS number is going to be 0, but that is ok
            helpers.dict_update_node(user, peer_node.handle_id,
                                     peer_properties, peer_properties.keys())

        elif peer_node.data.get('as_number') != '0':
            # warn about as number not being in peering
            logger.warning(
                'Peering found for Peering Partner %s without the AS number %s mentioned. Peering: %s',
                peer_properties['name'], peer_node.data.get('as_number'),
                peering)
        helpers.set_noclook_auto_manage(peer_node, True)

    PEER_AS_CACHE[peering['as_number']] = peer_node
    return peer_node