Browse Source

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
Mark Goddard 8 months ago
parent
commit
2485e41b7c

+ 35
- 8
networking_generic_switch/devices/netmiko_devices/__init__.py View File

@@ -20,6 +20,7 @@ import netmiko
20 20
 from oslo_config import cfg
21 21
 from oslo_log import log as logging
22 22
 import paramiko
23
+import six
23 24
 import tenacity
24 25
 from tooz import coordination
25 26
 
@@ -32,6 +33,32 @@ LOG = logging.getLogger(__name__)
32 33
 CONF = cfg.CONF
33 34
 
34 35
 
36
+def 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
+
35 62
 class NetmikoSwitch(devices.GenericSwitchDevice):
36 63
 
37 64
     ADD_NETWORK = None
@@ -158,6 +185,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
158 185
         LOG.debug(output)
159 186
         return output
160 187
 
188
+    @check_output('add network')
161 189
     def add_network(self, segmentation_id, network_id):
162 190
         # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars
163 191
         # is the max length of vlan name.
@@ -169,9 +197,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
169 197
             cmds += self._format_commands(self.ADD_NETWORK_TO_TRUNK,
170 198
                                           port=port,
171 199
                                           segmentation_id=segmentation_id)
172
-        output = self.send_commands_to_device(cmds)
173
-        self.check_output(output, 'add network')
200
+        return self.send_commands_to_device(cmds)
174 201
 
202
+    @check_output('delete network')
175 203
     def del_network(self, segmentation_id, network_id):
176 204
         # NOTE(zhenguo): Remove dashes from uuid as on most devices 32 chars
177 205
         # is the max length of vlan name.
@@ -184,9 +212,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
184 212
         cmds += self._format_commands(self.DELETE_NETWORK,
185 213
                                       segmentation_id=segmentation_id,
186 214
                                       network_id=network_id)
187
-        output = self.send_commands_to_device(cmds)
188
-        self.check_output(output, 'delete network')
215
+        return self.send_commands_to_device(cmds)
189 216
 
217
+    @check_output('plug port')
190 218
     def plug_port_to_network(self, port, segmentation_id):
191 219
         cmds = []
192 220
         ngs_port_default_vlan = self._get_port_default_vlan()
@@ -199,9 +227,9 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
199 227
             self.PLUG_PORT_TO_NETWORK,
200 228
             port=port,
201 229
             segmentation_id=segmentation_id)
202
-        output = self.send_commands_to_device(cmds)
203
-        self.check_output(output, 'plug port')
230
+        return self.send_commands_to_device(cmds)
204 231
 
232
+    @check_output('unplug port')
205 233
     def delete_port(self, port, segmentation_id):
206 234
         cmds = self._format_commands(self.DELETE_PORT,
207 235
                                      port=port,
@@ -216,8 +244,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
216 244
                 self.PLUG_PORT_TO_NETWORK,
217 245
                 port=port,
218 246
                 segmentation_id=ngs_port_default_vlan)
219
-        output = self.send_commands_to_device(cmds)
220
-        self.check_output(output, 'unplug port')
247
+        return self.send_commands_to_device(cmds)
221 248
 
222 249
     def send_config_set(self, net_connect, cmd_set):
223 250
         """Send a set of configuration lines to the device.

+ 3
- 2
networking_generic_switch/devices/netmiko_devices/brocade.py View File

@@ -74,7 +74,8 @@ class BrocadeFastIron(netmiko_devices.NetmikoSwitch):
74 74
         )
75 75
         self.delete_port(port, wrong_vlan)
76 76
 
77
+    @netmiko_devices.check_output('plug port')
77 78
     def plug_port_to_network(self, port, segmentation_id):
78 79
         self.clean_port_vlan_if_necessary(port)
79
-        super(BrocadeFastIron, self).plug_port_to_network(port,
80
-                                                          segmentation_id)
80
+        return super(BrocadeFastIron, self).plug_port_to_network(
81
+            port, segmentation_id)

Loading…
Cancel
Save