UCSD - Get Free VLANS from VLAN Pool Policy and VLAN List

Version 3
    Task Name

    UCSD has VLAN pool policies.  This workflow will get the number of free VLANS and the VLAN list that is free.

    Description

     

    Prerequisites

    Minimum UCSD version: 5.4.0.2

    CategoryCustom task
    Components
    User Inputs

     

    User Output

    Instructions for Regular Workflow Use:

    Instructions for Regular Workflow Use:

    1. Download the attached .ZIP file below to your computer. *Remember the location of the saved file on your computer.
    2. Unzip the file on your computer. Should end up with a .WFD file.
    3. Log in to UCS Director as a user that has "system-admin" privileges.
    4. Navigate to "Policies-->Orchestration" and click on "Import".
    5. Click "Browse" and navigate to the location on your computer where the .WFD file resides. Choose the .WFD file and click "Open".
    6. Click "Upload" and then "OK" once the file upload is completed. Then click "Next".
    7. Click the "Select" button next to "Import Workflows". Click the "Check All" button to check all checkboxes and then the "Select" button.
    8. Click "Submit".
    9. A new folder should appear in "Policies-->Orchestration" that contains the imported workflow. You will now need to update the included tasks with information about the specific environment.

     

    A thank you goes out to: Tejeswar Sahu

     

    The workflow:

     

    Screen Shot 2016-03-09 at 10.45.26 AM.png

     

    The workflow input and LOV provider from custom task:

     

    Screen Shot 2016-03-09 at 10.45.39 AM.png

     

    The UCSD VLAN pool Policies in question:

     

    Screen Shot 2016-03-09 at 10.48.18 AM.png

     

    The workflow run:

     

    Screen Shot 2016-03-09 at 10.45.18 AM.png

     

    The custom task code:

     

    importPackage(java.lang);

    importPackage(java.util);

    importPackage(com.cloupia.service.cIM.inframgr);//VDCUtil,InfraPersistenceUtil

    importPackage(com.cloupia.service.cIM.inframgr.profiles);//VLANPoolPolicy

    importPackage(com.cloupia.model.cIM);//GlobalConstants

    importPackage(com.cloupia.service.cIM.inframgr.reservation);//CapacityReservationPersistentUtil

     

     

     

     

    var policyName = input.vlanPoolPolicyName;

    logger.addInfo("Selected vlanPoolPolicyName is:"+policyName);

     

     

    var vpp = VDCUtil.getVLANPoolPolicyByName(policyName);

                if (vpp == null)

                {

                    loggerogger.addError("No such VLAN pool policy exists.");

                    throw new Exception("No such VLAN pool policy exists.");

                }

     

     

    var vlanList = getVLANListFromVLANPoolPolicy(vpp.getVlanRange());

    logger.addInfo("List = "+vlanList);

     

     

    logger.addInfo("NO of vlans in the vlan pool policy::"+vlanList.size());

     

      var dcName = vpp.getDcName();

        var resourceType = GlobalConstants.RESOURCE_TYPE.VLAN_DN+GlobalConstants.RESOURCE_TYPE.DELIM+dcName;

     

      var reservedVlanIds = CapacityReservationPersistentUtil.getCapacityReservationByType(resourceType);

      var vlanId = -1;

       var reservedSet = new HashSet();

       for(var count = 0;count<reservedVlanIds.size();count++){

      var cr = reservedVlanIds.get(count);

      reservedSet.add(cr.getResourceId());

       }

     

       var freeVlanList = getVlanIdfromVLANList(vlanList, dcName,reservedSet);

      logger.addInfo("Free List = "+freeVlanList);

     

     

     

    var noOfFreeVlans = freeVlanList.size();

    logger.addInfo("noOfFreeVlans = "+noOfFreeVlans);

     

     

    output.noOfFreeVlans = noOfFreeVlans;

    output.FreeVLANList = freeVlanList;

        

    ///////////////////////////////////////

     

     

    function getVlanIdfromVLANList(vlanList, dcName, reservedSet) //throws Exception

        {

      var freeVlanList = new ArrayList();

     

     

    var vlanIdMap = InfraPersistenceUtil.getVLANPoolTableIdsByDCName(dcName);

    for(var count = 0;count<vlanList.size();count++)

      

            {

            var vlanId = vlanList[count];

        

            if(reservedSet.contains(vlanId+""))

            {

      logger.addInfo("RESERVED:"+vlanId)

            continue;

            }

        

        

                if (vlanIdMap != null)

                {

                    if (!vlanIdMap.containsKey(vlanId))

                    {

      freeVlanList.add(vlanId);

      //logger.addInfo("1:FREE IP IS:"+vlanId);

                  

         } else

                    {

                        var existVpt = vlanIdMap.get(vlanId);

                        if (existVpt.isNetapp() || existVpt.isNetwork() || existVpt.isUcs() || existVpt.isVmware())

                        {

                            logger.addInfo("Skipping vlan Id since it's used. " + vlanId);

                            continue;

     

     

                        } else

                        {

                                logger.addInfo("Generated vlaId is not reserved and not used");

                                logger.addInfo("2: Allocating vlan id " + vlanId);

      freeVlanList.add(vlanId);

      //logger.addInfo("2:FREE IP IS:"+vlanId);

                                                 

                        }

                    }

                } else

                {

      freeVlanList.add(vlanId);

      // logger.addInfo("3:FREE IP IS:"+vlanId);

                }

            }

    logger.addInfo("NO OF AVAILABLE FREE VLANS:::"+freeVlanList.size());

           // return -1;

       return freeVlanList;

        }

     

     

     

     

     

     

    ///////////////////////////////////////

     

     

    //private List<Integer> getVLANListFromVLANPoolPolicy(String vlanID) throws Exception

    function getVLANListFromVLANPoolPolicy(vlanID) //throws Exception

       {

       /*try

            {

       var vlanList = new ArrayList();

        } catch (e)

            {

                logger.addError("Error While retrieving VLAN ID from VLAN Pool::::::"+e);

                throw new Exception("Error While retrieving VLAN ID from VLAN Pool");

            }

       return vlanList;

       */

      

      try

            {

               var vlanList = new ArrayList();

                var vlanArr = vlanID.split(",");

     

     

                if ((vlanArr != null) && (vlanArr.length > 0))

                {

                    for (var count = 0;count<vlanArr.length;count++)

                    {

      var vlanRange = vlanArr[count];

                        if (vlanRange.contains("-"))

                        {

                            var vlanRangeArr = vlanRange.split("-");

     

     

                        

                            if (vlanRangeArr[0] != null)

                                vlanRangeArr[0] = vlanRangeArr[0].trim();

     

     

                            if (vlanRangeArr[1] != null)

                                vlanRangeArr[1] = vlanRangeArr[1].trim();

                       

     

     

                            var startId = Integer.parseInt(vlanRangeArr[0]);

                            var endId = Integer.parseInt(vlanRangeArr[1]);

     

     

                            for (var i = startId; i <= endId; i++)

                                vlanList.add(i);

                        } else

                        {

                            vlanList.add(Integer.parseInt(vlanRange));

                        }

                    }

                }

     

     

                return vlanList;

     

            } catch (e)

            {

                logger.addError("Error While retrieving VLAN ID from VLAN Pool::::::"+e);

                throw new Exception("Error While retrieving VLAN ID from VLAN Pool");

            }

        }

    The custom Task Inputs and outputs:

     

    Unknown.png

     

    Unknown-1.png