private int getFirstFreeUnitNumberForController()

in src/com/vmware/vim25/mox/VirtualMachineDeviceManager.java [440:479]


  private int getFirstFreeUnitNumberForController(VirtualController controller) 
  {
    // If there are some devices attached then search which nodes are free
    // otherwise all nodes are free so just return the first one.
    if (controller.device == null) 
    {
      return 0;
    }
    
    int maxNodes = getMaxNodesPerControllerOfType(controller);

    if (controller.device.length < maxNodes) 
    {
      List<Integer> usedNodeList = new ArrayList<Integer>();
      VirtualDevice[] devices = getAllVirtualDevices();

      // If this is SCSI controller then its controller also occupies one node.
      if (controller instanceof VirtualSCSIController) 
      {
        usedNodeList.add(((VirtualSCSIController) controller).scsiCtlrUnitNumber);
      }

      for(VirtualDevice device : devices)
      {
        if(device.controllerKey!=null && device.controllerKey == controller.key) 
        {
          usedNodeList.add(device.unitNumber);
        }
      }
      for(int i=0; i<maxNodes; i++)
      {
        if (!usedNodeList.contains(i)) 
        {
          return i;
        }
      }
    }
    
    return -1;
  }