~salvatore-orlando/neutron/quantum-api

« back to all changes in this revision

Viewing changes to quantum/plugins/SamplePlugin.py

  • Committer: Salvatore Orlando
  • Date: 2011-06-07 17:25:16 UTC
  • Revision ID: salvatore.orlando@eu.citrix.com-20110607172516-230v5aj1lhejdcxb
Fixing pep8 errors
removing excess debug lines

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
from quantum.common import exceptions as exc
19
19
 
 
20
 
20
21
class QuantumEchoPlugin(object):
21
22
 
22
23
    """
23
24
    QuantumEchoPlugin is a demo plugin that doesn't
24
25
    do anything but demonstrated the concept of a
25
26
    concrete Quantum Plugin. Any call to this plugin
26
 
    will result in just a "print" to std. out with 
 
27
    will result in just a "print" to std. out with
27
28
    the name of the method that was called.
28
29
    """
29
 
    
 
30
 
30
31
    def get_all_networks(self, tenant_id):
31
32
        """
32
33
        Returns a dictionary containing all
33
34
        <network_uuid, network_name> for
34
 
        the specified tenant. 
 
35
        the specified tenant.
35
36
        """
36
37
        print("get_all_networks() called\n")
37
 
    
38
 
    
 
38
 
39
39
    def create_network(self, tenant_id, net_name):
40
40
        """
41
41
        Creates a new Virtual Network, and assigns it
42
42
        a symbolic name.
43
43
        """
44
44
        print("create_network() called\n")
45
 
    
46
 
    
 
45
 
47
46
    def delete_network(self, tenant_id, net_id):
48
47
        """
49
48
        Deletes the network with the specified network identifier
51
50
        """
52
51
        print("delete_network() called\n")
53
52
 
54
 
    
55
53
    def get_network_details(self, tenant_id, net_id):
56
54
        """
57
55
        Deletes the Virtual Network belonging to a the
58
56
        spec
59
57
        """
60
58
        print("get_network_details() called\n")
61
 
    
62
 
    
 
59
 
63
60
    def rename_network(self, tenant_id, net_id, new_name):
64
61
        """
65
62
        Updates the symbolic name belonging to a particular
66
63
        Virtual Network.
67
64
        """
68
65
        print("rename_network() called\n")
69
 
    
70
 
    
 
66
 
71
67
    def get_all_ports(self, tenant_id, net_id):
72
68
        """
73
69
        Retrieves all port identifiers belonging to the
74
70
        specified Virtual Network.
75
71
        """
76
72
        print("get_all_ports() called\n")
77
 
    
78
 
    
 
73
 
79
74
    def create_port(self, tenant_id, net_id):
80
75
        """
81
76
        Creates a port on the specified Virtual Network.
82
77
        """
83
78
        print("create_port() called\n")
84
 
    
85
 
    
 
79
 
86
80
    def delete_port(self, tenant_id, net_id, port_id):
87
81
        """
88
82
        Deletes a port on a specified Virtual Network,
91
85
        is deleted.
92
86
        """
93
87
        print("delete_port() called\n")
94
 
    
95
 
    
 
88
 
96
89
    def get_port_details(self, tenant_id, net_id, port_id):
97
90
        """
98
91
        This method allows the user to retrieve a remote interface
99
92
        that is attached to this particular port.
100
93
        """
101
94
        print("get_port_details() called\n")
102
 
    
103
 
    
 
95
 
104
96
    def plug_interface(self, tenant_id, net_id, port_id, remote_interface_id):
105
97
        """
106
98
        Attaches a remote interface to the specified port on the
107
99
        specified Virtual Network.
108
100
        """
109
101
        print("plug_interface() called\n")
110
 
    
111
 
    
 
102
 
112
103
    def unplug_interface(self, tenant_id, net_id, port_id):
113
104
        """
114
105
        Detaches a remote interface from the specified port on the
115
106
        specified Virtual Network.
116
107
        """
117
108
        print("unplug_interface() called\n")
118
 
    
119
 
    
 
109
 
120
110
    def get_interface_details(self, tenant_id, net_id, port_id):
121
111
        """
122
112
        Retrieves the remote interface that is attached at this
123
113
        particular port.
124
114
        """
125
115
        print("get_interface_details() called\n")
126
 
    
127
 
    
 
116
 
128
117
    def get_all_attached_interfaces(self, tenant_id, net_id):
129
118
        """
130
119
        Retrieves all remote interfaces that are attached to
132
121
        """
133
122
        print("get_all_attached_interfaces() called\n")
134
123
 
 
124
 
135
125
class DummyDataPlugin(object):
136
126
 
137
127
    """
139
129
    hard-coded data structures to aid in quantum
140
130
    client/cli development
141
131
    """
142
 
    
 
132
 
143
133
    def get_all_networks(self, tenant_id):
144
134
        """
145
135
        Returns a dictionary containing all
146
136
        <network_uuid, network_name> for
147
 
        the specified tenant. 
 
137
        the specified tenant.
148
138
        """
149
 
        nets = {"001": "lNet1", "002": "lNet2" , "003": "lNet3"}
 
139
        nets = {"001": "lNet1", "002": "lNet2", "003": "lNet3"}
150
140
        print("get_all_networks() called\n")
151
141
        return nets
152
 
    
153
 
    
 
142
 
154
143
    def create_network(self, tenant_id, net_name):
155
144
        """
156
145
        Creates a new Virtual Network, and assigns it
159
148
        print("create_network() called\n")
160
149
        # return network_id of the created network
161
150
        return 101
162
 
    
163
 
    
 
151
 
164
152
    def delete_network(self, tenant_id, net_id):
165
153
        """
166
154
        Deletes the network with the specified network identifier
168
156
        """
169
157
        print("delete_network() called\n")
170
158
 
171
 
    
172
159
    def get_network_details(self, tenant_id, net_id):
173
160
        """
174
161
        retrieved a list of all the remote vifs that
175
162
        are attached to the network
176
163
        """
177
164
        print("get_network_details() called\n")
178
 
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0", "/tenant1/networks/10/121/vif1.1"]
 
165
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0",
 
166
                       "/tenant1/networks/10/121/vif1.1"]
179
167
        return vifs_on_net
180
 
    
181
 
    
 
168
 
182
169
    def rename_network(self, tenant_id, net_id, new_name):
183
170
        """
184
171
        Updates the symbolic name belonging to a particular
185
172
        Virtual Network.
186
173
        """
187
174
        print("rename_network() called\n")
188
 
    
189
 
    
 
175
 
190
176
    def get_all_ports(self, tenant_id, net_id):
191
177
        """
192
178
        Retrieves all port identifiers belonging to the
195
181
        print("get_all_ports() called\n")
196
182
        port_ids_on_net = ["2", "3", "4"]
197
183
        return port_ids_on_net
198
 
    
199
 
    
 
184
 
200
185
    def create_port(self, tenant_id, net_id):
201
186
        """
202
187
        Creates a port on the specified Virtual Network.
204
189
        print("create_port() called\n")
205
190
        #return the port id
206
191
        return 201
207
 
    
208
 
    
 
192
 
209
193
    def delete_port(self, tenant_id, net_id, port_id):
210
194
        """
211
195
        Deletes a port on a specified Virtual Network,
214
198
        is deleted.
215
199
        """
216
200
        print("delete_port() called\n")
217
 
    
218
 
    
 
201
 
219
202
    def get_port_details(self, tenant_id, net_id, port_id):
220
203
        """
221
204
        This method allows the user to retrieve a remote interface
224
207
        print("get_port_details() called\n")
225
208
        #returns the remote interface UUID
226
209
        return "/tenant1/networks/net_id/portid/vif2.1"
227
 
    
228
 
    
 
210
 
229
211
    def plug_interface(self, tenant_id, net_id, port_id, remote_interface_id):
230
212
        """
231
213
        Attaches a remote interface to the specified port on the
232
214
        specified Virtual Network.
233
215
        """
234
216
        print("plug_interface() called\n")
235
 
    
236
 
    
 
217
 
237
218
    def unplug_interface(self, tenant_id, net_id, port_id):
238
219
        """
239
220
        Detaches a remote interface from the specified port on the
240
221
        specified Virtual Network.
241
222
        """
242
223
        print("unplug_interface() called\n")
243
 
    
244
 
    
 
224
 
245
225
    def get_interface_details(self, tenant_id, net_id, port_id):
246
226
        """
247
227
        Retrieves the remote interface that is attached at this
250
230
        print("get_interface_details() called\n")
251
231
        #returns the remote interface UUID
252
232
        return "/tenant1/networks/net_id/portid/vif2.0"
253
 
    
254
 
    
 
233
 
255
234
    def get_all_attached_interfaces(self, tenant_id, net_id):
256
235
        """
257
236
        Retrieves all remote interfaces that are attached to
259
238
        """
260
239
        print("get_all_attached_interfaces() called\n")
261
240
        # returns a list of all attached remote interfaces
262
 
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0", "/tenant1/networks/10/121/vif1.1"]
 
241
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0",
 
242
                       "/tenant1/networks/10/121/vif1.1"]
263
243
        return vifs_on_net
264
 
    
265
 
    
 
244
 
 
245
 
266
246
class FakePlugin(object):
267
247
    """
268
248
    FakePlugin is a demo plugin that provides
272
252
 
273
253
    #static data for networks and ports
274
254
    _port_dict_1 = {
275
 
                   1 : {'port-id': 1, 
276
 
                        'port-state': 'DOWN',
277
 
                        'attachment': None},
278
 
                   2 : {'port-id': 2, 
279
 
                        'port-state':'UP',
280
 
                        'attachment': None}
 
255
                   1: {'port-id': 1,
 
256
                       'port-state': 'DOWN',
 
257
                       'attachment': None},
 
258
                   2: {'port-id': 2,
 
259
                       'port-state': 'UP',
 
260
                       'attachment': None}
281
261
                   }
282
262
    _port_dict_2 = {
283
 
                   1 : {'port-id': 1, 
284
 
                        'port-state': 'UP',
285
 
                        'attachment': 'SomeFormOfVIFID'},
286
 
                   2 : {'port-id': 2, 
287
 
                        'port-state':'DOWN',
288
 
                        'attachment': None}
289
 
                   }        
290
 
    _networks={'001':
 
263
                   1: {'port-id': 1,
 
264
                       'port-state': 'UP',
 
265
                       'attachment': 'SomeFormOfVIFID'},
 
266
                   2: {'port-id': 2,
 
267
                       'port-state': 'DOWN',
 
268
                       'attachment': None}
 
269
                   }
 
270
    _networks = {'001':
291
271
                    {
292
 
                    'net-id':'001',
293
 
                    'net-name':'pippotest',
 
272
                    'net-id': '001',
 
273
                    'net-name': 'pippotest',
294
274
                    'net-ports': _port_dict_1
295
275
                    },
296
276
                    '002':
297
277
                    {
298
 
                    'net-id':'002',
299
 
                    'net-name':'cicciotest',
300
 
                    'net-ports': _port_dict_2                      
 
278
                    'net-id': '002',
 
279
                    'net-name': 'cicciotest',
 
280
                    'net-ports': _port_dict_2
301
281
                    }}
302
 
    
303
 
    
 
282
 
304
283
    def __init__(self):
305
 
        FakePlugin._net_counter=len(FakePlugin._networks)
306
 
    
 
284
        FakePlugin._net_counter = len(FakePlugin._networks)
 
285
 
307
286
    def _get_network(self, tenant_id, network_id):
308
287
        network = FakePlugin._networks.get(network_id)
309
288
        if not network:
310
289
            raise exc.NetworkNotFound(net_id=network_id)
311
290
        return network
312
291
 
313
 
    
314
292
    def _get_port(self, tenant_id, network_id, port_id):
315
293
        net = self._get_network(tenant_id, network_id)
316
294
        port = net['net-ports'].get(int(port_id))
317
295
        if not port:
318
296
            raise exc.PortNotFound(net_id=network_id, port_id=port_id)
319
297
        return port
320
 
    
 
298
 
321
299
    def _validate_port_state(self, port_state):
322
 
        if port_state.upper() not in ('UP','DOWN'):
 
300
        if port_state.upper() not in ('UP', 'DOWN'):
323
301
            raise exc.StateInvalid(port_state=port_state)
324
302
        return True
325
 
    
 
303
 
326
304
    def _validate_attachment(self, tenant_id, network_id, port_id,
327
305
                             remote_interface_id):
328
306
        network = self._get_network(tenant_id, network_id)
329
307
        for port in network['net-ports'].values():
330
308
            if port['attachment'] == remote_interface_id:
331
 
                raise exc.AlreadyAttached(net_id = network_id,
332
 
                                          port_id = port_id,
333
 
                                          att_id = port['attachment'],
334
 
                                          att_port_id = port['port-id'])
335
 
        
 
309
                raise exc.AlreadyAttached(net_id=network_id,
 
310
                                          port_id=port_id,
 
311
                                          att_id=port['attachment'],
 
312
                                          att_port_id=port['port-id'])
 
313
 
336
314
    def get_all_networks(self, tenant_id):
337
315
        """
338
316
        Returns a dictionary containing all
339
317
        <network_uuid, network_name> for
340
 
        the specified tenant. 
 
318
        the specified tenant.
341
319
        """
342
320
        print("get_all_networks() called\n")
343
321
        return FakePlugin._networks.values()
357
335
        """
358
336
        print("create_network() called\n")
359
337
        FakePlugin._net_counter += 1
360
 
        new_net_id=("0" * (3 - len(str(FakePlugin._net_counter)))) + \
 
338
        new_net_id = ("0" * (3 - len(str(FakePlugin._net_counter)))) + \
361
339
                    str(FakePlugin._net_counter)
362
340
        print new_net_id
363
 
        new_net_dict={'net-id':new_net_id,
364
 
                      'net-name':net_name,
365
 
                      'net-ports': {}}
366
 
        FakePlugin._networks[new_net_id]=new_net_dict
 
341
        new_net_dict = {'net-id': new_net_id,
 
342
                        'net-name': net_name,
 
343
                        'net-ports': {}}
 
344
        FakePlugin._networks[new_net_id] = new_net_dict
367
345
        # return network_id of the created network
368
346
        return new_net_dict
369
 
    
 
347
 
370
348
    def delete_network(self, tenant_id, net_id):
371
349
        """
372
350
        Deletes the network with the specified network identifier
384
362
            return net
385
363
        # Network not found
386
364
        raise exc.NetworkNotFound(net_id=net_id)
387
 
    
 
365
 
388
366
    def rename_network(self, tenant_id, net_id, new_name):
389
367
        """
390
368
        Updates the symbolic name belonging to a particular
392
370
        """
393
371
        print("rename_network() called\n")
394
372
        net = self._get_network(tenant_id, net_id)
395
 
        net['net-name']=new_name 
 
373
        net['net-name'] = new_name
396
374
        return net
397
375
 
398
376
    def get_all_ports(self, tenant_id, net_id):
412
390
        """
413
391
        print("get_port_details() called\n")
414
392
        return self._get_port(tenant_id, net_id, port_id)
415
 
        
 
393
 
416
394
    def create_port(self, tenant_id, net_id, port_state=None):
417
395
        """
418
396
        Creates a port on the specified Virtual Network.
420
398
        print("create_port() called\n")
421
399
        net = self._get_network(tenant_id, net_id)
422
400
        # check port state
423
 
        # TODO(salvatore-orlando): Validate port state in API?            
 
401
        # TODO(salvatore-orlando): Validate port state in API?
424
402
        self._validate_port_state(port_state)
425
403
        ports = net['net-ports']
426
 
        new_port_id = max(ports.keys())+1
427
 
        new_port_dict = {'port-id':new_port_id,
 
404
        new_port_id = max(ports.keys()) + 1
 
405
        new_port_dict = {'port-id': new_port_id,
428
406
                         'port-state': port_state,
429
407
                         'attachment': None}
430
408
        ports[new_port_id] = new_port_dict
431
 
        return new_port_dict 
 
409
        return new_port_dict
432
410
 
433
411
    def update_port(self, tenant_id, net_id, port_id, port_state):
434
412
        """
438
416
        port = self._get_port(tenant_id, net_id, port_id)
439
417
        self._validate_port_state(port_state)
440
418
        port['port-state'] = port_state
441
 
        return port 
442
 
        
 
419
        return port
 
420
 
443
421
    def delete_port(self, tenant_id, net_id, port_id):
444
422
        """
445
423
        Deletes a port on a specified Virtual Network,
451
429
        net = self._get_network(tenant_id, net_id)
452
430
        port = self._get_port(tenant_id, net_id, port_id)
453
431
        if port['attachment']:
454
 
            raise exc.PortInUse(net_id=net_id,port_id=port_id,
 
432
            raise exc.PortInUse(net_id=net_id, port_id=port_id,
455
433
                                att_id=port['attachment'])
456
434
        try:
457
435
            net['net-ports'].pop(int(port_id))
458
 
        except KeyError:   
 
436
        except KeyError:
459
437
            raise exc.PortNotFound(net_id=net_id, port_id=port_id)
460
438
 
461
439
    def get_interface_details(self, tenant_id, net_id, port_id):
466
444
        print("get_interface_details() called\n")
467
445
        port = self._get_port(tenant_id, net_id, port_id)
468
446
        return port['attachment']
469
 
           
 
447
 
470
448
    def plug_interface(self, tenant_id, net_id, port_id, remote_interface_id):
471
449
        """
472
450
        Attaches a remote interface to the specified port on the
478
456
                                  remote_interface_id)
479
457
        port = self._get_port(tenant_id, net_id, port_id)
480
458
        if port['attachment']:
481
 
            raise exc.PortInUse(net_id=net_id,port_id=port_id,
 
459
            raise exc.PortInUse(net_id=net_id, port_id=port_id,
482
460
                                att_id=port['attachment'])
483
461
        port['attachment'] = remote_interface_id
484
 
    
 
462
 
485
463
    def unplug_interface(self, tenant_id, net_id, port_id):
486
464
        """
487
465
        Detaches a remote interface from the specified port on the
492
470
        # TODO(salvatore-orlando):
493
471
        # Should unplug on port without attachment raise an Error?
494
472
        port['attachment'] = None
495
 
    
496
 
    #TODO - neeed to update methods from this point onwards
 
473
 
 
474
    # TODO - neeed to update methods from this point onwards
497
475
    def get_all_attached_interfaces(self, tenant_id, net_id):
498
476
        """
499
477
        Retrieves all remote interfaces that are attached to
501
479
        """
502
480
        print("get_all_attached_interfaces() called\n")
503
481
        # returns a list of all attached remote interfaces
504
 
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0", "/tenant1/networks/10/121/vif1.1"]
 
482
        vifs_on_net = ["/tenant1/networks/net_id/portid/vif2.0",
 
483
                       "/tenant1/networks/10/121/vif1.1"]
505
484
        return vifs_on_net
506
 
        
 
 
b'\\ No newline at end of file'