@@ -593,22 +593,34 @@ public List<? extends Nic> getNics(long vmId) {
593593 @ Override
594594 public String getNextAvailableMacAddressInNetwork (long networkId ) throws InsufficientAddressCapacityException {
595595 NetworkVO network = _networksDao .findById (networkId );
596- Integer zoneIdentifier = MACIdentifier .value ();
597- if (zoneIdentifier .intValue () == 0 ) {
598- zoneIdentifier = Long .valueOf (network .getDataCenterId ()).intValue ();
596+ if (network == null ) {
597+ throw new CloudRuntimeException ("Could not find network with id " + networkId );
599598 }
599+
600+ Integer zoneMacIdentifier = Long .valueOf (getMacIdentifier (network .getDataCenterId ())).intValue ();
600601 String mac ;
601602 do {
602- mac = _networksDao .getNextAvailableMacAddress (networkId , zoneIdentifier );
603+ mac = _networksDao .getNextAvailableMacAddress (networkId , zoneMacIdentifier );
603604 if (mac == null ) {
604605 throw new InsufficientAddressCapacityException ("Unable to create another mac address" , Network .class , networkId );
605606 }
606- } while (! isMACUnique (mac ));
607+ } while (! isMACUnique (mac , networkId ));
607608 return mac ;
608609 }
609610
610- private boolean isMACUnique (String mac ) {
611- return (_nicDao .findByMacAddress (mac ) == null );
611+ @ Override
612+ public String getUniqueMacAddress (long macAddress , long networkId , long datacenterId ) throws InsufficientAddressCapacityException {
613+ String macAddressStr = NetUtils .long2Mac (NetUtils .createSequenceBasedMacAddress (macAddress , getMacIdentifier (datacenterId )));
614+ if (!isMACUnique (macAddressStr , networkId )) {
615+ macAddressStr = getNextAvailableMacAddressInNetwork (networkId );
616+ }
617+ return macAddressStr ;
618+ }
619+
620+ @ Override
621+ public boolean isMACUnique (String mac , long networkId ) {
622+ return (_nicDao .findByMacAddress (mac , networkId ) == null );
623+
612624 }
613625
614626 @ Override
@@ -2815,4 +2827,18 @@ public boolean checkSecurityGroupSupportForNetwork(Account account, DataCenter z
28152827 }
28162828 return false ;
28172829 }
2830+
2831+ @ Override
2832+ public long getMacIdentifier (Long dataCenterId ) {
2833+ long macAddress = 0 ;
2834+ if (dataCenterId == null ) {
2835+ macAddress = NetworkModel .MACIdentifier .value ();
2836+ } else {
2837+ macAddress = NetworkModel .MACIdentifier .valueIn (dataCenterId );
2838+ if (macAddress == 0 ) {
2839+ macAddress = dataCenterId ;
2840+ }
2841+ }
2842+ return macAddress ;
2843+ }
28182844}
0 commit comments