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_constants as const
29
from quantum.plugins.cisco.common import cisco_exceptions as cexc
31
from ncclient import manager
33
LOG.basicConfig(level=LOG.WARN)
34
LOG.getLogger(const.LOGGER_COMPONENT_NAME)
37
# The following are standard strings, messages used to communicate with Nexus,
38
#only place holder values change for each message
39
exec_conf_prefix = """
40
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
41
<configure xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
42
<__XML__MODE__exec_configure>
46
exec_conf_postfix = """
47
</__XML__MODE__exec_configure>
53
cmd_vlan_conf_snippet = """
55
<vlan-id-create-delete>
56
<__XML__PARAM_value>%s</__XML__PARAM_value>
59
<vlan-name>%s</vlan-name>
62
<vstate>active</vstate>
68
</vlan-id-create-delete>
72
cmd_no_vlan_conf_snippet = """
75
<vlan-id-create-delete>
76
<__XML__PARAM_value>%s</__XML__PARAM_value>
77
</vlan-id-create-delete>
82
cmd_vlan_int_snippet = """
85
<interface>%s</interface>
86
<__XML__MODE_if-ethernet-switch>
87
<switchport></switchport>
92
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
93
<allow-vlans>%s</allow-vlans>
94
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
99
</__XML__MODE_if-ethernet-switch>
107
<interface>%s</interface>
108
<__XML__MODE_if-ethernet-switch>
109
<switchport></switchport>
116
</__XML__MODE_if-ethernet-switch>
121
cmd_no_switchport = """
124
<interface>%s</interface>
125
<__XML__MODE_if-ethernet-switch>
130
</__XML__MODE_if-ethernet-switch>
136
cmd_no_vlan_int_snippet = """
139
<interface>%s</interface>
140
<__XML__MODE_if-ethernet-switch>
141
<switchport></switchport>
147
<__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
148
<allow-vlans>%s</allow-vlans>
149
</__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
155
</__XML__MODE_if-ethernet-switch>
161
filter_show_vlan_brief_snippet = """
162
<show xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
169
class CiscoNEXUSDriver():
174
def nxos_connect(self, nexus_host, port, nexus_user, nexus_password):
175
m = manager.connect(host=nexus_host, port=22, username=nexus_user,
176
password=nexus_password)
179
def enable_vlan(self, mgr, vlanid, vlanname):
180
confstr = cmd_vlan_conf_snippet % (vlanid, vlanname)
181
confstr = exec_conf_prefix + confstr + exec_conf_postfix
182
mgr.edit_config(target='running', config=confstr)
184
def disable_vlan(self, mgr, vlanid):
185
confstr = cmd_no_vlan_conf_snippet % vlanid
186
confstr = exec_conf_prefix + confstr + exec_conf_postfix
187
mgr.edit_config(target='running', config=confstr)
189
def enable_port_trunk(self, mgr, interface):
190
confstr = cmd_port_trunk % (interface)
191
confstr = exec_conf_prefix + confstr + exec_conf_postfix
193
mgr.edit_config(target='running', config=confstr)
195
def disable_switch_port(self, mgr, interface):
196
confstr = cmd_no_switchport % (interface)
197
confstr = exec_conf_prefix + confstr + exec_conf_postfix
199
mgr.edit_config(target='running', config=confstr)
201
def enable_vlan_on_trunk_int(self, mgr, interface, vlanid):
202
confstr = cmd_vlan_int_snippet % (interface, vlanid)
203
confstr = exec_conf_prefix + confstr + exec_conf_postfix
205
mgr.edit_config(target='running', config=confstr)
207
def disable_vlan_on_trunk_int(self, mgr, interface, vlanid):
208
confstr = cmd_no_vlan_int_snippet % (interface, vlanid)
209
confstr = exec_conf_prefix + confstr + exec_conf_postfix
211
mgr.edit_config(target='running', config=confstr)
213
def test_nxos_api(self, host, user, password):
214
with self.nxos_connect(host, port=22, user=user,
215
password=password) as m:
216
#enable_vlan(m, '100', 'ccn1')
217
#enable_vlan_on_trunk_int(m, '2/1', '100')
218
#disable_vlan_on_trunk_int(m, '2/1', '100')
219
#disable_vlan(m, '100')
220
result = m.get(("subtree", filter_show_vlan_brief_snippet))
223
def create_vlan(self, vlan_name, vlan_id, nexus_host, nexus_user,
224
nexus_password, nexus_interface):
225
#TODO (Edgar) Move the SSH port to the configuration file
226
with self.nxos_connect(nexus_host, 22, nexus_user,
227
nexus_password) as m:
228
self.enable_vlan(m, vlan_id, vlan_name)
229
self.enable_vlan_on_trunk_int(m, nexus_interface, vlan_id)
231
def delete_vlan(self, vlan_id, nexus_host, nexus_user,
232
nexus_password, nexus_interface):
233
with self.nxos_connect(nexus_host, 22, nexus_user,
234
nexus_password) as m:
235
self.disable_vlan(m, vlan_id)
236
self.disable_switch_port(m, nexus_interface)
240
client = CiscoNEXUSDriver()
242
if __name__ == '__main__':