~snaiksat/neutron/sumit-fix-profileclient

« back to all changes in this revision

Viewing changes to quantum/plugins/cisco/nexus/cisco_nexus_network_driver.py

  • Committer: Edgar Magana
  • Date: 2011-08-01 19:40:07 UTC
  • Revision ID: eperdomo@cisco.com-20110801194007-fgjogilaiihtwtgt
Adding the Nexus OS driver based on the new PlugIn structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
#
 
3
# Copyright 2011 Cisco Systems, Inc.  All rights reserved.
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
#
 
17
# @author: Debojyoti Dutta, Cisco Systems, Inc.
 
18
# @author: Edgar Magana, Cisco Systems Inc.
 
19
#
 
20
"""
 
21
Implements a Nexus-OS NETCONF over SSHv2 API Client
 
22
"""
 
23
 
 
24
import logging as LOG
 
25
import string
 
26
import subprocess
 
27
 
 
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
 
31
 
 
32
from ncclient import manager
 
33
 
 
34
LOG.basicConfig(level=LOG.WARN)
 
35
LOG.getLogger(const.LOGGER_COMPONENT_NAME)
 
36
 
 
37
 
 
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>
 
44
"""
 
45
 
 
46
 
 
47
exec_conf_postfix = """
 
48
          </__XML__MODE__exec_configure>
 
49
        </configure>
 
50
      </config>
 
51
"""
 
52
 
 
53
 
 
54
cmd_vlan_conf_snippet = """
 
55
            <vlan>
 
56
              <vlan-id-create-delete>
 
57
                <__XML__PARAM_value>%s</__XML__PARAM_value>
 
58
                <__XML__MODE_vlan>
 
59
                  <name>
 
60
                    <vlan-name>%s</vlan-name>
 
61
                  </name>
 
62
                  <state>
 
63
                    <vstate>active</vstate>
 
64
                  </state>
 
65
                  <no>
 
66
                    <shutdown/>
 
67
                  </no>
 
68
                </__XML__MODE_vlan>
 
69
              </vlan-id-create-delete>
 
70
            </vlan>
 
71
"""
 
72
 
 
73
cmd_no_vlan_conf_snippet = """
 
74
          <no>
 
75
          <vlan>
 
76
            <vlan-id-create-delete>
 
77
              <__XML__PARAM_value>%s</__XML__PARAM_value>
 
78
            </vlan-id-create-delete>
 
79
          </vlan>
 
80
          </no>
 
81
"""
 
82
 
 
83
cmd_vlan_int_snippet = """
 
84
          <interface>
 
85
            <ethernet>
 
86
              <interface>%s</interface>
 
87
              <__XML__MODE_if-ethernet-switch>
 
88
                <switchport></switchport>
 
89
                <switchport>
 
90
                  <trunk>
 
91
                    <allowed>
 
92
                      <vlan>
 
93
                        <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
94
                          <allow-vlans>%s</allow-vlans>
 
95
                        </__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
96
                      </vlan>
 
97
                    </allowed>
 
98
                  </trunk>
 
99
                </switchport>
 
100
              </__XML__MODE_if-ethernet-switch>
 
101
            </ethernet>
 
102
          </interface>
 
103
"""
 
104
 
 
105
cmd_port_trunk = """
 
106
          <interface>
 
107
            <ethernet>
 
108
              <interface>%s</interface>
 
109
              <__XML__MODE_if-ethernet-switch>
 
110
                <switchport></switchport>
 
111
                <switchport>
 
112
                  <mode>
 
113
                    <trunk>
 
114
                    </trunk>
 
115
                  </mode>
 
116
                </switchport>
 
117
              </__XML__MODE_if-ethernet-switch>
 
118
            </ethernet>
 
119
          </interface>
 
120
"""
 
121
 
 
122
cmd_no_switchport = """
 
123
          <interface>
 
124
            <ethernet>
 
125
              <interface>%s</interface>
 
126
              <__XML__MODE_if-ethernet-switch>
 
127
                <no>
 
128
                  <switchport>
 
129
                  </switchport>
 
130
                </no>
 
131
              </__XML__MODE_if-ethernet-switch>
 
132
            </ethernet>
 
133
          </interface>
 
134
"""
 
135
 
 
136
 
 
137
cmd_no_vlan_int_snippet = """
 
138
          <interface>
 
139
            <ethernet>
 
140
              <interface>%s</interface>
 
141
              <__XML__MODE_if-ethernet-switch>
 
142
                <switchport></switchport>
 
143
                <no>
 
144
                <switchport>
 
145
                  <trunk>
 
146
                    <allowed>
 
147
                      <vlan>
 
148
                        <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
149
                          <allow-vlans>%s</allow-vlans>
 
150
                        </__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
151
                      </vlan>
 
152
                    </allowed>
 
153
                  </trunk>
 
154
                </switchport>
 
155
               </no>
 
156
              </__XML__MODE_if-ethernet-switch>
 
157
            </ethernet>
 
158
          </interface>
 
159
"""
 
160
 
 
161
 
 
162
filter_show_vlan_brief_snippet = """
 
163
      <show xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
 
164
        <vlan>
 
165
          <brief/>
 
166
        </vlan>
 
167
      </show> """
 
168
 
 
169
 
 
170
class CiscoNEXUSDriver():
 
171
 
 
172
    def __init__(self):
 
173
        pass
 
174
 
 
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)
 
178
            return m
 
179
 
 
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)
 
184
 
 
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)
 
189
 
 
190
    def enable_port_trunk(self, mgr, interface):
 
191
        confstr = cmd_port_trunk % (interface)
 
192
        confstr = exec_conf_prefix + confstr + exec_conf_postfix
 
193
        print confstr
 
194
        mgr.edit_config(target='running', config=confstr)
 
195
 
 
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
 
199
        print confstr
 
200
        mgr.edit_config(target='running', config=confstr)
 
201
 
 
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
 
205
        print confstr
 
206
        mgr.edit_config(target='running', config=confstr)
 
207
 
 
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))
 
216
            print result
 
217
 
 
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)
 
225
 
 
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)
 
230
 
 
231
 
 
232
def main():
 
233
    client = CiscoNEXUSDriver()
 
234
 
 
235
if __name__ == '__main__':
 
236
    main()