~rohitagarwalla/neutron/l2network-plugin-db

« back to all changes in this revision

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

  • Committer: rohitagarwalla
  • Date: 2011-08-09 18:57:14 UTC
  • mfrom: (34.1.24 trunk)
  • Revision ID: roagarwa@cisco.com-20110809185714-b8h783jys4e3p477
Merging in latest changes from lp:~cisco-openstack/quantum/l2network-plugin rev 58 

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_constants as const
 
29
from quantum.plugins.cisco.common import cisco_exceptions as cexc
 
30
 
 
31
from ncclient import manager
 
32
 
 
33
LOG.basicConfig(level=LOG.WARN)
 
34
LOG.getLogger(const.LOGGER_COMPONENT_NAME)
 
35
 
 
36
 
 
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>
 
43
"""
 
44
 
 
45
 
 
46
exec_conf_postfix = """
 
47
          </__XML__MODE__exec_configure>
 
48
        </configure>
 
49
      </config>
 
50
"""
 
51
 
 
52
 
 
53
cmd_vlan_conf_snippet = """
 
54
            <vlan>
 
55
              <vlan-id-create-delete>
 
56
                <__XML__PARAM_value>%s</__XML__PARAM_value>
 
57
                <__XML__MODE_vlan>
 
58
                  <name>
 
59
                    <vlan-name>%s</vlan-name>
 
60
                  </name>
 
61
                  <state>
 
62
                    <vstate>active</vstate>
 
63
                  </state>
 
64
                  <no>
 
65
                    <shutdown/>
 
66
                  </no>
 
67
                </__XML__MODE_vlan>
 
68
              </vlan-id-create-delete>
 
69
            </vlan>
 
70
"""
 
71
 
 
72
cmd_no_vlan_conf_snippet = """
 
73
          <no>
 
74
          <vlan>
 
75
            <vlan-id-create-delete>
 
76
              <__XML__PARAM_value>%s</__XML__PARAM_value>
 
77
            </vlan-id-create-delete>
 
78
          </vlan>
 
79
          </no>
 
80
"""
 
81
 
 
82
cmd_vlan_int_snippet = """
 
83
          <interface>
 
84
            <ethernet>
 
85
              <interface>%s</interface>
 
86
              <__XML__MODE_if-ethernet-switch>
 
87
                <switchport></switchport>
 
88
                <switchport>
 
89
                  <trunk>
 
90
                    <allowed>
 
91
                      <vlan>
 
92
                        <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
93
                          <allow-vlans>%s</allow-vlans>
 
94
                        </__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
95
                      </vlan>
 
96
                    </allowed>
 
97
                  </trunk>
 
98
                </switchport>
 
99
              </__XML__MODE_if-ethernet-switch>
 
100
            </ethernet>
 
101
          </interface>
 
102
"""
 
103
 
 
104
cmd_port_trunk = """
 
105
          <interface>
 
106
            <ethernet>
 
107
              <interface>%s</interface>
 
108
              <__XML__MODE_if-ethernet-switch>
 
109
                <switchport></switchport>
 
110
                <switchport>
 
111
                  <mode>
 
112
                    <trunk>
 
113
                    </trunk>
 
114
                  </mode>
 
115
                </switchport>
 
116
              </__XML__MODE_if-ethernet-switch>
 
117
            </ethernet>
 
118
          </interface>
 
119
"""
 
120
 
 
121
cmd_no_switchport = """
 
122
          <interface>
 
123
            <ethernet>
 
124
              <interface>%s</interface>
 
125
              <__XML__MODE_if-ethernet-switch>
 
126
                <no>
 
127
                  <switchport>
 
128
                  </switchport>
 
129
                </no>
 
130
              </__XML__MODE_if-ethernet-switch>
 
131
            </ethernet>
 
132
          </interface>
 
133
"""
 
134
 
 
135
 
 
136
cmd_no_vlan_int_snippet = """
 
137
          <interface>
 
138
            <ethernet>
 
139
              <interface>%s</interface>
 
140
              <__XML__MODE_if-ethernet-switch>
 
141
                <switchport></switchport>
 
142
                <no>
 
143
                <switchport>
 
144
                  <trunk>
 
145
                    <allowed>
 
146
                      <vlan>
 
147
                        <__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
148
                          <allow-vlans>%s</allow-vlans>
 
149
                        </__XML__BLK_Cmd_switchport_trunk_allowed_allow-vlans>
 
150
                      </vlan>
 
151
                    </allowed>
 
152
                  </trunk>
 
153
                </switchport>
 
154
               </no>
 
155
              </__XML__MODE_if-ethernet-switch>
 
156
            </ethernet>
 
157
          </interface>
 
158
"""
 
159
 
 
160
 
 
161
filter_show_vlan_brief_snippet = """
 
162
      <show xmlns="http://www.cisco.com/nxos:1.0:vlan_mgr_cli">
 
163
        <vlan>
 
164
          <brief/>
 
165
        </vlan>
 
166
      </show> """
 
167
 
 
168
 
 
169
class CiscoNEXUSDriver():
 
170
 
 
171
    def __init__(self):
 
172
        pass
 
173
 
 
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)
 
177
            return m
 
178
 
 
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)
 
183
 
 
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)
 
188
 
 
189
    def enable_port_trunk(self, mgr, interface):
 
190
        confstr = cmd_port_trunk % (interface)
 
191
        confstr = exec_conf_prefix + confstr + exec_conf_postfix
 
192
        print confstr
 
193
        mgr.edit_config(target='running', config=confstr)
 
194
 
 
195
    def disable_switch_port(self, mgr, interface):
 
196
        confstr = cmd_no_switchport % (interface)
 
197
        confstr = exec_conf_prefix + confstr + exec_conf_postfix
 
198
        print confstr
 
199
        mgr.edit_config(target='running', config=confstr)
 
200
 
 
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
 
204
        print confstr
 
205
        mgr.edit_config(target='running', config=confstr)
 
206
 
 
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
 
210
        print confstr
 
211
        mgr.edit_config(target='running', config=confstr)
 
212
 
 
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))
 
221
            print result
 
222
 
 
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)
 
230
 
 
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)
 
237
 
 
238
 
 
239
def main():
 
240
    client = CiscoNEXUSDriver()
 
241
 
 
242
if __name__ == '__main__':
 
243
    main()