Skip to content

Commit 72e6319

Browse files
author
laura.panzariello
committed
Merge branch 'develop' of https://github.com/globocom/GloboNetworkAPI into develop
2 parents 5f46d31 + 2bddc8d commit 72e6319

2 files changed

Lines changed: 131 additions & 4 deletions

File tree

networkapi/api_interface/facade.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,82 @@ def _generate_dict(interface):
577577
else:
578578
key_dict['CHANNEL_LACP_MODE'] = 'on'
579579

580+
# Normally used in junos plugin:
581+
key_dict['LACP_SYSTEM_ID_MAC'] = __generate_lacp_system_id_mac(interface.channel.nome)
580582
else:
581583
key_dict['BOOL_INTERFACE_IN_CHANNEL'] = 0
582584

585+
# Normally used in junos plugin:
586+
key_dict['CHASSIS_ID_LEAF_NUMBER'] = __generate_chassis_id_leaf_number(interface.equipamento.nome)
587+
588+
log.info("_generate_dict return value (dict values): {}".format(key_dict))
589+
583590
return key_dict
584591

585592

593+
def __generate_chassis_id_leaf_number(equipment_name):
594+
595+
"""
596+
This function build value for CHASSIS_ID_LEAF_NUMBER, and must result 0 or 1 value.
597+
For example: LF-LAB-JUN-01 returns 0 and LF-LAB-JUN-02 returns 1
598+
599+
:param str equipment_name:
600+
ex.: LF-LAB-JUN-01 and LF-LAB-JUN-02 (last character must be 1 or 2)
601+
602+
:returns: 0, 1 or 'N/A' (to indicate information not available)
603+
"""
604+
605+
leaf_id_last_char = equipment_name[-1]
606+
if leaf_id_last_char is '1' or '2':
607+
leaf_id = int(leaf_id_last_char)
608+
result = leaf_id - 1
609+
log.info("__generate_chassis_id_leaf_number - equipment_name:{} result:{}".format(equipment_name, result))
610+
return result
611+
else:
612+
message = "Could not set CHASSIS_ID_LEAF_NUMBER from name {}. This name not ends with '1' or '2'!".format(
613+
equipment_name)
614+
log.warning(message)
615+
return 'N/A'
616+
617+
618+
def __generate_lacp_system_id_mac(channel_name):
619+
620+
"""
621+
This function build value for LACP_SYSTEM_ID_MAC Generate based on channel name.
622+
623+
:param str channel_name:
624+
ex.: '123'
625+
626+
:returns:
627+
ex.: like '00:00:00:00:01:23' or 'N/A' (to indicate information not available)
628+
"""
629+
630+
try:
631+
# Put remaining zeros, ex.: 000000000123
632+
equipment_name_zero_filled = channel_name.zfill(12)
633+
634+
# Run for equipment_name_zero_filled and generate the result with ":"s
635+
mac_address_result = ""
636+
i = 1
637+
for char in equipment_name_zero_filled:
638+
mac_address_result = mac_address_result + char
639+
if i == 2:
640+
mac_address_result = mac_address_result + ":"
641+
i = 1
642+
continue
643+
i = i + 1
644+
645+
# At this point, the result has an extra character ":" (00:00:00:00:01:23:), and the code below will removed it.
646+
mac_address_result = mac_address_result[:-1]
647+
log.info("__generate_mac_address_based_on_name - channel_name:{} result:{}".format(
648+
channel_name, mac_address_result))
649+
return mac_address_result
650+
except Exception as e:
651+
message = "Could not set LACP_SYSTEM_ID_MAC from name {}.".format(channel_name)
652+
log.warning("{} {}".format(message, e))
653+
return 'N/A'
654+
655+
586656
def get_vlan_range(interface):
587657
log.info("get_vlan_range")
588658

networkapi/plugins/Juniper/JUNOS/tests.py

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from networkapi.plugins import exceptions
44
from networkapi.plugins.Juniper.JUNOS.plugin import JUNOS
55
from mock import patch, MagicMock
6-
from jnpr.junos.exception import LockError
6+
from jnpr.junos.exception import RPCError, RpcError, LockError, ConfigLoadError, CommitError
77

88

99
class JunosPluginTest(NetworkApiTestCase):
@@ -108,14 +108,71 @@ def test_exec_command_fail_to_lock(self, time_sleep, mock_config):
108108
test_exec_command_fail_to_lock
109109
"""
110110

111-
time_sleep.return_value = None # mocked to be executed instantly
112-
mock_config.lock.side_effect = LockError('')
111+
# Mocks
112+
time_sleep.return_value = None # to be executed instantly
113+
mock_config.lock.side_effect = LockError('') # Forced exception here
113114

115+
# Create plugin
114116
plugin = JUNOS(equipment_access=self.mock_equipment_access)
115-
plugin.configuration = mock_config
117+
plugin.configuration = mock_config # add mock to plugin instance
118+
119+
# Test
120+
with self.assertRaises(exceptions.APIException):
121+
plugin.exec_command("any command")
122+
plugin.configuration.lock.assert_called_with()
123+
124+
@patch('jnpr.junos.utils.config.Config')
125+
@patch('jnpr.junos.exception.RpcError')
126+
def test_exec_command_fail_to_load(self, mock_rpc_error, mock_config):
127+
128+
# Mocks
129+
config_load_error = ConfigLoadError('')
130+
config_load_error.rpc_error = mock_rpc_error
131+
mock_config.load.side_effect = config_load_error # Forced exception here
132+
133+
# Create plugin
134+
plugin = JUNOS(equipment_access=self.mock_equipment_access)
135+
plugin.configuration = mock_config # add mock to plugin instance
136+
137+
# Test
138+
with self.assertRaises(exceptions.APIException):
139+
plugin.exec_command("any command")
140+
plugin.configuration.load.assert_called_with(
141+
"any command", format='set', ignore_warning=plugin.ignore_warning_list)
142+
143+
@patch('jnpr.junos.utils.config.Config')
144+
def test_exec_command_fail_to_commit_check(self, mock_config):
145+
146+
# Mocks
147+
error = RpcError('')
148+
mock_config.commit_check.side_effect = error # Forced exception here
149+
150+
# Create plugin
151+
plugin = JUNOS(equipment_access=self.mock_equipment_access)
152+
plugin.configuration = mock_config # add mock to plugin instance
153+
154+
# Tests
155+
with self.assertRaises(exceptions.APIException):
156+
plugin.exec_command("any command")
157+
plugin.configuration.commit_check.assert_called_with()
158+
159+
@patch('jnpr.junos.utils.config.Config')
160+
@patch('jnpr.junos.exception.RpcError')
161+
def test_exec_command_fail_to_commit_check(self, mock_rpc_error, mock_config):
162+
163+
# Mocks
164+
error = CommitError('')
165+
error.rpc_error = mock_rpc_error
166+
mock_config.commit.side_effect = error # Forced exception here
167+
168+
# Create plugin
169+
plugin = JUNOS(equipment_access=self.mock_equipment_access)
170+
plugin.configuration = mock_config # add mock to plugin instance
116171

172+
# Tests
117173
with self.assertRaises(exceptions.APIException):
118174
plugin.exec_command("any command")
175+
plugin.configuration.commit.assert_called_with()
119176

120177
@patch('jnpr.junos.Device')
121178
def test_call_close_success(self, mock_device):

0 commit comments

Comments
 (0)