From f1dc7c0b40236a5606543fd1f9b5a96b5d268301 Mon Sep 17 00:00:00 2001 From: "eperdomo@cisco.com" <> Date: Mon, 15 Aug 2011 14:50:55 -0700 Subject: [PATCH] Adding a new file with all the XML snippets to make code easier to read Moving the Nexus SSH server port to the configuration file Removing main functions Making some changes based on Dan and Salvatore reviews --- quantum/plugins/cisco/README | 4 +- quantum/plugins/cisco/conf/nexus.ini | 2 + .../cisco/nexus/cisco_nexus_configuration.py | 1 + .../cisco/nexus/cisco_nexus_network_driver.py | 198 +++--------------- .../plugins/cisco/nexus/cisco_nexus_plugin.py | 7 +- .../cisco/nexus/cisco_nexus_snippets.py | 161 ++++++++++++++ 6 files changed, 197 insertions(+), 176 deletions(-) create mode 100644 quantum/plugins/cisco/nexus/cisco_nexus_snippets.py diff --git a/quantum/plugins/cisco/README b/quantum/plugins/cisco/README index f67c453f8..aa8a2880e 100755 --- a/quantum/plugins/cisco/README +++ b/quantum/plugins/cisco/README @@ -2,7 +2,7 @@ README: Quantum L2 Network Plugin Framework ============================================ -:Author: Sumit Naiksatam, Ram Durairaj, Mark Voelker, Edgar Magana, Shweta Padubidri, Rohit Agarwalla, Ying Liu +:Author: Sumit Naiksatam, Ram Durairaj, Mark Voelker, Edgar Magana, Shweta Padubidri, Rohit Agarwalla, Ying Liu, Debo Dutta :Contact: netstack@lists.launchpad.net :Web site: https://launchpad.net/~cisco-openstack :Copyright: 2011 Cisco Systems, Inc. @@ -125,6 +125,8 @@ nexus_ip_address=10.0.0.1 # Port number on the Nexus switch to which the UCSM 6120 is connected # Use shortened interface syntax, e.g. "3/23" not "Ethernet3/23". nexus_port=3/23 +#Port number where the SSH will be running at Nexus Switch, e.g.: 22 (Default) +nexus_ssh_port=22 [DRIVER] name=quantum.plugins.cisco.nexus.cisco_nexus_network_driver.CiscoNEXUSDriver diff --git a/quantum/plugins/cisco/conf/nexus.ini b/quantum/plugins/cisco/conf/nexus.ini index fede46eb8..a4efcb206 100644 --- a/quantum/plugins/cisco/conf/nexus.ini +++ b/quantum/plugins/cisco/conf/nexus.ini @@ -3,6 +3,8 @@ nexus_ip_address= #Port number of the Interface connected from the Nexus 7K Switch to UCSM 6120, e.g.: 3/23 nexus_port= +#Port number where the SSH will be running at the Nexus Switch, e.g.: 22 (Default) +nexus_ssh_port=22 [DRIVER] name=quantum.plugins.cisco.nexus.cisco_nexus_network_driver.CiscoNEXUSDriver diff --git a/quantum/plugins/cisco/nexus/cisco_nexus_configuration.py b/quantum/plugins/cisco/nexus/cisco_nexus_configuration.py index f1ad14902..b8987a5ce 100644 --- a/quantum/plugins/cisco/nexus/cisco_nexus_configuration.py +++ b/quantum/plugins/cisco/nexus/cisco_nexus_configuration.py @@ -30,6 +30,7 @@ cp = confp.CiscoConfigParser(os.path.dirname(os.path.realpath(__file__)) \ section = cp['SWITCH'] NEXUS_IP_ADDRESS = section['nexus_ip_address'] NEXUS_PORT = section['nexus_port'] +NEXUS_SSH_PORT = section['nexus_ssh_port'] section = cp['DRIVER'] NEXUS_DRIVER = section['name'] diff --git a/quantum/plugins/cisco/nexus/cisco_nexus_network_driver.py b/quantum/plugins/cisco/nexus/cisco_nexus_network_driver.py index 78090d5f9..f0c16274f 100644 --- a/quantum/plugins/cisco/nexus/cisco_nexus_network_driver.py +++ b/quantum/plugins/cisco/nexus/cisco_nexus_network_driver.py @@ -27,6 +27,7 @@ import subprocess from quantum.plugins.cisco.common import cisco_constants as const from quantum.plugins.cisco.common import cisco_exceptions as cexc +from quantum.plugins.cisco.nexus import cisco_nexus_snippets as snipp from ncclient import manager @@ -34,210 +35,61 @@ LOG.basicConfig(level=LOG.WARN) LOG.getLogger(const.LOGGER_COMPONENT_NAME) -# The following are standard strings, messages used to communicate with Nexus, -#only place holder values change for each message -exec_conf_prefix = """ - - - <__XML__MODE__exec_configure> -""" - - -exec_conf_postfix = """ - - - -""" - - -cmd_vlan_conf_snippet = """ - - - <__XML__PARAM_value>%s - <__XML__MODE_vlan> - - %s - - - active - - - - - - - -""" - -cmd_no_vlan_conf_snippet = """ - - - - <__XML__PARAM_value>%s - - - -""" - -cmd_vlan_int_snippet = """ - - - %s - <__XML__MODE_if-ethernet-switch> - - - - - - <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> - %s - - - - - - - - -""" - -cmd_port_trunk = """ - - - %s - <__XML__MODE_if-ethernet-switch> - - - - - - - - - - -""" - -cmd_no_switchport = """ - - - %s - <__XML__MODE_if-ethernet-switch> - - - - - - - -""" - - -cmd_no_vlan_int_snippet = """ - - - %s - <__XML__MODE_if-ethernet-switch> - - - - - - - <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> - %s - - - - - - - - - -""" - - -filter_show_vlan_brief_snippet = """ - - - - - """ - - class CiscoNEXUSDriver(): def __init__(self): pass - def nxos_connect(self, nexus_host, port, nexus_user, nexus_password): - m = manager.connect(host=nexus_host, port=22, username=nexus_user, - password=nexus_password) + def nxos_connect(self, nexus_host, nexus_ssh_port, nexus_user, + nexus_password): + m = manager.connect(host=nexus_host, port=nexus_ssh_port, + username=nexus_user, password=nexus_password) return m def enable_vlan(self, mgr, vlanid, vlanname): - confstr = cmd_vlan_conf_snippet % (vlanid, vlanname) - confstr = exec_conf_prefix + confstr + exec_conf_postfix + confstr = snipp.cmd_vlan_conf_snippet % (vlanid, vlanname) + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix mgr.edit_config(target='running', config=confstr) def disable_vlan(self, mgr, vlanid): - confstr = cmd_no_vlan_conf_snippet % vlanid - confstr = exec_conf_prefix + confstr + exec_conf_postfix + confstr = snipp.cmd_no_vlan_conf_snippet % vlanid + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix mgr.edit_config(target='running', config=confstr) def enable_port_trunk(self, mgr, interface): - confstr = cmd_port_trunk % (interface) - confstr = exec_conf_prefix + confstr + exec_conf_postfix - print confstr + confstr = snipp.cmd_port_trunk % (interface) + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix + LOG.debug("NexusDriver: %s" % confstr) mgr.edit_config(target='running', config=confstr) def disable_switch_port(self, mgr, interface): - confstr = cmd_no_switchport % (interface) - confstr = exec_conf_prefix + confstr + exec_conf_postfix - print confstr + confstr = snipp.cmd_no_switchport % (interface) + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix + LOG.debug("NexusDriver: %s" % confstr) mgr.edit_config(target='running', config=confstr) def enable_vlan_on_trunk_int(self, mgr, interface, vlanid): - confstr = cmd_vlan_int_snippet % (interface, vlanid) - confstr = exec_conf_prefix + confstr + exec_conf_postfix - print confstr + confstr = snipp.cmd_vlan_int_snippet % (interface, vlanid) + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix + LOG.debug("NexusDriver: %s" % confstr) mgr.edit_config(target='running', config=confstr) def disable_vlan_on_trunk_int(self, mgr, interface, vlanid): - confstr = cmd_no_vlan_int_snippet % (interface, vlanid) - confstr = exec_conf_prefix + confstr + exec_conf_postfix - print confstr + confstr = snipp.cmd_no_vlan_int_snippet % (interface, vlanid) + confstr = snipp.exec_conf_prefix + confstr + snipp.exec_conf_postfix + LOG.debug("NexusDriver: %s" % confstr) mgr.edit_config(target='running', config=confstr) - def test_nxos_api(self, host, user, password): - with self.nxos_connect(host, port=22, user=user, - password=password) as m: - #enable_vlan(m, '100', 'ccn1') - #enable_vlan_on_trunk_int(m, '2/1', '100') - #disable_vlan_on_trunk_int(m, '2/1', '100') - #disable_vlan(m, '100') - result = m.get(("subtree", filter_show_vlan_brief_snippet)) - print result - def create_vlan(self, vlan_name, vlan_id, nexus_host, nexus_user, - nexus_password, nexus_interface): - #TODO (Edgar) Move the SSH port to the configuration file - with self.nxos_connect(nexus_host, 22, nexus_user, + nexus_password, nexus_interface, nexus_ssh_port): + with self.nxos_connect(nexus_host, int(nexus_ssh_port), nexus_user, nexus_password) as m: self.enable_vlan(m, vlan_id, vlan_name) self.enable_vlan_on_trunk_int(m, nexus_interface, vlan_id) def delete_vlan(self, vlan_id, nexus_host, nexus_user, - nexus_password, nexus_interface): - with self.nxos_connect(nexus_host, 22, nexus_user, + nexus_password, nexus_interface, nexus_ssh_port): + with self.nxos_connect(nexus_host, int(nexus_ssh_port), nexus_user, nexus_password) as m: self.disable_vlan(m, vlan_id) self.disable_switch_port(m, nexus_interface) - - -def main(): - client = CiscoNEXUSDriver() - -if __name__ == '__main__': - main() diff --git a/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py b/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py index fdb4ef446..e27269ed6 100644 --- a/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py +++ b/quantum/plugins/cisco/nexus/cisco_nexus_plugin.py @@ -43,6 +43,7 @@ class NexusPlugin(L2DevicePluginBase): self._nexus_username = cred.Store.getUsername(conf.NEXUS_IP_ADDRESS) self._nexus_password = cred.Store.getPassword(conf.NEXUS_IP_ADDRESS) self._nexus_port = conf.NEXUS_PORT + self._nexus_ssh_port = conf.NEXUS_SSH_PORT def get_all_networks(self, tenant_id): """ @@ -61,7 +62,8 @@ class NexusPlugin(L2DevicePluginBase): """ LOG.debug("NexusPlugin:create_network() called\n") self._client.create_vlan(vlan_name, str(vlan_id), self._nexus_ip, - self._nexus_username, self._nexus_password, self._nexus_port) + self._nexus_username, self._nexus_password, self._nexus_port, + self._nexus_ssh_port) new_net_dict = {const.NET_ID: net_id, const.NET_NAME: net_name, @@ -81,7 +83,8 @@ class NexusPlugin(L2DevicePluginBase): vlan_id = self._get_vlan_id_for_network(tenant_id, net_id) if net: self._client.delete_vlan(str(vlan_id), self._nexus_ip, - self._nexus_username, self._nexus_password, self._nexus_port) + self._nexus_username, self._nexus_password, self._nexus_port, + self._nexus_ssh_port) self._networks.pop(net_id) return net # Network not found diff --git a/quantum/plugins/cisco/nexus/cisco_nexus_snippets.py b/quantum/plugins/cisco/nexus/cisco_nexus_snippets.py new file mode 100644 index 000000000..a3bc004d2 --- /dev/null +++ b/quantum/plugins/cisco/nexus/cisco_nexus_snippets.py @@ -0,0 +1,161 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2011 Cisco Systems, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# @author: Edgar Magana, Cisco Systems, Inc. + +""" +Nexus-OS XML-based configuration snippets +""" + +import logging as LOG +import string +import subprocess + +from quantum.plugins.cisco.common import cisco_constants as const + +LOG.basicConfig(level=LOG.WARN) +LOG.getLogger(const.LOGGER_COMPONENT_NAME) + + +# The following are standard strings, messages used to communicate with Nexus, +exec_conf_prefix = """ + + + <__XML__MODE__exec_configure> +""" + + +exec_conf_postfix = """ + + + +""" + + +cmd_vlan_conf_snippet = """ + + + <__XML__PARAM_value>%s + <__XML__MODE_vlan> + + %s + + + active + + + + + + + +""" + +cmd_no_vlan_conf_snippet = """ + + + + <__XML__PARAM_value>%s + + + +""" + +cmd_vlan_int_snippet = """ + + + %s + <__XML__MODE_if-ethernet-switch> + import logging as LOG + + + + + <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> + %s + + + + + + + + +""" + +cmd_port_trunk = """ + + + %s + <__XML__MODE_if-ethernet-switch> + + + + + + + + C: 1: Missing docstring + + +""" + +cmd_no_switchport = """ + + + %s + <__XML__MODE_if-ethernet-switch> + + + + + + + +""" + + +cmd_no_vlan_int_snippet = """ + + C: 1: Missing docstring + %s + <__XML__MODE_if-ethernet-switch> + + + + + + + <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans> + %s + + + + + + + + + +""" + + +filter_show_vlan_brief_snippet = """ + + + + + """ -- 2.45.2