cancel
Showing results for 
Search instead for 
Did you mean: 
cancel

Writing your own OpenStack Neutron ML2 Driver for Cisco UCS

1954
Views
0
Helpful
0
Comments
Cisco Employee

Overview

Cisco UCS also offers Python SDK which is a set of Python modules, each containing one or more classes, developed specifically to automate UCS Manager. 

Or Alternatively, you can simply submit the POST request to Cisco UCS API. The request will contain the XML to perform certain action in UCS Manager.

Let’s start coding and gets our hands dirty. This is a very simple example for writing your own plugin to automate Cisco UCS Manger.

Instruction

Write two python module

  1. my_mechanism.py
  2. my_mech_ucs.py

my_mechanism.py

from neutron.openstack.common import log as logging
from neutron.plugins.ml2 import driver_api as api
from my_mech_ucs import ucs

LOG = logging.getLogger(__name__)

class MyDriver(api.MechanismDriver):

   
   def initialize(self):
       pass

   def create_network_postcommit(self, context):
       segments = context.network_segments
       vlan_id = segments[0]['segmentation_id']
       LOG.debug("creating vlan_id=%s",vlan_id)
       ucsm = ucs()
       token=ucsm.GetAuthCookie()
       LOG.debug("UCSM Auth Cookie=%s",token)  
       ucsm.ProvisionVLAN(token,vlan_id)
       ucsm.UCSLogout(token)        

    
   def delete_network_postcommit(self, context):
       LOG.debug("This is from my plugin Afzal....")
       segments = context.network_segments
       vlan_id = segments[0]['segmentation_id']
       LOG.debug("deleting vlan_id=%s",vlan_id)
       ucsm = ucs()
       token=ucsm.GetAuthCookie()
       LOG.debug("UCSM Auth Cookie=%s",token)  
       ucsm.DeProvisionVLAN(token,vlan_id)
       ucsm.UCSLogout(token) 

my_mech_ucs.py

import urllib2
from xml.dom import minidom

class ucs():
    ip = "http://<ucsm ip address>/nuova"

    def GetAuthCookie(self):
        
        
            outcookie="nocookie"
                   Authdata= "<aaaLogin inName='config' inPassword='config'></aaaLogin>"
                 
                   request = urllib2.Request(self.ip, Authdata)
            request.add_header("Content-Type","application/xml")
 
                   response=urllib2.urlopen(request)
       
                   xmlout= response.read()
       
                   xmldoc = minidom.parseString(xmlout)
                   xmlelement = xmldoc.getElementsByTagName("aaaLogin")
                   outcookie = xmlelement[0].attributes['outCookie'].value
                   return outcookie
        

    def ProvisionVLAN(self, token, vlanid):
            vlanstring = "<configConfMos inHierarchical='false' cookie='" + token + "'><inConfigs><pair key='fabric/lan/net-VLAN" + str(vlanid) + "'><fabricVlan compressionType='included' defaultNet='no' dn='fabric/lan/net-VLAN" + str(vlanid) + "' id='" + str(vlanid) + "' mcastPolicyName='' name='VLAN"+ str(vlanid) +"' pubNwName='' sharing='none' status='created'></fabricVlan></pair></inConfigs></configConfMos>"
    
                request = urllib2.Request(self.ip, vlanstring)
                response=urllib2.urlopen(request)
                xmlout= response.read()


    def DeProvisionVLAN(self, token, vlanid):
            vlanstring = "<configConfMos inHierarchical='false' cookie='" + token + "'><inConfigs><pair key='fabric/lan/net-VLAN" + str(vlanid) + "'><fabricVlan compressionType='included' defaultNet='no' dn='fabric/lan/net-VLAN" + str(vlanid) + "' id='" + str(vlanid) + "' mcastPolicyName='' name='VLAN"+ str(vlanid) +"' pubNwName='' sharing='none' status='deleted'></fabricVlan></pair></inConfigs></configConfMos>"
    
                request = urllib2.Request(self.ip, vlanstring)
                response=urllib2.urlopen(request)
                xmlout= response.read()

    def UCSLogout(self, outcookie):
            strlogout = "<aaaLogout cookie='" + outcookie + "'/>"
            request = urllib2.Request(self.ip, strlogout)
                response=urllib2.urlopen(request)
            xmlout=response.read()

Setup Instructions

Some Screen Shots

ml2_conf.ini file

specify mydriver in mechanism_driver section as shown below:

 

ml2_conf.ini.png

 

Module file

moduel file.png

 

SELINUX Config

selinux.png

entry point

entry point.png

Module file entry in entry_point.txt. MyDriver is the class name in my_mechanism.py

 

Debug the module

With the following python code you can verify if the module is working. this would provision VLAN with the ID 20 is Cisco UCS Manager

 

#Python
>>>from my_mech_ucs import ucs
>>>x=uss()
>>>y=x.GetAuthCookie()
>>>print y
>>>x.ProvisionVLAN(y,20)
>>>x.UCSLogout(y)

Demo

The following demo show the working plugin.

Content for Community-Ad