Skip to content

Commit

Permalink
Merge pull request #293 from globocom/develop
Browse files Browse the repository at this point in the history
peering refactor + junos plugin
  • Loading branch information
Laura authored Oct 28, 2020
2 parents b3422e7 + e47b43c commit cee0722
Show file tree
Hide file tree
Showing 25 changed files with 1,522 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


# Docker image version
NETAPI_IMAGE_VERSION := 2.1.0
NETAPI_IMAGE_VERSION := 3.1.0

# Gets git current branch
curr_branch := $(shell git symbolic-ref --short -q HEAD)
Expand Down
54 changes: 49 additions & 5 deletions networkapi/api_asn/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ def get_by_pk(cls, id):
cls.log.error(u'Failure to search the ASN.')
raise exceptions.AsnError(u'Failure to search the ASN.')

def get_by_asn(cls, asn):
"""Get AS by id.
:return: AS.
:raise AsnNotFoundError: As not registered.
:raise AsnError: Failed to search for the As.
:raise OperationalError: Lock wait timeout exceeded
"""
try:
return Asn.objects.get(asn=asn)
except ObjectDoesNotExist:
cls.log.error(u'ASN not found. pk {}'.format(id))
raise exceptions.AsnNotFoundError(id)
except OperationalError:
cls.log.error(u'Lock wait timeout exceeded.')
raise OperationalError()
except Exception:
cls.log.error(u'Failure to search the ASN.')
raise exceptions.AsnError(u'Failure to search the ASN.')


def create_v4(self, as_map):
"""Create ASN."""

Expand Down Expand Up @@ -138,7 +160,7 @@ class Meta(BaseModel.Meta):
managed = True

@classmethod
def get_by_pk(cls, id):
def get_by_pk(cls, ids=None, asn=None, equipment=None):
"""Get AsnEquipment by id.
:return: AsnEquipment.
Expand All @@ -148,16 +170,26 @@ def get_by_pk(cls, id):
:raise OperationalError: Lock wait timeout exceeded
"""
try:
return AsnEquipment.objects.get(id=id)
logging.info("get asn_equipment by id, asn or equipment")
if ids:
return AsnEquipment.objects.get(id=int(ids))
elif asn:
return AsnEquipment.objects.filter(asn=int(asn))
elif equipment:
return AsnEquipment.objects.filter(equipment__id=int(equipment))

return AsnEquipment.objects.all()

except ObjectDoesNotExist:
cls.log.error(u'AsnEquipment not found. pk {}'.format(id))
raise exceptions.AsnEquipmentNotFoundError(id)
except OperationalError:
cls.log.error(u'Lock wait timeout exceeded.')
raise OperationalError()
except Exception:
cls.log.error(u'Failure to search the AS.')
raise exceptions.AsnEquipmentError(u'Failure to search the AS.')
except Exception as e:
cls.log.error(u'Failure to search the ASNEquipment. E: %s' % e)
raise exceptions.AsnEquipmentError(
u'Failure to search the ASNEquipment. E: %s' % e)

def create_v4(self, as_equipment):
"""Create AsnEquipment relationship."""
Expand All @@ -176,3 +208,15 @@ def delete_v4(self):
"""Delete AsnEquipment relationship."""

super(AsnEquipment, self).delete()

def update_v4(self, asn_equipment):
"""Update ASNEquipment """

equipment = get_model('equipamento', 'Equipamento')

self.equipment = equipment().get_by_pk(
asn_equipment.get('equipment')[0])
self.asn = Asn().get_by_pk(asn_equipment.get('asn'))
self.save()

return self
222 changes: 208 additions & 14 deletions networkapi/api_asn/v4/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import FieldError

from networkapi.api_asn.models import Asn
from networkapi.api_asn.models import AsnEquipment
from networkapi.api_asn.v4 import exceptions
from networkapi.api_asn.v4.exceptions import AsnErrorV4
from networkapi.api_asn.v4.exceptions import AsnNotFoundError, AsnError
Expand Down Expand Up @@ -38,7 +39,22 @@ def get_as_by_id(as_id):

try:
as_ = Asn.get_by_pk(id=as_id)
except AsnNotFoundError, e:
except AsnNotFoundError as e:
raise exceptions.AsnDoesNotExistException(str(e))

return as_


def get_as_by_asn(asn_):
"""Return an AS by id.
Args:
asn: ASN
"""

try:
as_ = Asn.get_by_asn(asn_)
except AsnNotFoundError as e:
raise exceptions.AsnDoesNotExistException(str(e))

return as_
Expand All @@ -56,9 +72,9 @@ def get_as_by_ids(autonomous_systems_ids):
try:
as_ = get_as_by_id(as_id).id
as_ids.append(as_)
except exceptions.AsnDoesNotExistException, e:
except exceptions.AsnDoesNotExistException as e:
raise ObjectDoesNotExistException(str(e))
except Exception, e:
except Exception as e:
raise NetworkAPIException(str(e))

as_s = Asn.objects.filter(id__in=as_ids)
Expand All @@ -72,13 +88,13 @@ def update_as(as_):
try:
as_obj = get_as_by_id(as_.get('id'))
as_obj.update_v4(as_)
except AsnErrorV4, e:
except AsnErrorV4 as e:
raise ValidationAPIException(str(e))
except ValidationAPIException, e:
except ValidationAPIException as e:
raise ValidationAPIException(str(e))
except exceptions.AsnDoesNotExistException, e:
except exceptions.AsnDoesNotExistException as e:
raise ObjectDoesNotExistException(str(e))
except Exception, e:
except Exception as e:
raise NetworkAPIException(str(e))

return as_obj
Expand All @@ -90,11 +106,11 @@ def create_as(as_):
try:
as_obj = Asn()
as_obj.create_v4(as_)
except AsnErrorV4, e:
except AsnErrorV4 as e:
raise ValidationAPIException(str(e))
except ValidationAPIException, e:
except ValidationAPIException as e:
raise ValidationAPIException(str(e))
except Exception, e:
except Exception as e:
raise NetworkAPIException(str(e))

return as_obj
Expand All @@ -107,12 +123,190 @@ def delete_as(as_ids):
try:
as_obj = get_as_by_id(as_id)
as_obj.delete_v4()
except exceptions.AsnDoesNotExistException, e:
except exceptions.AsnDoesNotExistException as e:
raise ObjectDoesNotExistException(str(e))
except exceptions.AsnAssociatedToEquipmentError, e:
except exceptions.AsnAssociatedToEquipmentError as e:
raise ValidationAPIException(str(e))
except AsnError, e:
except AsnError as e:
raise NetworkAPIException(str(e))
except Exception, e:
except Exception as e:
raise NetworkAPIException(str(e))


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 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 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_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 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 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 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

28 changes: 28 additions & 0 deletions networkapi/api_asn/v4/specs/asn_equipment_post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"title": "ASNEquipment Post",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [
"asn_equipment"
],
"properties": {
"networks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"asn": {
"type": "integer"
},
"equipment": {
"type": "array"
}
},
"required": [
"asn",
"equipment"
]
}
}
}
}
Loading

0 comments on commit cee0722

Please sign in to comment.