1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Cisco Systems, Inc. All rights reserved.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
# @author: Debojyoti Dutta, Cisco Systems, Inc.
18
# @author: Edgar Magana, Cisco Systems Inc.
21
Implements a Nexus-OS NETCONF over SSHv2 API Client
28
from quantum.plugins.cisco.common import cisco_configuration as conf
29
from quantum.plugins.cisco.common import cisco_constants as const
30
from quantum.plugins.cisco.common import cisco_exceptions as cexc
32
from ncclient import manager
34
LOG.basicConfig(level=LOG.WARN)
35
LOG.getLogger(const.LOGGER_COMPONENT_NAME)
38
# The following are standard strings, messages used to communicate with Nexus,
39
#only place holder values change for each message
40
exec_conf_prefix = """
41
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
42
<configure xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
43
<__XML__MODE__exec_configure>
47
exec_conf_postfix = """
48
</__XML__MODE__exec_configure>
54
cmd_vlan_conf_snippet = """
56
<vlan-id-create-delete>
57
<__XML__PARAM_value>%s</__XML__PARAM_value>
60
<vlan-name>%s</vlan-name>
63
<vstate>active</vstate>
69
</vlan-id-create-delete>
73
cmd_no_vlan_conf_snippet = """
76
<vlan-id-create-delete>
77
<__XML__PARAM_value>%s</__XML__PARAM_value>
78
</vlan-id-create-delete>
83
cmd_vlan_int_snippet = """
86
<interface>%s</interface>
87
<__XML__MODE_if-ethernet-switch>
88
<switchport></switchport>
93
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
94
<allow-vlans>%s</allow-vlans>
95
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
100
</__XML__MODE_if-ethernet-switch>
108
<interface>%s</interface>
109
<__XML__MODE_if-ethernet-switch>
110
<switchport></switchport>
117
</__XML__MODE_if-ethernet-switch>
122
cmd_no_switchport = """
125
<interface>%s</interface>
126
<__XML__MODE_if-ethernet-switch>
131
</__XML__MODE_if-ethernet-switch>
137
cmd_no_vlan_int_snippet = """
140
<interface>%s</interface>
141
<__XML__MODE_if-ethernet-switch>
142
<switchport></switchport>
148
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
149
<allow-vlans>%s</allow-vlans>
150
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
156
</__XML__MODE_if-ethernet-switch>
162
filter_show_vlan_brief_snippet = """
163
<show xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
170
class CiscoNEXUSDriver():
175
def nxos_connect(self, nexus_host, port, nexus_user, nexus_password):
176
m = manager.connect(host=nexus_host, port=22, username=nexus_user,
177
password=nexus_password)
180
def enable_vlan(self, mgr, vlanid, vlanname):
181
confstr = cmd_vlan_conf_snippet % (vlanid, vlanname)
182
confstr = exec_conf_prefix + confstr + exec_conf_postfix
183
mgr.edit_config(target='running', config=confstr)
185
def disable_vlan(self, mgr, vlanid):
186
confstr = cmd_no_vlan_conf_snippet % vlanid
187
confstr = exec_conf_prefix + confstr + exec_conf_postfix
188
mgr.edit_config(target='running', config=confstr)
190
def enable_port_trunk(self, mgr, interface):
191
confstr = cmd_port_trunk % (interface)
192
confstr = exec_conf_prefix + confstr + exec_conf_postfix
194
mgr.edit_config(target='running', config=confstr)
196
def enable_vlan_on_trunk_int(self, mgr, interface, vlanid):
197
confstr = cmd_vlan_int_snippet % (interface, vlanid)
198
confstr = exec_conf_prefix + confstr + exec_conf_postfix
200
mgr.edit_config(target='running', config=confstr)
202
def disable_vlan_on_trunk_int(self, mgr, interface, vlanid):
203
confstr = cmd_no_vlan_int_snippet % (interface, vlanid)
204
confstr = exec_conf_prefix + confstr + exec_conf_postfix
206
mgr.edit_config(target='running', config=confstr)
208
def test_nxos_api(self, host, user, password):
209
with self.nxos_connect(host, port=22, user=user,
210
password=password) as m:
211
#enable_vlan(m, '100', 'ccn1')
212
#enable_vlan_on_trunk_int(m, '2/1', '100')
213
#disable_vlan_on_trunk_int(m, '2/1', '100')
214
#disable_vlan(m, '100')
215
result = m.get(("subtree", filter_show_vlan_brief_snippet))
218
def create_vlan(self, vlan_name, vlan_id, nexus_host, nexus_user,
219
nexus_password, nexus_interface):
220
#TODO (Edgar) Move the SSH port to the configuration file
221
with self.nxos_connect(nexus_host, 22, nexus_user,
222
nexus_password) as m:
223
self.enable_vlan(m, vlan_id, vlan_name)
224
self.enable_port_trunk(m, nexus_interface)
226
def delete_vlan(self, vlan_id, nexus_host, nexus_user, nexus_password):
227
with self.nxos_connect(nexus_host, 22, nexus_user,
228
nexus_password) as m:
229
self.disable_vlan(m, vlan_id)
233
client = CiscoNEXUSDriver()
235
if __name__ == '__main__':