def link_asn_equipment(asn_id, equip_id): log.info("link_asn_equipment") from networkapi.api_asn.models import AsnEquipment asn_equipment = AsnEquipment() asn_equipment.create_v4(dict(equipment=equip_id, asn=asn_id))
def delete_asn_equipment(as_ids): """Delete ASNEquipment.""" try: asn_equipment = AsnEquipment() obj = asn_equipment.get_by_pk(ids=as_ids) obj.delete_v4() except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except exceptions.AsnAssociatedToEquipmentError as e: raise ValidationAPIException(str(e)) except AsnError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_asn_equipment_by_asn(asn_id): """Delete ASNEquipment.""" try: asn_obj = AsnEquipment() asn_equipment = asn_obj.get_by_pk(asn=asn_id) for obj in asn_equipment: obj.delete_v4() except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except exceptions.AsnAssociatedToEquipmentError as e: raise ValidationAPIException(str(e)) except AsnError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def update_asn_equipment(as_): """Update ASNEquipment.""" try: as_obj = AsnEquipment() if not as_.get('id'): raise Exception("The object do not have the id.") asn_equipment = as_obj.get_by_pk(ids=as_.get('id')) asn_equipment.update_v4(as_) except AsnErrorV4 as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return asn_equipment
def get_as_equipment_by_search(search=dict()): """Return a list of ASEquipment's by dict.""" try: as_equipment = AsnEquipment.get_by_pk() as_map = build_query_to_datatable_v3(as_equipment, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return as_map
def create_asn_equipment(asn_equipment): """Create ASNEquipment.""" try: asn_equipment_list = list() for equipment in asn_equipment.get("equipment"): obj = dict() obj["equipment"] = equipment obj["asn"] = asn_equipment.get("asn") as_obj = AsnEquipment() as_obj.create_v4(obj) asn_equipment_list.append({'id': as_obj.id}) except AsnErrorV4 as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return asn_equipment_list
def update_asn_equipment_by_asn(asn_id, as_): """ Update ASNEquipment. Return new asn_equipments new ids """ try: as_obj = AsnEquipment() asn_equipment = as_obj.get_by_pk(asn=asn_id) for obj in asn_equipment: obj.delete_v4() asn_equipment_obj = create_asn_equipment(as_) except AsnErrorV4 as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return asn_equipment_obj
def get_as_equipment_by_equip(equipment_id=None): """Return an ASEquipment by equipment id. Args: equipment_id: Id of Equipment """ try: as_equipment = list() for equip in equipment_id: as_equipment += AsnEquipment.get_by_pk(equipment=equip) except AsnNotFoundError as e: raise exceptions.AsnDoesNotExistException(str(e)) return as_equipment
def get_as_equipment_by_asn(asn_id=None): """Return an ASEquipment by asn id. Args: asn_id: Id of ASN """ try: as_equipment = list() for asn in asn_id: as_equipment += AsnEquipment.get_by_pk(asn=asn) except AsnNotFoundError as e: raise exceptions.AsnDoesNotExistException(str(e)) return as_equipment
def get_as_equipment_by_id(as_equipment_id=None): """Return an ASEquipment by id. Args: as_equipment_id: Id of ASEquipment """ try: as_equipment_list = list() for asn in as_equipment_id: as_equipment = AsnEquipment.get_by_pk(ids=asn) as_equipment_list.append(as_equipment) except AsnNotFoundError as e: raise exceptions.AsnDoesNotExistException(str(e)) return as_equipment_list
def create_neighbor_simple_v6(obj, user): from networkapi.ip.models import Ipv6 from networkapi.api_asn.models import AsnEquipment rack_name = obj.get('network_equipment') peer_equipment_ip = obj.get('peer_equipment_ip') peer_equipment_id = obj.get('peer_equipment_id') advertised_network = obj.get('advertised_network') oct1, oct2, oct3, oct4, oct5, oct6 = peer_equipment_ip.split(':') peer_obj_ip = Ipv6().get_by_blocks_equipment(oct1, oct2, oct3, oct4, oct5, oct6, peer_equipment_id) ips = Ipv6.objects.filter( networkipv6__id=peer_obj_ip.networkipv6.id).reverse() asn_equip = AsnEquipment().get_by_pk(equipment=peer_equipment_id) if not asn_equip: raise Exception("There`s no AS Number associated to the " "remote peer Equipment.") asn1 = asn_equip[0].asn.id peer_asn = asn1 if asn1 else None asn_remote = dict(id=peer_asn) peer_group_id = obj.get('peer_group') peer_group_ = peer_group_id if peer_group_id \ else _get_peer_group(advertised_network) peer_group = dict(id=peer_group_) response = list() neighbor_obj = dict() equipment_remote = dict(id=peer_equipment_id) ip_remote = dict(id=peer_obj_ip.id) neighbor_remote = dict(equipment=equipment_remote, ip=ip_remote, asn=asn_remote) for local_ip in ips[:2]: local_ip_id = local_ip.id for local_equipment in local_ip.equipments: if rack_name in local_equipment.nome: asn_equip_local = AsnEquipment().get_by_pk( equipment=local_equipment.id) if not asn_equip_local: raise Exception( "There`s no AS Number associated to the router.") asn = asn_equip_local[0].asn.id local_asn = asn if asn else None asn_local = dict(id=local_asn) equipment_local = dict(id=local_equipment.id) ip_local = dict(id=local_ip_id) neighbor_local = dict(equipment=equipment_local, ip=ip_local, asn=asn_local) neighbor_obj = dict( community=obj.get('community', True), soft_reconfiguration=obj.get('soft_reconfiguration', True), remove_private_as=obj.get('remove_private_as', False), next_hop_self=obj.get('next_hop_self', True), kind=obj.get('kind', 'I'), peer_group=peer_group, neighbor_remote=neighbor_remote, neighbor_local=neighbor_local) new_neighbor = create_neighbor_v6(neighbor_obj, user) response.append({'id': new_neighbor.id}) return response