Skip to content

Commit

Permalink
Merge pull request #321 from globocom/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Laura authored Jan 18, 2021
2 parents 8720164 + 99a6959 commit 140a7bf
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 83 deletions.
2 changes: 1 addition & 1 deletion networkapi/api_channel/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def create(self, data):
for interface in interfaces:

iface = Interface.objects.get(id=interface)
type_obj = TipoInterface.objects.get(tipo=int_type)
type_obj = TipoInterface.objects.get(tipo=int_type.lower())

if iface.channel:
raise InterfaceError(
Expand Down
6 changes: 2 additions & 4 deletions networkapi/api_interface/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def generate_and_deploy_interface_config_sync(user, id_interface):
file_to_deploy = _generate_config_file(interfaces)

# TODO Deploy config file
lockvar = LOCK_INTERFACE_DEPLOY_CONFIG % (interface.equipamento.id)
lockvar = LOCK_INTERFACE_DEPLOY_CONFIG % interface.equipamento.id
status_deploy = deploy_config_in_equipment_synchronous(
file_to_deploy, interface.equipamento, lockvar)

Expand Down Expand Up @@ -554,9 +554,7 @@ def _generate_dict(interface):
key_dict['NATIVE_VLAN'] = interface.vlan_nativa
key_dict['USE_MCLAG'] = 1
key_dict['INTERFACE_NAME'] = interface.interface
key_dict['INTERFACE_DESCRIPTION'] = str(
interface.ligacao_front.equipamento.nome
) + ' ' + str(interface.ligacao_front.interface)
key_dict['INTERFACE_DESCRIPTION'] = interface.descricao
key_dict['INTERFACE_TYPE'] = interface.tipo.tipo
if key_dict['INTERFACE_TYPE'] in 'trunk':
vlan_range_results = get_vlan_range(interface)
Expand Down
8 changes: 3 additions & 5 deletions networkapi/api_ip/tests/sanity/ipv4/sync/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def test_try_create_ip_associating_to_equipment(self):
response.data['ips'][0]['ip_formated'])

def test_try_create_ip_in_full_network(self):
"""Tests if NAPI deny an IPv4 manually creation in a full network."""
""" Tests if NAPI deny an IPv4 manually creation in a full network.
Refactor to allow create the ip."""

name_file = 'api_ip/tests/sanity/ipv4/json/post/ipv4_10_0_4_1_net_8.json'
response = self.client.post(
Expand All @@ -111,10 +112,7 @@ def test_try_create_ip_in_full_network(self):
content_type='application/json',
HTTP_AUTHORIZATION=self.get_http_authorization('test'))

self.compare_status(400, response.status_code)
self.compare_values(
'Ip 10.0.4.1 not available for network 8.',
response.data['detail'])
self.compare_status(201, response.status_code)

def test_try_create_out_of_range_ip_in_network(self):
"""Tests if NAPI deny out of range network IPv4 manually creation."""
Expand Down
8 changes: 3 additions & 5 deletions networkapi/api_ip/v4/tests/sanity/ipv4/sync/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def test_try_create_ip_associating_to_equipment(self):
response.data['ips'][0]['ip_formated'])

def test_try_create_ip_in_full_network(self):
"""V4 Tests if NAPI deny an IPv4 manually creation in a full network."""
""" V4 Tests if NAPI deny an IPv4 manually creation in a full network.
Refactor to allow create the ip."""

name_file = 'api_ip/tests/sanity/ipv4/json/post/ipv4_10_0_4_1_net_8.json'
response = self.client.post(
Expand All @@ -111,10 +112,7 @@ def test_try_create_ip_in_full_network(self):
content_type='application/json',
HTTP_AUTHORIZATION=self.get_http_authorization('test'))

self.compare_status(400, response.status_code)
self.compare_values(
'Ip 10.0.4.1 not available for network 8.',
response.data['detail'])
self.compare_status(201, response.status_code)

def test_try_create_out_of_range_ip_in_network(self):
"""V4 Tests if NAPI deny out of range network IPv4 manually creation."""
Expand Down
13 changes: 13 additions & 0 deletions networkapi/interface/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,25 @@ def connecting_interfaces(self, interfaces):

if link_a in "front":
interface_a.ligacao_front = interface_b
interface_a.descricao = str(interface_a.ligacao_front.equipamento.nome
) + ' ' + str(interface_a.ligacao_front.interface)

elif link_a in "back":
interface_a.ligacao_back = interface_b
interface_a.descricao = str(interface_a.ligacao_back.equipamento.nome
) + ' ' + str(interface_a.ligacao_back.interface)
interface_a.save()

if link_b in "front":
interface_b.ligacao_front = interface_a
interface_b.descricao = str(interface_b.ligacao_front.equipamento.nome
) + ' ' + str(interface_b.ligacao_front.interface)

elif link_b in "back":
interface_b.ligacao_back = interface_a
interface_b.descricao = str(interface_b.ligacao_back.equipamento.nome
) + ' ' + str(interface_b.ligacao_back.interface)

interface_b.save()

def disconnecting_interfaces(self, interfaces):
Expand All @@ -877,6 +888,7 @@ def disconnecting_interfaces(self, interfaces):
if interface_a.ligacao_back:
if interface_a.ligacao_back.id == interface_b.id:
interface_a.ligacao_back = None
interface_a.descricao = None
interface_a.save()

if interface_b.ligacao_front:
Expand All @@ -885,6 +897,7 @@ def disconnecting_interfaces(self, interfaces):
if interface_b.ligacao_back:
if interface_b.ligacao_back.id == interface_a.id:
interface_b.ligacao_back = None
interface_b.descricao = None
interface_b.save()

def remove_connection(self, front=None, back=None):
Expand Down
78 changes: 47 additions & 31 deletions networkapi/ip/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ def save_ipv4(self, equipment_id, user, net):
equipamentoambiente = get_model('equipamento', 'EquipamentoAmbiente')
equipamento = get_model('equipamento', 'Equipamento')
filterequiptype = get_model('filterequiptype', 'FilterEquipType')

try:

already_ip = False
Expand All @@ -1491,42 +1492,45 @@ def save_ipv4(self, equipment_id, user, net):
net4 = IPv4Network(
'%d.%d.%d.%d/%d' % (net.oct1, net.oct2, net.oct3, net.oct4, net.block))

# Find all ips ralated to network
# Find all ips related to network
ips = Ip.objects.filter(networkipv4__id=net.id)

ip4_object = IPv4Address(
'%s.%s.%s.%s' % (self.oct1, self.oct2, self.oct3, self.oct4))

# Cast all to API class
ipsv4 = set(
[IPv4Address('%d.%d.%d.%d' % (ip.oct1, ip.oct2, ip.oct3, ip.oct4)) for ip in ips])
[IPv4Address('%d.%d.%d.%d' % (
ip.oct1, ip.oct2, ip.oct3, ip.oct4)) for ip in ips])

flag = False

if ip4_object not in ipsv4:

if ip4_object in net4:
# allow to allocate the first and last ip
flag = True

# Get configuration
# conf = Configuration.get()

first_ip_network = int(net4.network)
bcast_ip_network = int(net4.broadcast)

ipv4_network = int(ip4_object)

if ipv4_network >= (first_ip_network) and ipv4_network < (bcast_ip_network):
flag = True
# first_ip_network = int(net4.network)
# bcast_ip_network = int(net4.broadcast)
#
# ipv4_network = int(ip4_object)
#
# if bcast_ip_network > ipv4_network >= first_ip_network:
# flag = True

else:

ip_aux = self.get_by_octs_and_net(
self.oct1, self.oct2, self.oct3, self.oct4, net.id)
try:
IpEquipamento.get_by_ip(ip_aux.id)
raise IpEquipmentAlreadyAssociation(None, u'Ip %s.%s.%s.%s already has association with an Equipament. Try using the association screen for this Ip.' % (
raise IpEquipmentAlreadyAssociation(
None,
u'Ip %s.%s.%s.%s already has association with an Equipament. '
u'Try using the association screen for this Ip.' % (
self.oct1, self.oct2, self.oct3, self.oct4))
except IpEquipmentNotFoundError, e:
except IpEquipmentNotFoundError:
flag = True
already_ip = True

Expand All @@ -1546,8 +1550,8 @@ def save_ipv4(self, equipment_id, user, net):

ip_equipment.equipamento = equipment

# # Filter case 2 - Adding new IpEquip for a equip that already have ip in other network with the same range ##

# Filter case 2 - Adding new IpEquip for a equip that already
# have ip in other network with the same range
# Get all IpEquipamento related to this equipment
ip_equips = IpEquipamento.objects.filter(
equipamento=equipment_id)
Expand All @@ -1561,22 +1565,30 @@ def save_ipv4(self, equipment_id, user, net):
ip_test.networkipv4 != self.networkipv4:

# Filter testing
if ip_test.networkipv4.vlan.ambiente.filter is None or self.networkipv4.vlan.ambiente.filter is None:
if ip_test.networkipv4.vlan.ambiente.filter is None \
or self.networkipv4.vlan.ambiente.filter is None:
raise IpRangeAlreadyAssociation(
None, u'Equipment is already associated with another ip with the same ip range.')
None, u'Equipment is already associated with another '
u'ip with the same ip range.')
else:
# Test both environment's filters
tp_equip_list_one = list()
for fet in filterequiptype.objects.filter(filter=self.networkipv4.vlan.ambiente.filter.id):
for fet in filterequiptype.objects.filter(
filter=self.networkipv4.vlan.ambiente.filter.id):
tp_equip_list_one.append(fet.equiptype)

tp_equip_list_two = list()
for fet in filterequiptype.objects.filter(filter=ip_test.networkipv4.vlan.ambiente.filter.id):
for fet in filterequiptype.objects.filter(
filter=ip_test.networkipv4.vlan.ambiente.filter.id):
tp_equip_list_two.append(fet.equiptype)

if equipment.tipo_equipamento not in tp_equip_list_one or equipment.tipo_equipamento not in tp_equip_list_two:
if equipment.tipo_equipamento not \
in tp_equip_list_one \
or equipment.tipo_equipamento not \
in tp_equip_list_two:
raise IpRangeAlreadyAssociation(
None, u'Equipment is already associated with another ip with the same ip range.')
None, u'Equipment is already associated with '
u'another ip with the same ip range.')

# # Filter case 2 - end ##

Expand All @@ -1593,7 +1605,8 @@ def save_ipv4(self, equipment_id, user, net):
pass

else:
raise IpNotAvailableError(None, u'Ip %s.%s.%s.%s not available for network %s.' % (
raise IpNotAvailableError(None, u'Ip %s.%s.%s.%s not available '
u'for network %s.' % (
self.oct1, self.oct2, self.oct3, self.oct4, net.id))

except IpRangeAlreadyAssociation, e:
Expand All @@ -1604,7 +1617,8 @@ def save_ipv4(self, equipment_id, user, net):
raise InvalidValueError(
None, 'ip', u'%s.%s.%s.%s' % (self.oct1, self.oct2, self.oct3, self.oct4))
except IpNotAvailableError, e:
raise IpNotAvailableError(None, u'Ip %s.%s.%s.%s not available for network %s.' % (
raise IpNotAvailableError(None, u'Ip %s.%s.%s.%s not available for '
u'network %s.' % (
self.oct1, self.oct2, self.oct3, self.oct4, net.id))
except (IpError, EquipamentoError), e:
self.log.error(
Expand Down Expand Up @@ -1976,15 +1990,17 @@ def create_v3(self, ip_map, locks_used=[]):
if ip4_object not in ipsv4:

if ip4_object in net4:
# allow to allocate the first and last ip
flag = True

first_ip_network = int(net4.network)
bcast_ip_network = int(net4.broadcast)

ipv4_network = int(ip4_object)

# first_ip_network = int(net4.network)
# bcast_ip_network = int(net4.broadcast)
#
# ipv4_network = int(ip4_object)
#
# First and last ip are reserved in network
if first_ip_network <= ipv4_network < bcast_ip_network:
flag = True
# if first_ip_network <= ipv4_network < bcast_ip_network:
# flag = True

if flag is False:
raise IpNotAvailableError(
Expand Down
Loading

0 comments on commit 140a7bf

Please sign in to comment.