Skip to content

Commit cee0722

Browse files
author
Laura
authored
Merge pull request #293 from globocom/develop
peering refactor + junos plugin
2 parents b3422e7 + e47b43c commit cee0722

25 files changed

Lines changed: 1522 additions & 105 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
# Docker image version
7-
NETAPI_IMAGE_VERSION := 2.1.0
7+
NETAPI_IMAGE_VERSION := 3.1.0
88

99
# Gets git current branch
1010
curr_branch := $(shell git symbolic-ref --short -q HEAD)

networkapi/api_asn/models.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ def get_by_pk(cls, id):
6161
cls.log.error(u'Failure to search the ASN.')
6262
raise exceptions.AsnError(u'Failure to search the ASN.')
6363

64+
def get_by_asn(cls, asn):
65+
"""Get AS by id.
66+
67+
:return: AS.
68+
69+
:raise AsnNotFoundError: As not registered.
70+
:raise AsnError: Failed to search for the As.
71+
:raise OperationalError: Lock wait timeout exceeded
72+
"""
73+
try:
74+
return Asn.objects.get(asn=asn)
75+
except ObjectDoesNotExist:
76+
cls.log.error(u'ASN not found. pk {}'.format(id))
77+
raise exceptions.AsnNotFoundError(id)
78+
except OperationalError:
79+
cls.log.error(u'Lock wait timeout exceeded.')
80+
raise OperationalError()
81+
except Exception:
82+
cls.log.error(u'Failure to search the ASN.')
83+
raise exceptions.AsnError(u'Failure to search the ASN.')
84+
85+
6486
def create_v4(self, as_map):
6587
"""Create ASN."""
6688

@@ -138,7 +160,7 @@ class Meta(BaseModel.Meta):
138160
managed = True
139161

140162
@classmethod
141-
def get_by_pk(cls, id):
163+
def get_by_pk(cls, ids=None, asn=None, equipment=None):
142164
"""Get AsnEquipment by id.
143165
144166
:return: AsnEquipment.
@@ -148,16 +170,26 @@ def get_by_pk(cls, id):
148170
:raise OperationalError: Lock wait timeout exceeded
149171
"""
150172
try:
151-
return AsnEquipment.objects.get(id=id)
173+
logging.info("get asn_equipment by id, asn or equipment")
174+
if ids:
175+
return AsnEquipment.objects.get(id=int(ids))
176+
elif asn:
177+
return AsnEquipment.objects.filter(asn=int(asn))
178+
elif equipment:
179+
return AsnEquipment.objects.filter(equipment__id=int(equipment))
180+
181+
return AsnEquipment.objects.all()
182+
152183
except ObjectDoesNotExist:
153184
cls.log.error(u'AsnEquipment not found. pk {}'.format(id))
154185
raise exceptions.AsnEquipmentNotFoundError(id)
155186
except OperationalError:
156187
cls.log.error(u'Lock wait timeout exceeded.')
157188
raise OperationalError()
158-
except Exception:
159-
cls.log.error(u'Failure to search the AS.')
160-
raise exceptions.AsnEquipmentError(u'Failure to search the AS.')
189+
except Exception as e:
190+
cls.log.error(u'Failure to search the ASNEquipment. E: %s' % e)
191+
raise exceptions.AsnEquipmentError(
192+
u'Failure to search the ASNEquipment. E: %s' % e)
161193

162194
def create_v4(self, as_equipment):
163195
"""Create AsnEquipment relationship."""
@@ -176,3 +208,15 @@ def delete_v4(self):
176208
"""Delete AsnEquipment relationship."""
177209

178210
super(AsnEquipment, self).delete()
211+
212+
def update_v4(self, asn_equipment):
213+
"""Update ASNEquipment """
214+
215+
equipment = get_model('equipamento', 'Equipamento')
216+
217+
self.equipment = equipment().get_by_pk(
218+
asn_equipment.get('equipment')[0])
219+
self.asn = Asn().get_by_pk(asn_equipment.get('asn'))
220+
self.save()
221+
222+
return self

networkapi/api_asn/v4/facade.py

Lines changed: 208 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.core.exceptions import FieldError
55

66
from networkapi.api_asn.models import Asn
7+
from networkapi.api_asn.models import AsnEquipment
78
from networkapi.api_asn.v4 import exceptions
89
from networkapi.api_asn.v4.exceptions import AsnErrorV4
910
from networkapi.api_asn.v4.exceptions import AsnNotFoundError, AsnError
@@ -38,7 +39,22 @@ def get_as_by_id(as_id):
3839

3940
try:
4041
as_ = Asn.get_by_pk(id=as_id)
41-
except AsnNotFoundError, e:
42+
except AsnNotFoundError as e:
43+
raise exceptions.AsnDoesNotExistException(str(e))
44+
45+
return as_
46+
47+
48+
def get_as_by_asn(asn_):
49+
"""Return an AS by id.
50+
51+
Args:
52+
asn: ASN
53+
"""
54+
55+
try:
56+
as_ = Asn.get_by_asn(asn_)
57+
except AsnNotFoundError as e:
4258
raise exceptions.AsnDoesNotExistException(str(e))
4359

4460
return as_
@@ -56,9 +72,9 @@ def get_as_by_ids(autonomous_systems_ids):
5672
try:
5773
as_ = get_as_by_id(as_id).id
5874
as_ids.append(as_)
59-
except exceptions.AsnDoesNotExistException, e:
75+
except exceptions.AsnDoesNotExistException as e:
6076
raise ObjectDoesNotExistException(str(e))
61-
except Exception, e:
77+
except Exception as e:
6278
raise NetworkAPIException(str(e))
6379

6480
as_s = Asn.objects.filter(id__in=as_ids)
@@ -72,13 +88,13 @@ def update_as(as_):
7288
try:
7389
as_obj = get_as_by_id(as_.get('id'))
7490
as_obj.update_v4(as_)
75-
except AsnErrorV4, e:
91+
except AsnErrorV4 as e:
7692
raise ValidationAPIException(str(e))
77-
except ValidationAPIException, e:
93+
except ValidationAPIException as e:
7894
raise ValidationAPIException(str(e))
79-
except exceptions.AsnDoesNotExistException, e:
95+
except exceptions.AsnDoesNotExistException as e:
8096
raise ObjectDoesNotExistException(str(e))
81-
except Exception, e:
97+
except Exception as e:
8298
raise NetworkAPIException(str(e))
8399

84100
return as_obj
@@ -90,11 +106,11 @@ def create_as(as_):
90106
try:
91107
as_obj = Asn()
92108
as_obj.create_v4(as_)
93-
except AsnErrorV4, e:
109+
except AsnErrorV4 as e:
94110
raise ValidationAPIException(str(e))
95-
except ValidationAPIException, e:
111+
except ValidationAPIException as e:
96112
raise ValidationAPIException(str(e))
97-
except Exception, e:
113+
except Exception as e:
98114
raise NetworkAPIException(str(e))
99115

100116
return as_obj
@@ -107,12 +123,190 @@ def delete_as(as_ids):
107123
try:
108124
as_obj = get_as_by_id(as_id)
109125
as_obj.delete_v4()
110-
except exceptions.AsnDoesNotExistException, e:
126+
except exceptions.AsnDoesNotExistException as e:
111127
raise ObjectDoesNotExistException(str(e))
112-
except exceptions.AsnAssociatedToEquipmentError, e:
128+
except exceptions.AsnAssociatedToEquipmentError as e:
113129
raise ValidationAPIException(str(e))
114-
except AsnError, e:
130+
except AsnError as e:
115131
raise NetworkAPIException(str(e))
116-
except Exception, e:
132+
except Exception as e:
117133
raise NetworkAPIException(str(e))
118134

135+
136+
def get_as_equipment_by_search(search=dict()):
137+
"""Return a list of ASEquipment's by dict."""
138+
139+
try:
140+
as_equipment = AsnEquipment.get_by_pk()
141+
as_map = build_query_to_datatable_v3(as_equipment, search)
142+
except FieldError as e:
143+
raise ValidationAPIException(str(e))
144+
except Exception as e:
145+
raise NetworkAPIException(str(e))
146+
else:
147+
return as_map
148+
149+
150+
def get_as_equipment_by_id(as_equipment_id=None):
151+
"""Return an ASEquipment by id.
152+
153+
Args:
154+
as_equipment_id: Id of ASEquipment
155+
156+
"""
157+
158+
try:
159+
as_equipment_list = list()
160+
for asn in as_equipment_id:
161+
as_equipment = AsnEquipment.get_by_pk(ids=asn)
162+
as_equipment_list.append(as_equipment)
163+
except AsnNotFoundError as e:
164+
raise exceptions.AsnDoesNotExistException(str(e))
165+
166+
return as_equipment_list
167+
168+
169+
def get_as_equipment_by_asn(asn_id=None):
170+
"""Return an ASEquipment by asn id.
171+
172+
Args:
173+
asn_id: Id of ASN
174+
175+
"""
176+
177+
try:
178+
as_equipment = list()
179+
for asn in asn_id:
180+
as_equipment += AsnEquipment.get_by_pk(asn=asn)
181+
182+
except AsnNotFoundError as e:
183+
raise exceptions.AsnDoesNotExistException(str(e))
184+
185+
return as_equipment
186+
187+
188+
def get_as_equipment_by_equip(equipment_id=None):
189+
"""Return an ASEquipment by equipment id.
190+
191+
Args:
192+
equipment_id: Id of Equipment
193+
194+
"""
195+
196+
try:
197+
as_equipment = list()
198+
for equip in equipment_id:
199+
as_equipment += AsnEquipment.get_by_pk(equipment=equip)
200+
201+
except AsnNotFoundError as e:
202+
raise exceptions.AsnDoesNotExistException(str(e))
203+
204+
return as_equipment
205+
206+
207+
def create_asn_equipment(asn_equipment):
208+
"""Create ASNEquipment."""
209+
210+
try:
211+
asn_equipment_list = list()
212+
213+
for equipment in asn_equipment.get("equipment"):
214+
obj = dict()
215+
obj["equipment"] = equipment
216+
obj["asn"] = asn_equipment.get("asn")
217+
as_obj = AsnEquipment()
218+
as_obj.create_v4(obj)
219+
asn_equipment_list.append({'id': as_obj.id})
220+
221+
except AsnErrorV4 as e:
222+
raise ValidationAPIException(str(e))
223+
except ValidationAPIException as e:
224+
raise ValidationAPIException(str(e))
225+
except Exception as e:
226+
raise NetworkAPIException(str(e))
227+
228+
return asn_equipment_list
229+
230+
231+
def delete_asn_equipment(as_ids):
232+
"""Delete ASNEquipment."""
233+
234+
try:
235+
asn_equipment = AsnEquipment()
236+
obj = asn_equipment.get_by_pk(ids=as_ids)
237+
obj.delete_v4()
238+
except exceptions.AsnDoesNotExistException as e:
239+
raise ObjectDoesNotExistException(str(e))
240+
except exceptions.AsnAssociatedToEquipmentError as e:
241+
raise ValidationAPIException(str(e))
242+
except AsnError as e:
243+
raise NetworkAPIException(str(e))
244+
except Exception as e:
245+
raise NetworkAPIException(str(e))
246+
247+
248+
def delete_asn_equipment_by_asn(asn_id):
249+
"""Delete ASNEquipment."""
250+
251+
try:
252+
asn_obj = AsnEquipment()
253+
asn_equipment = asn_obj.get_by_pk(asn=asn_id)
254+
for obj in asn_equipment:
255+
obj.delete_v4()
256+
257+
except exceptions.AsnDoesNotExistException as e:
258+
raise ObjectDoesNotExistException(str(e))
259+
except exceptions.AsnAssociatedToEquipmentError as e:
260+
raise ValidationAPIException(str(e))
261+
except AsnError as e:
262+
raise NetworkAPIException(str(e))
263+
except Exception as e:
264+
raise NetworkAPIException(str(e))
265+
266+
267+
def update_asn_equipment(as_):
268+
"""Update ASNEquipment."""
269+
270+
try:
271+
as_obj = AsnEquipment()
272+
if not as_.get('id'):
273+
raise Exception("The object do not have the id.")
274+
asn_equipment = as_obj.get_by_pk(ids=as_.get('id'))
275+
asn_equipment.update_v4(as_)
276+
except AsnErrorV4 as e:
277+
raise ValidationAPIException(str(e))
278+
except ValidationAPIException as e:
279+
raise ValidationAPIException(str(e))
280+
except exceptions.AsnDoesNotExistException as e:
281+
raise ObjectDoesNotExistException(str(e))
282+
except Exception as e:
283+
raise NetworkAPIException(str(e))
284+
285+
return asn_equipment
286+
287+
288+
def update_asn_equipment_by_asn(asn_id, as_):
289+
"""
290+
Update ASNEquipment.
291+
Return new asn_equipments new ids
292+
"""
293+
294+
try:
295+
as_obj = AsnEquipment()
296+
asn_equipment = as_obj.get_by_pk(asn=asn_id)
297+
for obj in asn_equipment:
298+
obj.delete_v4()
299+
300+
asn_equipment_obj = create_asn_equipment(as_)
301+
302+
except AsnErrorV4 as e:
303+
raise ValidationAPIException(str(e))
304+
except ValidationAPIException as e:
305+
raise ValidationAPIException(str(e))
306+
except exceptions.AsnDoesNotExistException as e:
307+
raise ObjectDoesNotExistException(str(e))
308+
except Exception as e:
309+
raise NetworkAPIException(str(e))
310+
311+
return asn_equipment_obj
312+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"title": "ASNEquipment Post",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"type": "object",
5+
"required": [
6+
"asn_equipment"
7+
],
8+
"properties": {
9+
"networks": {
10+
"type": "array",
11+
"items": {
12+
"type": "object",
13+
"properties": {
14+
"asn": {
15+
"type": "integer"
16+
},
17+
"equipment": {
18+
"type": "array"
19+
}
20+
},
21+
"required": [
22+
"asn",
23+
"equipment"
24+
]
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)