Merge lp:~rohitagarwalla/neutron/l2network-plugin-db into lp:~cisco-openstack/neutron/plugin-framework
- l2network-plugin-db
- Merge into plugin-framework
Status: | Merged |
---|---|
Merged at revision: | 40 |
Proposed branch: | lp:~rohitagarwalla/neutron/l2network-plugin-db |
Merge into: | lp:~cisco-openstack/neutron/plugin-framework |
Diff against target: |
1851 lines (+1814/-0) 7 files modified
quantum/plugins/cisco/README (+8/-0) quantum/plugins/cisco/db/db_conn.ini (+5/-0) quantum/plugins/cisco/db/db_test_plugin.py (+1049/-0) quantum/plugins/cisco/db/l2network_db.py (+239/-0) quantum/plugins/cisco/db/l2network_models.py (+86/-0) quantum/plugins/cisco/db/ucs_db.py (+314/-0) quantum/plugins/cisco/db/ucs_models.py (+113/-0) |
To merge this branch: | bzr merge lp:~rohitagarwalla/neutron/l2network-plugin-db |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Sumit Naiksatam (community) | Approve | ||
Review via email: mp+69910@code.launchpad.net |
Commit message
Description of the change
persistence of l2network & ucs plugins using mysql
- db_conn.ini - configuration details of making a connection to the database
- db_test_plugin.py - contains abstraction methods for storing database values in a dict and unit test cases for DB testing
- l2network_db.py - db methods for l2network models
- l2network_models.py - class definitions for the l2 network tables
- ucs_db.py - db methods for ucs models
- ucs_models.py - class definition for the ucs tables
dynamic loading of the 2nd layer plugin db's based on passed arguments
Create, Delete, Get, Getall, Update database methods at - Quantum, L2Network and Ucs
Unit test cases for create, delete, getall and update operations for L2Network and Ucs plugins
pep8 checks done
branch based off revision 34 plugin-framework
- 36. By Rohit Agarwalla
-
merged the latest changes from plugin-framework branch - revision 39
conforming to the new cisco plugin directory structure and moving all db related modules into cisco/db folder
updated db_test_plugin.py
- added import of cisco constants module
- added LOG.getLogger for logging component name
- updated import module paths for l2network_models/db and ucs_models/db to use the new directory structure
- updated (rearranged) imports section to obey openstack alphabetical placement convention
updated db_conn.ini
- updated database name from cisco_naas to quantum_l2network
unit test cases ran successfully and pep8 checks done again
Sumit Naiksatam (snaiksat) wrote : | # |
Great, lgtm. I recommend going ahead with the merge. Thanks!
Preview Diff
1 | === modified file 'quantum/plugins/cisco/README' |
2 | --- quantum/plugins/cisco/README 2011-07-31 18:38:26 +0000 |
3 | +++ quantum/plugins/cisco/README 2011-08-01 03:39:28 +0000 |
4 | @@ -76,3 +76,11 @@ |
5 | /usr/lib/python2.6/site-packages/nova/virt/cisco_ucs.py |
6 | |
7 | * Restart nova-compute service |
8 | + |
9 | +**L2network database usage requirements - |
10 | +* mysql database "quantum_l2network" is required for persistence of the l2network plugin data. |
11 | + + The database can be created as follows - |
12 | + - # mysql -uroot -pnova; (username/password here is root/nova) |
13 | + - mysql> create database quantum_l2network; |
14 | + - mysql> use quantum_l2network; |
15 | + + The database and login details must be specified in quantum/plugins/cisco/db/db_conn.ini. |
16 | |
17 | === added file 'quantum/plugins/cisco/db/db_conn.ini' |
18 | --- quantum/plugins/cisco/db/db_conn.ini 1970-01-01 00:00:00 +0000 |
19 | +++ quantum/plugins/cisco/db/db_conn.ini 2011-08-01 03:39:28 +0000 |
20 | @@ -0,0 +1,5 @@ |
21 | +[DATABASE] |
22 | +name = quantum_l2network |
23 | +user = root |
24 | +pass = nova |
25 | +host = 127.0.0.1 |
26 | |
27 | === added file 'quantum/plugins/cisco/db/db_test_plugin.py' |
28 | --- quantum/plugins/cisco/db/db_test_plugin.py 1970-01-01 00:00:00 +0000 |
29 | +++ quantum/plugins/cisco/db/db_test_plugin.py 2011-08-01 03:39:28 +0000 |
30 | @@ -0,0 +1,1049 @@ |
31 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
32 | + |
33 | +# Copyright 2011, Cisco Systems, Inc. |
34 | +# |
35 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
36 | +# not use this file except in compliance with the License. You may obtain |
37 | +# a copy of the License at |
38 | +# |
39 | +# http://www.apache.org/licenses/LICENSE-2.0 |
40 | +# |
41 | +# Unless required by applicable law or agreed to in writing, software |
42 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
43 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
44 | +# License for the specific language governing permissions and limitations |
45 | +# under the License. |
46 | +# @author: Rohit Agarwalla, Cisco Systems, Inc. |
47 | + |
48 | +import ConfigParser |
49 | +import os |
50 | +import logging as LOG |
51 | +import unittest |
52 | + |
53 | +from optparse import OptionParser |
54 | +from quantum.plugins.cisco.common import cisco_constants as const |
55 | + |
56 | +import quantum.db.api as db |
57 | +import quantum.db.models |
58 | +import quantum.plugins.cisco.db.l2network_db as l2network_db |
59 | +import quantum.plugins.cisco.db.l2network_models |
60 | + |
61 | +CONF_FILE = "db_conn.ini" |
62 | +LOG.getLogger(const.LOGGER_COMPONENT_NAME) |
63 | + |
64 | + |
65 | +def find_config(basepath): |
66 | + for root, dirs, files in os.walk(basepath): |
67 | + if CONF_FILE in files: |
68 | + return os.path.join(root, CONF_FILE) |
69 | + return None |
70 | + |
71 | + |
72 | +def db_conf(configfile=None): |
73 | + config = ConfigParser.ConfigParser() |
74 | + if configfile == None: |
75 | + if os.path.exists(CONF_FILE): |
76 | + configfile = CONF_FILE |
77 | + else: |
78 | + configfile = \ |
79 | + find_config(os.path.abspath(os.path.dirname(__file__))) |
80 | + if configfile == None: |
81 | + raise Exception("Configuration file \"%s\" doesn't exist" % |
82 | + (configfile)) |
83 | + LOG.debug("Using configuration file: %s" % configfile) |
84 | + config.read(configfile) |
85 | + |
86 | + DB_NAME = config.get("DATABASE", "name") |
87 | + DB_USER = config.get("DATABASE", "user") |
88 | + DB_PASS = config.get("DATABASE", "pass") |
89 | + DB_HOST = config.get("DATABASE", "host") |
90 | + options = {"sql_connection": "mysql://%s:%s@%s/%s" % (DB_USER, |
91 | + DB_PASS, DB_HOST, DB_NAME)} |
92 | + db.configure_db(options) |
93 | + |
94 | + |
95 | +class UcsDB(object): |
96 | + def get_all_ucsmbindings(self): |
97 | + bindings = [] |
98 | + try: |
99 | + for x in ucs_db.get_all_ucsmbinding(): |
100 | + LOG.debug("Getting ucsm binding : %s" % x.ucsm_ip) |
101 | + bind_dict = {} |
102 | + bind_dict["ucsm-ip"] = str(x.ucsm_ip) |
103 | + bind_dict["network-id"] = str(x.network_id) |
104 | + bindings.append(bind_dict) |
105 | + except Exception, e: |
106 | + LOG.error("Failed to get all bindings: %s" % str(e)) |
107 | + return bindings |
108 | + |
109 | + def get_ucsmbinding(self, ucsm_ip): |
110 | + binding = [] |
111 | + try: |
112 | + for x in ucs_db.get_ucsmbinding(ucsm_ip): |
113 | + LOG.debug("Getting ucsm binding : %s" % x.ucsm_ip) |
114 | + bind_dict = {} |
115 | + bind_dict["ucsm-ip"] = str(res.ucsm_ip) |
116 | + bind_dict["network-id"] = str(res.network_id) |
117 | + binding.append(bind_dict) |
118 | + except Exception, e: |
119 | + LOG.error("Failed to get binding: %s" % str(e)) |
120 | + return binding |
121 | + |
122 | + def create_ucsmbinding(self, ucsm_ip, networ_id): |
123 | + bind_dict = {} |
124 | + try: |
125 | + res = ucs_db.add_ucsmbinding(ucsm_ip, networ_id) |
126 | + LOG.debug("Created ucsm binding: %s" % res.ucsm_ip) |
127 | + bind_dict["ucsm-ip"] = str(res.ucsm_ip) |
128 | + bind_dict["network-id"] = str(res.network_id) |
129 | + return bind_dict |
130 | + except Exception, e: |
131 | + LOG.error("Failed to create ucsm binding: %s" % str(e)) |
132 | + |
133 | + def delete_ucsmbinding(self, ucsm_ip): |
134 | + try: |
135 | + res = ucs_db.remove_ucsmbinding(ucsm_ip) |
136 | + LOG.debug("Deleted ucsm binding : %s" % res.ucsm_ip) |
137 | + bind_dict = {} |
138 | + bind_dict["ucsm-ip"] = str(res.ucsm_ip) |
139 | + return bind_dict |
140 | + except Exception, e: |
141 | + raise Exception("Failed to delete dynamic vnic: %s" % str(e)) |
142 | + |
143 | + def update_ucsmbinding(self, ucsm_ip, network_id): |
144 | + try: |
145 | + res = ucs_db.update_ucsmbinding(ucsm_ip, network_id) |
146 | + LOG.debug("Updating ucsm binding : %s" % res.ucsm_ip) |
147 | + bind_dict = {} |
148 | + bind_dict["ucsm-ip"] = str(res.ucsm_ip) |
149 | + bind_dict["network-id"] = str(res.network_id) |
150 | + return bind_dict |
151 | + except Exception, e: |
152 | + raise Exception("Failed to update dynamic vnic: %s" % str(e)) |
153 | + |
154 | + def get_all_dynamicvnics(self): |
155 | + vnics = [] |
156 | + try: |
157 | + for x in ucs_db.get_all_dynamicvnics(): |
158 | + LOG.debug("Getting dynamic vnic : %s" % x.uuid) |
159 | + vnic_dict = {} |
160 | + vnic_dict["vnic-id"] = str(x.uuid) |
161 | + vnic_dict["device-name"] = x.device_name |
162 | + vnic_dict["blade-id"] = str(x.blade_id) |
163 | + vnics.append(vnic_dict) |
164 | + except Exception, e: |
165 | + LOG.error("Failed to get all dynamic vnics: %s" % str(e)) |
166 | + return vnics |
167 | + |
168 | + def get_dynamicvnic(self, vnic_id): |
169 | + vnic = [] |
170 | + try: |
171 | + for x in ucs_db.get_dynamicvnic(vnic_id): |
172 | + LOG.debug("Getting dynamic vnic : %s" % x.uuid) |
173 | + vnic_dict = {} |
174 | + vnic_dict["vnic-id"] = str(x.uuid) |
175 | + vnic_dict["device-name"] = x.device_name |
176 | + vnic_dict["blade-id"] = str(x.blade_id) |
177 | + vnic.append(vnic_dict) |
178 | + except Exception, e: |
179 | + LOG.error("Failed to get dynamic vnic: %s" % str(e)) |
180 | + return vnic |
181 | + |
182 | + def create_dynamicvnic(self, device_name, blade_id): |
183 | + vnic_dict = {} |
184 | + try: |
185 | + res = ucs_db.add_dynamicvnic(device_name, blade_id) |
186 | + LOG.debug("Created dynamic vnic: %s" % res.uuid) |
187 | + vnic_dict["vnic-id"] = str(res.uuid) |
188 | + vnic_dict["device-name"] = res.device_name |
189 | + vnic_dict["blade-id"] = str(res.blade_id) |
190 | + return vnic_dict |
191 | + except Exception, e: |
192 | + LOG.error("Failed to create dynamic vnic: %s" % str(e)) |
193 | + |
194 | + def delete_dynamicvnic(self, vnic_id): |
195 | + try: |
196 | + res = ucs_db.remove_dynamicvnic(vnic_id) |
197 | + LOG.debug("Deleted dynamic vnic : %s" % res.uuid) |
198 | + vnic_dict = {} |
199 | + vnic_dict["vnic-id"] = str(res.uuid) |
200 | + return vnic_dict |
201 | + except Exception, e: |
202 | + raise Exception("Failed to delete dynamic vnic: %s" % str(e)) |
203 | + |
204 | + def update_dynamicvnic(self, vnic_id, device_name=None, blade_id=None): |
205 | + try: |
206 | + res = ucs_db.update_dynamicvnic(vnic_id, device_name, blade_id) |
207 | + LOG.debug("Updating dynamic vnic : %s" % res.uuid) |
208 | + vnic_dict = {} |
209 | + vnic_dict["vnic-id"] = str(res.uuid) |
210 | + vnic_dict["device-name"] = res.device_name |
211 | + vnic_dict["blade-id"] = str(res.blade_id) |
212 | + return vnic_dict |
213 | + except Exception, e: |
214 | + raise Exception("Failed to update dynamic vnic: %s" % str(e)) |
215 | + |
216 | + def get_all_blades(self): |
217 | + blades = [] |
218 | + try: |
219 | + for x in ucs_db.get_all_blades(): |
220 | + LOG.debug("Getting blade : %s" % x.uuid) |
221 | + blade_dict = {} |
222 | + blade_dict["blade-id"] = str(x.uuid) |
223 | + blade_dict["mgmt-ip"] = str(x.mgmt_ip) |
224 | + blade_dict["mac-addr"] = str(x.mac_addr) |
225 | + blade_dict["chassis-id"] = str(x.chassis_id) |
226 | + blade_dict["ucsm-ip"] = str(x.ucsm_ip) |
227 | + blades.append(blade_dict) |
228 | + except Exception, e: |
229 | + LOG.error("Failed to get all blades: %s" % str(e)) |
230 | + return blades |
231 | + |
232 | + def get_blade(self, blade_id): |
233 | + blade = [] |
234 | + try: |
235 | + for x in ucs_db.get_blade(blade_id): |
236 | + LOG.debug("Getting blade : %s" % x.uuid) |
237 | + blade_dict = {} |
238 | + blade_dict["blade-id"] = str(x.uuid) |
239 | + blade_dict["mgmt-ip"] = str(x.mgmt_ip) |
240 | + blade_dict["mac-addr"] = str(x.mac_addr) |
241 | + blade_dict["chassis-id"] = str(x.chassis_id) |
242 | + blade_dict["ucsm-ip"] = str(x.ucsm_ip) |
243 | + blade.append(blade_dict) |
244 | + except Exception, e: |
245 | + LOG.error("Failed to get all blades: %s" % str(e)) |
246 | + return blade |
247 | + |
248 | + def create_blade(self, mgmt_ip, mac_addr, chassis_id, ucsm_ip): |
249 | + blade_dict = {} |
250 | + try: |
251 | + res = ucs_db.add_blade(mgmt_ip, mac_addr, chassis_id, ucsm_ip) |
252 | + LOG.debug("Created blade: %s" % res.uuid) |
253 | + blade_dict["blade-id"] = str(res.uuid) |
254 | + blade_dict["mgmt-ip"] = str(res.mgmt_ip) |
255 | + blade_dict["mac-addr"] = str(res.mac_addr) |
256 | + blade_dict["chassis-id"] = str(res.chassis_id) |
257 | + blade_dict["ucsm-ip"] = str(res.ucsm_ip) |
258 | + return blade_dict |
259 | + except Exception, e: |
260 | + LOG.error("Failed to create blade: %s" % str(e)) |
261 | + |
262 | + def delete_blade(self, blade_id): |
263 | + try: |
264 | + res = ucs_db.remove_blade(blade_id) |
265 | + LOG.debug("Deleted blade : %s" % res.uuid) |
266 | + blade_dict = {} |
267 | + blade_dict["blade-id"] = str(res.uuid) |
268 | + return blade_dict |
269 | + except Exception, e: |
270 | + raise Exception("Failed to delete blade: %s" % str(e)) |
271 | + |
272 | + def update_blade(self, blade_id, mgmt_ip=None, mac_addr=None,\ |
273 | + chassis_id=None, ucsm_ip=None): |
274 | + try: |
275 | + res = ucs_db.update_blade(blade_id, mgmt_ip, mac_addr, \ |
276 | + chassis_id, ucsm_ip) |
277 | + LOG.debug("Updating blade : %s" % res.uuid) |
278 | + blade_dict = {} |
279 | + blade_dict["blade-id"] = str(res.uuid) |
280 | + blade_dict["mgmt-ip"] = str(res.mgmt_ip) |
281 | + blade_dict["mac-addr"] = str(res.mac_addr) |
282 | + blade_dict["chassis-id"] = str(res.chassis_id) |
283 | + blade_dict["ucsm-ip"] = str(res.ucsm_ip) |
284 | + return blade_dict |
285 | + except Exception, e: |
286 | + raise Exception("Failed to update blade: %s" % str(e)) |
287 | + |
288 | + def get_all_port_bindings(self): |
289 | + port_bindings = [] |
290 | + try: |
291 | + for x in ucs_db.get_all_portbindings(): |
292 | + LOG.debug("Getting port binding for port: %s" % x.port_id) |
293 | + port_bind_dict = {} |
294 | + port_bind_dict["port-id"] = x.port_id |
295 | + port_bind_dict["dynamic-vnic-id"] = str(x.dynamic_vnic_id) |
296 | + port_bind_dict["portprofile-name"] = x.portprofile_name |
297 | + port_bind_dict["vlan-name"] = x.vlan_name |
298 | + port_bind_dict["vlan-id"] = str(x.vlan_id) |
299 | + port_bind_dict["qos"] = x.qos |
300 | + port_bindings.append(port_bind_dict) |
301 | + except Exception, e: |
302 | + LOG.error("Failed to get all port bindings: %s" % str(e)) |
303 | + return port_bindings |
304 | + |
305 | + def get_port_binding(self): |
306 | + port_binding = [] |
307 | + try: |
308 | + for x in ucs_db.get_portbinding(port_id): |
309 | + LOG.debug("Getting port binding for port: %s" % x.port_id) |
310 | + port_bind_dict = {} |
311 | + port_bind_dict["port-id"] = x.port_id |
312 | + port_bind_dict["dynamic-vnic-id"] = str(x.dynamic_vnic_id) |
313 | + port_bind_dict["portprofile-name"] = x.portprofile_name |
314 | + port_bind_dict["vlan-name"] = x.vlan_name |
315 | + port_bind_dict["vlan-id"] = str(x.vlan_id) |
316 | + port_bind_dict["qos"] = x.qos |
317 | + port_bindings.append(port_bind_dict) |
318 | + except Exception, e: |
319 | + LOG.error("Failed to get port binding: %s" % str(e)) |
320 | + return port_binding |
321 | + |
322 | + def create_port_binding(self, port_id, dynamic_vnic_id, portprofile_name, \ |
323 | + vlan_name, vlan_id, qos): |
324 | + port_bind_dict = {} |
325 | + try: |
326 | + res = ucs_db.add_portbinding(port_id, dynamic_vnic_id, \ |
327 | + portprofile_name, vlan_name, vlan_id, qos) |
328 | + LOG.debug("Created port binding: %s" % res.port_id) |
329 | + port_bind_dict["port-id"] = res.port_id |
330 | + port_bind_dict["dynamic-vnic-id"] = str(res.dynamic_vnic_id) |
331 | + port_bind_dict["portprofile-name"] = res.portprofile_name |
332 | + port_bind_dict["vlan-name"] = res.vlan_name |
333 | + port_bind_dict["vlan-id"] = str(res.vlan_id) |
334 | + port_bind_dict["qos"] = res.qos |
335 | + return port_bind_dict |
336 | + except Exception, e: |
337 | + LOG.error("Failed to create port binding: %s" % str(e)) |
338 | + |
339 | + def delete_port_binding(self, port_id): |
340 | + try: |
341 | + res = ucs_db.remove_portbinding(port_id) |
342 | + LOG.debug("Deleted port binding : %s" % res.port_id) |
343 | + port_bind_dict = {} |
344 | + port_bind_dict["port-id"] = res.port_id |
345 | + return port_bind_dict |
346 | + except Exception, e: |
347 | + raise Exception("Failed to delete port profile: %s" % str(e)) |
348 | + |
349 | + def update_port_binding(self, port_id, dynamic_vnic_id, \ |
350 | + portprofile_name, vlan_name, vlan_id, qos): |
351 | + try: |
352 | + res = ucs_db.update_portbinding(port_id, dynamic_vnic_id, \ |
353 | + portprofile_name, vlan_name, vlan_id, qos) |
354 | + LOG.debug("Updating port binding: %s" % res.port_id) |
355 | + port_bind_dict = {} |
356 | + port_bind_dict["port-id"] = res.port_id |
357 | + port_bind_dict["dynamic-vnic-id"] = str(res.dynamic_vnic_id) |
358 | + port_bind_dict["portprofile-name"] = res.portprofile_name |
359 | + port_bind_dict["vlan-name"] = res.vlan_name |
360 | + port_bind_dict["vlan-id"] = str(res.vlan_id) |
361 | + port_bind_dict["qos"] = res.qos |
362 | + return port_bind_dict |
363 | + except Exception, e: |
364 | + raise Exception("Failed to update portprofile binding:%s" % str(e)) |
365 | + |
366 | + |
367 | +class QuantumDB(object): |
368 | + def get_all_networks(self, tenant_id): |
369 | + nets = [] |
370 | + try: |
371 | + for x in db.network_list(tenant_id): |
372 | + LOG.debug("Getting network: %s" % x.uuid) |
373 | + net_dict = {} |
374 | + net_dict["tenant-id"] = x.tenant_id |
375 | + net_dict["net-id"] = str(x.uuid) |
376 | + net_dict["net-name"] = x.name |
377 | + nets.append(net_dict) |
378 | + except Exception, e: |
379 | + LOG.error("Failed to get all networks: %s" % str(e)) |
380 | + return nets |
381 | + |
382 | + def get_network(self, network_id): |
383 | + net = [] |
384 | + try: |
385 | + for x in db.network_get(network_id): |
386 | + LOG.debug("Getting network: %s" % x.uuid) |
387 | + net_dict = {} |
388 | + net_dict["tenant-id"] = x.tenant_id |
389 | + net_dict["net-id"] = str(x.uuid) |
390 | + net_dict["net-name"] = x.name |
391 | + nets.append(net_dict) |
392 | + except Exception, e: |
393 | + LOG.error("Failed to get network: %s" % str(e)) |
394 | + return net |
395 | + |
396 | + def create_network(self, tenant_id, net_name): |
397 | + net_dict = {} |
398 | + try: |
399 | + res = db.network_create(tenant_id, net_name) |
400 | + LOG.debug("Created network: %s" % res.uuid) |
401 | + net_dict["tenant-id"] = res.tenant_id |
402 | + net_dict["net-id"] = str(res.uuid) |
403 | + net_dict["net-name"] = res.name |
404 | + return net_dict |
405 | + except Exception, e: |
406 | + LOG.error("Failed to create network: %s" % str(e)) |
407 | + |
408 | + def delete_network(self, net_id): |
409 | + try: |
410 | + net = db.network_destroy(net_id) |
411 | + LOG.debug("Deleted network: %s" % net.uuid) |
412 | + net_dict = {} |
413 | + net_dict["net-id"] = str(net.uuid) |
414 | + return net_dict |
415 | + except Exception, e: |
416 | + raise Exception("Failed to delete port: %s" % str(e)) |
417 | + |
418 | + def rename_network(self, tenant_id, net_id, new_name): |
419 | + try: |
420 | + net = db.network_rename(net_id, tenant_id, new_name) |
421 | + LOG.debug("Renamed network: %s" % net.uuid) |
422 | + net_dict = {} |
423 | + net_dict["net-id"] = str(net.uuid) |
424 | + net_dict["net-name"] = net.name |
425 | + return net_dict |
426 | + except Exception, e: |
427 | + raise Exception("Failed to rename network: %s" % str(e)) |
428 | + |
429 | + def get_all_ports(self, net_id): |
430 | + ports = [] |
431 | + try: |
432 | + for x in db.port_list(net_id): |
433 | + LOG.debug("Getting port: %s" % x.uuid) |
434 | + port_dict = {} |
435 | + port_dict["port-id"] = str(x.uuid) |
436 | + port_dict["net-id"] = str(x.network_id) |
437 | + port_dict["int-id"] = x.interface_id |
438 | + port_dict["state"] = x.state |
439 | + ports.append(port_dict) |
440 | + return ports |
441 | + except Exception, e: |
442 | + LOG.error("Failed to get all ports: %s" % str(e)) |
443 | + |
444 | + def get_port(self, port_id): |
445 | + port = [] |
446 | + try: |
447 | + for x in db.port_get(port_id): |
448 | + LOG.debug("Getting port: %s" % x.uuid) |
449 | + port_dict = {} |
450 | + port_dict["port-id"] = str(x.uuid) |
451 | + port_dict["net-id"] = str(x.network_id) |
452 | + port_dict["int-id"] = x.interface_id |
453 | + port_dict["state"] = x.state |
454 | + port.append(port_dict) |
455 | + return port |
456 | + except Exception, e: |
457 | + LOG.error("Failed to get port: %s" % str(e)) |
458 | + |
459 | + def create_port(self, net_id): |
460 | + port_dict = {} |
461 | + try: |
462 | + port = db.port_create(net_id) |
463 | + LOG.debug("Creating port %s" % port.uuid) |
464 | + port_dict["port-id"] = str(port.uuid) |
465 | + port_dict["net-id"] = str(port.network_id) |
466 | + port_dict["int-id"] = port.interface_id |
467 | + port_dict["state"] = port.state |
468 | + return port_dict |
469 | + except Exception, e: |
470 | + LOG.error("Failed to create port: %s" % str(e)) |
471 | + |
472 | + def delete_port(self, port_id): |
473 | + try: |
474 | + port = db.port_destroy(port_id) |
475 | + LOG.debug("Deleted port %s" % port.uuid) |
476 | + port_dict = {} |
477 | + port_dict["port-id"] = str(port.uuid) |
478 | + return port_dict |
479 | + except Exception, e: |
480 | + raise Exception("Failed to delete port: %s" % str(e)) |
481 | + |
482 | + def update_port(self, port_id, port_state): |
483 | + try: |
484 | + port = db.port_set_state(port_id, port_state) |
485 | + LOG.debug("Updated port %s" % port.uuid) |
486 | + port_dict = {} |
487 | + port_dict["port-id"] = str(port.uuid) |
488 | + port_dict["net-id"] = str(port.network_id) |
489 | + port_dict["int-id"] = port.interface_id |
490 | + port_dict["state"] = port.state |
491 | + return port_dict |
492 | + except Exception, e: |
493 | + raise Exception("Failed to update port state: %s" % str(e)) |
494 | + |
495 | + |
496 | +class L2networkDB(object): |
497 | + def get_all_vlan_bindings(self): |
498 | + vlans = [] |
499 | + try: |
500 | + for x in l2network_db.get_all_vlan_bindings(): |
501 | + LOG.debug("Getting vlan bindings for vlan: %s" % x.vlan_id) |
502 | + vlan_dict = {} |
503 | + vlan_dict["vlan-id"] = str(x.vlan_id) |
504 | + vlan_dict["vlan-name"] = x.vlan_name |
505 | + vlan_dict["net-id"] = str(x.network_id) |
506 | + vlans.append(vlan_dict) |
507 | + except Exception, e: |
508 | + LOG.error("Failed to get all vlan bindings: %s" % str(e)) |
509 | + return vlans |
510 | + |
511 | + def get_vlan_binding(self, network_id): |
512 | + vlan = [] |
513 | + try: |
514 | + for x in l2network_db.get_vlan_binding(network_id): |
515 | + LOG.debug("Getting vlan binding for vlan: %s" % x.vlan_id) |
516 | + vlan_dict = {} |
517 | + vlan_dict["vlan-id"] = str(x.vlan_id) |
518 | + vlan_dict["vlan-name"] = x.vlan_name |
519 | + vlan_dict["net-id"] = str(x.network_id) |
520 | + vlan.append(vlan_dict) |
521 | + except Exception, e: |
522 | + LOG.error("Failed to get vlan binding: %s" % str(e)) |
523 | + return vlan |
524 | + |
525 | + def create_vlan_binding(self, vlan_id, vlan_name, network_id): |
526 | + vlan_dict = {} |
527 | + try: |
528 | + res = l2network_db.add_vlan_binding(vlan_id, vlan_name, network_id) |
529 | + LOG.debug("Created vlan binding for vlan: %s" % res.vlan_id) |
530 | + vlan_dict["vlan-id"] = str(res.vlan_id) |
531 | + vlan_dict["vlan-name"] = res.vlan_name |
532 | + vlan_dict["net-id"] = str(res.network_id) |
533 | + return vlan_dict |
534 | + except Exception, e: |
535 | + LOG.error("Failed to create vlan binding: %s" % str(e)) |
536 | + |
537 | + def delete_vlan_binding(self, network_id): |
538 | + try: |
539 | + res = l2network_db.remove_vlan_binding(network_id) |
540 | + LOG.debug("Deleted vlan binding for vlan: %s" % res.vlan_id) |
541 | + vlan_dict = {} |
542 | + vlan_dict["vlan-id"] = str(res.vlan_id) |
543 | + return vlan_dict |
544 | + except Exception, e: |
545 | + raise Exception("Failed to delete vlan binding: %s" % str(e)) |
546 | + |
547 | + def update_vlan_binding(self, network_id, vlan_id, vlan_name): |
548 | + try: |
549 | + res = l2network_db.update_vlan_binding(network_id, vlan_id, \ |
550 | + vlan_name) |
551 | + LOG.debug("Updating vlan binding for vlan: %s" % res.vlan_id) |
552 | + vlan_dict = {} |
553 | + vlan_dict["vlan-id"] = str(res.vlan_id) |
554 | + vlan_dict["vlan-name"] = res.vlan_name |
555 | + vlan_dict["net-id"] = str(res.network_id) |
556 | + return vlan_dict |
557 | + except Exception, e: |
558 | + raise Exception("Failed to update vlan binding: %s" % str(e)) |
559 | + |
560 | + def get_all_portprofiles(self): |
561 | + pps = [] |
562 | + try: |
563 | + for x in l2network_db.get_all_portprofiles(): |
564 | + LOG.debug("Getting port profile : %s" % x.uuid) |
565 | + pp_dict = {} |
566 | + pp_dict["portprofile-id"] = str(x.uuid) |
567 | + pp_dict["portprofile-name"] = x.name |
568 | + pp_dict["vlan-id"] = str(x.vlan_id) |
569 | + pp_dict["qos"] = x.qos |
570 | + pps.append(pp_dict) |
571 | + except Exception, e: |
572 | + LOG.error("Failed to get all port profiles: %s" % str(e)) |
573 | + return pps |
574 | + |
575 | + def get_portprofile(self, port_id): |
576 | + pp = [] |
577 | + try: |
578 | + for x in l2network_db.get_portprofile(port_id): |
579 | + LOG.debug("Getting port profile : %s" % x.uuid) |
580 | + pp_dict = {} |
581 | + pp_dict["portprofile-id"] = str(x.uuid) |
582 | + pp_dict["portprofile-name"] = x.name |
583 | + pp_dict["vlan-id"] = str(x.vlan_id) |
584 | + pp_dict["qos"] = x.qos |
585 | + pp.append(pp_dict) |
586 | + except Exception, e: |
587 | + LOG.error("Failed to get port profile: %s" % str(e)) |
588 | + return pp |
589 | + |
590 | + def create_portprofile(self, name, vlan_id, qos): |
591 | + pp_dict = {} |
592 | + try: |
593 | + res = l2network_db.add_portprofile(name, vlan_id, qos) |
594 | + LOG.debug("Created port profile: %s" % res.uuid) |
595 | + pp_dict["portprofile-id"] = str(res.uuid) |
596 | + pp_dict["portprofile-name"] = res.name |
597 | + pp_dict["vlan-id"] = str(res.vlan_id) |
598 | + pp_dict["qos"] = res.qos |
599 | + return pp_dict |
600 | + except Exception, e: |
601 | + LOG.error("Failed to create port profile: %s" % str(e)) |
602 | + |
603 | + def delete_portprofile(self, pp_id): |
604 | + try: |
605 | + res = l2network_db.remove_portprofile(pp_id) |
606 | + LOG.debug("Deleted port profile : %s" % res.uuid) |
607 | + pp_dict = {} |
608 | + pp_dict["pp-id"] = str(res.uuid) |
609 | + return pp_dict |
610 | + except Exception, e: |
611 | + raise Exception("Failed to delete port profile: %s" % str(e)) |
612 | + |
613 | + def update_portprofile(self, pp_id, name, vlan_id, qos): |
614 | + try: |
615 | + res = l2network_db.update_portprofile(pp_id, name, vlan_id, qos) |
616 | + LOG.debug("Updating port profile : %s" % res.uuid) |
617 | + pp_dict = {} |
618 | + pp_dict["portprofile-id"] = str(res.uuid) |
619 | + pp_dict["portprofile-name"] = res.name |
620 | + pp_dict["vlan-id"] = str(res.vlan_id) |
621 | + pp_dict["qos"] = res.qos |
622 | + return pp_dict |
623 | + except Exception, e: |
624 | + raise Exception("Failed to update port profile: %s" % str(e)) |
625 | + |
626 | + def get_all_pp_bindings(self): |
627 | + pp_bindings = [] |
628 | + try: |
629 | + for x in l2network_db.get_all_pp_bindings(): |
630 | + LOG.debug("Getting port profile binding: %s" % \ |
631 | + x.portprofile_id) |
632 | + ppbinding_dict = {} |
633 | + ppbinding_dict["portprofile-id"] = str(x.portprofile_id) |
634 | + ppbinding_dict["net-id"] = str(x.network_id) |
635 | + ppbinding_dict["tenant-id"] = x.tenant_id |
636 | + ppbinding_dict["default"] = x.default |
637 | + pp_bindings.append(ppbinding_dict) |
638 | + except Exception, e: |
639 | + LOG.error("Failed to get all port profiles: %s" % str(e)) |
640 | + return pp_bindings |
641 | + |
642 | + def get_pp_binding(self, pp_id): |
643 | + pp_binding = [] |
644 | + try: |
645 | + for x in l2network_db.get_pp_binding(pp_id): |
646 | + LOG.debug("Getting port profile binding: %s" % \ |
647 | + x.portprofile_id) |
648 | + ppbinding_dict = {} |
649 | + ppbinding_dict["portprofile-id"] = str(x.portprofile_id) |
650 | + ppbinding_dict["net-id"] = str(x.network_id) |
651 | + ppbinding_dict["tenant-id"] = x.tenant_id |
652 | + ppbinding_dict["default"] = x.default |
653 | + pp_bindings.append(ppbinding_dict) |
654 | + except Exception, e: |
655 | + LOG.error("Failed to get port profile binding: %s" % str(e)) |
656 | + return pp_binding |
657 | + |
658 | + def create_pp_binding(self, tenant_id, net_id, pp_id, default): |
659 | + ppbinding_dict = {} |
660 | + try: |
661 | + res = l2network_db.add_pp_binding(tenant_id, net_id, pp_id, \ |
662 | + default) |
663 | + LOG.debug("Created port profile binding: %s" % res.portprofile_id) |
664 | + ppbinding_dict["portprofile-id"] = str(res.portprofile_id) |
665 | + ppbinding_dict["net-id"] = str(res.network_id) |
666 | + ppbinding_dict["tenant-id"] = res.tenant_id |
667 | + ppbinding_dict["default"] = res.default |
668 | + return ppbinding_dict |
669 | + except Exception, e: |
670 | + LOG.error("Failed to create port profile binding: %s" % str(e)) |
671 | + |
672 | + def delete_pp_binding(self, pp_id): |
673 | + try: |
674 | + res = l2network_db.remove_pp_binding(pp_id) |
675 | + LOG.debug("Deleted port profile binding : %s" % res.portprofile_id) |
676 | + ppbinding_dict = {} |
677 | + ppbinding_dict["portprofile-id"] = str(res.portprofile_id) |
678 | + return ppbinding_dict |
679 | + except Exception, e: |
680 | + raise Exception("Failed to delete port profile: %s" % str(e)) |
681 | + |
682 | + def update_pp_binding(self, pp_id, tenant_id, net_id, default): |
683 | + try: |
684 | + res = l2network_db.update_pp_binding(pp_id, tenant_id, net_id,\ |
685 | + default) |
686 | + LOG.debug("Updating port profile binding: %s" % res.portprofile_id) |
687 | + ppbinding_dict = {} |
688 | + ppbinding_dict["portprofile-id"] = str(res.portprofile_id) |
689 | + ppbinding_dict["net-id"] = str(res.network_id) |
690 | + ppbinding_dict["tenant-id"] = res.tenant_id |
691 | + ppbinding_dict["default"] = res.default |
692 | + return ppbinding_dict |
693 | + except Exception, e: |
694 | + raise Exception("Failed to update portprofile binding:%s" % str(e)) |
695 | + |
696 | + |
697 | +class UcsDBTest(unittest.TestCase): |
698 | + def setUp(self): |
699 | + self.dbtest = UcsDB() |
700 | + LOG.debug("Setup") |
701 | + |
702 | + def testACreateUcsmBinding(self): |
703 | + binding1 = self.dbtest.create_ucsmbinding("1.2.3.4", "net1") |
704 | + self.assertTrue(binding1["ucsm-ip"] == "1.2.3.4") |
705 | + self.tearDownUcsmBinding() |
706 | + |
707 | + def testBGetAllUcsmBindings(self): |
708 | + binding1 = self.dbtest.create_ucsmbinding("1.2.3.4", "net1") |
709 | + binding2 = self.dbtest.create_ucsmbinding("2.3.4.5", "net1") |
710 | + bindings = self.dbtest.get_all_ucsmbindings() |
711 | + count = 0 |
712 | + for x in bindings: |
713 | + if "net" in x["network-id"]: |
714 | + count += 1 |
715 | + self.assertTrue(count == 2) |
716 | + self.tearDownUcsmBinding() |
717 | + |
718 | + def testCDeleteUcsmBinding(self): |
719 | + binding1 = self.dbtest.create_ucsmbinding("1.2.3.4", "net1") |
720 | + self.dbtest.delete_ucsmbinding(binding1["ucsm-ip"]) |
721 | + bindings = self.dbtest.get_all_ucsmbindings() |
722 | + count = 0 |
723 | + for x in bindings: |
724 | + if "net " in x["network-id"]: |
725 | + count += 1 |
726 | + self.assertTrue(count == 0) |
727 | + self.tearDownUcsmBinding() |
728 | + |
729 | + def testDUpdateUcsmBinding(self): |
730 | + binding1 = self.dbtest.create_ucsmbinding("1.2.3.4", "net1") |
731 | + binding1 = self.dbtest.update_ucsmbinding(binding1["ucsm-ip"], \ |
732 | + "newnet1") |
733 | + bindings = self.dbtest.get_all_ucsmbindings() |
734 | + count = 0 |
735 | + for x in bindings: |
736 | + if "new" in x["network-id"]: |
737 | + count += 1 |
738 | + self.assertTrue(count == 1) |
739 | + self.tearDownUcsmBinding() |
740 | + |
741 | + def testECreateDynamicVnic(self): |
742 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
743 | + "9.8.7.6") |
744 | + vnic1 = self.dbtest.create_dynamicvnic("eth1", blade1["blade-id"]) |
745 | + self.assertTrue(vnic1["device-name"] == "eth1") |
746 | + self.tearDownDyanmicVnic() |
747 | + |
748 | + def testFGetAllDyanmicVnics(self): |
749 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
750 | + "9.8.7.6") |
751 | + vnic1 = self.dbtest.create_dynamicvnic("eth1", blade1["blade-id"]) |
752 | + vnic2 = self.dbtest.create_dynamicvnic("eth2", blade1["blade-id"]) |
753 | + vnics = self.dbtest.get_all_dynamicvnics() |
754 | + count = 0 |
755 | + for x in vnics: |
756 | + if "eth" in x["device-name"]: |
757 | + count += 1 |
758 | + self.assertTrue(count == 2) |
759 | + self.tearDownDyanmicVnic() |
760 | + |
761 | + def testGDeleteDyanmicVnic(self): |
762 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
763 | + "9.8.7.6") |
764 | + vnic1 = self.dbtest.create_dynamicvnic("eth1", blade1["blade-id"]) |
765 | + self.dbtest.delete_dynamicvnic(vnic1["vnic-id"]) |
766 | + vnics = self.dbtest.get_all_dynamicvnics() |
767 | + count = 0 |
768 | + for x in vnics: |
769 | + if "eth " in x["device-name"]: |
770 | + count += 1 |
771 | + self.assertTrue(count == 0) |
772 | + self.tearDownDyanmicVnic() |
773 | + |
774 | + def testHUpdateDynamicVnic(self): |
775 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
776 | + "9.8.7.6") |
777 | + vnic1 = self.dbtest.create_dynamicvnic("eth1", blade1["blade-id"]) |
778 | + vnic1 = self.dbtest.update_dynamicvnic(vnic1["vnic-id"], "neweth1", \ |
779 | + "newblade2") |
780 | + vnics = self.dbtest.get_all_dynamicvnics() |
781 | + count = 0 |
782 | + for x in vnics: |
783 | + if "new" in x["device-name"]: |
784 | + count += 1 |
785 | + self.assertTrue(count == 1) |
786 | + self.tearDownDyanmicVnic() |
787 | + |
788 | + def testICreateUcsBlade(self): |
789 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
790 | + "9.8.7.6") |
791 | + self.assertTrue(blade1["mgmt-ip"] == "1.2.3.4") |
792 | + self.tearDownUcsBlade() |
793 | + |
794 | + def testJGetAllUcsBlade(self): |
795 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
796 | + "9.8.7.6") |
797 | + blade2 = self.dbtest.create_blade("2.3.4.5", "efgh", "chassis1", \ |
798 | + "9.8.7.6") |
799 | + blades = self.dbtest.get_all_blades() |
800 | + count = 0 |
801 | + for x in blades: |
802 | + if "chassis" in x["chassis-id"]: |
803 | + count += 1 |
804 | + self.assertTrue(count == 2) |
805 | + self.tearDownUcsBlade() |
806 | + |
807 | + def testKDeleteUcsBlade(self): |
808 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
809 | + "9.8.7.6") |
810 | + self.dbtest.delete_blade(blade1["blade-id"]) |
811 | + blades = self.dbtest.get_all_blades() |
812 | + count = 0 |
813 | + for x in blades: |
814 | + if "chassis " in x["chassis-id"]: |
815 | + count += 1 |
816 | + self.assertTrue(count == 0) |
817 | + self.tearDownUcsBlade() |
818 | + |
819 | + def testLUpdateUcsBlade(self): |
820 | + blade1 = self.dbtest.create_blade("1.2.3.4", "abcd", "chassis1", \ |
821 | + "9.8.7.6") |
822 | + blade2 = self.dbtest.update_blade(blade1["blade-id"], "2.3.4.5", \ |
823 | + "newabcd", "chassis1", "9.8.7.6") |
824 | + blades = self.dbtest.get_all_blades() |
825 | + count = 0 |
826 | + for x in blades: |
827 | + if "new" in x["mac-addr"]: |
828 | + count += 1 |
829 | + self.assertTrue(count == 1) |
830 | + self.tearDownUcsBlade() |
831 | + |
832 | + def testMCreatePortBinding(self): |
833 | + port_bind1 = self.dbtest.create_port_binding("port1", "dv1", "pp1", \ |
834 | + "vlan1", 10, "qos1") |
835 | + self.assertTrue(port_bind1["port-id"] == "port1") |
836 | + self.tearDownPortBinding() |
837 | + |
838 | + def testNGetAllPortBinding(self): |
839 | + port_bind1 = self.dbtest.create_port_binding("port1", "dv1", "pp1", \ |
840 | + "vlan1", 10, "qos1") |
841 | + port_bind2 = self.dbtest.create_port_binding("port2", "dv2", "pp2", \ |
842 | + "vlan2", 20, "qos2") |
843 | + port_bindings = self.dbtest.get_all_port_bindings() |
844 | + count = 0 |
845 | + for x in port_bindings: |
846 | + if "port" in x["port-id"]: |
847 | + count += 1 |
848 | + self.assertTrue(count == 2) |
849 | + self.tearDownPortBinding() |
850 | + |
851 | + def testODeletePortBinding(self): |
852 | + port_bind1 = self.dbtest.create_port_binding("port1", "dv1", "pp1", \ |
853 | + "vlan1", 10, "qos1") |
854 | + self.dbtest.delete_port_binding("port1") |
855 | + port_bindings = self.dbtest.get_all_port_bindings() |
856 | + count = 0 |
857 | + for x in port_bindings: |
858 | + if "port " in x["port-id"]: |
859 | + count += 1 |
860 | + self.assertTrue(count == 0) |
861 | + self.tearDownPortBinding() |
862 | + |
863 | + def testPUpdatePortBinding(self): |
864 | + port_bind1 = self.dbtest.create_port_binding("port1", "dv1", "pp1", \ |
865 | + "vlan1", 10, "qos1") |
866 | + port_bind1 = self.dbtest.update_port_binding("port1", "newdv1", \ |
867 | + "newpp1", "newvlan1", 11, "newqos1") |
868 | + port_bindings = self.dbtest.get_all_port_bindings() |
869 | + count = 0 |
870 | + for x in port_bindings: |
871 | + if "new" in x["dynamic-vnic-id"]: |
872 | + count += 1 |
873 | + self.assertTrue(count == 1) |
874 | + self.tearDownPortBinding() |
875 | + |
876 | + def tearDownUcsmBinding(self): |
877 | + print "Tearing Down Ucsm Bindings" |
878 | + binds = self.dbtest.get_all_ucsmbindings() |
879 | + for bind in binds: |
880 | + ip = bind["ucsm-ip"] |
881 | + self.dbtest.delete_ucsmbinding(ip) |
882 | + |
883 | + def tearDownDyanmicVnic(self): |
884 | + print "Tearing Down Dynamic Vnics" |
885 | + vnics = self.dbtest.get_all_dynamicvnics() |
886 | + for vnic in vnics: |
887 | + id = vnic["vnic-id"] |
888 | + self.dbtest.delete_dynamicvnic(id) |
889 | + self.tearDownUcsBlade() |
890 | + |
891 | + def tearDownUcsBlade(self): |
892 | + print "Tearing Down Blades" |
893 | + blades = self.dbtest.get_all_blades() |
894 | + for blade in blades: |
895 | + id = blade["blade-id"] |
896 | + self.dbtest.delete_blade(id) |
897 | + |
898 | + def tearDownPortBinding(self): |
899 | + print "Tearing Down Port Binding" |
900 | + port_bindings = self.dbtest.get_all_port_bindings() |
901 | + for port_binding in port_bindings: |
902 | + id = port_binding["port-id"] |
903 | + self.dbtest.delete_port_binding(id) |
904 | + |
905 | + |
906 | +class L2networkDBTest(unittest.TestCase): |
907 | + def setUp(self): |
908 | + self.dbtest = L2networkDB() |
909 | + LOG.debug("Setup") |
910 | + |
911 | + def testACreateVlanBinding(self): |
912 | + vlan1 = self.dbtest.create_vlan_binding(10, "vlan1", "netid1") |
913 | + self.assertTrue(vlan1["vlan-id"] == "10") |
914 | + self.tearDownVlanBinding() |
915 | + |
916 | + def testBGetAllVlanBindings(self): |
917 | + vlan1 = self.dbtest.create_vlan_binding(10, "vlan1", "netid1") |
918 | + vlan2 = self.dbtest.create_vlan_binding(20, "vlan2", "netid2") |
919 | + vlans = self.dbtest.get_all_vlan_bindings() |
920 | + count = 0 |
921 | + for x in vlans: |
922 | + if "netid" in x["net-id"]: |
923 | + count += 1 |
924 | + self.assertTrue(count == 2) |
925 | + self.tearDownVlanBinding() |
926 | + |
927 | + def testCDeleteVlanBinding(self): |
928 | + vlan1 = self.dbtest.create_vlan_binding(10, "vlan1", "netid1") |
929 | + self.dbtest.delete_vlan_binding("netid1") |
930 | + vlans = self.dbtest.get_all_vlan_bindings() |
931 | + count = 0 |
932 | + for x in vlans: |
933 | + if "netid " in x["net-id"]: |
934 | + count += 1 |
935 | + self.assertTrue(count == 0) |
936 | + self.tearDownVlanBinding() |
937 | + |
938 | + def testDUpdateVlanBinding(self): |
939 | + vlan1 = self.dbtest.create_vlan_binding(10, "vlan1", "netid1") |
940 | + vlan1 = self.dbtest.update_vlan_binding("netid1", 11, "newvlan1") |
941 | + vlans = self.dbtest.get_all_vlan_bindings() |
942 | + count = 0 |
943 | + for x in vlans: |
944 | + if "new" in x["vlan-name"]: |
945 | + count += 1 |
946 | + self.assertTrue(count == 1) |
947 | + self.tearDownVlanBinding() |
948 | + |
949 | + def testICreatePortProfile(self): |
950 | + pp1 = self.dbtest.create_portprofile("portprofile1", 10, "qos1") |
951 | + self.assertTrue(pp1["portprofile-name"] == "portprofile1") |
952 | + self.tearDownPortProfile() |
953 | + |
954 | + def testJGetAllPortProfile(self): |
955 | + pp1 = self.dbtest.create_portprofile("portprofile1", 10, "qos1") |
956 | + pp2 = self.dbtest.create_portprofile("portprofile2", 20, "qos2") |
957 | + pps = self.dbtest.get_all_portprofiles() |
958 | + count = 0 |
959 | + for x in pps: |
960 | + if "portprofile" in x["portprofile-name"]: |
961 | + count += 1 |
962 | + self.assertTrue(count == 2) |
963 | + self.tearDownPortProfile() |
964 | + |
965 | + def testKDeletePortProfile(self): |
966 | + pp1 = self.dbtest.create_portprofile("portprofile1", 10, "qos1") |
967 | + self.dbtest.delete_portprofile(pp1["portprofile-id"]) |
968 | + pps = self.dbtest.get_all_portprofiles() |
969 | + count = 0 |
970 | + for x in pps: |
971 | + if "portprofile " in x["portprofile-name"]: |
972 | + count += 1 |
973 | + self.assertTrue(count == 0) |
974 | + self.tearDownPortProfile() |
975 | + |
976 | + def testLUpdatePortProfile(self): |
977 | + pp1 = self.dbtest.create_portprofile("portprofile1", 10, "qos1") |
978 | + pp1 = self.dbtest.update_portprofile(pp1["portprofile-id"], \ |
979 | + "newportprofile1", 20, "qos2") |
980 | + pps = self.dbtest.get_all_portprofiles() |
981 | + count = 0 |
982 | + for x in pps: |
983 | + if "new" in x["portprofile-name"]: |
984 | + count += 1 |
985 | + self.assertTrue(count == 1) |
986 | + self.tearDownPortProfile() |
987 | + |
988 | + def testMCreatePortProfileBinding(self): |
989 | + pp_binding1 = self.dbtest.create_pp_binding("t1", "net1", \ |
990 | + "portprofile1", "0") |
991 | + self.assertTrue(pp_binding1["portprofile-id"] == "portprofile1") |
992 | + self.tearDownPortProfileBinding() |
993 | + |
994 | + def testNGetAllPortProfileBinding(self): |
995 | + pp_binding1 = self.dbtest.create_pp_binding("t1", "net1", \ |
996 | + "portprofile1", "0") |
997 | + pp_binding2 = self.dbtest.create_pp_binding("t2", "net2", \ |
998 | + "portprofile2", "0") |
999 | + pp_bindings = self.dbtest.get_all_pp_bindings() |
1000 | + count = 0 |
1001 | + for x in pp_bindings: |
1002 | + if "portprofile" in x["portprofile-id"]: |
1003 | + count += 1 |
1004 | + self.assertTrue(count == 2) |
1005 | + self.tearDownPortProfileBinding() |
1006 | + |
1007 | + def testODeletePortProfileBinding(self): |
1008 | + pp_binding1 = self.dbtest.create_pp_binding("t1", "net1", \ |
1009 | + "portprofile1", "0") |
1010 | + self.dbtest.delete_pp_binding(pp_binding1["portprofile-id"]) |
1011 | + pp_bindings = self.dbtest.get_all_pp_bindings() |
1012 | + count = 0 |
1013 | + for x in pp_bindings: |
1014 | + if "portprofile " in x["portprofile-id"]: |
1015 | + count += 1 |
1016 | + self.assertTrue(count == 0) |
1017 | + self.tearDownPortProfileBinding() |
1018 | + |
1019 | + def testPUpdatePortProfileBinding(self): |
1020 | + pp_binding1 = self.dbtest.create_pp_binding("t1", "net1", \ |
1021 | + "portprofile1", "0") |
1022 | + pp_binding1 = self.dbtest.update_pp_binding("portprofile1", \ |
1023 | + "newt1", "newnet1", "1") |
1024 | + pp_bindings = self.dbtest.get_all_pp_bindings() |
1025 | + count = 0 |
1026 | + for x in pp_bindings: |
1027 | + if "new" in x["net-id"]: |
1028 | + count += 1 |
1029 | + self.assertTrue(count == 1) |
1030 | + self.tearDownPortProfileBinding() |
1031 | + |
1032 | + def tearDownVlanBinding(self): |
1033 | + print "Tearing Down Vlan Binding" |
1034 | + vlans = self.dbtest.get_all_vlan_bindings() |
1035 | + for vlan in vlans: |
1036 | + id = vlan["net-id"] |
1037 | + self.dbtest.delete_vlan_binding(id) |
1038 | + |
1039 | + def tearDownPortProfile(self): |
1040 | + print "Tearing Down Port Profile" |
1041 | + pps = self.dbtest.get_all_portprofiles() |
1042 | + for pp in pps: |
1043 | + id = pp["portprofile-id"] |
1044 | + self.dbtest.delete_portprofile(id) |
1045 | + |
1046 | + def tearDownPortProfileBinding(self): |
1047 | + print "Tearing Down Port Profile Binding" |
1048 | + pp_bindings = self.dbtest.get_all_pp_bindings() |
1049 | + for pp_binding in pp_bindings: |
1050 | + id = pp_binding["portprofile-id"] |
1051 | + self.dbtest.delete_pp_binding(id) |
1052 | + |
1053 | +if __name__ == "__main__": |
1054 | + usagestr = "Usage: %prog [OPTIONS] <command> [args]" |
1055 | + parser = OptionParser(usage=usagestr) |
1056 | + parser.add_option("-v", "--verbose", dest="verbose", |
1057 | + action="store_true", default=False, help="turn on verbose logging") |
1058 | + |
1059 | + options, args = parser.parse_args() |
1060 | + |
1061 | + if options.verbose: |
1062 | + LOG.basicConfig(level=LOG.DEBUG) |
1063 | + else: |
1064 | + LOG.basicConfig(level=LOG.WARN) |
1065 | + |
1066 | + #load the models and db based on the 2nd level plugin argument |
1067 | + if args[0] == "ucs": |
1068 | + ucs_db = __import__("quantum.plugins.cisco.db.ucs_db", \ |
1069 | + fromlist=["ucs_db"]) |
1070 | + ucs_model = __import__("quantum.plugins.cisco.db.ucs_models", \ |
1071 | + fromlist=["ucs_models"]) |
1072 | + |
1073 | + db_conf() |
1074 | + |
1075 | + # Run the tests |
1076 | + suite = unittest.TestLoader().loadTestsFromTestCase(L2networkDBTest) |
1077 | + unittest.TextTestRunner(verbosity=2).run(suite) |
1078 | + suite = unittest.TestLoader().loadTestsFromTestCase(UcsDBTest) |
1079 | + unittest.TextTestRunner(verbosity=2).run(suite) |
1080 | |
1081 | === added file 'quantum/plugins/cisco/db/l2network_db.py' |
1082 | --- quantum/plugins/cisco/db/l2network_db.py 1970-01-01 00:00:00 +0000 |
1083 | +++ quantum/plugins/cisco/db/l2network_db.py 2011-08-01 03:39:28 +0000 |
1084 | @@ -0,0 +1,239 @@ |
1085 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
1086 | + |
1087 | +# Copyright 2011, Cisco Systems, Inc. |
1088 | +# |
1089 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
1090 | +# not use this file except in compliance with the License. You may obtain |
1091 | +# a copy of the License at |
1092 | +# |
1093 | +# http://www.apache.org/licenses/LICENSE-2.0 |
1094 | +# |
1095 | +# Unless required by applicable law or agreed to in writing, software |
1096 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
1097 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
1098 | +# License for the specific language governing permissions and limitations |
1099 | +# under the License. |
1100 | +# @author: Rohit Agarwalla, Cisco Systems, Inc. |
1101 | + |
1102 | +from sqlalchemy.orm import exc |
1103 | + |
1104 | +import l2network_models |
1105 | +import quantum.db.api as db |
1106 | +import quantum.db.models as models |
1107 | + |
1108 | + |
1109 | +def get_all_vlan_bindings(): |
1110 | + """Lists all the vlan to network associations""" |
1111 | + session = db.get_session() |
1112 | + try: |
1113 | + bindings = session.query(l2network_models.VlanBinding).\ |
1114 | + all() |
1115 | + return bindings |
1116 | + except exc.NoResultFound: |
1117 | + return [] |
1118 | + |
1119 | + |
1120 | +def get_vlan_binding(netid): |
1121 | + """Lists the vlan given a network_id""" |
1122 | + session = db.get_session() |
1123 | + try: |
1124 | + binding = session.query(l2network_models.VlanBinding).\ |
1125 | + filter_by(network_id=netid).\ |
1126 | + one() |
1127 | + return binding |
1128 | + except exc.NoResultFound: |
1129 | + raise Exception("No network found with net-id = %s" % network_id) |
1130 | + |
1131 | + |
1132 | +def add_vlan_binding(vlanid, vlanname, netid): |
1133 | + """Adds a vlan to network association""" |
1134 | + session = db.get_session() |
1135 | + try: |
1136 | + binding = session.query(l2network_models.VlanBinding).\ |
1137 | + filter_by(vlan_id=vlanid).\ |
1138 | + one() |
1139 | + raise Exception("Vlan with id \"%s\" already exists" % vlanid) |
1140 | + except exc.NoResultFound: |
1141 | + binding = l2network_models.VlanBinding(vlanid, vlanname, netid) |
1142 | + session.add(binding) |
1143 | + session.flush() |
1144 | + return binding |
1145 | + |
1146 | + |
1147 | +def remove_vlan_binding(netid): |
1148 | + """Removes a vlan to network association""" |
1149 | + session = db.get_session() |
1150 | + try: |
1151 | + binding = session.query(l2network_models.VlanBinding).\ |
1152 | + filter_by(network_id=netid).\ |
1153 | + one() |
1154 | + session.delete(binding) |
1155 | + session.flush() |
1156 | + return binding |
1157 | + except exc.NoResultFound: |
1158 | + pass |
1159 | + |
1160 | + |
1161 | +def update_vlan_binding(netid, newvlanid=None, newvlanname=None): |
1162 | + """Updates a vlan to network association""" |
1163 | + session = db.get_session() |
1164 | + try: |
1165 | + binding = session.query(l2network_models.VlanBinding).\ |
1166 | + filter_by(network_id=netid).\ |
1167 | + one() |
1168 | + if newvlanid: |
1169 | + binding.vlan_id = newvlanid |
1170 | + if newvlanname: |
1171 | + binding.vlan_name = newvlanname |
1172 | + session.merge(binding) |
1173 | + session.flush() |
1174 | + return binding |
1175 | + except exc.NoResultFound: |
1176 | + raise Exception("No vlan binding found with network_id = %s" % netid) |
1177 | + |
1178 | + |
1179 | +def get_all_portprofiles(): |
1180 | + """Lists all the port profiles""" |
1181 | + session = db.get_session() |
1182 | + try: |
1183 | + pps = session.query(l2network_models.PortProfile).\ |
1184 | + all() |
1185 | + return pps |
1186 | + except exc.NoResultFound: |
1187 | + return [] |
1188 | + |
1189 | + |
1190 | +def get_portprofile(ppid): |
1191 | + """Lists a port profile""" |
1192 | + session = db.get_session() |
1193 | + try: |
1194 | + pp = session.query(l2network_models.PortProfile).\ |
1195 | + filter_by(uuid=ppid).\ |
1196 | + one() |
1197 | + return pp |
1198 | + except exc.NoResultFound: |
1199 | + raise Exception("No portprofile found with id = %s" % ppid) |
1200 | + |
1201 | + |
1202 | +def add_portprofile(ppname, vlanid, qos): |
1203 | + """Adds a port profile""" |
1204 | + session = db.get_session() |
1205 | + try: |
1206 | + pp = session.query(l2network_models.PortProfile).\ |
1207 | + filter_by(name=ppname).\ |
1208 | + one() |
1209 | + raise Exception("Port profile with name %s already exists" % ppname) |
1210 | + except exc.NoResultFound: |
1211 | + pp = l2network_models.PortProfile(ppname, vlanid, qos) |
1212 | + session.add(pp) |
1213 | + session.flush() |
1214 | + return pp |
1215 | + |
1216 | + |
1217 | +def remove_portprofile(ppid): |
1218 | + """Removes a port profile""" |
1219 | + session = db.get_session() |
1220 | + try: |
1221 | + pp = session.query(l2network_models.PortProfile).\ |
1222 | + filter_by(uuid=ppid).\ |
1223 | + one() |
1224 | + session.delete(pp) |
1225 | + session.flush() |
1226 | + return pp |
1227 | + except exc.NoResultFound: |
1228 | + pass |
1229 | + |
1230 | + |
1231 | +def update_portprofile(ppid, newppname=None, newvlanid=None, newqos=None): |
1232 | + """Updates port profile""" |
1233 | + session = db.get_session() |
1234 | + try: |
1235 | + pp = session.query(l2network_models.PortProfile).\ |
1236 | + filter_by(uuid=ppid).\ |
1237 | + one() |
1238 | + if newppname: |
1239 | + pp.name = newppname |
1240 | + if newvlanid: |
1241 | + pp.vlan_id = newvlanid |
1242 | + if newqos: |
1243 | + pp.qos = newqos |
1244 | + session.merge(pp) |
1245 | + session.flush() |
1246 | + return pp |
1247 | + except exc.NoResultFound: |
1248 | + raise Exception("No port profile with id = %s" % ppid) |
1249 | + |
1250 | + |
1251 | +def get_all_pp_bindings(): |
1252 | + """Lists all the port profiles""" |
1253 | + session = db.get_session() |
1254 | + try: |
1255 | + bindings = session.query(l2network_models.PortProfileBinding).\ |
1256 | + all() |
1257 | + return bindings |
1258 | + except exc.NoResultFound: |
1259 | + return [] |
1260 | + |
1261 | + |
1262 | +def get_pp_binding(ppid): |
1263 | + """Lists a port profile binding""" |
1264 | + session = db.get_session() |
1265 | + try: |
1266 | + binding = session.query(l2network_models.PortProfileBinding).\ |
1267 | + filter_by(portprofile_id=ppid).\ |
1268 | + one() |
1269 | + return binding |
1270 | + except exc.NoResultFound: |
1271 | + raise Exception("No portprofile binding found with id = %s" % ppid) |
1272 | + |
1273 | + |
1274 | +def add_pp_binding(tenantid, networkid, ppid, default): |
1275 | + """Adds a port profile binding""" |
1276 | + session = db.get_session() |
1277 | + try: |
1278 | + binding = session.query(l2network_models.PortProfileBinding).\ |
1279 | + filter_by(portprofile_id=ppid).\ |
1280 | + one() |
1281 | + raise Exception("Port profile binding with id \"%s\" already \ |
1282 | + exists" % ppid) |
1283 | + except exc.NoResultFound: |
1284 | + binding = l2network_models.PortProfileBinding(tenantid, networkid, \ |
1285 | + ppid, default) |
1286 | + session.add(binding) |
1287 | + session.flush() |
1288 | + return binding |
1289 | + |
1290 | + |
1291 | +def remove_pp_binding(ppid): |
1292 | + """Removes a port profile binding""" |
1293 | + session = db.get_session() |
1294 | + try: |
1295 | + binding = session.query(l2network_models.PortProfileBinding).\ |
1296 | + filter_by(portprofile_id=ppid).\ |
1297 | + one() |
1298 | + session.delete(binding) |
1299 | + session.flush() |
1300 | + return binding |
1301 | + except exc.NoResultFound: |
1302 | + pass |
1303 | + |
1304 | + |
1305 | +def update_pp_binding(ppid, newtenantid=None, newnetworkid=None, \ |
1306 | + newdefault=None): |
1307 | + """Updates port profile binding""" |
1308 | + session = db.get_session() |
1309 | + try: |
1310 | + binding = session.query(l2network_models.PortProfileBinding).\ |
1311 | + filter_by(portprofile_id=ppid).\ |
1312 | + one() |
1313 | + if newtenantid: |
1314 | + binding.tenant_id = newtenantid |
1315 | + if newnetworkid: |
1316 | + binding.network_id = newnetworkid |
1317 | + if newdefault: |
1318 | + binding.default = newdefault |
1319 | + session.merge(binding) |
1320 | + session.flush() |
1321 | + return binding |
1322 | + except exc.NoResultFound: |
1323 | + raise Exception("No port profile binding with id = %s" % ppid) |
1324 | |
1325 | === added file 'quantum/plugins/cisco/db/l2network_models.py' |
1326 | --- quantum/plugins/cisco/db/l2network_models.py 1970-01-01 00:00:00 +0000 |
1327 | +++ quantum/plugins/cisco/db/l2network_models.py 2011-08-01 03:39:28 +0000 |
1328 | @@ -0,0 +1,86 @@ |
1329 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
1330 | + |
1331 | +# Copyright 2011, Cisco Systems, Inc. |
1332 | +# |
1333 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
1334 | +# not use this file except in compliance with the License. You may obtain |
1335 | +# a copy of the License at |
1336 | +# |
1337 | +# http://www.apache.org/licenses/LICENSE-2.0 |
1338 | +# |
1339 | +# Unless required by applicable law or agreed to in writing, software |
1340 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
1341 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
1342 | +# License for the specific language governing permissions and limitations |
1343 | +# under the License. |
1344 | +# @author: Rohit Agarwalla, Cisco Systems, Inc. |
1345 | + |
1346 | +import uuid |
1347 | + |
1348 | +from sqlalchemy import Column, Integer, String, ForeignKey, Boolean |
1349 | +from sqlalchemy.orm import relation |
1350 | + |
1351 | +from quantum.db.models import BASE |
1352 | + |
1353 | + |
1354 | +class VlanBinding(BASE): |
1355 | + """Represents a binding of vlan_id to network_id""" |
1356 | + __tablename__ = 'vlan_bindings' |
1357 | + |
1358 | + vlan_id = Column(Integer, primary_key=True) |
1359 | + vlan_name = Column(String(255)) |
1360 | + network_id = Column(String(255), nullable=False) |
1361 | + #foreign key to networks.uuid |
1362 | + |
1363 | + def __init__(self, vlan_id, vlan_name, network_id): |
1364 | + self.vlan_id = vlan_id |
1365 | + self.vlan_name = vlan_name |
1366 | + self.network_id = network_id |
1367 | + |
1368 | + def __repr__(self): |
1369 | + return "<VlanBinding(%d,%s,%s)>" % \ |
1370 | + (self.vlan_id, self.vlan_name, self.network_id) |
1371 | + |
1372 | + |
1373 | +class PortProfile(BASE): |
1374 | + """Represents Cisco plugin level PortProfile for a network""" |
1375 | + __tablename__ = 'portprofiles' |
1376 | + |
1377 | + uuid = Column(String(255), primary_key=True) |
1378 | + name = Column(String(255)) |
1379 | + vlan_id = Column(Integer) |
1380 | + qos = Column(String(255)) |
1381 | + |
1382 | + def __init__(self, name, vlan_id, qos=None): |
1383 | + self.uuid = uuid.uuid4() |
1384 | + self.name = name |
1385 | + self.vlan_id = vlan_id |
1386 | + self.qos = qos |
1387 | + |
1388 | + def __repr__(self): |
1389 | + return "<PortProfile(%s,%s,%d,%s)>" % \ |
1390 | + (self.uuid, self.name, self.vlan_id, self.qos) |
1391 | + |
1392 | + |
1393 | +class PortProfileBinding(BASE): |
1394 | + """Represents PortProfile binding to tenant and network""" |
1395 | + __tablename__ = 'portprofile_bindings' |
1396 | + |
1397 | + id = Column(Integer, primary_key=True, autoincrement=True) |
1398 | + tenant_id = Column(String(255)) |
1399 | + |
1400 | + network_id = Column(String(255), nullable=False) |
1401 | + #foreign key to networks.uuid |
1402 | + portprofile_id = Column(String(255), nullable=False) |
1403 | + #foreign key to portprofiles.uuid |
1404 | + default = Column(Boolean) |
1405 | + |
1406 | + def __init__(self, tenant_id, network_id, portprofile_id, default): |
1407 | + self.tenant_id = tenant_id |
1408 | + self.network_id = network_id |
1409 | + self.portprofile_id = portprofile_id |
1410 | + self.default = default |
1411 | + |
1412 | + def __repr__(self): |
1413 | + return "<PortProfile Binding(%s,%s,%s,%s)>" % \ |
1414 | + (self.tenant_id, self.network_id, self.portprofile_id, self.default) |
1415 | |
1416 | === added file 'quantum/plugins/cisco/db/ucs_db.py' |
1417 | --- quantum/plugins/cisco/db/ucs_db.py 1970-01-01 00:00:00 +0000 |
1418 | +++ quantum/plugins/cisco/db/ucs_db.py 2011-08-01 03:39:28 +0000 |
1419 | @@ -0,0 +1,314 @@ |
1420 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
1421 | + |
1422 | +# Copyright 2011, Cisco Systems, Inc. |
1423 | +# |
1424 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
1425 | +# not use this file except in compliance with the License. You may obtain |
1426 | +# a copy of the License at |
1427 | +# |
1428 | +# http://www.apache.org/licenses/LICENSE-2.0 |
1429 | +# |
1430 | +# Unless required by applicable law or agreed to in writing, software |
1431 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
1432 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
1433 | +# License for the specific language governing permissions and limitations |
1434 | +# under the License. |
1435 | +# @author: Rohit Agarwalla, Cisco Systems, Inc. |
1436 | + |
1437 | +from sqlalchemy.orm import exc |
1438 | + |
1439 | +import quantum.db.api as db |
1440 | +import ucs_models |
1441 | + |
1442 | + |
1443 | +def get_all_ucsmbinding(): |
1444 | + """Lists all the ucsm bindings""" |
1445 | + session = db.get_session() |
1446 | + try: |
1447 | + bindings = session.query(ucs_models.UcsmBinding).\ |
1448 | + all() |
1449 | + return bindings |
1450 | + except exc.NoResultFound: |
1451 | + return [] |
1452 | + |
1453 | + |
1454 | +def get_ucsmbinding(ucsm_ip): |
1455 | + """Lists a ucsm binding""" |
1456 | + session = db.get_session() |
1457 | + try: |
1458 | + binding = session.query(ucs_models.UcsmBinding).\ |
1459 | + filter_by(ucsm_ip=ucsm_ip).\ |
1460 | + one() |
1461 | + return binding |
1462 | + except exc.NoResultFound: |
1463 | + raise Exception("No binding found with ip = %s" % ucsm_ip) |
1464 | + |
1465 | + |
1466 | +def add_ucsmbinding(ucsm_ip, network_id): |
1467 | + """Adds a ucsm binding""" |
1468 | + session = db.get_session() |
1469 | + try: |
1470 | + ip = session.query(ucs_models.UcsmBinding).\ |
1471 | + filter_by(ucsm_ip=ucsm_ip).\ |
1472 | + one() |
1473 | + raise Exception("Binding with ucsm ip \"%s\" already exists" % ucsm_ip) |
1474 | + except exc.NoResultFound: |
1475 | + binding = ucs_models.UcsmBinding(ucsm_ip, network_id) |
1476 | + session.add(binding) |
1477 | + session.flush() |
1478 | + return binding |
1479 | + |
1480 | + |
1481 | +def remove_ucsmbinding(ucsm_ip): |
1482 | + """Removes a ucsm binding""" |
1483 | + session = db.get_session() |
1484 | + try: |
1485 | + binding = session.query(ucs_models.UcsmBinding).\ |
1486 | + filter_by(ucsm_ip=ucsm_ip).\ |
1487 | + one() |
1488 | + session.delete(binding) |
1489 | + session.flush() |
1490 | + return binding |
1491 | + except exc.NoResultFound: |
1492 | + pass |
1493 | + |
1494 | + |
1495 | +def update_ucsmbinding(ucsm_ip, new_network_id): |
1496 | + """Updates ucsm binding""" |
1497 | + session = db.get_session() |
1498 | + try: |
1499 | + binding = session.query(ucs_models.UcsmBinding).\ |
1500 | + filter_by(ucsm_ip=ucsm_ip).\ |
1501 | + one() |
1502 | + if new_network_id: |
1503 | + binding.network_id = new_network_id |
1504 | + session.merge(binding) |
1505 | + session.flush() |
1506 | + return binding |
1507 | + except exc.NoResultFound: |
1508 | + raise Exception("No binding with ip = %s" % ucsm_ip) |
1509 | + |
1510 | + |
1511 | +def get_all_dynamicvnics(): |
1512 | + """Lists all the dynamic vnics""" |
1513 | + session = db.get_session() |
1514 | + try: |
1515 | + vnics = session.query(ucs_models.DynamicVnic).\ |
1516 | + all() |
1517 | + return vnics |
1518 | + except exc.NoResultFound: |
1519 | + return [] |
1520 | + |
1521 | + |
1522 | +def get_dynamicvnic(vnic_id): |
1523 | + """Lists a dynamic vnic""" |
1524 | + session = db.get_session() |
1525 | + try: |
1526 | + vnic = session.query(ucs_models.DynamicVnic).\ |
1527 | + filter_by(uuid=vnic_id).\ |
1528 | + one() |
1529 | + return vnic |
1530 | + except exc.NoResultFound: |
1531 | + raise Exception("No dynamic vnic found with id = %s" % vnic_id) |
1532 | + |
1533 | + |
1534 | +def add_dynamicvnic(device_name, blade_id): |
1535 | + """Adds a dynamic vnic""" |
1536 | + session = db.get_session() |
1537 | + try: |
1538 | + name = session.query(ucs_models.DynamicVnic).\ |
1539 | + filter_by(device_name=device_name).\ |
1540 | + one() |
1541 | + raise Exception("Dynamic vnic with device name %s already exists" % \ |
1542 | + device_name) |
1543 | + except exc.NoResultFound: |
1544 | + vnic = ucs_models.DynamicVnic(device_name, blade_id) |
1545 | + session.add(vnic) |
1546 | + session.flush() |
1547 | + return vnic |
1548 | + |
1549 | + |
1550 | +def remove_dynamicvnic(vnic_id): |
1551 | + """Removes a dynamic vnic""" |
1552 | + session = db.get_session() |
1553 | + try: |
1554 | + vnic = session.query(ucs_models.DynamicVnic).\ |
1555 | + filter_by(uuid=vnic_id).\ |
1556 | + one() |
1557 | + session.delete(vnic) |
1558 | + session.flush() |
1559 | + return vnic |
1560 | + except exc.NoResultFound: |
1561 | + pass |
1562 | + |
1563 | + |
1564 | +def update_dynamicvnic(vnic_id, new_device_name=None, new_blade_id=None): |
1565 | + """Updates dynamic vnic""" |
1566 | + session = db.get_session() |
1567 | + try: |
1568 | + vnic = session.query(ucs_models.DynamicVnic).\ |
1569 | + filter_by(uuid=vnic_id).\ |
1570 | + one() |
1571 | + if new_device_name: |
1572 | + vnic.device_name = new_device_name |
1573 | + if new_blade_id: |
1574 | + vnic.blade_id = new_blade_id |
1575 | + session.merge(vnic) |
1576 | + session.flush() |
1577 | + return vnic |
1578 | + except exc.NoResultFound: |
1579 | + raise Exception("No dynamic vnic with id = %s" % vnic_id) |
1580 | + |
1581 | + |
1582 | +def get_all_blades(): |
1583 | + """Lists all the blades details""" |
1584 | + session = db.get_session() |
1585 | + try: |
1586 | + blades = session.query(ucs_models.UcsBlade).\ |
1587 | + all() |
1588 | + return blades |
1589 | + except exc.NoResultFound: |
1590 | + return [] |
1591 | + |
1592 | + |
1593 | +def get_blade(blade_id): |
1594 | + """Lists a blade details""" |
1595 | + session = db.get_session() |
1596 | + try: |
1597 | + blade = session.query(ucs_models.UcsBlade).\ |
1598 | + filter_by(uuid=blade_id).\ |
1599 | + one() |
1600 | + return blade |
1601 | + except exc.NoResultFound: |
1602 | + raise Exception("No blade found with id = %s" % blade_id) |
1603 | + |
1604 | + |
1605 | +def add_blade(mgmt_ip, mac_addr, chassis_id, ucsm_ip): |
1606 | + """Adds a blade""" |
1607 | + session = db.get_session() |
1608 | + try: |
1609 | + ip = session.query(ucs_models.UcsBlade).\ |
1610 | + filter_by(mgmt_ip=mgmt_ip).\ |
1611 | + one() |
1612 | + raise Exception("Blade with ip \"%s\" already exists" % mgmt_ip) |
1613 | + except exc.NoResultFound: |
1614 | + blade = ucs_models.UcsBlade(mgmt_ip, mac_addr, chassis_id, ucsm_ip) |
1615 | + session.add(blade) |
1616 | + session.flush() |
1617 | + return blade |
1618 | + |
1619 | + |
1620 | +def remove_blade(blade_id): |
1621 | + """Removes a blade""" |
1622 | + session = db.get_session() |
1623 | + try: |
1624 | + blade = session.query(ucs_models.UcsBlade).\ |
1625 | + filter_by(uuid=blade_id).\ |
1626 | + one() |
1627 | + session.delete(blade) |
1628 | + session.flush() |
1629 | + return blade |
1630 | + except exc.NoResultFound: |
1631 | + pass |
1632 | + |
1633 | + |
1634 | +def update_blade(blade_id, new_mgmt_ip=None, new_mac_addr=None, \ |
1635 | + new_chassis_id=None, new_ucsm_ip=None): |
1636 | + """Updates details of a blade""" |
1637 | + session = db.get_session() |
1638 | + try: |
1639 | + blade = session.query(ucs_models.UcsBlade).\ |
1640 | + filter_by(uuid=blade_id).\ |
1641 | + one() |
1642 | + if new_mgmt_ip: |
1643 | + blade.mgmt_ip = new_mgmt_ip |
1644 | + if new_mac_addr: |
1645 | + blade.mac_addr = new_mac_addr |
1646 | + if new_chassis_id: |
1647 | + blade.chassis_id = new_chassis_id |
1648 | + if new_ucsm_ip: |
1649 | + blade.ucsm_ip = new_ucsm_ip |
1650 | + session.merge(blade) |
1651 | + session.flush() |
1652 | + return blade |
1653 | + except exc.NoResultFound: |
1654 | + raise Exception("No blade with id = %s" % blade_id) |
1655 | + |
1656 | + |
1657 | +def get_all_portbindings(): |
1658 | + """Lists all the port bindings""" |
1659 | + session = db.get_session() |
1660 | + try: |
1661 | + port_bindings = session.query(ucs_models.PortBinding).\ |
1662 | + all() |
1663 | + return port_bindings |
1664 | + except exc.NoResultFound: |
1665 | + return [] |
1666 | + |
1667 | + |
1668 | +def get_portbinding(port_id): |
1669 | + """Lists a port binding""" |
1670 | + session = db.get_session() |
1671 | + try: |
1672 | + port_binding = session.query(ucs_models.PortBinding).\ |
1673 | + filter_by(port_id=port_id).\ |
1674 | + one() |
1675 | + return port_binding |
1676 | + except exc.NoResultFound: |
1677 | + raise Exception("No port binding found with port id = %s" % port_id) |
1678 | + |
1679 | + |
1680 | +def add_portbinding(port_id, dynamic_vnic_id, portprofile_name, \ |
1681 | + vlan_name, vlan_id, qos): |
1682 | + """Adds a port binding""" |
1683 | + session = db.get_session() |
1684 | + try: |
1685 | + port_binding = session.query(ucs_models.PortBinding).\ |
1686 | + filter_by(port_id=port_id).\ |
1687 | + one() |
1688 | + raise Exception("Port Binding with portid %s already exists" % port_id) |
1689 | + except exc.NoResultFound: |
1690 | + port_binding = ucs_models.PortBinding(port_id, dynamic_vnic_id, \ |
1691 | + portprofile_name, vlan_name, vlan_id, qos) |
1692 | + session.add(port_binding) |
1693 | + session.flush() |
1694 | + return port_binding |
1695 | + |
1696 | + |
1697 | +def remove_portbinding(port_id): |
1698 | + """Removes a port binding""" |
1699 | + session = db.get_session() |
1700 | + try: |
1701 | + port_binding = session.query(ucs_models.PortBinding).\ |
1702 | + filter_by(port_id=port_id).\ |
1703 | + one() |
1704 | + session.delete(port_binding) |
1705 | + session.flush() |
1706 | + return port_binding |
1707 | + except exc.NoResultFound: |
1708 | + pass |
1709 | + |
1710 | + |
1711 | +def update_portbinding(port_id, dynamic_vnic_id=None, portprofile_name=None, \ |
1712 | + vlan_name=None, vlan_id=None, qos=None): |
1713 | + """Updates port binding""" |
1714 | + session = db.get_session() |
1715 | + try: |
1716 | + port_binding = session.query(ucs_models.PortBinding).\ |
1717 | + filter_by(port_id=port_id).\ |
1718 | + one() |
1719 | + if dynamic_vnic_id: |
1720 | + port_binding.dynamic_vnic_id = dynamic_vnic_id |
1721 | + if portprofile_name: |
1722 | + port_binding.portprofile_name = portprofile_name |
1723 | + if vlan_name: |
1724 | + port_binding.vlan_name = vlan_name |
1725 | + if vlan_name: |
1726 | + port_binding.vlan_id = vlan_id |
1727 | + if qos: |
1728 | + port_binding.qos = qos |
1729 | + session.merge(port_binding) |
1730 | + session.flush() |
1731 | + return port_binding |
1732 | + except exc.NoResultFound: |
1733 | + raise Exception("No port binding with port id = %s" % port_id) |
1734 | |
1735 | === added file 'quantum/plugins/cisco/db/ucs_models.py' |
1736 | --- quantum/plugins/cisco/db/ucs_models.py 1970-01-01 00:00:00 +0000 |
1737 | +++ quantum/plugins/cisco/db/ucs_models.py 2011-08-01 03:39:28 +0000 |
1738 | @@ -0,0 +1,113 @@ |
1739 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
1740 | + |
1741 | +# Copyright 2011, Cisco Systems, Inc. |
1742 | +# |
1743 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
1744 | +# not use this file except in compliance with the License. You may obtain |
1745 | +# a copy of the License at |
1746 | +# |
1747 | +# http://www.apache.org/licenses/LICENSE-2.0 |
1748 | +# |
1749 | +# Unless required by applicable law or agreed to in writing, software |
1750 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
1751 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
1752 | +# License for the specific language governing permissions and limitations |
1753 | +# under the License. |
1754 | +# @author: Rohit Agarwalla, Cisco Systems, Inc. |
1755 | + |
1756 | +import uuid |
1757 | + |
1758 | +from sqlalchemy import Column, Integer, String, ForeignKey, Boolean |
1759 | +from sqlalchemy.orm import relation |
1760 | + |
1761 | +from quantum.db.models import BASE |
1762 | + |
1763 | + |
1764 | +class UcsmBinding(BASE): |
1765 | + """Represents a binding of ucsm to network_id""" |
1766 | + __tablename__ = 'ucsm_bindings' |
1767 | + |
1768 | + id = Column(Integer, primary_key=True, autoincrement=True) |
1769 | + ucsm_ip = Column(String(255)) |
1770 | + network_id = Column(String(255), nullable=False) |
1771 | + #foreign key to networks.uuid |
1772 | + |
1773 | + def __init__(self, ucsm_ip, network_id): |
1774 | + self.ucsm_ip = ucsm_ip |
1775 | + self.network_id = network_id |
1776 | + |
1777 | + def __repr__(self): |
1778 | + return "<UcsmBinding(%s,%s)>" % \ |
1779 | + (self.ucsm_ip, self.network_id) |
1780 | + |
1781 | + |
1782 | +class DynamicVnic(BASE): |
1783 | + """Represents Cisco UCS Dynamic Vnics""" |
1784 | + __tablename__ = 'dynamic_vnics' |
1785 | + |
1786 | + uuid = Column(String(255), primary_key=True) |
1787 | + device_name = Column(String(255)) |
1788 | + blade_id = Column(String(255), ForeignKey("ucs_blades.uuid"), \ |
1789 | + nullable=False) |
1790 | + |
1791 | + def __init__(self, device_name, blade_id): |
1792 | + self.uuid = uuid.uuid4() |
1793 | + self.device_name = device_name |
1794 | + self.blade_id = blade_id |
1795 | + |
1796 | + def __repr__(self): |
1797 | + return "<Dyanmic Vnic(%s,%s,%s)>" % \ |
1798 | + (self.uuid, self.device_name, self.blade_id) |
1799 | + |
1800 | + |
1801 | +class UcsBlade(BASE): |
1802 | + """Represents details of ucs blades""" |
1803 | + __tablename__ = 'ucs_blades' |
1804 | + |
1805 | + uuid = Column(String(255), primary_key=True) |
1806 | + mgmt_ip = Column(String(255)) |
1807 | + mac_addr = Column(String(255)) |
1808 | + chassis_id = Column(String(255)) |
1809 | + ucsm_ip = Column(String(255)) |
1810 | + dynamic_vnics = relation(DynamicVnic, order_by=DynamicVnic.uuid, \ |
1811 | + backref="blade") |
1812 | + |
1813 | + def __init__(self, mgmt_ip, mac_addr, chassis_id, ucsm_ip): |
1814 | + self.uuid = uuid.uuid4() |
1815 | + self.mgmt_ip = mgmt_ip |
1816 | + self.mac_addr = mac_addr |
1817 | + self.chassis_id = chassis_id |
1818 | + self.ucsm_ip = ucsm_ip |
1819 | + |
1820 | + def __repr__(self): |
1821 | + return "<UcsBlades (%s,%s,%s,%s,%s)>" % \ |
1822 | + (self.uuid, self.mgmt_ip, self.mac_addr, self.chassis_id, self.ucsm_ip) |
1823 | + |
1824 | + |
1825 | +class PortBinding(BASE): |
1826 | + """Represents Port binding to device interface""" |
1827 | + __tablename__ = 'port_bindings' |
1828 | + |
1829 | + id = Column(Integer, primary_key=True, autoincrement=True) |
1830 | + port_id = Column(String(255), nullable=False) |
1831 | + #foreign key to ports.uuid |
1832 | + dynamic_vnic_id = Column(String(255), nullable=False) |
1833 | + #foreign key to dynamic_vnics.uuid |
1834 | + portprofile_name = Column(String(255)) |
1835 | + vlan_name = Column(String(255)) |
1836 | + vlan_id = Column(Integer) |
1837 | + qos = Column(String(255)) |
1838 | + |
1839 | + def __init__(self, port_id, dynamic_vnic_id, portprofile_name, vlan_name, \ |
1840 | + vlan_id, qos): |
1841 | + self.port_id = port_id |
1842 | + self.dynamic_vnic_id = dynamic_vnic_id |
1843 | + self.portprofile_name = portprofile_name |
1844 | + self.vlan_name = vlan_name |
1845 | + self.vlan_id = vlan_id |
1846 | + self.qos = qos |
1847 | + |
1848 | + def __repr__(self): |
1849 | + return "<PortProfile Binding(%s,%s,%s,%s,%s,%s)>" % \ |
1850 | + (self.port_id, self.dynamic_vnic_id, self.portprofile_name, \ |
1851 | + self.vlan_name, self.vlan_id, self.qos) |
Thanks, great job! Looks mostly good to me. I ran the tests provided with this code and they executed fine. Just a few small fixes suggested (some are nits):
1. As discussed in a separate thread, please move the code to the new "db' directory.
2. The quantum component is following the convention of "getting" a logger component and logging to that (so that the logs get flagged with the name of the component). Can you please do this in the modules in which you are writing to the logs? This is how to do it:
from quantum. plugins. cisco.common import cisco_constants as const
LOG.basicConfig (level= LOG.WARN) const.LOGGER_ COMPONENT_ NAME)
LOG.getLogger(
3. The requirement for the creation of "cisco_naas" DB should be documented in the README file. Please also provide instructions on how to create it.
4. Specifically on the name "cisco_naas", Ram earlier had the suggestion that it be called something else. I would definitely recommend getting away from "naas" since we are not using it any more. Maybe call it "quantum_l2network" since we are calling our plugin "l2network-plugin".
5. The nova coding guidelines require that imports be arranged in the alphabetical order. The following order does not seem correct to me, please check this (and other places as well):
+import ConfigParser
33 +import os
34 +import logging as LOG
35 +import unittest
36 +from optparse import OptionParser
More info on imports order can be found in the HACKING file in the nova branch.