~shweta-ap05/neutron/shweta-l2network-plugin-persistence

« back to all changes in this revision

Viewing changes to quantum/plugins/cisco/ucs/cisco_ucs_network_driver.py

Merging from Sumit's branch pylint fixes and incorporating review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
1
2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
#
3
4
# Copyright 2011 Cisco Systems, Inc.  All rights reserved.
17
18
# @author: Sumit Naiksatam, Cisco Systems Inc.
18
19
#
19
20
"""
 
21
 
 
22
"""
20
23
Implements a UCSM XML API Client
21
24
"""
22
25
 
23
26
import httplib
24
27
import logging as LOG
25
 
import string
26
 
import subprocess
27
28
from xml.etree import ElementTree as et
28
 
import urllib
29
29
 
30
30
from quantum.plugins.cisco.common import cisco_constants as const
31
31
from quantum.plugins.cisco.common import cisco_exceptions as cexc
114
114
 
115
115
 
116
116
class CiscoUCSMDriver():
 
117
    """UCSM Driver"""
117
118
 
118
119
    def __init__(self):
119
120
        pass
120
121
 
121
122
    def _post_data(self, ucsm_ip, ucsm_username, ucsm_password, data):
 
123
        """Send command to UCSM in http request"""
122
124
        conn = httplib.HTTPConnection(ucsm_ip)
123
125
        login_data = "<aaaLogin inName=\"" + ucsm_username + \
124
126
        "\" inPassword=\"" + ucsm_password + "\" />"
129
131
        LOG.debug(response.reason)
130
132
        LOG.debug(response_data)
131
133
        # TODO (Sumit): If login is not successful, throw exception
132
 
        xmlTree = et.XML(response_data)
133
 
        cookie = xmlTree.attrib["outCookie"]
 
134
        xml_tree = et.XML(response_data)
 
135
        cookie = xml_tree.attrib["outCookie"]
134
136
 
135
137
        data = data.replace(COOKIE_VALUE, cookie)
136
138
        LOG.debug("POST: %s" % data)
150
152
        LOG.debug(response_data)
151
153
 
152
154
    def _create_vlan_post_data(self, vlan_name, vlan_id):
 
155
        """Create command"""
153
156
        data = CREATE_VLAN.replace(VLAN_NAME, vlan_name)
154
157
        data = data.replace(VLAN_ID, vlan_id)
155
158
        return data
156
159
 
157
160
    def _create_profile_post_data(self, profile_name, vlan_name):
 
161
        """Create command"""
158
162
        data = CREATE_PROFILE.replace(PROFILE_NAME, profile_name)
159
163
        data = data.replace(VLAN_NAME, vlan_name)
160
164
        return data
161
165
 
162
 
    def _create_profile_client_post_data(self, profile_name,
 
166
    def _create_pclient_post_data(self, profile_name,
163
167
                                         profile_client_name):
 
168
        """Create command"""
164
169
        data = ASSOCIATE_PROFILE.replace(PROFILE_NAME, profile_name)
165
170
        data = data.replace(PROFILE_CLIENT, profile_client_name)
166
171
        return data
167
172
 
168
 
    def _change_vlan_in_profile_post_data(self, profile_name, old_vlan_name,
 
173
    def _change_vlaninprof_post_data(self, profile_name, old_vlan_name,
169
174
                                          new_vlan_name):
 
175
        """Create command"""
170
176
        data = CHANGE_VLAN_IN_PROFILE.replace(PROFILE_NAME, profile_name)
171
177
        data = data.replace(OLD_VLAN_NAME, old_vlan_name)
172
178
        data = data.replace(VLAN_NAME, new_vlan_name)
173
179
        return data
174
180
 
175
181
    def _delete_vlan_post_data(self, vlan_name):
 
182
        """Create command"""
176
183
        data = DELETE_VLAN.replace(VLAN_NAME, vlan_name)
177
184
        return data
178
185
 
179
186
    def _delete_profile_post_data(self, profile_name):
 
187
        """Create command"""
180
188
        data = DELETE_PROFILE.replace(PROFILE_NAME, profile_name)
181
189
        return data
182
190
 
183
191
    def _get_next_dynamic_nic(self):
 
192
        """Get an avaialble dynamic nic on the host"""
184
193
        dynamic_nic_id = gvif.get_next_dynic()
185
194
        if len(dynamic_nic_id) > 0:
186
195
            return dynamic_nic_id
187
196
        else:
188
 
            raise cisco_exceptions.NoMoreNics(net_id=net_id, port_id=port_id)
 
197
            raise cexc.NoMoreNics()
189
198
 
190
199
    def create_vlan(self, vlan_name, vlan_id, ucsm_ip, ucsm_username,
191
200
                    ucsm_password):
 
201
        """Create request for UCSM"""
192
202
        data = self._create_vlan_post_data(vlan_name, vlan_id)
193
203
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
194
204
 
195
205
    def create_profile(self, profile_name, vlan_name, ucsm_ip, ucsm_username,
196
206
                       ucsm_password):
 
207
        """Create request for UCSM"""
197
208
        data = self._create_profile_post_data(profile_name, vlan_name)
198
209
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
199
 
        data = self._create_profile_client_post_data(profile_name,
 
210
        data = self._create_pclient_post_data(profile_name,
200
211
                                                     profile_name[-16:])
201
212
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
202
213
 
203
214
    def change_vlan_in_profile(self, profile_name, old_vlan_name,
204
215
                               new_vlan_name, ucsm_ip, ucsm_username,
205
216
                               ucsm_password):
206
 
        data = self._change_vlan_in_profile_post_data(profile_name,
 
217
        """Create request for UCSM"""
 
218
        data = self._change_vlaninprof_post_data(profile_name,
207
219
                                                      old_vlan_name,
208
220
                                                      new_vlan_name)
209
221
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
210
222
 
211
223
    def get_dynamic_nic(self, host):
 
224
        """Get an avaialble dynamic nic on the host"""
212
225
        # TODO (Sumit): Check availability per host
213
226
        # TODO (Sumit): If not available raise exception
214
227
        # TODO (Sumit): This simple logic assumes that create-port and
222
235
        return dynamic_nic_name
223
236
 
224
237
    def delete_vlan(self, vlan_name, ucsm_ip, ucsm_username, ucsm_password):
 
238
        """Create request for UCSM"""
225
239
        data = self._delete_vlan_post_data(vlan_name)
226
240
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
227
241
 
228
242
    def delete_profile(self, profile_name, ucsm_ip, ucsm_username,
229
243
                       ucsm_password):
 
244
        """Create request for UCSM"""
230
245
        data = self._delete_profile_post_data(profile_name)
231
246
        self._post_data(ucsm_ip, ucsm_username, ucsm_password, data)
232
247
 
233
248
    def release_dynamic_nic(self, host):
 
249
        """Release a reserved dynamic nic on the host"""
234
250
        # TODO (Sumit): Release on a specific host
235
251
        pass
236
 
 
237
 
 
238
 
def main():
239
 
    client = CiscoUCSMDriver()
240
 
    #client.create_vlan("quantum-vlan-3", "3","172.20.231.27","admin",
241
 
    #                   "c3l12345")
242
 
    #client.create_profile("q-prof-3", "quantum-vlan-3","172.20.231.27",
243
 
    #                      "admin", "c3l12345")
244
 
    #client.get_dynamic_nic("dummy")
245
 
    #client.get_dynamic_nic("dummy")
246
 
    #client.release_dynamic_nic("dummy")
247
 
    print client.get_dynamic_nic("dummy")
248
 
    """
249
 
    client.change_vlan_in_profile("br100", "default", "test-2",
250
 
                                  "172.20.231.27","admin",
251
 
                                  "c3l12345")
252
 
    client.change_vlan_in_profile("br100", "test-2", "default",
253
 
                                  "172.20.231.27", "admin", "c3l12345")
254
 
    """
255
 
 
256
 
if __name__ == '__main__':
257
 
    main()