summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Goddard <mark@stackhpc.com>2018-08-15 12:10:24 +0100
committerMark Goddard <mark@stackhpc.com>2018-08-21 18:23:08 +0100
commit2485e41b7c7be8ae8246c5034f9de9dad034cc3b (patch)
tree3aba95d8216c22dbe69316259cf704999e1d2039
parent4d3a08b1cce8c92d0e581a0cbfeb73cc07bfb97c (diff)
Convert error detection to use a decorator
The check_output decorator calls check_output with whatever output is returned by the decorated method. Change-Id: Iac17285680013aad44b10e3b65bfcbed4e3b6f70 Story: 2003148 Task: 24642
Notes
Notes (review): Code-Review+2: Dmitry Tantsur <divius.inside@gmail.com> Code-Review+2: Vladyslav Drok <vdrok@mirantis.com> Workflow+1: Vladyslav Drok <vdrok@mirantis.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Wed, 29 Aug 2018 18:09:00 +0000 Reviewed-on: https://review.openstack.org/592009 Project: openstack/networking-generic-switch Branch: refs/heads/master
-rw-r--r--networking_generic_switch/devices/netmiko_devices/__init__.py43
-rw-r--r--networking_generic_switch/devices/netmiko_devices/brocade.py5
2 files changed, 38 insertions, 10 deletions
diff --git a/networking_generic_switch/devices/netmiko_devices/__init__.py b/networking_generic_switch/devices/netmiko_devices/__init__.py
index 6279878..5e4900d 100644
--- a/networking_generic_switch/devices/netmiko_devices/__init__.py
+++ b/networking_generic_switch/devices/netmiko_devices/__init__.py
@@ -20,6 +20,7 @@ import netmiko
20from oslo_config import cfg 20from oslo_config import cfg
21from oslo_log import log as logging 21from oslo_log import log as logging
22import paramiko 22import paramiko
23import six
23import tenacity 24import tenacity
24from tooz import coordination 25from tooz import coordination
25 26
@@ -32,6 +33,32 @@ LOG = logging.getLogger(__name__)
32CONF = cfg.CONF 33CONF = cfg.CONF
33 34
34 35
36def check_output(operation):
37 """Returns a decorator that checks the output of an operation.
38
39 :param operation: Operation being attempted. One of 'add network',
40 'delete network', 'plug port', 'unplug port'.
41 """
42 def decorator(func):
43 """The real decorator."""
44
45 @six.wraps(func)
46 def wrapper(self, *args, **kwargs):
47 """Wrapper that checks the output of an operation.
48
49 :returns: The return value of the wrapped method.
50 :raises: GenericSwitchNetmikoConfigError if the driver detects that
51 an error has occurred.
52 """
53 output = func(self, *args, **kwargs)
54 self.check_output(output, operation)
55 return output
56
57 return wrapper
58
59 return decorator
60
61
35class NetmikoSwitch(devices.GenericSwitchDevice): 62class NetmikoSwitch(devices.GenericSwitchDevice):
36 63
37 ADD_NETWORK = None 64 ADD_NETWORK = None
@@ -158,6 +185,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
158 LOG.debug(output) 185 LOG.debug(output)
159 return output 186 return output
160 187
188 @check_output('add network')
161 def add_network(self, segmentation_id, network_id): 189 def add_network(self, segmentation_id, network_id):
162 # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars 190 # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars
163 # is the max length of vlan name. 191 # is the max length of vlan name.
@@ -169,9 +197,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
169 cmds += self._format_commands(self.ADD_NETWORK_TO_TRUNK, 197 cmds += self._format_commands(self.ADD_NETWORK_TO_TRUNK,
170 port=port, 198 port=port,
171 segmentation_id=segmentation_id) 199 segmentation_id=segmentation_id)
172 output = self.send_commands_to_device(cmds) 200 return self.send_commands_to_device(cmds)
173 self.check_output(output, 'add network')
174 201
202 @check_output('delete network')
175 def del_network(self, segmentation_id, network_id): 203 def del_network(self, segmentation_id, network_id):
176 # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars 204 # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars
177 # is the max length of vlan name. 205 # is the max length of vlan name.
@@ -184,9 +212,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
184 cmds += self._format_commands(self.DELETE_NETWORK, 212 cmds += self._format_commands(self.DELETE_NETWORK,
185 segmentation_id=segmentation_id, 213 segmentation_id=segmentation_id,
186 network_id=network_id) 214 network_id=network_id)
187 output = self.send_commands_to_device(cmds) 215 return self.send_commands_to_device(cmds)
188 self.check_output(output, 'delete network')
189 216
217 @check_output('plug port')
190 def plug_port_to_network(self, port, segmentation_id): 218 def plug_port_to_network(self, port, segmentation_id):
191 cmds = [] 219 cmds = []
192 ngs_port_default_vlan = self._get_port_default_vlan() 220 ngs_port_default_vlan = self._get_port_default_vlan()
@@ -199,9 +227,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
199 self.PLUG_PORT_TO_NETWORK, 227 self.PLUG_PORT_TO_NETWORK,
200 port=port, 228 port=port,
201 segmentation_id=segmentation_id) 229 segmentation_id=segmentation_id)
202 output = self.send_commands_to_device(cmds) 230 return self.send_commands_to_device(cmds)
203 self.check_output(output, 'plug port')
204 231
232 @check_output('unplug port')
205 def delete_port(self, port, segmentation_id): 233 def delete_port(self, port, segmentation_id):
206 cmds = self._format_commands(self.DELETE_PORT, 234 cmds = self._format_commands(self.DELETE_PORT,
207 port=port, 235 port=port,
@@ -216,8 +244,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
216 self.PLUG_PORT_TO_NETWORK, 244 self.PLUG_PORT_TO_NETWORK,
217 port=port, 245 port=port,
218 segmentation_id=ngs_port_default_vlan) 246 segmentation_id=ngs_port_default_vlan)
219 output = self.send_commands_to_device(cmds) 247 return self.send_commands_to_device(cmds)
220 self.check_output(output, 'unplug port')
221 248
222 def send_config_set(self, net_connect, cmd_set): 249 def send_config_set(self, net_connect, cmd_set):
223 """Send a set of configuration lines to the device. 250 """Send a set of configuration lines to the device.
diff --git a/networking_generic_switch/devices/netmiko_devices/brocade.py b/networking_generic_switch/devices/netmiko_devices/brocade.py
index 24baf0c..b25a780 100644
--- a/networking_generic_switch/devices/netmiko_devices/brocade.py
+++ b/networking_generic_switch/devices/netmiko_devices/brocade.py
@@ -74,7 +74,8 @@ class BrocadeFastIron(netmiko_devices.NetmikoSwitch):
74 ) 74 )
75 self.delete_port(port, wrong_vlan) 75 self.delete_port(port, wrong_vlan)
76 76
77 @netmiko_devices.check_output('plug port')
77 def plug_port_to_network(self, port, segmentation_id): 78 def plug_port_to_network(self, port, segmentation_id):
78 self.clean_port_vlan_if_necessary(port) 79 self.clean_port_vlan_if_necessary(port)
79 super(BrocadeFastIron, self).plug_port_to_network(port, 80 return super(BrocadeFastIron, self).plug_port_to_network(
80 segmentation_id) 81 port, segmentation_id)